Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/iostream.c

    re945826 rb63e376  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr 30 14:00:53 2016
    13 // Update Count     : 302
     12// Last Modified On : Mon Mar  7 13:51:23 2016
     13// Update Count     : 227
    1414//
    1515
     
    2727ostype * ?|?( ostype *os, char c ) {
    2828        prtfmt( os, "%c", c );
    29         sepOff( os );
    3029        return os;
    3130} // ?|?
     
    3332forall( dtype ostype | ostream( ostype ) )
    3433ostype * ?|?( ostype *os, short int si ) {
    35         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     34        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    3635        prtfmt( os, "%hd", si );
    3736        return os;
     
    4039forall( dtype ostype | ostream( ostype ) )
    4140ostype * ?|?( ostype *os, unsigned short int usi ) {
    42         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     41        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    4342        prtfmt( os, "%hu", usi );
    4443        return os;
     
    4746forall( dtype ostype | ostream( ostype ) )
    4847ostype * ?|?( ostype *os, int i ) {
    49         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     48        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    5049        prtfmt( os, "%d", i );
    5150        return os;
     
    5453forall( dtype ostype | ostream( ostype ) )
    5554ostype * ?|?( ostype *os, unsigned int ui ) {
    56         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     55        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    5756        prtfmt( os, "%u", ui );
    5857        return os;
     
    6160forall( dtype ostype | ostream( ostype ) )
    6261ostype * ?|?( ostype *os, long int li ) {
    63         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     62        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    6463        prtfmt( os, "%ld", li );
    6564        return os;
     
    6867forall( dtype ostype | ostream( ostype ) )
    6968ostype * ?|?( ostype *os, unsigned long int uli ) {
    70         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     69        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    7170        prtfmt( os, "%lu", uli );
    7271        return os;
     
    7574forall( dtype ostype | ostream( ostype ) )
    7675ostype * ?|?( ostype *os, long long int lli ) {
    77         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     76        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    7877        prtfmt( os, "%lld", lli );
    7978        return os;
     
    8281forall( dtype ostype | ostream( ostype ) )
    8382ostype * ?|?( ostype *os, unsigned long long int ulli ) {
    84         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     83        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    8584        prtfmt( os, "%llu", ulli );
    8685        return os;
     
    8988forall( dtype ostype | ostream( ostype ) )
    9089ostype * ?|?( ostype *os, float f ) {
    91         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     90        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    9291        prtfmt( os, "%g", f );
    9392        return os;
     
    9695forall( dtype ostype | ostream( ostype ) )
    9796ostype * ?|?( ostype *os, double d ) {
    98         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     97        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    9998        prtfmt( os, "%.*lg", DBL_DIG, d );
    10099        return os;
     
    103102forall( dtype ostype | ostream( ostype ) )
    104103ostype * ?|?( ostype *os, long double ld ) {
    105         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     104        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    106105        prtfmt( os, "%.*Lg", LDBL_DIG, ld );
    107106        return os;
     
    111110ostype * ?|?( ostype *os, float _Complex fc ) {
    112111        os | crealf( fc );
    113         _Bool temp = sepDisable( os );                                          // disable separators within complex value
    114         if ( cimagf( fc ) >= 0 ) os | '+';                                      // negative value prints '-'
    115         os | cimagf( fc ) | 'i';
    116         sepReset( os, temp );                                                           // reset separator
     112        if ( cimagf( fc ) >= 0 ) os | '+';
     113        os | "" | cimagf( fc ) | 'i';
    117114        return os;
    118115} // ?|?
     
    121118ostype * ?|?( ostype *os, double _Complex dc ) {
    122119        os | creal( dc );
    123         _Bool temp = sepDisable( os );                                          // disable separators within complex value
    124         if ( cimag( dc ) >= 0 ) os | '+';                                       // negative value prints '-'
    125         os | cimag( dc ) | 'i';
    126         sepReset( os, temp );                                                           // reset separator
     120        if ( cimag( dc ) >= 0 ) os | '+';
     121        os | "" | cimag( dc ) | 'i';
    127122        return os;
    128123} // ?|?
     
    131126ostype * ?|?( ostype *os, long double _Complex ldc ) {
    132127        os | creall( ldc );
    133         _Bool temp = sepDisable( os );                                          // disable separators within complex value
    134         if ( cimagl( ldc ) >= 0 ) os | '+';                                     // negative value prints '-'
    135         os | cimagl( ldc ) | 'i';
    136         sepReset( os, temp );                                                           // reset separator
     128        if ( cimagl( ldc ) >= 0 ) os | '+';
     129        os | "" | cimagl( ldc ) | 'i';
    137130        return os;
    138131} // ?|?
     
    141134ostype * ?|?( ostype *os, const char *cp ) {
    142135        enum { Open = 1, Close, OpenClose };
    143         static const unsigned char mask[256] = {
     136        static const char mask[256] = {
    144137                // opening delimiters
    145138                ['('] : Open, ['['] : Open, ['{'] : Open,
    146                 ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
    147                 [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
     139                ['$'] : Open, [L'£'] : Open, [L'¥'] : Open, [L'¢'] : Open, [L'¿'] : Open, [L'«'] : Open,
    148140                // closing delimiters
    149141                [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
    150142                [')'] : Close, [']'] : Close, ['}'] : Close,
    151                 ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
     143                ['%'] : Close, [L'»'] : Close,
    152144                // opening-closing delimiters
    153145                ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose,
    154                 [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
    155146        }; // mask
    156147
    157   if ( cp[0] == '\0' ) { sepOff( os ); return os; }             // null string => no separator
    158 
    159         // first character IS NOT spacing or closing punctuation => add left separator
    160         unsigned char ch = cp[0];                                                       // must make unsigned
    161         if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
     148        int len = strlen( cp );
     149        // null string => no separator
     150  if ( len == 0 ) { sepOff( os ); return os; }
     151        // first character NOT spacing or closing punctuation => add left separator
     152        if ( sepPrt( os ) && isspace( cp[0] ) == 0 && mask[ cp[0] ] != Close && mask[ cp[0] ] != OpenClose ) {
    162153                prtfmt( os, "%s", sepGet( os ) );
    163154        } // if
    164 
    165         // if string starts line, must reset to determine open state because separator is off
    166         sepReset( os );                                                                         // reset separator
    167 
    168155        // last character IS spacing or opening punctuation => turn off separator for next item
    169         unsigned int len = strlen( cp ), posn = len - 1;
    170         ch = cp[posn];                                                                          // must make unsigned
    171         if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
     156        unsigned int posn = len - 1;
     157        if ( isspace( cp[posn] ) || mask[ cp[posn] ] == Open || mask[ cp[posn] ] == OpenClose ) {
     158                sepOff( os );
     159        } else {
    172160                sepOn( os );
    173         } else {
    174                 sepOff( os );
    175161        } // if
    176162        return write( os, cp, len );
     
    179165forall( dtype ostype | ostream( ostype ) )
    180166ostype * ?|?( ostype *os, const void *p ) {
    181         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     167        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    182168        prtfmt( os, "%p", p );
    183169        return os;
    184170} // ?|?
     171
    185172
    186173forall( dtype ostype | ostream( ostype ) )
     
    209196} // sepOff
    210197
    211 forall( dtype ostype | ostream( ostype ) )
    212 ostype * sepEnable( ostype * os ) {
    213         sepEnable( os );
    214         return os;
    215 } // sepEnable
    216 
    217 forall( dtype ostype | ostream( ostype ) )
    218 ostype * sepDisable( ostype * os ) {
    219         sepDisable( os );
    220         return os;
    221 } // sepDisable
    222 
    223198//---------------------------------------
    224199
     
    335310} // ?|?
    336311
    337 _Istream_cstrUC cstr( char * s ) { _Istream_cstrUC s = { s }; return s; }
    338 forall( dtype istype | istream( istype ) )
    339 istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
    340         scanfmt( is, "%s", cstr.s );
    341         return is;
    342 } // cstr
    343 
    344 _Istream_cstrC cstr( char * s, int size ) { _Istream_cstrC s = { s, size }; return s; }
    345 forall( dtype istype | istream( istype ) )
    346 istype * ?|?( istype * is, _Istream_cstrC cstr ) {
     312_Istream_str1 str( char * s ) { _Istream_str1 s = { s }; return s; }
     313forall( dtype istype | istream( istype ) )
     314istype * ?|?( istype * is, _Istream_str1 str ) {
     315        scanfmt( is, "%s", str.s );
     316        return is;
     317} // str
     318
     319_Istream_str2 str( char * s, int size ) { _Istream_str2 s = { s, size }; return s; }
     320forall( dtype istype | istream( istype ) )
     321istype * ?|?( istype * is, _Istream_str2 str ) {
    347322        char buf[16];
    348         sprintf( buf, "%%%ds", cstr.size );
    349         scanfmt( is, buf, cstr.s );
    350         return is;
    351 } // cstr
     323        sprintf( buf, "%%%ds", str.size );
     324        scanfmt( is, buf, str.s );
     325        return is;
     326} // str
    352327
    353328// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.