Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/iostream.c

    r6ba0659 rb63e376  
    77// iostream.c --
    88//
    9 // Author           : Richard C. Bilson
     9// Author           : Peter A. Buhr
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb 17 14:19:56 2016
    13 // Update Count     : 76
     12// Last Modified On : Mon Mar  7 13:51:23 2016
     13// Update Count     : 227
    1414//
    1515
     
    2121#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
    2222#include <complex.h>                                                                    // creal, cimag
     23#include <ctype.h>                                                                              // isspace, ispunct
    2324}
    2425
    2526forall( dtype ostype | ostream( ostype ) )
    2627ostype * ?|?( ostype *os, char c ) {
    27         return write( os, &c, 1 );
     28        prtfmt( os, "%c", c );
     29        return os;
     30} // ?|?
     31
     32forall( dtype ostype | ostream( ostype ) )
     33ostype * ?|?( ostype *os, short int si ) {
     34        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     35        prtfmt( os, "%hd", si );
     36        return os;
     37} // ?|?
     38
     39forall( dtype ostype | ostream( ostype ) )
     40ostype * ?|?( ostype *os, unsigned short int usi ) {
     41        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     42        prtfmt( os, "%hu", usi );
     43        return os;
    2844} // ?|?
    2945
    3046forall( dtype ostype | ostream( ostype ) )
    3147ostype * ?|?( ostype *os, int i ) {
    32         char buffer[32];
    33         return write( os, buffer, sprintf( buffer, "%d", i ) );
    34 } // ?|?
    35 
    36 forall( dtype ostype | ostream( ostype ) )
    37 ostype * ?|?( ostype *os, unsigned int i ) {
    38         char buffer[32];
    39         return write( os, buffer, sprintf( buffer, "%u", i ) );
    40 } // ?|?
    41 
    42 forall( dtype ostype | ostream( ostype ) )
    43 ostype * ?|?( ostype *os, long int i ) {
    44         char buffer[32];
    45         return write( os, buffer, sprintf( buffer, "%ld", i ) );
    46 } // ?|?
    47 
    48 forall( dtype ostype | ostream( ostype ) )
    49 ostype * ?|?( ostype *os, long long int i ) {
    50         char buffer[32];
    51         return write( os, buffer, sprintf( buffer, "%lld", i ) );
    52 } // ?|?
    53 
    54 forall( dtype ostype | ostream( ostype ) )
    55 ostype * ?|?( ostype *os, unsigned long int i ) {
    56         char buffer[32];
    57         return write( os, buffer, sprintf( buffer, "%lu", i ) );
    58 } // ?|?
    59 
    60 forall( dtype ostype | ostream( ostype ) )
    61 ostype * ?|?( ostype *os, unsigned long long int i ) {
    62         char buffer[32];
    63         return write( os, buffer, sprintf( buffer, "%llu", i ) );
     48        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     49        prtfmt( os, "%d", i );
     50        return os;
     51} // ?|?
     52
     53forall( dtype ostype | ostream( ostype ) )
     54ostype * ?|?( ostype *os, unsigned int ui ) {
     55        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     56        prtfmt( os, "%u", ui );
     57        return os;
     58} // ?|?
     59
     60forall( dtype ostype | ostream( ostype ) )
     61ostype * ?|?( ostype *os, long int li ) {
     62        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     63        prtfmt( os, "%ld", li );
     64        return os;
     65} // ?|?
     66
     67forall( dtype ostype | ostream( ostype ) )
     68ostype * ?|?( ostype *os, unsigned long int uli ) {
     69        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     70        prtfmt( os, "%lu", uli );
     71        return os;
     72} // ?|?
     73
     74forall( dtype ostype | ostream( ostype ) )
     75ostype * ?|?( ostype *os, long long int lli ) {
     76        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     77        prtfmt( os, "%lld", lli );
     78        return os;
     79} // ?|?
     80
     81forall( dtype ostype | ostream( ostype ) )
     82ostype * ?|?( ostype *os, unsigned long long int ulli ) {
     83        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     84        prtfmt( os, "%llu", ulli );
     85        return os;
    6486} // ?|?
    6587
    6688forall( dtype ostype | ostream( ostype ) )
    6789ostype * ?|?( ostype *os, float f ) {
    68         char buffer[32];
    69         return write( os, buffer, sprintf( buffer, "%g", f ) );
     90        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     91        prtfmt( os, "%g", f );
     92        return os;
    7093} // ?|?
    7194
    7295forall( dtype ostype | ostream( ostype ) )
    7396ostype * ?|?( ostype *os, double d ) {
    74         char buffer[32];
    75         return write( os, buffer, sprintf( buffer, "%.*lg", DBL_DIG, d ) );
    76 } // ?|?
    77 
    78 forall( dtype ostype | ostream( ostype ) )
    79 ostype * ?|?( ostype *os, long double d ) {
    80         char buffer[32];
    81         return write( os, buffer, sprintf( buffer, "%.*Lg", LDBL_DIG, d ) );
    82 } // ?|?
    83 
    84 forall( dtype ostype | ostream( ostype ) )
    85 ostype * ?|?( ostype *os, float _Complex c ) {
    86         return os | crealf( c ) | (cimagf( c ) < 0 ? "" : "+") | cimagf( c ) | 'i';
    87 } // ?|?
    88 
    89 forall( dtype ostype | ostream( ostype ) )
    90 ostype * ?|?( ostype *os, double _Complex c ) {
    91         return os | creal( c ) | (cimag( c ) < 0 ? "" : "+") | cimag( c ) | 'i';
    92 } // ?|?
    93 
    94 forall( dtype ostype | ostream( ostype ) )
    95 ostype * ?|?( ostype *os, long double _Complex c ) {
    96         return os | creall( c ) | (cimagl( c ) < 0 ? "" : "+") | cimagl( c ) | 'i';
     97        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     98        prtfmt( os, "%.*lg", DBL_DIG, d );
     99        return os;
     100} // ?|?
     101
     102forall( dtype ostype | ostream( ostype ) )
     103ostype * ?|?( ostype *os, long double ld ) {
     104        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     105        prtfmt( os, "%.*Lg", LDBL_DIG, ld );
     106        return os;
     107} // ?|?
     108
     109forall( dtype ostype | ostream( ostype ) )
     110ostype * ?|?( ostype *os, float _Complex fc ) {
     111        os | crealf( fc );
     112        if ( cimagf( fc ) >= 0 ) os | '+';
     113        os | "" | cimagf( fc ) | 'i';
     114        return os;
     115} // ?|?
     116
     117forall( dtype ostype | ostream( ostype ) )
     118ostype * ?|?( ostype *os, double _Complex dc ) {
     119        os | creal( dc );
     120        if ( cimag( dc ) >= 0 ) os | '+';
     121        os | "" | cimag( dc ) | 'i';
     122        return os;
     123} // ?|?
     124
     125forall( dtype ostype | ostream( ostype ) )
     126ostype * ?|?( ostype *os, long double _Complex ldc ) {
     127        os | creall( ldc );
     128        if ( cimagl( ldc ) >= 0 ) os | '+';
     129        os | "" | cimagl( ldc ) | 'i';
     130        return os;
     131} // ?|?
     132
     133forall( dtype ostype | ostream( ostype ) )
     134ostype * ?|?( ostype *os, const char *cp ) {
     135        enum { Open = 1, Close, OpenClose };
     136        static const char mask[256] = {
     137                // opening delimiters
     138                ['('] : Open, ['['] : Open, ['{'] : Open,
     139                ['$'] : Open, [L'£'] : Open, [L'¥'] : Open, [L'¢'] : Open, [L'¿'] : Open, [L'«'] : Open,
     140                // closing delimiters
     141                [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
     142                [')'] : Close, [']'] : Close, ['}'] : Close,
     143                ['%'] : Close, [L'»'] : Close,
     144                // opening-closing delimiters
     145                ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose,
     146        }; // mask
     147
     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 ) {
     153                prtfmt( os, "%s", sepGet( os ) );
     154        } // if
     155        // last character IS spacing or opening punctuation => turn off separator for next item
     156        unsigned int posn = len - 1;
     157        if ( isspace( cp[posn] ) || mask[ cp[posn] ] == Open || mask[ cp[posn] ] == OpenClose ) {
     158                sepOff( os );
     159        } else {
     160                sepOn( os );
     161        } // if
     162        return write( os, cp, len );
    97163} // ?|?
    98164
    99165forall( dtype ostype | ostream( ostype ) )
    100166ostype * ?|?( ostype *os, const void *p ) {
    101         char buffer[32];
    102         return write( os, buffer, sprintf( buffer, "%p", p ) );
    103 } // ?|?
    104 
    105 forall( dtype ostype | ostream( ostype ) )
    106 ostype * ?|?( ostype *os, const char *cp ) {
    107         return write( os, cp, strlen( cp ) );
    108 } // ?|?
    109 
    110 
    111 forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) )
    112 retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
    113   return manip( os );
    114 }
     167        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     168        prtfmt( os, "%p", p );
     169        return os;
     170} // ?|?
     171
     172
     173forall( dtype ostype | ostream( ostype ) )
     174ostype * ?|?( ostype *os, ostype * (* manip)( ostype * ) ) {
     175        return manip( os );
     176} // ?|?
    115177
    116178forall( dtype ostype | ostream( ostype ) )
    117179ostype * endl( ostype * os ) {
    118         os | "\n";
     180        os | '\n';
    119181        flush( os );
     182        sepOff( os );
    120183        return os;
    121184} // endl
    122185
     186forall( dtype ostype | ostream( ostype ) )
     187ostype * sepOn( ostype * os ) {
     188        sepOn( os );
     189        return os;
     190} // sepOn
     191
     192forall( dtype ostype | ostream( ostype ) )
     193ostype * sepOff( ostype * os ) {
     194        sepOff( os );
     195        return os;
     196} // sepOff
     197
    123198//---------------------------------------
    124199
    125 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
    126                 dtype os_type | ostream( os_type ) )
    127 void write( iterator_type begin, iterator_type end, os_type *os ) {
    128         void print( elt_type i ) { os | i | ' '; }
     200forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
     201void write( iteratortype begin, iteratortype end, ostype *os ) {
     202        void print( elttype i ) { os | i; }
    129203        for_each( begin, end, print );
    130204} // ?|?
    131205
    132 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
    133                 dtype os_type | ostream( os_type ) )
    134 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
    135         void print( elt_type i ) { os | i | ' '; }
     206forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
     207void write_reverse( iteratortype begin, iteratortype end, ostype *os ) {
     208        void print( elttype i ) { os | i; }
    136209        for_each_reverse( begin, end, print );
    137210} // ?|?
     
    140213
    141214forall( dtype istype | istream( istype ) )
    142 istype * ?|?( istype *is, char *cp ) {
    143         return read( is, cp, 1 );
    144 } // ?|?
    145 
    146 forall( dtype istype | istream( istype ) )
    147 istype * ?|?( istype *is, int *ip ) {
    148         return get( is, ip );
    149 } // ?|?
     215istype * ?|?( istype * is, char * c ) {
     216        scanfmt( is, "%c", c );
     217        return is;
     218} // ?|?
     219
     220forall( dtype istype | istream( istype ) )
     221istype * ?|?( istype * is, short int * si ) {
     222        scanfmt( is, "%hd", si );
     223        return is;
     224} // ?|?
     225
     226forall( dtype istype | istream( istype ) )
     227istype * ?|?( istype * is, unsigned short int * usi ) {
     228        scanfmt( is, "%hu", usi );
     229        return is;
     230} // ?|?
     231
     232forall( dtype istype | istream( istype ) )
     233istype * ?|?( istype * is, int * i ) {
     234        scanfmt( is, "%d", i );
     235        return is;
     236} // ?|?
     237
     238forall( dtype istype | istream( istype ) )
     239istype * ?|?( istype * is, unsigned int * ui ) {
     240        scanfmt( is, "%u", ui );
     241        return is;
     242} // ?|?
     243
     244forall( dtype istype | istream( istype ) )
     245istype * ?|?( istype * is, long int * li ) {
     246        scanfmt( is, "%ld", li );
     247        return is;
     248} // ?|?
     249
     250forall( dtype istype | istream( istype ) )
     251istype * ?|?( istype * is, unsigned long int * ulli ) {
     252        scanfmt( is, "%lu", ulli );
     253        return is;
     254} // ?|?
     255
     256forall( dtype istype | istream( istype ) )
     257istype * ?|?( istype * is, long long int * lli ) {
     258        scanfmt( is, "%lld", lli );
     259        return is;
     260} // ?|?
     261
     262forall( dtype istype | istream( istype ) )
     263istype * ?|?( istype * is, unsigned long long int * ulli ) {
     264        scanfmt( is, "%llu", ulli );
     265        return is;
     266} // ?|?
     267
     268
     269forall( dtype istype | istream( istype ) )
     270istype * ?|?( istype * is, float * f ) {
     271        scanfmt( is, "%f", f );
     272        return is;
     273} // ?|?
     274
     275forall( dtype istype | istream( istype ) )
     276istype * ?|?( istype * is, double * d ) {
     277        scanfmt( is, "%lf", d );
     278        return is;
     279} // ?|?
     280
     281forall( dtype istype | istream( istype ) )
     282istype * ?|?( istype * is, long double * ld ) {
     283        scanfmt( is, "%Lf", ld );
     284        return is;
     285} // ?|?
     286
     287
     288forall( dtype istype | istream( istype ) )
     289istype * ?|?( istype * is, float _Complex * fc ) {
     290        float re, im;
     291        scanfmt( is, "%g%gi", &re, &im );
     292        *fc = re + im * _Complex_I;
     293        return is;
     294} // ?|?
     295
     296forall( dtype istype | istream( istype ) )
     297istype * ?|?( istype * is, double _Complex * dc ) {
     298        double re, im;
     299        scanfmt( is, "%lf%lfi", &re, &im );
     300        *dc = re + im * _Complex_I;
     301        return is;
     302} // ?|?
     303
     304forall( dtype istype | istream( istype ) )
     305istype * ?|?( istype * is, long double _Complex * ldc ) {
     306        long double re, im;
     307        scanfmt( is, "%Lf%Lfi", &re, &im );
     308        *ldc = re + im * _Complex_I;
     309        return is;
     310} // ?|?
     311
     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 ) {
     322        char buf[16];
     323        sprintf( buf, "%%%ds", str.size );
     324        scanfmt( is, buf, str.s );
     325        return is;
     326} // str
    150327
    151328// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.