Ignore:
Timestamp:
Jul 8, 2024, 8:37:18 PM (11 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
d287f3e
Parents:
a4e1b09
Message:

update reading boolean and match text

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    ra4e1b09 rc015e2d  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Feb 12 09:26:05 2024
    13 // Update Count     : 1966
     12// Last Modified On : Mon Jul  8 16:49:05 2024
     13// Update Count     : 2017
    1414//
    1515
     
    772772
    773773
     774#define FALSE "false"
     775#define TRUE "true"
     776
    774777forall( istype & | basic_istream( istype ) ) {
    775778        istype & ?|?( istype & is, bool & b ) {
    776779                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    777                 char val[6];
    778                 int args = fmt( is, "%5s", val );
    779                 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );
    780                 if ( strcmp( val, "true" ) == 0 ) b = true;
    781                 else if ( strcmp( val, "false" ) == 0 ) b = false;
    782                 else {
    783                         fprintf( stderr, "invalid Boolean constant\n" );
    784                         abort();                                                                        // cannot use abort stream
     780                int len = -1;                                                                   // len not set if no match
     781                // Optional leading whitespace at start of strings.
     782                fmt( is, " " FALSE "%n", &len );                                // try false
     783                if ( len != sizeof( FALSE ) - 1 ) {                             // remove null terminate
     784                        fmt( is, " " TRUE "%n", &len );                         // try true
     785                        if ( len != sizeof( TRUE ) - 1 ) throwResume ExceptionInst( missing_data );
    785786                } // if
    786787                return is;
     
    940941        } // ?|?
    941942
    942         istype & ?|?( istype & is, const char fmt[] ) {
     943        istype & ?|?( istype & is, const char fmt[] ) {         // match text
    943944                if ( eof( is ) ) throwResume ExceptionInst( missing_data );
    944945                size_t len = strlen( fmt );
     
    946947                strcpy( fmtstr, fmt );                                                  // copy format and add %n
    947948                strcpy( &fmtstr[len], "%n" );
    948                 int len2 = -1;
    949                 fmt( is, fmtstr, &len2 );
    950                 if ( ! eof( is ) && len2 == -1 ) throwResume ExceptionInst( missing_data );
     949                len = -1;
     950                // scanf cursor does not move if no match
     951                fmt( is, fmtstr, &len );
     952                if ( ! eof( is ) && len == -1 ) throwResume ExceptionInst( missing_data );
    951953                return is;
    952954        } // ?|?
Note: See TracChangeset for help on using the changeset viewer.