Changeset 4aae2bd for libcfa


Ignore:
Timestamp:
Jan 4, 2024, 12:02:07 PM (4 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
e0cc9e0
Parents:
be10079
Message:

continue to work on quoted formatting and generalization

Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    rbe10079 r4aae2bd  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Nov 17 13:33:12 2023
    13 // Update Count     : 1853
     12// Last Modified On : Wed Jan  3 10:53:13 2024
     13// Update Count     : 1898
    1414//
    1515
     
    984984        }
    985985
    986         istype & ?|?( istype & is, _Istream_Cquoted f ) {
     986        istype & ?|?( istype & is, _Istream_Cquoted f ) with( f ) {
    987987                char fmtstr[32];                                                                // storage scanset and format codes
    988988                fmtstr[0] = '%';
     
    992992                bool check = true;
    993993
    994                 if ( f.flags.ignore ) { check = false; fmtstr[1] = '*'; pos += 1; }
    995                 int rwd = f.wd;
    996                 if ( f.wd != -1 ) {                                                             // => just ignore versus ignore with width
     994                if ( cstr.flags.ignore ) { check = false; fmtstr[1] = '*'; pos += 1; }
     995                int rwd = cstr.wd;
     996                if ( cstr.wd != -1 ) {                                          // => just ignore versus ignore with width
    997997                        // wd is buffer bytes available (for input chars + null terminator)
    998998                        // rwd is count of input chars
    999999                        // no maximum width necessary because text ignored => width is read width
    1000                         if ( f.flags.rwd ) check = false;
    1001                         else rwd = f.wd - 1;
     1000                        if ( cstr.flags.rwd ) check = false;
     1001                        else rwd = cstr.wd - 1;
    10021002                        pos += sprintf( &fmtstr[pos], "%d", rwd );
    10031003                } // if
    10041004
    10051005                int len = 0;                                                                    // may not be set in fmt
    1006                 if ( ! f.flags.inex ) {                                                 // => quoted getline
    1007                         // fprintf( stderr, "quoted\n" );
     1006                char enddelim;
     1007                if ( ! cstr.flags.inex ) {                                              // => quoted getline
    10081008                        args = fmt( is, "%*[ \f\n\r\t\v]" );            // remove leading whitespace
    10091009                        if ( eof( is ) ) goto Eof;
    1010 //                      args = fmt( is, (const char *)f.delimiter ); // remove leading quote
    1011                         args = fmt( is, "'%n", &len ); // remove leading quote
    1012                         fprintf( stderr, "quoted %d %d\n", args, len );
     1010                        char rfmt[4] = { cstr.delimiters[0], '%', 'n', '\0' };
     1011                        args = fmt( is, rfmt, &len );                           // remove leading quote
    10131012                        if ( len == 0 || eof( is ) ) goto Eof;
    10141013                } // if
    1015                 sprintf( &fmtstr[pos], "[^%c]%%n", f.delimiter[0] );
    1016                 // fprintf( stderr, "getline %s %d\n", fmtstr, f.wd );
    1017                 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*'
    1018                 else args = fmt( is, fmtstr, f.s, &len );
    1019                 // fprintf( stderr, "getline %s %d %d %d\n", fmtstr, args, f.wd, eof( is ) );
     1014                enddelim = cstr.delimiters[1] == '\0' ? cstr.delimiters[0] : cstr.delimiters[1];
     1015                sprintf( &fmtstr[pos], "[^%c]%%n", enddelim );
     1016                if ( cstr.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*'
     1017                else args = fmt( is, fmtstr, cstr.s, &len );
    10201018                if ( check && len == rwd && ! eof( is ) ) {             // might not fit
    10211019                        char peek;
    10221020                        fmt( is, "%c", &peek );                                         // check for delimiter
    1023                         // fprintf( stderr, "peek %d '%c'\n", args, peek );
    10241021                        if ( ! eof( is ) ) {
    1025                                 if ( peek != f.delimiter[0] ) {
     1022                                if ( peek != enddelim ) {
    10261023                                        ungetc( is, peek );
    10271024                                        throwResume ExceptionInst( cstring_length );
     
    10301027                } else fmt( is, "%*c" );                                                // remove delimiter
    10311028          Eof: ;
    1032                 if ( rwd > 0 && args == 0 ) f.s[0] = '\0';              // read failed => no pattern match => set string to null
     1029                if ( rwd > 0 && args == 0 ) cstr.s[0] = '\0';   // read failed => no pattern match => set string to null
    10331030                if ( args == 1 && eof( is ) ) {                                 // data but scan ended at EOF
    1034                         // fprintf( stderr, "clear\n" );
    10351031                        clear( is );                                                            // => reset EOF => detect again on next read
    10361032                } // if
     
    10381034        }
    10391035
    1040         istype & ?|?( istype & is, _Istream_Cstr f ) {
     1036        istype & ?|?( istype & is, _Istream_Cstr f ) with( f ) {
    10411037                const char * scanset;
    10421038                size_t nscanset = 0;
    1043                 if ( f.flags.delimiter ) scanset = f.delimiter; // getline ?
     1039                if ( flags.delimiter ) scanset = delimiters;    // getline ?
    10441040                else scanset = f.scanset;
    10451041                if ( scanset ) nscanset = strlen( scanset );
     
    10841080                        if ( f.flags.delimiter ) {                                      // getline
    10851081                                int len = 0;                                                    // may not be set in fmt
    1086                                 if ( ! f.flags.inex ) {                                 // => quoted getline
    1087                                         // fprintf( stderr, "quoted\n" );
    1088                                         args = fmt( is, "%*[ \f\n\r\t\v]" ); // remove leading whitespace
    1089                                         if ( eof( is ) ) goto X;
    1090                                         args = fmt( is, "\"" );                         // remove leading quote
    1091                                         if ( eof( is ) ) goto X;
    1092                                 } // if
    1093                                 // fprintf( stderr, "getline\n" );
    1094                                 // sprintf( &fmtstr[pos], "[%s%s]%%n", f.flags.inex ? "^" : "", scanset );
    1095                                 sprintf( &fmtstr[pos], "[^%s]%%n", scanset );
    1096                                 // fprintf( stderr, "getline %s %d\n", fmtstr, f.wd );
     1082                                sprintf( &fmtstr[pos], "[^%c]%%n", f.delimiters[0] );
    10971083                                if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*'
    10981084                                else args = fmt( is, fmtstr, f.s, &len );
    1099                                 // fprintf( stderr, "getline %s %d %d %d\n", fmtstr, args, f.wd, eof( is ) );
    11001085                                if ( check && len == rwd && ! eof( is ) ) {     // might not fit
    1101                                         char peek;
    1102                                         fmt( is, "%c", &peek );                         // check for delimiter
    1103                                         // fprintf( stderr, "peek %d '%c'\n", args, peek );
     1086                                        fmtstr[0] = f.delimiters[0]; fmtstr[1] = '%'; fmtstr[2] = 'n'; fmtstr[3] = '\0';
     1087                                        fmt( is, fmtstr, &len );                        // remove delimiter
    11041088                                        if ( ! eof( is ) ) {
    1105                                                 if ( peek != f.delimiter[0] ) {
    1106                                                         ungetc( is, peek );
     1089//                                              if ( peek != f.delimiter[0] ) {
     1090                                                if ( len != 1 ) {
     1091//                                                      ungetc( is, peek );
    11071092                                                        throwResume ExceptionInst( cstring_length );
    11081093                                                } // if
    11091094                                        } // if
    1110                                 } else fmt( is, "%*c" );                        // remove delimiter
    1111                           X: ;
     1095                                } else fmt( is, "%*c" );                                // remove delimiter
    11121096                        } else {
    11131097                                // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
  • libcfa/src/iostream.hfa

    rbe10079 r4aae2bd  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Nov 15 17:55:31 2023
    13 // Update Count     : 596
     12// Last Modified On : Wed Jan  3 10:53:18 2024
     13// Update Count     : 610
    1414//
    1515
     
    392392        union {
    393393                const char * scanset;
    394                 char delimiter[2];
     394                char delimiters[3];                                                             // [0] => left, [1] => right
    395395        };
    396396        int wd;                                                                                         // width
     
    412412
    413413struct _Istream_Cquoted {
    414         char * s;
    415         inline _Istream_str_base;
     414        _Istream_Cstr cstr;
    416415}; // _Istream_Cquoted
    417416
     
    419418        // width must include room for null terminator
    420419        _Istream_Cstr wdi( unsigned int wd, char s[] ) { return (_Istream_Cstr)@{ s, { {0p}, wd, {.all : 0} } }; }
    421         // read width does not include null terminator
    422420        _Istream_Cstr wdi( unsigned int wd, unsigned int rwd, char s[] ) {
    423421                if ( wd <= rwd ) throw (cstring_length){ &cstring_length_vt };
    424422                return (_Istream_Cstr)@{ s, { {0p}, rwd, {.flags.rwd : true} } };
    425423        }
    426         _Istream_Cquoted & quoted( _Istream_Cstr & fmt, const char delimiter = '"' ) {
    427                 fmt.delimiter[0] = delimiter; fmt.delimiter[1] = '\0';
     424        _Istream_Cquoted & quoted( _Istream_Cstr & fmt, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) {
     425                fmt.delimiters[0] = Ldelimiter;  fmt.delimiters[1] = Rdelimiter;  fmt.delimiters[2] = '\0';
    428426                return (_Istream_Cquoted &)fmt;
    429427        }
    430428        _Istream_Cstr & getline( _Istream_Cstr & fmt, const char delimiter = '\n' ) {
    431                 fmt.delimiter[0] = delimiter; fmt.delimiter[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt; }
     429                fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt;
     430        }
    432431        _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
    433432        _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.