Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r37ceccb rf842032  
    1 
    21//
    32// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
     
    1110// Created On       : Wed May 27 17:56:53 2015
    1211// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Sun Oct  8 12:10:21 2023
    14 // Update Count     : 1564
     12// Last Modified On : Thu Oct 19 19:29:54 2023
     13// Update Count     : 1800
    1514//
    1615
     
    4847                return os;
    4948        } // ?|?
    50         OSTYPE_VOID_IMPL( bool )
     49        OSYPE_VOID_IMPL( bool )
    5150
    5251        ostype & ?|?( ostype & os, char c ) {
     
    785784                return is;
    786785        } // ?|?
    787         ISTYPE_VOID_IMPL( bool & )
    788786
    789787        istype & ?|?( istype & is, char & c ) {
     
    797795                return is;
    798796        } // ?|?
    799         ISTYPE_VOID_IMPL( char & )
    800797
    801798        istype & ?|?( istype & is, signed char & sc ) {
     
    803800                return is;
    804801        } // ?|?
    805         ISTYPE_VOID_IMPL( signed char & )
    806802
    807803        istype & ?|?( istype & is, unsigned char & usc ) {
     
    809805                return is;
    810806        } // ?|?
    811         ISTYPE_VOID_IMPL( unsigned char & )
    812807
    813808        istype & ?|?( istype & is, short int & si ) {
     
    815810                return is;
    816811        } // ?|?
    817         ISTYPE_VOID_IMPL( short int & )
    818812
    819813        istype & ?|?( istype & is, unsigned short int & usi ) {
     
    821815                return is;
    822816        } // ?|?
    823         ISTYPE_VOID_IMPL( unsigned short int & )
    824817
    825818        istype & ?|?( istype & is, int & i ) {
     
    827820                return is;
    828821        } // ?|?
    829         ISTYPE_VOID_IMPL( int & )
    830822
    831823        istype & ?|?( istype & is, unsigned int & ui ) {
     
    833825                return is;
    834826        } // ?|?
    835         ISTYPE_VOID_IMPL( unsigned int & )
    836827
    837828        istype & ?|?( istype & is, long int & li ) {
     
    839830                return is;
    840831        } // ?|?
    841         ISTYPE_VOID_IMPL( long int & )
    842832
    843833        istype & ?|?( istype & is, unsigned long int & ulli ) {
     
    845835                return is;
    846836        } // ?|?
    847         ISTYPE_VOID_IMPL( unsigned long int & )
    848837
    849838        istype & ?|?( istype & is, long long int & lli ) {
     
    851840                return is;
    852841        } // ?|?
    853         ISTYPE_VOID_IMPL( long long int & )
    854842
    855843        istype & ?|?( istype & is, unsigned long long int & ulli ) {
     
    857845                return is;
    858846        } // ?|?
    859         ISTYPE_VOID_IMPL( unsigned long long int & )
    860847
    861848        #if defined( __SIZEOF_INT128__ )
     
    863850                return (istype &)(is | (unsigned int128 &)llli);
    864851        } // ?|?
    865         ISTYPE_VOID_IMPL( int128 & )
    866852
    867853        istype & ?|?( istype & is, unsigned int128 & ullli ) {
     
    880866                return is;
    881867        } // ?|?
    882         ISTYPE_VOID_IMPL( unsigned int128 & )
    883868        #endif // __SIZEOF_INT128__
    884869
     
    887872                return is;
    888873        } // ?|?
    889         ISTYPE_VOID_IMPL( float & )
    890874
    891875        istype & ?|?( istype & is, double & d ) {
     
    893877                return is;
    894878        } // ?|?
    895         ISTYPE_VOID_IMPL( double & )
    896879
    897880        istype & ?|?( istype & is, long double & ld ) {
     
    899882                return is;
    900883        } // ?|?
    901         ISTYPE_VOID_IMPL( long double & )
    902884
    903885        istype & ?|?( istype & is, float _Complex & fc ) {
     
    907889                return is;
    908890        } // ?|?
    909         ISTYPE_VOID_IMPL( float _Complex & )
    910891
    911892        istype & ?|?( istype & is, double _Complex & dc ) {
     
    915896                return is;
    916897        } // ?|?
    917         ISTYPE_VOID_IMPL( double _Complex & )
    918898
    919899        istype & ?|?( istype & is, long double _Complex & ldc ) {
     
    923903                return is;
    924904        } // ?|?
    925         ISTYPE_VOID_IMPL( long double _Complex & )
    926905
    927906        istype & ?|?( istype & is, const char fmt[] ) {
     
    929908                return is;
    930909        } // ?|?
    931         ISTYPE_VOID_IMPL( const char * )
    932910
    933911        // manipulators
     
    937915
    938916        void ?|?( istype & is, istype & (* manip)( istype & ) ) {
    939                 manip( is ); ends( is );
     917                manip( is );
    940918        } // ?|?
    941919
     
    966944                        char fmtstr[ sizeof("%*[]") + nscanset ];
    967945                        int pos = 0;
    968                         fmtstr[pos] = '%';                  pos += 1;
    969                         fmtstr[pos] = '*';                  pos += 1;
    970                         fmtstr[pos] = '[';                  pos += 1;
     946                        strcpy( &fmtstr[pos], "%*[" );  pos += 3;
    971947                        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" );
     948                        strcpy( &fmtstr[pos], "]" );
     949                        fmt( is, fmtstr, "" );                                          // skip scanset
     950                } else {
     951                        char ch;
     952//                      fprintf( stderr, "skip " );
     953                        for ( f.wd ) {                                                          // skip N characters
     954                          if ( eof( is ) ) break;
     955                                fmt( is, "%c", &ch );
     956//                              fprintf( stderr, "`%c' ", ch );
     957                        } // for
     958                } // if
    977959                return is;
    978960        }
    979         ISTYPE_VOID_IMPL( _Istream_Cskip )
    980961
    981962        istype & ?|?( istype & is, _Istream_Cstr f ) {
    982                 const char * scanset = f.scanset;
     963                const char * scanset;
     964                size_t nscanset = 0;
    983965                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;
     966                else scanset = f.scanset;
     967                if ( scanset ) nscanset = strlen( scanset );
     968
     969                char fmtstr[nscanset + 32];                                             // storage for scanset and format codes
    989970                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 ) {
     971
     972                int pos = 1;
     973                int args;
     974                bool check = true;
     975
     976                if ( f.flags.ignore ) { check = false; fmtstr[1] = '*'; pos += 1; }
     977                int rwd = f.wd;
     978                if ( f.wd != -1 ) {                                                             // => just ignore versus ignore with width
    993979                        // wd is buffer bytes available (for input chars + null terminator)
    994980                        // rwd is count of input chars
    995                         int rwd;
    996                         if (f.flags.rwd) {
    997                                 verify (f.wd >= 0);
    998                                 rwd = f.wd;
     981                        // no maximum width necessary because text ignored => width is read width
     982                        if ( f.flags.rwd ) check = false;
     983                        else rwd = f.wd - 1;
     984                        pos += sprintf( &fmtstr[pos], "%d", rwd );
     985                } // if
     986
     987                if ( ! scanset ) {                                                              // %s, %*s, %ws, %*ws
     988//                      fprintf( stderr, "cstr %s\n", f.s );
     989                        strcpy( &fmtstr[pos], "s%n" );
     990                        int len = 0;                                                            // may not be set in fmt
     991                        if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*'
     992                        else args = fmt( is, fmtstr, f.s, &len );
     993//                      fprintf( stderr, "cstr %s %d %d %d %s\n", fmtstr, args, len, f.wd, f.s );
     994                        if ( check && len >= rwd && ! eof( is ) ) {     // might not fit
     995                                char peek;
     996                                fmt( is, "%c", &peek );                                 // check for whitespace terminator
     997//                              fprintf( stderr, "peek %d '%c'\n", args, peek );
     998                                if ( ! eof( is ) ) {
     999                                        ungetc( is, peek );
     1000                                        if ( ! isspace( peek ) ) throw ExceptionInst( cstring_length );
     1001                                } // if
     1002                        } // if
     1003                } else {
     1004                        if ( f.flags.delimiter ) {                                      // getline
     1005//                              fprintf( stderr, "getline\n" );
     1006                                sprintf( &fmtstr[pos], "[%s%s]%%n", f.flags.inex ? "^" : "", scanset );
     1007//                              fprintf( stderr, "getline %s %d\n", fmtstr, f.wd );
     1008                                int len = 0;                                                    // may not be set in fmt
     1009                                if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*'
     1010                                else args = fmt( is, fmtstr, f.s, &len );
     1011//                              fprintf( stderr, "getline %s %d %d %d\n", fmtstr, args, f.wd, eof( is ) );
     1012                                if ( check && len == rwd && ! eof( is ) ) {     // might not fit
     1013                                        char peek;
     1014                                        fmt( is, "%c", &peek );                         // check for delimiter
     1015//                                      fprintf( stderr, "peek %d '%c'\n", args, peek );
     1016                                        if ( ! eof( is ) ) {
     1017                                                if ( peek != f.delimiter[0] ) {
     1018                                                        ungetc( is, peek );
     1019                                                        throw ExceptionInst( cstring_length );
     1020                                                } // if
     1021                                        } // if
     1022                                } else fmt( is, "%*c" );                                //  remove delimiter
    9991023                        } else {
    1000                                 verify (f.wd >= 1);
    1001                                 rwd = f.wd - 1;
     1024                                // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
     1025                                // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
     1026                                sprintf( &fmtstr[pos], "[%s%s]%%n", f.flags.inex ? "^" : "", scanset );
     1027//                              fprintf( stderr, "incl/excl %s %d\n", fmtstr, f.wd );
     1028                                int len = 0;                                                    // may not be set in fmt
     1029                                if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*'
     1030                                else args = fmt( is, fmtstr, f.s, &len );
     1031//                              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] );
     1032                                if ( check && len == rwd && ! eof( is ) ) {     // might not fit
     1033//                                      fprintf( stderr, "overflow\n" );
     1034                                        char peek;
     1035                                        fmt( is, "%c", &peek );                         // check for whitespace terminator
     1036//                                      fprintf( stderr, "peek %d '%c'\n", args, peek );
     1037                                        if ( ! eof( is ) ) {
     1038                                                ungetc( is, peek );
     1039                                                if ( f.flags.inex ^ strchr( f.scanset, peek ) != 0p ) throw ExceptionInst( cstring_length );
     1040                                        } // if
     1041                                } // if
    10021042                        } // 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
    1055                 return is;
    1056         } // ?|?
    1057         ISTYPE_VOID_IMPL( _Istream_Cstr )
     1043                } // if
     1044                if ( args == 1 && eof( is ) ) {                                 // data but scan ended at EOF
     1045//                      fprintf( stderr, "clear\n" );
     1046                        clear( is );                                                            // => reset EOF => detect again on next read
     1047                } // if
     1048                return is;
     1049        } // ?|?
    10581050
    10591051        istype & ?|?( istype & is, _Istream_Char f ) {
     
    10611053                return is;
    10621054        } // ?|?
    1063         ISTYPE_VOID_IMPL( _Istream_Char )
    10641055} // distribution
    10651056
     
    10781069                return is; \
    10791070        } /* ?|? */ \
    1080         ISTYPE_VOID_IMPL( _Istream_Manip(T) ) \
    10811071} // distribution
    10821072
     
    11061096                return is;
    11071097        } // ?|?
    1108         ISTYPE_VOID_IMPL( _Istream_Manip(float _Complex) )
    11091098
    11101099        istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
     
    11171106                return is;
    11181107        } // ?|?
    1119         ISTYPE_VOID_IMPL( _Istream_Manip(double _Complex) )
    11201108
    11211109        istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
     
    11281116                return is;
    11291117        } // ?|?
    1130         ISTYPE_VOID_IMPL( _Istream_Manip(long double _Complex) )
    11311118} // distribution
    11321119
Note: See TracChangeset for help on using the changeset viewer.