Changeset e0dc038
- Timestamp:
- Oct 17, 2023, 9:32:34 PM (14 months ago)
- Branches:
- master
- Children:
- 2d7cb19, f842032
- Parents:
- ca995e3
- Location:
- libcfa/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/fstream.cfa
rca995e3 re0dc038 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Aug 18 10:41:17202313 // Update Count : 54 112 // Last Modified On : Tue Oct 17 08:38:49 2023 13 // Update Count : 544 14 14 // 15 15 … … 311 311 } // if 312 312 va_end( args ); 313 // if ( len == 0 ) throw ExceptionInst( missing_data ); 313 314 return len; 314 315 } // fmt -
libcfa/src/fstream.hfa
rca995e3 re0dc038 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Aug 18 10:41:15202313 // Update Count : 2 5812 // Last Modified On : Fri Oct 13 13:55:21 2023 13 // Update Count : 260 14 14 // 15 15 … … 119 119 void ends( ifstream & ); 120 120 int fmt( ifstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); 121 ifstream & ungetc( ifstream & is, char c ); 122 bool eof( ifstream & is ); 121 123 122 124 bool fail( ifstream & is ); 123 125 void clear( ifstream & ); 124 bool eof( ifstream & is );125 126 void open( ifstream & is, const char name[], const char mode[] ); // FIX ME: use default = "r" 126 127 void open( ifstream & is, const char name[] ); 127 128 void close( ifstream & is ); 128 129 129 ifstream & read( ifstream & is, char data[], size_t size ); 130 ifstream & ungetc( ifstream & is, char c );131 130 132 131 void ?{}( ifstream & is ); -
libcfa/src/iostream.cfa
rca995e3 re0dc038 1 2 1 // 3 2 // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo … … 11 10 // Created On : Wed May 27 17:56:53 2015 12 11 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Sun Oct 8 12:10:21202314 // Update Count : 1 56412 // Last Modified On : Tue Oct 17 20:57:05 2023 13 // Update Count : 1795 15 14 // 16 15 … … 966 965 char fmtstr[ sizeof("%*[]") + nscanset ]; 967 966 int pos = 0; 968 fmtstr[pos] = '%'; pos += 1; 969 fmtstr[pos] = '*'; pos += 1; 970 fmtstr[pos] = '['; pos += 1; 967 strcpy( &fmtstr[pos], "%*[" ); pos += 3; 971 968 strcpy( &fmtstr[pos], f.scanset ); pos += nscanset; 972 fmtstr[pos] = ']'; pos += 1; 973 fmtstr[pos] = '\0'; 974 fmt( is, fmtstr, (void*)0 ); // last arg is dummy: suppress gcc warning 975 } 976 else for ( f.wd ) fmt( is, "%*c" ); 969 strcpy( &fmtstr[pos], "]" ); 970 fmt( is, fmtstr, "" ); // skip scanset 971 } else { 972 char ch; 973 // fprintf( stderr, "skip " ); 974 for ( f.wd ) { // skip N characters 975 if ( eof( is ) ) break; 976 fmt( is, "%c", &ch ); 977 // fprintf( stderr, "`%c' ", ch ); 978 } // for 979 } // if 977 980 return is; 978 981 } … … 980 983 981 984 istype & ?|?( istype & is, _Istream_Cstr f ) { 982 const char * scanset = f.scanset; 985 const char * scanset; 986 size_t nscanset = 0; 983 987 if ( f.flags.delimiter ) scanset = f.delimiter; // getline ? 984 985 size_t len = 0; 986 if ( scanset ) len = strlen( scanset ); 987 char fmtstr[len + 16]; 988 int start = 1; 988 else scanset = f.scanset; 989 if ( scanset ) nscanset = strlen( scanset ); 990 991 char fmtstr[nscanset + 32]; // storage for scanset and format codes 989 992 fmtstr[0] = '%'; 990 if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; } 991 // no maximum width necessary because text ignored => width is read width 992 if ( f.wd != -1 ) { 993 994 int pos = 1; 995 int args; 996 bool check = true; 997 998 if ( f.flags.ignore ) { check = false; fmtstr[1] = '*'; pos += 1; } 999 int rwd = f.wd; 1000 if ( f.wd != -1 ) { // => just ignore versus ignore with width 993 1001 // wd is buffer bytes available (for input chars + null terminator) 994 1002 // rwd is count of input chars 995 int rwd; 996 if (f.flags.rwd) { 997 verify (f.wd >= 0); 998 rwd = f.wd; 1003 // no maximum width necessary because text ignored => width is read width 1004 if ( f.flags.rwd ) check = false; 1005 else rwd = f.wd - 1; 1006 pos += sprintf( &fmtstr[pos], "%d", rwd ); 1007 } // if 1008 1009 if ( ! scanset ) { // %s, %*s, %ws, %*ws 1010 // fprintf( stderr, "cstr %s\n", f.s ); 1011 strcpy( &fmtstr[pos], "s%n" ); 1012 int len = 0; // may not be set in fmt 1013 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1014 else args = fmt( is, fmtstr, f.s, &len ); 1015 // fprintf( stderr, "cstr %s %d %d %d %s\n", fmtstr, args, len, f.wd, f.s ); 1016 if ( check && len >= rwd && ! eof( is ) ) { // might not fit 1017 char peek; 1018 fmt( is, "%c", &peek ); // check for whitespace terminator 1019 // fprintf( stderr, "peek %d '%c'\n", args, peek ); 1020 if ( ! eof( is ) ) { 1021 ungetc( is, peek ); 1022 if ( ! isspace( peek ) ) throw ExceptionInst( cstring_length ); 1023 } // if 1024 } // if 1025 } else { 1026 if ( f.flags.delimiter ) { // getline 1027 // fprintf( stderr, "getline\n" ); 1028 sprintf( &fmtstr[pos], "[%s%s]%%n", f.flags.inex ? "^" : "", scanset ); 1029 // fprintf( stderr, "getline %s %d\n", fmtstr, f.wd ); 1030 int len = 0; // may not be set in fmt 1031 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1032 else args = fmt( is, fmtstr, f.s, &len ); 1033 // fprintf( stderr, "getline %s %d %d %d\n", fmtstr, args, f.wd, eof( is ) ); 1034 if ( check && len == rwd && ! eof( is ) ) { // might not fit 1035 char peek; 1036 fmt( is, "%c", &peek ); // check for delimiter 1037 // fprintf( stderr, "peek %d '%c'\n", args, peek ); 1038 if ( ! eof( is ) ) { 1039 if ( peek != f.delimiter[0] ) { 1040 ungetc( is, peek ); 1041 throw ExceptionInst( cstring_length ); 1042 } // if 1043 } // if 1044 } else fmt( is, "%*c" ); // remove delimiter 999 1045 } else { 1000 verify (f.wd >= 1); 1001 rwd = f.wd - 1; 1046 // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] 1047 // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] 1048 sprintf( &fmtstr[pos], "[%s%s]%%n", f.flags.inex ? "^" : "", scanset ); 1049 // fprintf( stderr, "incl/excl %s %d\n", fmtstr, f.wd ); 1050 int len = 0; // may not be set in fmt 1051 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1052 else args = fmt( is, fmtstr, f.s, &len ); 1053 // fprintf( stderr, "incl/excl %s \"%s\" %d %d %d %d %d %c\n", fmtstr, f.s, args, f.wd, len, eof( is ), check, f.s[f.wd] ); 1054 if ( check && len == rwd && ! eof( is ) ) { // might not fit 1055 // fprintf( stderr, "overflow\n" ); 1056 char peek; 1057 fmt( is, "%c", &peek ); // check for whitespace terminator 1058 // fprintf( stderr, "peek %d '%c'\n", args, peek ); 1059 if ( ! eof( is ) ) { 1060 ungetc( is, peek ); 1061 if ( f.flags.inex ^ strchr( f.scanset, peek ) != 0p ) throw ExceptionInst( cstring_length ); 1062 } // if 1063 } // if 1002 1064 } // if 1003 start += sprintf( &fmtstr[start], "%d", rwd ); 1004 } 1005 1006 if ( ! scanset ) { 1007 // %s, %*s, %ws, %*ws 1008 fmtstr[start] = 's'; fmtstr[start + 1] = '\0'; 1009 // printf( "cstr %s\n", fmtstr ); 1010 } else { 1011 // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] 1012 // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] 1013 fmtstr[start] = '['; start += 1; 1014 if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; } 1015 strcpy( &fmtstr[start], scanset ); // copy includes '\0' 1016 len += start; 1017 fmtstr[len] = ']'; fmtstr[len + 1] = '\0'; 1018 // printf( "incl/excl %s\n", fmtstr ); 1019 } // if 1020 1021 int check = f.wd - 2; 1022 if (! f.flags.ignore ) { 1023 if ( ! f.flags.rwd ) f.s[check] = '\0'; // insert sentinel 1024 } 1025 len = fmt( is, fmtstr, f.s ); 1026 //fprintf( stderr, "KK %s %zd %d %c %s\n", fmtstr, len, check, f.s[check], f.s ); 1027 1028 if ( ! f.flags.ignore && ! f.flags.rwd && f.s[check] != '\0' ) { // sentinel overwritten ? 1029 // buffer filled, but would we have kept going? 1030 if ( ! eof( is ) ) { 1031 char peek; 1032 fmt( is, "%c", &peek ); 1033 ungetc( is, peek ); 1034 bool hasMore; 1035 if (f.flags.delimiter) { // getline 1036 hasMore = (peek != f.delimiter[0]); 1037 } else if (f.scanset) { // incl/excl 1038 bool peekMatch = strchr(f.scanset, peek) != 0p; 1039 hasMore = f.flags.inex ? (!peekMatch) : (peekMatch); 1040 } else { // %s 1041 hasMore = !isspace(peek); 1042 } 1043 if (hasMore) throw (cstring_length){ &cstring_length_vt }; 1044 } // if 1045 } // if 1046 1047 if ( f.flags.delimiter ) { // getline ? 1048 if ( len == 0 ) f.s[0] = '\0'; // empty read => argument unchanged => set empty 1049 if ( ! eof( is ) ) { // ignore delimiter, may not be present because of width 1050 char delimiter; 1051 fmt( is, "%c", &delimiter ); 1052 if ( delimiter != f.delimiter[0] ) ungetc( is, delimiter ); 1053 } // if 1054 } //if 1065 } // if 1066 if ( args == 1 && eof( is ) ) { // data but scan ended at EOF 1067 // fprintf( stderr, "clear\n" ); 1068 clear( is ); // => reset EOF => detect again on next read 1069 } // if 1055 1070 return is; 1056 1071 } // ?|? -
libcfa/src/iostream.hfa
rca995e3 re0dc038 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Oct 8 12:02:55202313 // Update Count : 5 6812 // Last Modified On : Mon Oct 16 21:12:01 2023 13 // Update Count : 573 14 14 // 15 15 … … 324 324 istype & ungetc( istype &, char ); 325 325 bool eof( istype & ); 326 void clear( istype & ); 326 327 }; // basic_istream 327 328 … … 329 330 trait istream { 330 331 bool fail( istype & ); 331 void clear( istype &);332 void open( istype & is, const char name[], const char mode[] ); 332 333 void open( istype & is, const char name[] ); 333 334 void close( istype & is ); … … 402 403 403 404 ExceptionDecl( cstring_length ); 405 ExceptionDecl( missing_data ); 404 406 405 407 // *********************************** manipulators *********************************** 406 408 409 // skip does not compose with other C string manipulators. 407 410 struct _Istream_Cskip { 408 411 const char * scanset;
Note: See TracChangeset
for help on using the changeset viewer.