Changeset 38de914 for libcfa


Ignore:
Timestamp:
Aug 30, 2023, 11:20:14 AM (12 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
55b060d
Parents:
7e1dbd7
Message:

second attempt at input manipulators for strings

Location:
libcfa/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/containers/string.cfa

    r7e1dbd7 r38de914  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 28 19:00:45 2023
    13 // Update Count     : 146
     12// Last Modified On : Tue Aug 29 18:32:34 2023
     13// Update Count     : 153
    1414//
    1515
     
    118118}
    119119
    120 static void readstr( ifstream & is, _Istream_str f, char fmtstr[], char cstr[] ) {
    121         int check = f.rwd - 1;
    122 
    123         if ( ! f.flags.rwd ) cstr[check] = '\0';                        // insert sentinel
    124         int len = fmt( is, fmtstr, cstr );
    125         // fprintf( stderr, "KK %s %zd %d %c %s\n", fmtstr, len, check, cstr[check], cstr );
    126 
    127         if ( ! f.flags.rwd && cstr[check] != '\0' )                     // sentinel overwritten ?
    128                 throw (cstring_length){ &cstring_length_vt };
    129 
    130         if ( f.flags.delimit ) {                                                        // getline ?
    131                 if ( len == 0 ) cstr[0] = '\0';                                 // empty read => argument unchanged => set empty
    132                 if ( ! eof( is ) ) fmt( is, "%*c" );                    // ignore delimiter
    133         } //if
    134 } // readstr
    135 
    136120ifstream & ?|?( ifstream & is, _Istream_str f ) {
    137121        // skip, same as for char *
    138122        if ( ! &f.s ) {
    139123                // fprintf( stderr,  "skip %s %d\n", f.scanset, f.wd );
    140                 if ( f.rwd == -1 ) fmt( is, f.scanset, "" ); // no input arguments
    141                 else for ( f.rwd ) fmt( is, "%*c" );
     124                if ( f.wd == -1 ) fmt( is, f.scanset, "" ); // no input arguments
     125                else for ( f.wd ) fmt( is, "%*c" );
    142126                return is;
    143127        } // if
    144128
    145         enum { gwd = 16 + 2, wd = gwd - 1 };                            // guarded and unguarded width
     129        enum { gwd = 128 + 2, wd = gwd - 1 };                           // guarded and unguarded width
    146130        char cstr[gwd];                                                                         // read in chunks
    147131        bool cont = false;;
    148132
    149         if ( f.rwd == -1 ) f.rwd = wd;
     133        if ( f.wd == -1 ) f.wd = wd;
    150134        const char * scanset = f.scanset;;
    151135        if ( f.flags.delimit ) scanset = f.delimit;                     // getline ?
     
    157141        fmtstr[0] = '%';
    158142        if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
    159         if ( f.rwd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.rwd ); }
     143        if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
    160144
    161145        if ( ! scanset ) {
  • libcfa/src/containers/string.hfa

    r7e1dbd7 r38de914  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 28 18:32:59 2023
    13 // Update Count     : 40
     12// Last Modified On : Tue Aug 29 18:28:04 2023
     13// Update Count     : 47
    1414//
    1515
     
    5858void ?|?( ifstream & in, string & this );
    5959
    60 
    6160struct _Istream_str {
    6261        string & s;
    63         union {
    64                 const char * scanset;
    65                 char delimit[2];
    66         };
    67         int rwd;                                                                                        // read width
    68         union {
    69                 unsigned char all;
    70                 struct {
    71                         unsigned char ignore:1;                                         // do not change input argument
    72                         unsigned char inex:1;                                           // include/exclude characters in scanset
    73                         unsigned char delimit:1;                                        // delimit character
    74                         unsigned char rwd:1;                                            // read width
    75                 } flags;
    76         };
     62        inline _Istream_str_base;
    7763}; // _Istream_str
    7864
    7965static inline {
    8066        // read width does not include null terminator
    81         _Istream_str wdi( unsigned int rwd, string & s ) { return (_Istream_str)@{ s, {0p}, rwd, {.flags.rwd : true} }; }
    82         _Istream_str skip( const char scanset[] ) { return (_Istream_str)@{ *0p, {scanset}, -1, {.all : 0} }; }
    83         _Istream_str skip( unsigned int wd ) { return (_Istream_str)@{ *0p, {0p}, wd, {.all : 0} }; }
     67        _Istream_str wdi( unsigned int rwd, string & s ) { return (_Istream_str)@{ s, {{0p}, rwd, {.flags.rwd : true}} }; }
     68        _Istream_str skip( const char scanset[] ) { return (_Istream_str)@{ *0p, {{scanset}, -1, {.all : 0}} }; }
     69        _Istream_str skip( unsigned int wd ) { return (_Istream_str)@{ *0p, {{0p}, wd, {.all : 0}} }; }
    8470        _Istream_str getline( string & s, const char delimit = '\n' ) {
    85                 return (_Istream_str)@{ s, {.delimit : { delimit, '\0' } }, -1, {.flags.delimit : true, .flags.inex : true} };
     71                return (_Istream_str)@{ s, {{.delimit : { delimit, '\0' } }, -1, {.flags.delimit : true, .flags.inex : true}} };
    8672        }
    8773        _Istream_str & getline( _Istream_str & fmt, const char delimit = '\n' ) {
    8874                fmt.delimit[0] = delimit; fmt.delimit[1] = '\0'; fmt.flags.delimit = true; fmt.flags.inex = true; return fmt;
    8975        }
    90         _Istream_str incl( const char scanset[], string & s ) { return (_Istream_str)@{ s, {scanset}, -1, {.flags.inex : false} }; }
     76        _Istream_str incl( const char scanset[], string & s ) { return (_Istream_str)@{ s, {{scanset}, -1, {.flags.inex : false}} }; }
    9177        _Istream_str & incl( const char scanset[], _Istream_str & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
    92         _Istream_str excl( const char scanset[], string & s ) { return (_Istream_str)@{ s, {scanset}, -1, {.flags.inex : true} }; }
     78        _Istream_str excl( const char scanset[], string & s ) { return (_Istream_str)@{ s, {{scanset}, -1, {.flags.inex : true}} }; }
    9379        _Istream_str & excl( const char scanset[], _Istream_str & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
    94         _Istream_str ignore( string & s ) { return (_Istream_str)@{ s, {0p}, -1, {.flags.ignore : true} }; }
     80        _Istream_str ignore( string & s ) { return (_Istream_str)@{ s, {{0p}, -1, {.flags.ignore : true}} }; }
    9581        _Istream_str & ignore( _Istream_str & fmt ) { fmt.flags.ignore = true; return fmt; }
    9682} // distribution
  • libcfa/src/iostream.cfa

    r7e1dbd7 r38de914  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 28 13:36:16 2023
    13 // Update Count     : 1527
     12// Last Modified On : Wed Aug 30 10:56:08 2023
     13// Update Count     : 1541
    1414//
    1515
     
    962962
    963963forall( istype & | basic_istream( istype ) ) {
     964        void readstr( istype & is, _Istream_str_base f, char fmtstr[], char cstr[] ) {
     965                int check = f.wd - 1;
     966
     967                if ( ! f.flags.rwd ) cstr[check] = '\0';                // insert sentinel
     968                int len = fmt( is, fmtstr, cstr );
     969                // fprintf( stderr, "KK %s %zd %d %c %s\n", fmtstr, len, check, cstr[check], cstr );
     970
     971                if ( ! f.flags.rwd && cstr[check] != '\0' )             // sentinel overwritten ?
     972                        throw (cstring_length){ &cstring_length_vt };
     973
     974                if ( f.flags.delimit ) {                                                // getline ?
     975                        if ( len == 0 ) cstr[0] = '\0';                         // empty read => argument unchanged => set empty
     976                        if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
     977                } //if
     978        } // readstr
     979
    964980        istype & ?|?( istype & is, _Istream_Cstr f ) {
    965981                // skip
     
    971987                } // if
    972988
    973                 int check = f.wd - 1;
    974                 const char * scanset = f.scanset;;
     989                const char * scanset = f.scanset;
    975990                if ( f.flags.delimit ) scanset = f.delimit;             // getline ?
    976 
    977                 // getline
    978                 // if ( f.flags.delimit ) {
    979                 //      enum { size = 32 };
    980                 //      char fmtstr[size];
    981                 //      snprintf( fmtstr, size, "%%%d[^%c]s", f.wd, f.delimit );
    982                 //      if ( ! f.flags.rwd ) f.s[check] = '\0';         // insert sentinel
    983                 //      int len = fmt( is, fmtstr, f.s );                       // read upto delimiter
    984                 //      if ( ! f.flags.rwd && f.s[check] != '\0' )      // sentinel overwritten ?
    985                 //              throw (cstring_length){ &cstring_length_vt };
    986                 //      if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
    987                 //      // fprintf( stderr, "getline %s %s %d %d %d '%c'\n", fmtstr, f.s, len, f.wd, check, f.s[check] );
    988                 //      if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
    989                 //      return is;
    990                 // } // if
    991991
    992992                size_t len = 0;
     
    10131013                } // if
    10141014
    1015                 if ( ! f.flags.rwd ) f.s[check] = '\0';                 // insert sentinel
    1016                 len = fmt( is, fmtstr, f.s );
    1017                 //fprintf( stderr, "KK %s %zd %d %c %s\n", fmtstr, len, check, f.s[check], f.s );
    1018                 if ( ! f.flags.rwd && f.s[check] != '\0' )              // sentinel overwritten ?
    1019                         throw (cstring_length){ &cstring_length_vt };
    1020 
    1021                 if ( f.flags.delimit ) {                                                // getline ?
    1022                         if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
    1023                         if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
    1024                 } //if
     1015                readstr( is, f, fmtstr, f.s );
     1016                // int check = f.wd - 1;
     1017
     1018                // if ( ! f.flags.rwd ) f.s[check] = '\0';                      // insert sentinel
     1019                // len = fmt( is, fmtstr, f.s );
     1020                // //fprintf( stderr, "KK %s %zd %d %c %s\n", fmtstr, len, check, f.s[check], f.s );
     1021                // if ( ! f.flags.rwd && f.s[check] != '\0' )           // sentinel overwritten ?
     1022                //      throw (cstring_length){ &cstring_length_vt };
     1023
     1024                // if ( f.flags.delimit ) {                                             // getline ?
     1025                //      if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
     1026                //      if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
     1027                // } //if
    10251028                return is;
    10261029        } // ?|?
  • libcfa/src/iostream.hfa

    r7e1dbd7 r38de914  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Aug 25 14:55:06 2023
    13 // Update Count     : 535
     12// Last Modified On : Tue Aug 29 19:15:06 2023
     13// Update Count     : 542
    1414//
    1515
     
    406406// *********************************** manipulators ***********************************
    407407
    408 struct _Istream_Cstr {
    409         char * s;
     408struct _Istream_str_base {
    410409        union {
    411410                const char * scanset;
     
    422421                } flags;
    423422        };
     423}; // _Istream_str_base
     424
     425struct _Istream_Cstr {
     426        char * s;
     427        inline _Istream_str_base;
    424428}; // _Istream_Cstr
    425429
    426430static inline {
    427431        // width must include room for null terminator
    428         _Istream_Cstr wdi( unsigned int wd, char s[] ) { return (_Istream_Cstr)@{ s, {0p}, wd, {.all : 0} }; }
     432        _Istream_Cstr wdi( unsigned int wd, char s[] ) { return (_Istream_Cstr)@{ s, { {0p}, wd, {.all : 0} } }; }
    429433        // read width does not include null terminator
    430434        _Istream_Cstr wdi( unsigned int wd, unsigned int rwd, char s[] ) {
    431435                if ( wd <= rwd ) throw (cstring_length){ &cstring_length_vt };
    432                 return (_Istream_Cstr)@{ s, {0p}, rwd, {.flags.rwd : true} };
     436                return (_Istream_Cstr)@{ s, { {0p}, rwd, {.flags.rwd : true} } };
    433437        }
    434         _Istream_Cstr skip( const char scanset[] ) { return (_Istream_Cstr)@{ 0p, {scanset}, -1, {.all : 0} }; }
    435         _Istream_Cstr skip( unsigned int wd ) { return (_Istream_Cstr)@{ 0p, {0p}, wd, {.all : 0} }; }
     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} } }; }
    436440        _Istream_Cstr & getline( _Istream_Cstr & fmt, const char delimit = '\n' ) {
    437441                fmt.delimit[0] = delimit; fmt.delimit[1] = '\0'; fmt.flags.delimit = true; fmt.flags.inex = true; return fmt; }
    438442        _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
    439443        _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
    440         _Istream_Cstr ignore( char s[] ) { return (_Istream_Cstr)@{ s, {0p}, -1, {.flags.ignore : true} }; }
     444        _Istream_Cstr ignore( char s[] ) { return (_Istream_Cstr)@{ s, { {0p}, -1, {.flags.ignore : true} } }; }
    441445        _Istream_Cstr & ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; }
    442446} // distribution
    443447forall( istype & | basic_istream( istype ) ) {
     448        void readstr( istype & is, _Istream_str_base f, char fmtstr[], char cstr[] );
    444449        istype & ?|?( istype & is, _Istream_Cstr f );
    445450        ISTYPE_VOID( _Istream_Cstr );
Note: See TracChangeset for help on using the changeset viewer.