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

third attempt at input manipulators for strings

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.