Changeset eae8b37 for libcfa/src/iostream.cfa
- Timestamp:
- Dec 4, 2024, 10:17:49 PM (2 weeks ago)
- Branches:
- master
- Children:
- fc276f3
- Parents:
- 509ec82
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/iostream.cfa
r509ec82 reae8b37 1202 1202 1203 1203 1204 forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial( E ) ) 1205 istype & ?|?( istype & is, E & e ) { 1206 // fprintf( stderr, "here0\n" ); 1207 if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); 1208 1209 // Match longest input enumerator string to enumerator labels, where enumerator names are unique. 1210 1211 int N = countof( E ), lnths[N], fred = 0; 1212 // printf( "N %d\n", N ); 1213 int r = 0; 1214 // for ( s; E : r; 0~@ ) { 1215 for ( s; E ) { // scan string rows gathering lengths 1216 lnths[r] = strlen( label( s ) ); 1217 if ( lnths[r] > fred ) fred = lnths[r]; 1218 // fprintf( stderr, "%s %d %d\n", label( s ), lnths[r], fred ); 1219 r += 1; 1220 } // for 1221 1222 int mcol = -1; // last match column 1223 char ch, curr = '\0', prev = '\0'; 1224 1225 fmt( is, " " ); // skip optional whitespace 1226 if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); 1227 1228 for ( c; fred ) { // scan columns of the label matix (some columns missing) 1229 int args = fmt( is, "%c", &ch ); // read character 1230 // fprintf( stderr, "fmt args: %d eof: %d\n", args, eof(is) ); 1231 if ( eof( is ) ) { 1232 // fprintf( stderr, "Eof1\n" ); 1233 if ( c == 0 ) return is; // no characters read ? 1234 clear( is ); // => read something => reset EOF => detect again on next read 1235 // fprintf( stderr, "Eof2\n" ); 1236 break; 1237 } // if 1238 if ( args != 1 ) throwResume ExceptionInst( missing_data ); // may be unnecessary since reading single character 1239 1240 // printf( "read '%c'\n", ch ); 1241 for ( r; N ) { // scan enumeration strings for matching character in current column 1242 // printf( "%d %d %d\n", c, r, lnths[r] ); 1243 if ( c < lnths[r] ) { // string long enough for this column check ? 1244 char match = label( fromInt( r ) )[c]; // optimization 1245 // printf( "%c '%c'\n", match, ch ); 1246 // Stop on first match, could be other matches. 1247 if ( (match == ch) && (c == 0 || curr == label( fromInt( r ) )[c - 1]) ) { 1248 // printf( "match %d %d %d '%c' '%c' '%c' '%c' 'c'\n", c, r, lnths[r], match, ch, prev, label( fromInt( r ) )[c - 1] ); 1249 mcol = c; // matching column 1250 prev = curr; // last matching character 1251 curr = ch; // current matching character 1252 break; 1253 } // if 1254 } // if 1255 } else { 1256 // fprintf( stderr, "finished mcol: %d ch: '%c' curr: '%c' prev: '%c'\n", mcol, ch, curr, prev ); 1257 ungetc( ch, is ); // push back last unmatching character 1258 if ( mcol == -1 ) throwResume ExceptionInst( missing_data ); // no matching character in first column 1259 break; 1260 } // for 1261 // printf( "\n" ); 1262 // } else { 1263 // fprintf( stderr, "finished2 %d\n", mcol ); 1264 } // for 1265 1266 for ( c; N ) { // scan enumeration strings of length "mcol" for match 1267 if ( mcol == lnths[c] - 1 ) { 1268 char match = label( fromInt( c ) )[mcol]; // optimization 1269 // printf( "finished1 mcol: %d c: %d lnth: %d match: '%c' curr: '%c' prev: '%c'\n", mcol, c, lnths[c], match, curr, prev ); 1270 if ( (match == curr) && (mcol == 0 || prev == label( fromInt( c ) )[mcol - 1]) ) { 1271 e = fromInt( c ); 1272 break; 1273 } // if 1274 } // if 1275 } else { 1276 // fprintf( stderr, "finished3 %d\n", mcol ); 1277 throwResume ExceptionInst( missing_data ); // no match in this column 1278 } // for 1279 return is; 1280 } 1281 1282 forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { 1283 ostype & ?|?( ostype & os, E e ) { 1284 return os | label( e ); 1285 } 1286 OSTYPE_VOID_IMPL( os, E ) 1287 } 1288 1204 1289 // Local Variables: // 1205 1290 // tab-width: 4 //
Note: See TracChangeset
for help on using the changeset viewer.