Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r04138cc reae8b37  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Jan 22 07:31:19 2025
    13 // Update Count     : 2079
     12// Last Modified On : Sun Oct 13 10:53:09 2024
     13// Update Count     : 2041
    1414//
    1515
     
    777777forall( istype & | basic_istream( istype ) ) {
    778778        istype & ?|?( istype & is, bool & b ) {
     779                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    779780                int len = -1;                                                                   // len not set if no match
    780                 fmt( is, " " );                                                                 // remove leading whitespace
    781                 fmt( is, FALSE "%n", &len );                                    // try false, returns 0
     781                // remove optional leading whitespace at start of strings.
     782                fmt( is, " " FALSE "%n", &len );                                // try false
    782783                if ( len != sizeof( FALSE ) - 1 ) {                             // -1 removes null terminate
    783                         if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    784                         fmt( is, " " );                                                         // remove leading whitespace
    785                         fmt( is, TRUE "%n", &len );                                     // try true, returns 0
     784                        fmt( is, " " TRUE "%n", &len );                         // try true
    786785                        if ( len != sizeof( TRUE ) - 1 ) throwResume ExceptionInst( missing_data );
    787786                        b = true;
     
    793792
    794793        istype & ?|?( istype & is, char & c ) {
     794                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    795795                char temp;
    796796                for () {
    797                         int args = fmt( is, "%c", &temp );                      // can be called with EOF on
     797                        int args = fmt( is, "%c", &temp );
    798798                        if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    799799                        assert( args == 1 );                                            // if not EOF => a single character must be read
     
    805805
    806806        istype & ?|?( istype & is, signed char & sc ) {
     807                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    807808                int args = fmt( is, "%hhi", &sc );                              // can be multiple characters (100)
    808809                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    809                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    810810                return is;
    811811        } // ?|?
    812812
    813813        istype & ?|?( istype & is, unsigned char & usc ) {
     814                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    814815                int args = fmt( is, "%hhi", &usc );                             // can be multiple characters (-100)
    815816                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    816                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    817817                return is;
    818818        } // ?|?
    819819
    820820        istype & ?|?( istype & is, short int & si ) {
    821                 int args = fmt( is, "%hi", &si );                               // can be called with EOF on
     821                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     822                int args = fmt( is, "%hi", &si );
    822823                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    823                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    824824                return is;
    825825        } // ?|?
    826826
    827827        istype & ?|?( istype & is, unsigned short int & usi ) {
    828                 int args = fmt( is, "%hi", &usi );                              // can be called with EOF on
     828                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     829                int args = fmt( is, "%hi", &usi );
    829830                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    830                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    831831                return is;
    832832        } // ?|?
    833833
    834834        istype & ?|?( istype & is, int & i ) {
    835                 int args = fmt( is, "%i", &i );                                 // can be called with EOF on
     835                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     836                int args = fmt( is, "%i", &i );
    836837                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    837                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    838838                return is;
    839839        } // ?|?
    840840
    841841        istype & ?|?( istype & is, unsigned int & ui ) {
    842                 int args = fmt( is, "%i", &ui );                                // can be called with EOF on
     842                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     843                int args = fmt( is, "%i", &ui );
    843844                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    844                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    845845                return is;
    846846        } // ?|?
    847847
    848848        istype & ?|?( istype & is, long int & li ) {
    849                 int args = fmt( is, "%li", &li );                               // can be called with EOF on
     849                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     850                int args = fmt( is, "%li", &li );
    850851                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    851                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    852852                return is;
    853853        } // ?|?
    854854
    855855        istype & ?|?( istype & is, unsigned long int & ulli ) {
    856                 int args = fmt( is, "%li", &ulli );                             // can be called with EOF on
     856                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     857                int args = fmt( is, "%li", &ulli );
    857858                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    858                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    859859                return is;
    860860        } // ?|?
    861861
    862862        istype & ?|?( istype & is, long long int & lli ) {
    863                 int args = fmt( is, "%lli", &lli );                             // can be called with EOF on
     863                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     864                int args = fmt( is, "%lli", &lli );
    864865                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    865                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    866866                return is;
    867867        } // ?|?
    868868
    869869        istype & ?|?( istype & is, unsigned long long int & ulli ) {
    870                 int args = fmt( is, "%lli", &ulli );                    // can be called with EOF on
     870                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     871                int args = fmt( is, "%lli", &ulli );
    871872                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    872                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    873873                return is;
    874874        } // ?|?
     
    897897
    898898        istype & ?|?( istype & is, float & f ) {
    899                 int args = fmt( is, "%f", &f );                                 // can be called with EOF on
     899                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     900                int args = fmt( is, "%f", &f );
    900901                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    901                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    902902                return is;
    903903        } // ?|?
    904904
    905905        istype & ?|?( istype & is, double & d ) {
    906                 int args = fmt( is, "%lf", &d );                                // can be called with EOF on
     906                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     907                int args = fmt( is, "%lf", &d );
    907908                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    908                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    909909                return is;
    910910        } // ?|?
    911911
    912912        istype & ?|?( istype & is, long double & ld ) {
    913                 int args = fmt( is, "%Lf", &ld );                               // can be called with EOF on
     913                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     914                int args = fmt( is, "%Lf", &ld );
    914915                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    915                 if ( eof( is ) && args != 1 ) throwResume ExceptionInst( end_of_file );
    916916                return is;
    917917        } // ?|?
    918918
    919919        istype & ?|?( istype & is, float _Complex & fc ) {
     920                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    920921                float re, im;
    921                 int args = fmt( is, "%f%fi", &re, &im );                // can be called with EOF on
     922                int args = fmt( is, "%f%fi", &re, &im );
    922923                if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data );
    923                 if ( eof( is ) && args != 2 ) throwResume ExceptionInst( end_of_file );
    924924                fc = re + im * _Complex_I;
    925925                return is;
     
    927927
    928928        istype & ?|?( istype & is, double _Complex & dc ) {
     929                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    929930                double re, im;
    930                 int args = fmt( is, "%lf%lfi", &re, &im );              // can be called with EOF on
     931                int args = fmt( is, "%lf%lfi", &re, &im );
    931932                if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data );
    932                 if ( eof( is ) && args != 2 ) throwResume ExceptionInst( end_of_file );
    933933                dc = re + im * _Complex_I;
    934934                return is;
     
    936936
    937937        istype & ?|?( istype & is, long double _Complex & ldc ) {
     938                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    938939                long double re, im;
    939                 int args = fmt( is, "%Lf%Lfi", &re, &im );              // can be called with EOF on
     940                int args = fmt( is, "%Lf%Lfi", &re, &im );
    940941                if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data );
    941                 if ( eof( is ) && args != 2 ) throwResume ExceptionInst( end_of_file );
    942942                ldc = re + im * _Complex_I;
    943943                return is;
     
    945945
    946946        istype & ?|?( istype & is, const char fmt[] ) {         // match text
     947                if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    947948                size_t len = strlen( fmt );
    948949                char fmtstr[len + 16];
     
    950951                strcpy( &fmtstr[len], "%n" );
    951952                len = -1;
    952                 // scanf cursor does not move if no match, so eof cannot be detected.
    953                 fmt( is, fmtstr, &len );                                                // can be called with EOF on
    954                 if ( ! eof( is ) && len == -1 ) throwResume ExceptionInst( missing_data );
    955                 if ( eof( is ) && len == -1 ) throwResume ExceptionInst( end_of_file );
     953                // scanf cursor does not move if no match
     954                fmt( is, fmtstr, &len );
     955                if ( len == -1 ) throwResume ExceptionInst( missing_data );
    956956                return is;
    957957        } // ?|?
     
    999999                        char ch;
    10001000                        for ( f.wd ) {                                                          // skip N characters
    1001                                 int args = fmt( is, "%c", &ch );                // can be called with EOF on
     1001                                int args = fmt( is, "%c", &ch );
    10021002                                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    10031003                        } // for
     
    12041204forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial( E ) )
    12051205istype & ?|?( istype & is, E & e ) {
    1206 //      if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     1206//      fprintf( stderr, "here0\n" );
     1207        if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    12071208
    12081209        // Match longest input enumerator string to enumerator labels, where enumerator names are unique.
    12091210
    12101211        int N = countof( E ), lnths[N], fred = 0;
     1212//      printf( "N %d\n", N );
    12111213        int r = 0;
     1214        // for ( s; E : r; 0~@ ) {
    12121215        for ( s; E ) {                                                                          // scan string rows gathering lengths
    12131216                lnths[r] = strlen( label( s ) );
    12141217                if ( lnths[r] > fred ) fred = lnths[r];
     1218//              fprintf( stderr, "%s %d %d\n", label( s ), lnths[r], fred );
    12151219                r += 1;
    12161220        } // for
     
    12191223        char ch, curr = '\0', prev = '\0';
    12201224
    1221         fmt( is, " " );                                                                         // remove leading whitespace
     1225        fmt( is, " " );                                                                         // skip optional whitespace
    12221226        if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    12231227
    12241228        for ( c; fred ) {                                                                       // scan columns of the label matix (some columns missing)
    12251229                int args = fmt( is, "%c", &ch );                                // read character
     1230//              fprintf( stderr, "fmt args: %d eof: %d\n", args, eof(is) );
    12261231          if ( eof( is ) ) {
     1232//                      fprintf( stderr, "Eof1\n" );
    12271233                        if ( c == 0 ) return is;                                        // no characters read ?
    12281234                        clear( is );                                                            // => read something => reset EOF => detect again on next read
     1235//                      fprintf( stderr, "Eof2\n" );
    12291236                        break;
    12301237                } // if
    12311238          if ( args != 1 ) throwResume ExceptionInst( missing_data ); // may be unnecessary since reading single character
    12321239
     1240//              printf( "read '%c'\n", ch );
    12331241                for ( r; N ) {                                                                  // scan enumeration strings for matching character in current column
     1242//                      printf( "%d %d %d\n", c, r, lnths[r] );
    12341243                        if ( c < lnths[r] ) {                                           // string long enough for this column check ?
    12351244                                char match = label( fromInt( r ) )[c];  // optimization
     1245//                              printf( "%c '%c'\n", match, ch );
    12361246                                // Stop on first match, could be other matches.
    12371247                                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] );
    12381249                                        mcol = c;                                                       // matching column
    12391250                                        prev = curr;                                            // last matching character
     
    12431254                        } // if
    12441255                } else {
     1256//                      fprintf( stderr, "finished mcol: %d ch: '%c' curr: '%c' prev: '%c'\n", mcol, ch, curr, prev );
    12451257                        ungetc( ch, is );                                                       // push back last unmatching character
    12461258                        if ( mcol == -1 ) throwResume ExceptionInst( missing_data ); // no matching character in first column
    12471259                        break;
    12481260                } // for
     1261//              printf( "\n" );
     1262//      } else {
     1263//              fprintf( stderr, "finished2 %d\n", mcol );
    12491264        } // for
    12501265
     
    12521267                if ( mcol == lnths[c] - 1 ) {
    12531268                        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 );
    12541270                        if ( (match == curr) && (mcol == 0 || prev == label( fromInt( c ) )[mcol - 1]) ) {
    12551271                                e = fromInt( c );
     
    12581274                } // if
    12591275        } else {
     1276//              fprintf( stderr, "finished3 %d\n", mcol );
    12601277                throwResume ExceptionInst( missing_data );              // no match in this column
    12611278        } // for
Note: See TracChangeset for help on using the changeset viewer.