Changeset 686912c for libcfa


Ignore:
Timestamp:
Aug 31, 2023, 1:25:38 PM (19 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
0f107e4, 2a301ff
Parents:
0b8c951d
Message:

third attempt at input manipulators for strings

Location:
libcfa/src
Files:
4 edited

Legend:

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

    r0b8c951d r686912c  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 29 18:32:34 2023
    13 // Update Count     : 153
     12// Last Modified On : Thu Aug 31 13:20:41 2023
     13// Update Count     : 161
    1414//
    1515
     
    127127        } // if
    128128
     129        // .---------------,
     130        // | | | | |...|0|0| check and guard
     131        // `---------------'
    129132        enum { gwd = 128 + 2, wd = gwd - 1 };                           // guarded and unguarded width
    130133        char cstr[gwd];                                                                         // read in chunks
    131         bool cont = false;;
     134        bool cont = false;
    132135
    133136        if ( f.wd == -1 ) f.wd = wd;
    134         const char * scanset = f.scanset;;
    135         if ( f.flags.delimit ) scanset = f.delimit;                     // getline ?
    136 
    137         size_t len = 0;
    138         if ( scanset ) len = strlen( scanset );
    139         char fmtstr[len + 16];
    140         int start = 1;
    141         fmtstr[0] = '%';
    142         if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
    143         if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
    144 
    145         if ( ! scanset ) {
    146                 // %s, %*s, %ws, %*ws
    147                 fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
    148                 // printf( "cstr %s\n", fmtstr );
    149         } else {
    150                 // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
    151                 // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
    152                 fmtstr[start] = '['; start += 1;
    153                 if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
    154                 strcpy( &fmtstr[start], scanset );                              // copy includes '\0'
    155                 len += start;
    156                 fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
    157                 // printf( "incl/excl %s\n", fmtstr );
    158         } // if
     137        _Istream_Cstr cfmt = { cstr, (_Istream_str_base)f };
    159138
    160139        cstr[wd] = '\0';                                                                        // guard null terminate string
    161140        try {
    162                 readstr( is, f, fmtstr, cstr );
     141                is | cfmt;
    163142        } catch( cstring_length * ) {
    164143                cont = true;
     
    169148                cont = false;
    170149                try {
    171                         readstr( is, f, fmtstr, cstr );
     150                        is | cfmt;
    172151                } catch( cstring_length * ) {
    173152                        cont = true;                                                            // continue not allowed
     
    183162}
    184163
    185 // void getline( ifstream & in, string & str, const char delimit = '\n' ) {
    186 //      // .---------------,
    187 //      // | | | | |...|0|0| check and guard
    188 //      // `---------------'
    189 //      enum { gwd = 128 + 2, wd = gwd - 1 };                           // guarded and unguarded width
    190 //      char cstr[gwd];                                                                         // read in chunks
    191 //      bool cont = false;;
    192 
    193 //      cstr[wd] = '\0';                                                                        // guard null terminate string
    194 //      try {
    195 //              in | getline( wdi( wd, cstr ), delimit );               // must have room for string terminator
    196 //              if ( eof( in  ) ) return;                                               // do not change argument
    197 //      } catch( cstring_length * ) {
    198 //              cont = true;
    199 //      } finally {
    200 //              str = cstr;                                                                             // ok to initialize string
    201 //      } // try
    202 //      for ( ; cont; )  {                                                                      // overflow read ?
    203 //              cont = false;
    204 //              try {
    205 //                      in | getline( wdi( wd, cstr ), delimit );       // must have room for string terminator
    206 //                      if ( eof( in  ) ) return;
    207 //              } catch( cstring_length * ) {
    208 //                      cont = true;                                                            // continue not allowed
    209 //              } finally {
    210 //                      str += cstr;                                                            // build string chunk at a time
    211 //              } // try
    212 //      } // for
    213 // }
    214 
    215164////////////////////////////////////////////////////////
    216165// Slicing
  • libcfa/src/collections/string.hfa

    r0b8c951d r686912c  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 29 18:28:04 2023
    13 // Update Count     : 47
     12// Last Modified On : Thu Aug 31 11:47:27 2023
     13// Update Count     : 49
    1414//
    1515
     
    8383ifstream & ?|?( ifstream & is, _Istream_str f );
    8484void ?|?( ifstream & is, _Istream_str t );
    85 void getline( ifstream & in, string & this, const char delimit = '\n' );
    8685
    8786// Concatenation
  • libcfa/src/iostream.cfa

    r0b8c951d r686912c  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Aug 30 10:56:08 2023
    13 // Update Count     : 1541
     12// Last Modified On : Thu Aug 31 11:27:56 2023
     13// Update Count     : 1545
    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 
    980964        istype & ?|?( istype & is, _Istream_Cstr f ) {
    981965                // skip
     
    1013997                } // if
    1014998
    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
     999                int check = f.wd - 1;
     1000                if ( ! f.flags.rwd ) f.s[check] = '\0';                 // insert sentinel
     1001                len = fmt( is, fmtstr, f.s );
     1002                //fprintf( stderr, "KK %s %zd %d %c %s\n", fmtstr, len, check, f.s[check], f.s );
     1003
     1004                if ( ! f.flags.rwd && f.s[check] != '\0' )              // sentinel overwritten ?
     1005                        throw (cstring_length){ &cstring_length_vt };
     1006
     1007                if ( f.flags.delimit ) {                                                // getline ?
     1008                        if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
     1009                        if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
     1010                } //if
    10281011                return is;
    10291012        } // ?|?
  • libcfa/src/iostream.hfa

    r0b8c951d r686912c  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 29 19:15:06 2023
    13 // Update Count     : 542
     12// Last Modified On : Thu Aug 31 10:55:35 2023
     13// Update Count     : 544
    1414//
    1515
     
    446446} // distribution
    447447forall( istype & | basic_istream( istype ) ) {
    448         void readstr( istype & is, _Istream_str_base f, char fmtstr[], char cstr[] );
    449448        istype & ?|?( istype & is, _Istream_Cstr f );
    450449        ISTYPE_VOID( _Istream_Cstr );
Note: See TracChangeset for help on using the changeset viewer.