Changeset 714e206


Ignore:
Timestamp:
Feb 10, 2024, 5:59:21 PM (3 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
9d4628b
Parents:
956299b
Message:

more cleanup, changes related to detection of missing values during input

Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/collections/string_res.cfa

    r956299b r714e206  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  7 22:21:33 2024
    13 // Update Count     : 81
     12// Last Modified On : Sat Feb 10 17:47:22 2024
     13// Update Count     : 83
    1414//
    1515
     
    252252
    253253ifstream & ?|?( ifstream & is, _Istream_Rquoted f ) with( f.rstr ) {
     254        if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    254255        int args;
    255256  fini: {
    256                 args = fmt( is, "%*[ \f\n\r\t\v]" );                    // remove leading whitespace
    257           if ( eof( is ) ) break fini;
    258                 char rfmt[4] = { delimiters[0], '%', 'n', '\0' };
    259                 int len = 0;                                                                    // may not be set in fmt
    260                 args = fmt( is, rfmt, &len );                                   // remove leading quote
    261           if ( len == 0 || eof( is ) ) break fini;
     257                char rfmt[5] = { ' ', delimiters[0], '%', 'n', '\0' };
     258                int len = -1;                                                                   // may not be set in fmt
     259                args = fmt( is, rfmt, &len );                                   // remove leading whitespace and quote
     260          if ( eof( is ) || len == -1 ) break fini;
    262261
    263262                // Change the remainder of the read into a getline by reseting the closing delimiter.
  • libcfa/src/iostream.cfa

    r956299b r714e206  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  7 22:19:59 2024
    13 // Update Count     : 1918
     12// Last Modified On : Sat Feb 10 09:28:32 2024
     13// Update Count     : 1946
    1414//
    1515
     
    774774forall( istype & | basic_istream( istype ) ) {
    775775        istype & ?|?( istype & is, bool & b ) {
     776                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    776777                char val[6];
    777778                int args = fmt( is, "%5s", val );
    778                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     779                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    779780                if ( strcmp( val, "true" ) == 0 ) b = true;
    780781                else if ( strcmp( val, "false" ) == 0 ) b = false;
     
    787788
    788789        istype & ?|?( istype & is, char & c ) {
     790                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    789791                char temp;
    790792                for () {
    791793                        int args = fmt( is, "%c", &temp );
    792                         if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     794                        if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    793795                        // do not overwrite parameter with newline unless appropriate
    794796                        if ( temp != '\n' || getANL$( is ) ) { c = temp; break; }
     
    799801
    800802        istype & ?|?( istype & is, signed char & sc ) {
     803                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    801804                int args = fmt( is, "%hhi", &sc );
    802                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     805                if ( args != 1 ) throwResume ExceptionInst( missing_data );
    803806                return is;
    804807        } // ?|?
    805808
    806809        istype & ?|?( istype & is, unsigned char & usc ) {
     810                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    807811                int args = fmt( is, "%hhi", &usc );
    808                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     812                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    809813                return is;
    810814        } // ?|?
    811815
    812816        istype & ?|?( istype & is, short int & si ) {
     817                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    813818                int args = fmt( is, "%hi", &si );
    814                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     819                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    815820                return is;
    816821        } // ?|?
    817822
    818823        istype & ?|?( istype & is, unsigned short int & usi ) {
     824                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    819825                int args = fmt( is, "%hi", &usi );
    820                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     826                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    821827                return is;
    822828        } // ?|?
    823829
    824830        istype & ?|?( istype & is, int & i ) {
     831                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    825832                int args = fmt( is, "%i", &i );
    826                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     833                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    827834                return is;
    828835        } // ?|?
    829836
    830837        istype & ?|?( istype & is, unsigned int & ui ) {
     838                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    831839                int args = fmt( is, "%i", &ui );
    832                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     840                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    833841                return is;
    834842        } // ?|?
    835843
    836844        istype & ?|?( istype & is, long int & li ) {
     845                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    837846                int args = fmt( is, "%li", &li );
    838                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     847                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    839848                return is;
    840849        } // ?|?
    841850
    842851        istype & ?|?( istype & is, unsigned long int & ulli ) {
     852                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    843853                int args = fmt( is, "%li", &ulli );
    844                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     854                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    845855                return is;
    846856        } // ?|?
    847857
    848858        istype & ?|?( istype & is, long long int & lli ) {
     859                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    849860                int args = fmt( is, "%lli", &lli );
    850                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     861                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    851862                return is;
    852863        } // ?|?
    853864
    854865        istype & ?|?( istype & is, unsigned long long int & ulli ) {
     866                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    855867                int args = fmt( is, "%lli", &ulli );
    856                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     868                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    857869                return is;
    858870        } // ?|?
     
    881893
    882894        istype & ?|?( istype & is, float & f ) {
     895                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    883896                int args = fmt( is, "%f", &f );
    884                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     897                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    885898                return is;
    886899        } // ?|?
    887900
    888901        istype & ?|?( istype & is, double & d ) {
     902                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    889903                int args = fmt( is, "%lf", &d );
    890                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     904                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    891905                return is;
    892906        } // ?|?
    893907
    894908        istype & ?|?( istype & is, long double & ld ) {
     909                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    895910                int args = fmt( is, "%Lf", &ld );
    896                 if ( args != -1 && args != 1 ) throwResume ExceptionInst( missing_data );
     911                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    897912                return is;
    898913        } // ?|?
    899914
    900915        istype & ?|?( istype & is, float _Complex & fc ) {
     916                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    901917                float re, im;
    902918                int args = fmt( is, "%f%fi", &re, &im );
    903                 if ( args != -1 && args != 2 ) throwResume ExceptionInst( missing_data );
     919                if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data );
    904920                fc = re + im * _Complex_I;
    905921                return is;
     
    907923
    908924        istype & ?|?( istype & is, double _Complex & dc ) {
     925                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    909926                double re, im;
    910927                int args = fmt( is, "%lf%lfi", &re, &im );
    911                 if ( args != -1 && args != 2 ) throwResume ExceptionInst( missing_data );
     928                if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data );
    912929                dc = re + im * _Complex_I;
    913930                return is;
     
    915932
    916933        istype & ?|?( istype & is, long double _Complex & ldc ) {
     934                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    917935                long double re, im;
    918936                int args = fmt( is, "%Lf%Lfi", &re, &im );
    919                 if ( args != -1 && args != 2 ) throwResume ExceptionInst( missing_data );
     937                if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data );
    920938                ldc = re + im * _Complex_I;
    921939                return is;
     
    923941
    924942        istype & ?|?( istype & is, const char fmt[] ) {
     943                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    925944                size_t len = strlen( fmt );
    926                 char fmt2[len + 16];
    927                 strcpy( fmt2, fmt );                                                    // copy format and add %n
    928                 strcpy( &fmt2[len], "%n" );
     945                char fmtstr[len + 16];
     946                strcpy( fmtstr, fmt );                                                  // copy format and add %n
     947                strcpy( &fmtstr[len], "%n" );
    929948                int len2 = -1;
    930                 int args = fmt( is, fmt2, &len2 );
    931                 if ( args != -1 && len2 == -1 ) throwResume ExceptionInst( missing_data );
     949                fmt( is, fmtstr, &len2 );
     950                if ( ! eof( is ) && len2 == -1 ) throwResume ExceptionInst( missing_data );
    932951                return is;
    933952        } // ?|?
     
    963982forall( istype & | basic_istream( istype ) ) {
    964983        istype & ?|?( istype & is, _Istream_Cskip f ) {
    965                 // printf( "skip %s %d\n", f.scanset, f.wd );
     984                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    966985                if ( f.scanset ) {
    967986                        int nscanset = strlen(f.scanset);
     
    971990                        strcpy( &fmtstr[pos], f.scanset );  pos += nscanset;
    972991                        strcpy( &fmtstr[pos], "]" );
    973                         fmt( is, fmtstr, "" );                                          // skip scanset
     992                        fmt( is, fmtstr, "" );                                          // skip scanset, zero or more
    974993                } else {
    975994                        char ch;
    976                         // fprintf( stderr, "skip " );
    977995                        for ( f.wd ) {                                                          // skip N characters
    978                           if ( eof( is ) ) break;
    979                                 fmt( is, "%c", &ch );
    980                                 // fprintf( stderr, "`%c' ", ch );
     996                                int args = fmt( is, "%c", &ch );
     997                                if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    981998                        } // for
    982999                } // if
     
    9851002
    9861003        istype & ?|?( istype & is, _Istream_Cquoted f ) with( f.cstr ) {
     1004                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    9871005                int args;
    9881006          fini: {
    989                         args = fmt( is, "%*[ \f\n\r\t\v]" );            // remove leading whitespace
    990                   if ( eof( is ) ) break fini;
    991                         char rfmt[4] = { delimiters[0], '%', 'n', '\0' };
    992                         int len = 0;                                                            // may not be set in fmt
    993                         args = fmt( is, rfmt, &len );                           // remove leading quote
    994                   if ( len == 0 || eof( is ) ) break fini;
     1007                        char rfmt[5] = { ' ', delimiters[0], '%', 'n', '\0' };
     1008                        int len = -1;                                                           // may not be set in fmt
     1009                        args = fmt( is, rfmt, &len );                           // remove leading whitespace and quote
     1010                        if ( eof( is ) || len == -1 ) break fini;
    9951011
    9961012                        // Change the remainder of the read into a getline by reseting the closing delimiter.
     
    10111027
    10121028        istype & ?|?( istype & is, _Istream_Cstr f ) with( f.cstr ) {
     1029                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    10131030                const char * scanset;
    10141031                size_t nscanset = 0;
     
    10461063                                fmt( is, "%c", &peek );                                 // check for whitespace terminator
    10471064                                // fprintf( stderr, "peek %d '%c'\n", args, peek );
    1048                                 if ( ! eof( is ) ) {
     1065                                if ( ! eof( is ) ) {                                    // can only fail at eof
    10491066                                        ungetc( is, peek );
    10501067                                        if ( ! isspace( peek ) ) throwResume ExceptionInst( cstring_length );
     
    10561073                        if ( flags.delimiter ) {                                        // getline
    10571074                                int len = 0;                                                    // may not be set in fmt
    1058                                 if ( delimiters[2] != '\0' ) {                  // read single character ?
     1075                                if ( delimiters[2] != '\0' ) {                  // (quoted) read single character ?
    10591076                                        sprintf( &fmtstr[pos], "c%%n" );
    10601077                                } else {
     
    10631080                                if ( flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*'
    10641081                                else args = fmt( is, fmtstr, s, &len );
     1082
     1083                                // cannot have empty character constant ''
     1084                                if ( delimiters[2] != '\0' && len != 1 ) throwResume ExceptionInst( missing_data );
     1085
    10651086                                if ( check && len == rwd && ! eof( is ) ) {     // might not fit
    10661087                                        char peek;
     
    10961117                } // if
    10971118                if ( args == 1 && eof( is ) ) {                                 // data but scan ended at EOF
    1098                         // fprintf( stderr, "clear\n" );
    10991119                        clear( is );                                                            // => reset EOF => detect again on next read
    11001120                } // if
Note: See TracChangeset for help on using the changeset viewer.