Ignore:
Timestamp:
Sep 2, 2023, 3:01:05 PM (9 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
389fbf5
Parents:
0f107e4
Message:

add string output manipulators, third attempt at input manipulators for strings

File:
1 edited

Legend:

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

    r0f107e4 r34c6e1e6  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 31 13:20:41 2023
    13 // Update Count     : 161
     12// Last Modified On : Sat Sep  2 12:05:57 2023
     13// Update Count     : 206
    1414//
    1515
     
    110110}
    111111
     112ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ) {
     113        size_t len = size( f.val );
     114        char cstr[len + 1];                                                                     // room for null terminator
     115        for ( i; len ) cstr[i] = f.val[i];                                      // copy string
     116        cstr[len] = '\0';                                                                       // terminate
     117        _Ostream_Manip(const char *) cf @= { cstr, f.wd, f.pc, f.base, {f.all} };
     118        os | cf | nonl;
     119        return os;
     120} // ?|?
     121
     122void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
     123        (ofstream &)(os | f); ends( os );
     124}
     125
    112126ifstream & ?|?(ifstream & in, string & this) {
    113127    return in | (*this.inner); // read to internal string_res
     
    118132}
    119133
    120 ifstream & ?|?( ifstream & is, _Istream_str f ) {
    121         // skip, same as for char *
    122         if ( ! &f.s ) {
    123                 // fprintf( stderr,  "skip %s %d\n", f.scanset, f.wd );
    124                 if ( f.wd == -1 ) fmt( is, f.scanset, "" ); // no input arguments
    125                 else for ( f.wd ) fmt( is, "%*c" );
    126                 return is;
    127         } // if
    128 
     134ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
    129135        // .---------------,
    130         // | | | | |...|0|0| check and guard
     136        // | | | | |...|0|0| null terminator and guard if missing
    131137        // `---------------'
    132         enum { gwd = 128 + 2, wd = gwd - 1 };                           // guarded and unguarded width
     138        enum { gwd = 128 + 1, wd = gwd - 1 };                           // guard and unguard width
    133139        char cstr[gwd];                                                                         // read in chunks
    134140        bool cont = false;
    135141
    136         if ( f.wd == -1 ) f.wd = wd;
    137         _Istream_Cstr cfmt = { cstr, (_Istream_str_base)f };
     142        _Istream_Cstr cf = { cstr, (_Istream_str_base)f };
     143        if ( ! cf.flags.rwd ) cf.wd = wd;
    138144
    139145        cstr[wd] = '\0';                                                                        // guard null terminate string
    140146        try {
    141                 is | cfmt;
     147                is | cf;
    142148        } catch( cstring_length * ) {
    143149                cont = true;
     
    148154                cont = false;
    149155                try {
    150                         is | cfmt;
     156                        is | cf;
    151157                } catch( cstring_length * ) {
    152158                        cont = true;                                                            // continue not allowed
     
    158164} // ?|?
    159165
    160 void ?|?( ifstream & in, _Istream_str f ) {
     166void ?|?( ifstream & in, _Istream_Sstr f ) {
    161167    (ifstream &)(in | f); ends( in );
    162168}
Note: See TracChangeset for help on using the changeset viewer.