Changeset b19b362 for libcfa


Ignore:
Timestamp:
May 3, 2026, 8:25:27 AM (4 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
c136863
Parents:
efa8439a
Message:

add quote output manipulator for printing char, char *, string surrounded by quote characters

Location:
libcfa/src
Files:
3 edited

Legend:

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

    refa8439a rb19b362  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Sep 14 10:58:28 2025
    13 // Update Count     : 311
     12// Last Modified On : Sat May  2 19:09:55 2026
     13// Update Count     : 322
    1414//
    1515
     
    8787
    8888static inline {
    89         _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all = 0 } }; }
    90         _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all = 0 } }; }
    91         _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all = 0 } }; }
    92         _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all = 0 } }; }
    93         _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc = true } }; }
    94         _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; }
    95         _Ostream_Manip(string) & wd( unsigned int w, unsigned int pc, _Ostream_Manip(string) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
     89        _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ .val = s, .wd = 1, .pc = 0, .base = 'b', { .all = 0 } }; }
     90        _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ .val = s, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; }
     91        _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ .val = s, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; }
     92        _Ostream_Manip(string) quote( string s ) { return (_Ostream_Manip(string))@{ .val = s, .wd = 1, .pc = 0, .base = 's', { .flags.quote = true } }; }
     93        _Ostream_Manip(string) wd( unsigned int wd, string s ) { return (_Ostream_Manip(string))@{ .val = s, .wd = wd, .pc = 0, .base = 's', { .all = 0 } }; }
     94        _Ostream_Manip(string) wd( unsigned int wd, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ .val = s, .wd = wd, .pc = pc, .base = 's', { .flags.pc = true } }; }
     95        _Ostream_Manip(string) & wd( unsigned int wd, _Ostream_Manip(string) & fmt ) { fmt.wd = wd; return fmt; }
     96        _Ostream_Manip(string) & wd( unsigned int wd, unsigned int pc, _Ostream_Manip(string) & fmt ) { fmt.wd = wd; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
    9697        _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; }
    9798        _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
     99        _Ostream_Manip(string) & upcase( _Ostream_Manip(string) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
     100        _Ostream_Manip(string) & quote( _Ostream_Manip(string) & fmt ) { fmt.flags.quote = true; return fmt; } \
    98101} // distribution
    99102
  • libcfa/src/iostream.cfa

    refa8439a rb19b362  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun May 18 08:23:40 2025
    13 // Update Count     : 2175
     12// Last Modified On : Sun May  3 08:08:29 2026
     13// Update Count     : 2241
    1414//
    1515
     
    5252                fmt( os, "%c", c );
    5353                if ( c == '\n' ) setNL$( os, true );
    54                 return nosep( os );
     54                return nosep( os );                                                             // no separator after char
    5555        } // ?|?
    5656        OSTYPE_VOID_IMPL( os, char )
     
    204204        ostype & ?|?( ostype & os, float _Complex fc ) with ( basic_ostream_table ) {
    205205                if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
    206 //              os | crealf( fc ) | nonl;
    207206                PRINT_WITH_DP( os, "%'g", crealf( fc ) );
    208207                PRINT_WITH_DP( os, "%'+g", cimagf( fc ) );
     
    271270                if ( ch == '\n' ) setNL$( os, true );                   // check *AFTER* sepPrt$ call above as it resets NL flag
    272271                return os;
    273 //              return write( os, s, len );
    274272        } // ?|?
    275273        OSTYPE_VOID_IMPL( os, const char * )
     
    645643#define FLOATING_POINT_FMT_IMPL( T, DFMTNP, DFMTP ) \
    646644forall( ostype & | basic_ostream( ostype ) ) { \
    647         static void eng( T &value, int & pc, int & exp10 ) { \
     645        static void eng( T & value, int & pc, int & exp10 ) { \
    648646                exp10 = lrint( floor( log10( abs( value ) ) ) ); /* round to desired precision */ \
    649647                if ( exp10 < 0 ) exp10 -= 2; \
     
    697695                        fmtuc.flags.pc = f.flags.pc;
    698696                        fmtuc.flags.nobsdp = f.flags.nobsdp;
    699 //                      os | fmtuc | nonl;
     697                        return (ostype &)(os | fmtuc);
     698                } // if
     699                if ( f.flags.quote ) {                                                  // print as string
     700                        char qv[] = "' '";
     701                        qv[1] = f.val;
     702                        f.flags.quote = false;                                          // already quoted
     703                        _Ostream_Manip(const char *) fmtuc @= { qv, f.wd, f.pc, 's', {f.all} };
    700704                        (ostype &)(os | fmtuc);
    701                         return os;
    702                 } // if
    703 
    704                 if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
    705 
    706                 #define CFMTNP "% * "
     705                        return nosep( os );                                                     // no separator after char
     706                } // if
     707
     708                if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
     709
     710                #define CFMTNP "% *c"
    707711                char fmtstr[sizeof(CFMTNP)];                                    // sizeof includes '\0'
    708712                memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) );
     
    713717                fmtstr[star] = '%';
    714718
    715                 fmtstr[sizeof(CFMTNP)-2] = f.base;                              // sizeof includes '\0'
    716719                // printf( "%d %s\n", f.wd, &fmtstr[star] );
    717720                fmt( os, &fmtstr[star], f.wd, f.val );
    718                 return os;
     721                return nosep( os );                                                             // no separator after char
    719722        } // ?|?
    720723        OSTYPE_VOID_IMPL( os, _Ostream_Manip(char) )
     
    733736                        for ( i; 0 ~ @ : @; f.val[i] != '\0' ) {
    734737                                fmtuc.val = f.val[i];
    735 //                              os | fmtuc | nonl;
    736738                                (ostype &)(os | fmtuc);
    737739                        } // for
    738740                        return os;
    739741                } // if
     742                if ( f.flags.quote ) {                                                  // print as string
     743                        int len = strlen( f.val );
     744                        char qv[len + 3];                                                       // space for quotes and '\0'
     745                        qv[0] = '"';                                                            // copy string surrounded with quotes
     746                        strcpy( &qv[1], f.val );
     747                        qv[len + 1] = '"';
     748                        qv[len + 2] = '\0';
     749                        f.flags.quote = false;                                          // already quoted
     750                        _Ostream_Manip(const char *) fmtuc @= { qv, f.wd, f.pc, 's', {f.all} };
     751                        return (ostype &)(os | fmtuc);
     752                } // if
    740753
    741754                if ( f.val[0] != '\0' &&                                                // null string => no leading separator
    742755                         sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
    743756
    744                 #define SFMTNP "% * "
    745                 #define SFMTP "% *.* "
     757                #define SFMTNP "% *s"
     758                #define SFMTP "% *.*s"
    746759                char fmtstr[sizeof(SFMTP)];                                             // sizeof includes '\0'
    747760                if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) );
     
    755768                if ( ! f.flags.pc ) {                                                   // no precision
    756769                        // printf( "%d %s\n", f.wd, &fmtstr[star] );
    757                         fmtstr[sizeof(SFMTNP)-2] = f.base;                      // sizeof includes '\0'
    758770                        fmt( os, &fmtstr[star], f.wd, f.val );
    759771                } else {                                                                                // precision
    760                         fmtstr[sizeof(SFMTP)-2] = f.base;                       // sizeof includes '\0'
    761772                        // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] );
    762773                        fmt( os, &fmtstr[star], f.wd, f.pc, f.val );
     
    796807                for () {
    797808                        int args = fmt( is, "%c", &temp );                      // can be called with EOF on
    798                         if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
     809                  if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    799810                        assert( args == 1 );                                            // if not EOF => a single character must be read
    800811                        // do not overwrite parameter with newline unless appropriate
    801                         if ( temp != '\n' || getANL$( is ) ) { c = temp; break; }
     812                  if ( temp != '\n' || getANL$( is ) ) { c = temp; break; }
    802813                } // for
    803814                return is;
  • libcfa/src/iostream.hfa

    refa8439a rb19b362  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Jan 14 21:23:53 2026
    13 // Update Count     : 772
     12// Last Modified On : Sat May  2 18:48:56 2026
     13// Update Count     : 780
    1414//
    1515
     
    193193        unsigned char sign:1;                                                           // plus / minus sign
    194194        unsigned char pad0:1;                                                           // zero pad
     195        unsigned char quote:1;                                                          // print quotes
    195196};
    196197
     
    224225        _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \
    225226        _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
    226         _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
    227227        _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = CODE, { .flags.sign = true } }; } \
    228228        _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
     229        _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
    229230} /* distribution */ \
    230231forall( ostype & | basic_ostream( ostype ) ) { \
     
    288289        _Ostream_Manip(char) oct( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; }
    289290        _Ostream_Manip(char) hex( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; }
    290         _Ostream_Manip(char) wd( unsigned int wd, char c ) { return (_Ostream_Manip(char))@{ c, wd, 0, .base = 'c', { .all = 0 } }; }
     291        _Ostream_Manip(char) quote( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'c', { .flags.quote = true } }; }
     292        _Ostream_Manip(char) wd( unsigned int wd, char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = wd, .pc = 0, .base = 'c', { .all = 0 } }; }
    291293        _Ostream_Manip(char) & wd( unsigned int wd, _Ostream_Manip(char) & fmt ) { fmt.wd = wd; return fmt; }
    292294        _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; }
    293295        _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
    294296        _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
     297        _Ostream_Manip(char) & quote( _Ostream_Manip(char) & fmt ) { fmt.flags.quote = true; return fmt; }
    295298} // distribution
    296299forall( ostype & | basic_ostream( ostype ) ) {
     
    305308        _Ostream_Manip(const char *) oct( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; }
    306309        _Ostream_Manip(const char *) hex( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; }
    307         _Ostream_Manip(const char *) wd( unsigned int wd, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, wd, 0, .base = 's', { .all = 0 } }; }
    308         _Ostream_Manip(const char *) wd( unsigned int wd, unsigned int pc, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, .wd = wd, .pc = pc, .base = 's', { .flags.pc = true } }; }
     310        _Ostream_Manip(const char *) quote( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 's', { .flags.quote = true } }; }
     311        _Ostream_Manip(const char *) wd( unsigned int wd, const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = wd, .pc = 0, .base = 's', { .all = 0 } }; }
     312        _Ostream_Manip(const char *) wd( unsigned int wd, unsigned int pc, const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = wd, .pc = pc, .base = 's', { .flags.pc = true } }; }
    309313        _Ostream_Manip(const char *) & wd( unsigned int wd, _Ostream_Manip(const char *) & fmt ) { fmt.wd = wd; return fmt; }
    310314        _Ostream_Manip(const char *) & wd( unsigned int wd, unsigned int pc, _Ostream_Manip(const char *) & fmt ) { fmt.wd = wd; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
    311315        _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; }
     316        _Ostream_Manip(const char *) & upcase( _Ostream_Manip(const char *) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
    312317        _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
     318        _Ostream_Manip(const char *) & quote( _Ostream_Manip(const char *) & fmt ) { fmt.flags.quote = true; return fmt; } \
    313319} // distribution
    314320forall( ostype & | basic_ostream( ostype ) ) {
Note: See TracChangeset for help on using the changeset viewer.