- Timestamp:
- Jan 4, 2024, 12:02:07 PM (4 months ago)
- Branches:
- master
- Children:
- e0cc9e0
- Parents:
- be10079
- Location:
- libcfa/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/iostream.cfa
rbe10079 r4aae2bd 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Nov 17 13:33:12 202313 // Update Count : 18 5312 // Last Modified On : Wed Jan 3 10:53:13 2024 13 // Update Count : 1898 14 14 // 15 15 … … 984 984 } 985 985 986 istype & ?|?( istype & is, _Istream_Cquoted f ) {986 istype & ?|?( istype & is, _Istream_Cquoted f ) with( f ) { 987 987 char fmtstr[32]; // storage scanset and format codes 988 988 fmtstr[0] = '%'; … … 992 992 bool check = true; 993 993 994 if ( f.flags.ignore ) { check = false; fmtstr[1] = '*'; pos += 1; }995 int rwd = f.wd;996 if ( f.wd != -1 ) {// => just ignore versus ignore with width994 if ( cstr.flags.ignore ) { check = false; fmtstr[1] = '*'; pos += 1; } 995 int rwd = cstr.wd; 996 if ( cstr.wd != -1 ) { // => just ignore versus ignore with width 997 997 // wd is buffer bytes available (for input chars + null terminator) 998 998 // rwd is count of input chars 999 999 // no maximum width necessary because text ignored => width is read width 1000 if ( f.flags.rwd ) check = false;1001 else rwd = f.wd - 1;1000 if ( cstr.flags.rwd ) check = false; 1001 else rwd = cstr.wd - 1; 1002 1002 pos += sprintf( &fmtstr[pos], "%d", rwd ); 1003 1003 } // if 1004 1004 1005 1005 int len = 0; // may not be set in fmt 1006 if ( ! f.flags.inex ) { // => quoted getline1007 // fprintf( stderr, "quoted\n" );1006 char enddelim; 1007 if ( ! cstr.flags.inex ) { // => quoted getline 1008 1008 args = fmt( is, "%*[ \f\n\r\t\v]" ); // remove leading whitespace 1009 1009 if ( eof( is ) ) goto Eof; 1010 // args = fmt( is, (const char *)f.delimiter ); // remove leading quote 1011 args = fmt( is, "'%n", &len ); // remove leading quote 1012 fprintf( stderr, "quoted %d %d\n", args, len ); 1010 char rfmt[4] = { cstr.delimiters[0], '%', 'n', '\0' }; 1011 args = fmt( is, rfmt, &len ); // remove leading quote 1013 1012 if ( len == 0 || eof( is ) ) goto Eof; 1014 1013 } // if 1015 sprintf( &fmtstr[pos], "[^%c]%%n", f.delimiter[0] ); 1016 // fprintf( stderr, "getline %s %d\n", fmtstr, f.wd ); 1017 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1018 else args = fmt( is, fmtstr, f.s, &len ); 1019 // fprintf( stderr, "getline %s %d %d %d\n", fmtstr, args, f.wd, eof( is ) ); 1014 enddelim = cstr.delimiters[1] == '\0' ? cstr.delimiters[0] : cstr.delimiters[1]; 1015 sprintf( &fmtstr[pos], "[^%c]%%n", enddelim ); 1016 if ( cstr.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1017 else args = fmt( is, fmtstr, cstr.s, &len ); 1020 1018 if ( check && len == rwd && ! eof( is ) ) { // might not fit 1021 1019 char peek; 1022 1020 fmt( is, "%c", &peek ); // check for delimiter 1023 // fprintf( stderr, "peek %d '%c'\n", args, peek );1024 1021 if ( ! eof( is ) ) { 1025 if ( peek != f.delimiter[0]) {1022 if ( peek != enddelim ) { 1026 1023 ungetc( is, peek ); 1027 1024 throwResume ExceptionInst( cstring_length ); … … 1030 1027 } else fmt( is, "%*c" ); // remove delimiter 1031 1028 Eof: ; 1032 if ( rwd > 0 && args == 0 ) f.s[0] = '\0';// read failed => no pattern match => set string to null1029 if ( rwd > 0 && args == 0 ) cstr.s[0] = '\0'; // read failed => no pattern match => set string to null 1033 1030 if ( args == 1 && eof( is ) ) { // data but scan ended at EOF 1034 // fprintf( stderr, "clear\n" );1035 1031 clear( is ); // => reset EOF => detect again on next read 1036 1032 } // if … … 1038 1034 } 1039 1035 1040 istype & ?|?( istype & is, _Istream_Cstr f ) {1036 istype & ?|?( istype & is, _Istream_Cstr f ) with( f ) { 1041 1037 const char * scanset; 1042 1038 size_t nscanset = 0; 1043 if ( f .flags.delimiter ) scanset = f.delimiter; // getline ?1039 if ( flags.delimiter ) scanset = delimiters; // getline ? 1044 1040 else scanset = f.scanset; 1045 1041 if ( scanset ) nscanset = strlen( scanset ); … … 1084 1080 if ( f.flags.delimiter ) { // getline 1085 1081 int len = 0; // may not be set in fmt 1086 if ( ! f.flags.inex ) { // => quoted getline 1087 // fprintf( stderr, "quoted\n" ); 1088 args = fmt( is, "%*[ \f\n\r\t\v]" ); // remove leading whitespace 1089 if ( eof( is ) ) goto X; 1090 args = fmt( is, "\"" ); // remove leading quote 1091 if ( eof( is ) ) goto X; 1092 } // if 1093 // fprintf( stderr, "getline\n" ); 1094 // sprintf( &fmtstr[pos], "[%s%s]%%n", f.flags.inex ? "^" : "", scanset ); 1095 sprintf( &fmtstr[pos], "[^%s]%%n", scanset ); 1096 // fprintf( stderr, "getline %s %d\n", fmtstr, f.wd ); 1082 sprintf( &fmtstr[pos], "[^%c]%%n", f.delimiters[0] ); 1097 1083 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1098 1084 else args = fmt( is, fmtstr, f.s, &len ); 1099 // fprintf( stderr, "getline %s %d %d %d\n", fmtstr, args, f.wd, eof( is ) );1100 1085 if ( check && len == rwd && ! eof( is ) ) { // might not fit 1101 char peek; 1102 fmt( is, "%c", &peek ); // check for delimiter 1103 // fprintf( stderr, "peek %d '%c'\n", args, peek ); 1086 fmtstr[0] = f.delimiters[0]; fmtstr[1] = '%'; fmtstr[2] = 'n'; fmtstr[3] = '\0'; 1087 fmt( is, fmtstr, &len ); // remove delimiter 1104 1088 if ( ! eof( is ) ) { 1105 if ( peek != f.delimiter[0] ) { 1106 ungetc( is, peek ); 1089 // if ( peek != f.delimiter[0] ) { 1090 if ( len != 1 ) { 1091 // ungetc( is, peek ); 1107 1092 throwResume ExceptionInst( cstring_length ); 1108 1093 } // if 1109 1094 } // if 1110 } else fmt( is, "%*c" ); // remove delimiter 1111 X: ; 1095 } else fmt( is, "%*c" ); // remove delimiter 1112 1096 } else { 1113 1097 // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] -
libcfa/src/iostream.hfa
rbe10079 r4aae2bd 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Nov 15 17:55:31 202313 // Update Count : 59612 // Last Modified On : Wed Jan 3 10:53:18 2024 13 // Update Count : 610 14 14 // 15 15 … … 392 392 union { 393 393 const char * scanset; 394 char delimiter [2];394 char delimiters[3]; // [0] => left, [1] => right 395 395 }; 396 396 int wd; // width … … 412 412 413 413 struct _Istream_Cquoted { 414 char * s; 415 inline _Istream_str_base; 414 _Istream_Cstr cstr; 416 415 }; // _Istream_Cquoted 417 416 … … 419 418 // width must include room for null terminator 420 419 _Istream_Cstr wdi( unsigned int wd, char s[] ) { return (_Istream_Cstr)@{ s, { {0p}, wd, {.all : 0} } }; } 421 // read width does not include null terminator422 420 _Istream_Cstr wdi( unsigned int wd, unsigned int rwd, char s[] ) { 423 421 if ( wd <= rwd ) throw (cstring_length){ &cstring_length_vt }; 424 422 return (_Istream_Cstr)@{ s, { {0p}, rwd, {.flags.rwd : true} } }; 425 423 } 426 _Istream_Cquoted & quoted( _Istream_Cstr & fmt, const char delimiter = '"' ) {427 fmt.delimiter [0] = delimiter; fmt.delimiter[1] = '\0';424 _Istream_Cquoted & quoted( _Istream_Cstr & fmt, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) { 425 fmt.delimiters[0] = Ldelimiter; fmt.delimiters[1] = Rdelimiter; fmt.delimiters[2] = '\0'; 428 426 return (_Istream_Cquoted &)fmt; 429 427 } 430 428 _Istream_Cstr & getline( _Istream_Cstr & fmt, const char delimiter = '\n' ) { 431 fmt.delimiter[0] = delimiter; fmt.delimiter[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt; } 429 fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt; 430 } 432 431 _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; } 433 432 _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
Note: See TracChangeset
for help on using the changeset viewer.