Changeset 0f107e4


Ignore:
Timestamp:
Sep 2, 2023, 2:57:43 PM (8 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
34c6e1e6
Parents:
686912c
Message:

add reading fixed strings, seperate skip input manipulator, getline checks for delimiter

Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r686912c r0f107e4  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 31 11:27:56 2023
    13 // Update Count     : 1545
     12// Last Modified On : Sat Sep  2 14:42:01 2023
     13// Update Count     : 1561
    1414//
    1515
     
    922922        ISTYPE_VOID_IMPL( long double _Complex & )
    923923
    924         // istype & ?|?( istype & is, const char fmt[] ) {
    925         //      fmt( is, fmt, "" );
    926         //      return is;
    927         // } // ?|?
    928 
    929         // istype & ?|?( istype & is, char s[] ) {
    930         //      fmt( is, "%s", s );
    931         //      return is;
    932         // } // ?|?
    933         // ISTYPE_VOID_IMPL( char * )
     924        istype & ?|?( istype & is, const char fmt[] ) {
     925                fmt( is, fmt, "" );
     926                return is;
     927        } // ?|?
     928        ISTYPE_VOID_IMPL( const char * )
    934929
    935930        // manipulators
     
    962957
    963958forall( istype & | basic_istream( istype ) ) {
     959        istype & ?|?( istype & is, _Istream_Cskip f ) {
     960                // printf( "skip %s %d\n", f.scanset, f.wd );
     961                if ( f.scanset ) fmt( is, f.scanset, "" );              // no input arguments
     962                else for ( f.wd ) fmt( is, "%*c" );
     963                return is;
     964        }
     965        ISTYPE_VOID_IMPL( _Istream_Cskip )
     966
    964967        istype & ?|?( istype & is, _Istream_Cstr f ) {
    965                 // skip
    966                 if ( ! f.s ) {
    967                         // printf( "skip %s %d\n", f.scanset, f.wd );
    968                         if ( f.wd == -1 ) fmt( is, f.scanset, "" );     // no input arguments
    969                         else for ( f.wd ) fmt( is, "%*c" );
    970                         return is;
    971                 } // if
    972 
    973968                const char * scanset = f.scanset;
    974                 if ( f.flags.delimit ) scanset = f.delimit;             // getline ?
     969                if ( f.flags.delimiter ) scanset = f.delimiter; // getline ?
    975970
    976971                size_t len = 0;
     
    980975                fmtstr[0] = '%';
    981976                if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
     977                // no maximum width necessary because text ignored => width is read width
    982978                if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
    983979
     
    10051001                        throw (cstring_length){ &cstring_length_vt };
    10061002
    1007                 if ( f.flags.delimit ) {                                                // getline ?
     1003                if ( f.flags.delimiter ) {                                              // getline ?
    10081004                        if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
    1009                         if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
     1005                        if ( ! eof( is ) ) {                                            // ignore delimiter, may not be present because of width
     1006                                char delimiter;
     1007                                fmt( is, "%c", &delimiter );
     1008                                if ( delimiter != f.delimiter[0] ) ungetc( is, delimiter );
     1009                        } // if
    10101010                } //if
    10111011                return is;
  • libcfa/src/iostream.hfa

    r686912c r0f107e4  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 31 10:55:35 2023
    13 // Update Count     : 544
     12// Last Modified On : Sat Sep  2 14:42:13 2023
     13// Update Count     : 567
    1414//
    1515
     
    388388        ISTYPE_VOID( long double _Complex & );
    389389
    390 //      istype & ?|?( istype &, const char [] );
    391 //      istype & ?|?( istype &, char [] );
    392 //      ISTYPE_VOID( char [] );
     390        istype & ?|?( istype &, const char [] );
     391        ISTYPE_VOID( const char [] );
    393392
    394393        // manipulators
     
    406405// *********************************** manipulators ***********************************
    407406
     407struct _Istream_Cskip {
     408        const char * scanset;
     409        unsigned wd;                                                                            // scan width
     410}; // _Istream_Cskip
     411
     412static inline {
     413        _Istream_Cskip skip( const char scanset[] ) { return (_Istream_Cskip)@{ scanset, 0 }; }
     414        _Istream_Cskip skip( unsigned int wd ) { return (_Istream_Cskip)@{ 0p, wd }; }
     415} // distribution
     416forall( istype & | basic_istream( istype ) ) {
     417        istype & ?|?( istype & is, _Istream_Cskip f );
     418        ISTYPE_VOID( _Istream_Cskip );
     419}
     420
    408421struct _Istream_str_base {
    409422        union {
    410423                const char * scanset;
    411                 char delimit[2];
     424                char delimiter[2];
    412425        };
    413426        int wd;                                                                                         // width
     
    417430                        unsigned char ignore:1;                                         // do not change input argument
    418431                        unsigned char inex:1;                                           // include/exclude characters in scanset
    419                         unsigned char delimit:1;                                        // delimit character
     432                        unsigned char delimiter:1;                                      // delimit character
    420433                        unsigned char rwd:1;                                            // read width
    421434                } flags;
     
    436449                return (_Istream_Cstr)@{ s, { {0p}, rwd, {.flags.rwd : true} } };
    437450        }
    438         _Istream_Cstr skip( const char scanset[] ) { return (_Istream_Cstr)@{ 0p, { {scanset}, -1, {.all : 0} } }; }
    439         _Istream_Cstr skip( unsigned int wd ) { return (_Istream_Cstr)@{ 0p, { {0p}, wd, {.all : 0} } }; }
    440         _Istream_Cstr & getline( _Istream_Cstr & fmt, const char delimit = '\n' ) {
    441                 fmt.delimit[0] = delimit; fmt.delimit[1] = '\0'; fmt.flags.delimit = true; fmt.flags.inex = true; return fmt; }
     451        _Istream_Cstr & getline( _Istream_Cstr & fmt, const char delimiter = '\n' ) {
     452                fmt.delimiter[0] = delimiter; fmt.delimiter[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt; }
    442453        _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
    443454        _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
Note: See TracChangeset for help on using the changeset viewer.