Ignore:
Timestamp:
Aug 14, 2023, 7:10:38 PM (12 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
7baff35
Parents:
3543e99
Message:

add new wdi manipulator for C-strings to specify string and read size

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r3543e99 r2f34fde  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 10 18:15:44 2023
    13 // Update Count     : 1442
     12// Last Modified On : Mon Aug 14 18:37:02 2023
     13// Update Count     : 1460
    1414//
    1515
     
    972972                // getline
    973973                if ( f.flags.delimit ) {
    974                         char fmtstr[] = "%[^ ]s";                                       // delimit inserted into space
    975                         fmtstr[3] = f.delimit;
    976                         fmt( is, fmtstr, f.s );                                         // read upto delimiter
    977                         fmt( is, "%*c" );                                                       // ignore delimiter
     974                        enum { size = 16 };
     975                        char fmtstr[size];
     976                        snprintf( fmtstr, size, "%%%d[^%c]s", f.wd, f.delimit );
     977                        f.s[f.wd - 1] = '\0';                                           // insert sentinel
     978                        int len = fmt( is, fmtstr, f.s );                       // read upto delimiter
     979                        if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
     980                        if ( f.s[f.wd - 1] != '\0' )                            // sentinel overwritten ?
     981                                throw (cstring_length){ &cstring_length_vt };
     982                        if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
    978983                        return is;
    979984                } // if
    980985
    981                 // incl/excl
    982986                size_t len = 0;
    983987                if ( f.scanset ) len = strlen( f.scanset );
     
    987991                if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
    988992                if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
    989                 // cstr %s, %*s, %ws, %*ws
     993
    990994                if ( ! f.scanset ) {
     995                        // %s, %*s, %ws, %*ws
    991996                        fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
    992997                        // printf( "cstr %s\n", fmtstr );
    993                         fmt( is, fmtstr, f.s );
    994                         return is;
    995                 } // if
    996                 // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
    997                 // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
    998                 fmtstr[start] = '['; start += 1;
    999                 if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
    1000                 strcpy( &fmtstr[start], f.scanset );                    // copy includes '\0'
    1001                 len += start;
    1002                 fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
    1003                 // printf( "incl/excl %s\n", fmtstr );
     998                } else {
     999                        // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
     1000                        // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
     1001                        fmtstr[start] = '['; start += 1;
     1002                        if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
     1003                        strcpy( &fmtstr[start], f.scanset );            // copy includes '\0'
     1004                        len += start;
     1005                        fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
     1006                        // printf( "incl/excl %s\n", fmtstr );
     1007                } // if
     1008
     1009                int check = f.wd - 1;
     1010                if ( f.flags.rwd ) check += 1;                                  // provide place for sentinel
     1011                f.s[check] = '\0';                                                              // insert sentinel
    10041012                fmt( is, fmtstr, f.s );
     1013                if ( f.s[check] != '\0' )                                               // sentinel overwritten ?
     1014                        throw (cstring_length){ &cstring_length_vt };
    10051015                return is;
    10061016        } // ?|?
     
    10811091} // distribution
    10821092
     1093
    10831094// Local Variables: //
    10841095// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.