Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/iostream.c

    rb63e376 r53ba273  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Mar  7 13:51:23 2016
    13 // Update Count     : 227
     12// Last Modified On : Wed Apr  6 16:13:29 2016
     13// Update Count     : 278
    1414//
    1515
     
    2727ostype * ?|?( ostype *os, char c ) {
    2828        prtfmt( os, "%c", c );
     29        sepOff( os );
    2930        return os;
    3031} // ?|?
     
    3233forall( dtype ostype | ostream( ostype ) )
    3334ostype * ?|?( ostype *os, short int si ) {
    34         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     35        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     36        sepReset( os );
    3537        prtfmt( os, "%hd", si );
    3638        return os;
     
    3941forall( dtype ostype | ostream( ostype ) )
    4042ostype * ?|?( ostype *os, unsigned short int usi ) {
    41         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     43        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     44        sepReset( os );
    4245        prtfmt( os, "%hu", usi );
    4346        return os;
     
    4649forall( dtype ostype | ostream( ostype ) )
    4750ostype * ?|?( ostype *os, int i ) {
    48         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     51        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     52        sepReset( os );
    4953        prtfmt( os, "%d", i );
    5054        return os;
     
    5357forall( dtype ostype | ostream( ostype ) )
    5458ostype * ?|?( ostype *os, unsigned int ui ) {
    55         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     59        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     60        sepReset( os );
    5661        prtfmt( os, "%u", ui );
    5762        return os;
     
    6065forall( dtype ostype | ostream( ostype ) )
    6166ostype * ?|?( ostype *os, long int li ) {
    62         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     67        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     68        sepReset( os );
    6369        prtfmt( os, "%ld", li );
    6470        return os;
     
    6773forall( dtype ostype | ostream( ostype ) )
    6874ostype * ?|?( ostype *os, unsigned long int uli ) {
    69         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     75        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     76        sepReset( os );
    7077        prtfmt( os, "%lu", uli );
    7178        return os;
     
    7481forall( dtype ostype | ostream( ostype ) )
    7582ostype * ?|?( ostype *os, long long int lli ) {
    76         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     83        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     84        sepReset( os );
    7785        prtfmt( os, "%lld", lli );
    7886        return os;
     
    8189forall( dtype ostype | ostream( ostype ) )
    8290ostype * ?|?( ostype *os, unsigned long long int ulli ) {
    83         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     91        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     92        sepReset( os );
    8493        prtfmt( os, "%llu", ulli );
    8594        return os;
     
    8897forall( dtype ostype | ostream( ostype ) )
    8998ostype * ?|?( ostype *os, float f ) {
    90         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     99        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     100        sepReset( os );
    91101        prtfmt( os, "%g", f );
    92102        return os;
     
    95105forall( dtype ostype | ostream( ostype ) )
    96106ostype * ?|?( ostype *os, double d ) {
    97         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     107        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     108        sepReset( os );
    98109        prtfmt( os, "%.*lg", DBL_DIG, d );
    99110        return os;
     
    102113forall( dtype ostype | ostream( ostype ) )
    103114ostype * ?|?( ostype *os, long double ld ) {
    104         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     115        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     116        sepReset( os );
    105117        prtfmt( os, "%.*Lg", LDBL_DIG, ld );
    106118        return os;
     
    110122ostype * ?|?( ostype *os, float _Complex fc ) {
    111123        os | crealf( fc );
    112         if ( cimagf( fc ) >= 0 ) os | '+';
    113         os | "" | cimagf( fc ) | 'i';
     124        _Bool temp = sepDisable( os );                                          // disable separators within complex value
     125        if ( cimagf( fc ) >= 0 ) os | '+';                                      // negative value prints '-'
     126        os | cimagf( fc ) | 'i';
     127        sepReset( os, temp );                                                           // reset separator
    114128        return os;
    115129} // ?|?
     
    118132ostype * ?|?( ostype *os, double _Complex dc ) {
    119133        os | creal( dc );
    120         if ( cimag( dc ) >= 0 ) os | '+';
    121         os | "" | cimag( dc ) | 'i';
     134        _Bool temp = sepDisable( os );                                          // disable separators within complex value
     135        if ( cimag( dc ) >= 0 ) os | '+';                                       // negative value prints '-'
     136        os | cimag( dc ) | 'i';
     137        sepReset( os, temp );                                                           // reset separator
    122138        return os;
    123139} // ?|?
     
    126142ostype * ?|?( ostype *os, long double _Complex ldc ) {
    127143        os | creall( ldc );
    128         if ( cimagl( ldc ) >= 0 ) os | '+';
    129         os | "" | cimagl( ldc ) | 'i';
     144        _Bool temp = sepDisable( os );                                          // disable separators within complex value
     145        if ( cimagl( ldc ) >= 0 ) os | '+';                                     // negative value prints '-'
     146        os | cimagl( ldc ) | 'i';
     147        sepReset( os, temp );                                                           // reset separator
    130148        return os;
    131149} // ?|?
     
    134152ostype * ?|?( ostype *os, const char *cp ) {
    135153        enum { Open = 1, Close, OpenClose };
    136         static const char mask[256] = {
     154        static const unsigned char mask[256] = {
    137155                // opening delimiters
    138156                ['('] : Open, ['['] : Open, ['{'] : Open,
    139                 ['$'] : Open, [L'£'] : Open, [L'¥'] : Open, [L'¢'] : Open, [L'¿'] : Open, [L'«'] : Open,
     157                ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
    140158                // closing delimiters
    141159                [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
    142160                [')'] : Close, [']'] : Close, ['}'] : Close,
    143                 ['%'] : Close, [L'»'] : Close,
     161                ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
    144162                // opening-closing delimiters
    145163                ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose,
     164                ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
    146165        }; // mask
    147166
     
    149168        // null string => no separator
    150169  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 ) {
     170        // first character IS NOT spacing or closing punctuation => add left separator
     171        unsigned char ch = cp[0];                                                       // must make unsigned
     172        if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
    153173                prtfmt( os, "%s", sepGet( os ) );
    154174        } // if
    155175        // last character IS spacing or opening punctuation => turn off separator for next item
    156176        unsigned int posn = len - 1;
    157         if ( isspace( cp[posn] ) || mask[ cp[posn] ] == Open || mask[ cp[posn] ] == OpenClose ) {
     177        ch = cp[posn];                                                                          // must make unsigned
     178        if ( mask[ ch ] == Open || mask[ ch ] == OpenClose ) {
    158179                sepOff( os );
    159180        } else {
     
    165186forall( dtype ostype | ostream( ostype ) )
    166187ostype * ?|?( ostype *os, const void *p ) {
    167         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     188        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     189        sepReset( os );
    168190        prtfmt( os, "%p", p );
    169191        return os;
     
    196218} // sepOff
    197219
     220forall( dtype ostype | ostream( ostype ) )
     221ostype * sepEnable( ostype * os ) {
     222        sepEnable( os );
     223        return os;
     224} // sepEnable
     225
     226forall( dtype ostype | ostream( ostype ) )
     227ostype * sepDisable( ostype * os ) {
     228        sepDisable( os );
     229        return os;
     230} // sepDisable
     231
    198232//---------------------------------------
    199233
     
    310344} // ?|?
    311345
    312 _Istream_str1 str( char * s ) { _Istream_str1 s = { s }; return s; }
    313 forall( dtype istype | istream( istype ) )
    314 istype * ?|?( 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; }
    320 forall( dtype istype | istream( istype ) )
    321 istype * ?|?( istype * is, _Istream_str2 str ) {
     346_Istream_cstrUC cstr( char * s ) { _Istream_cstrUC s = { s }; return s; }
     347forall( dtype istype | istream( istype ) )
     348istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
     349        scanfmt( is, "%s", cstr.s );
     350        return is;
     351} // cstr
     352
     353_Istream_cstrC cstr( char * s, int size ) { _Istream_cstrC s = { s, size }; return s; }
     354forall( dtype istype | istream( istype ) )
     355istype * ?|?( istype * is, _Istream_cstrC cstr ) {
    322356        char buf[16];
    323         sprintf( buf, "%%%ds", str.size );
    324         scanfmt( is, buf, str.s );
    325         return is;
    326 } // str
     357        sprintf( buf, "%%%ds", cstr.size );
     358        scanfmt( is, buf, cstr.s );
     359        return is;
     360} // cstr
    327361
    328362// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.