Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/iostream.c

    r90c3b1c r6ba0659  
    77// iostream.c --
    88//
    9 // Author           : Peter A. Buhr
     9// Author           : Richard C. Bilson
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Mar  2 16:20:57 2016
    13 // Update Count     : 207
     12// Last Modified On : Wed Feb 17 14:19:56 2016
     13// Update Count     : 76
    1414//
    1515
     
    2121#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
    2222#include <complex.h>                                                                    // creal, cimag
    23 #include <ctype.h>                                                                              // isspace, ispunct
    2423}
    2524
    2625forall( dtype ostype | ostream( ostype ) )
    2726ostype * ?|?( ostype *os, char c ) {
    28         prtfmt( os, "%c", c );
    29         return os;
    30 } // ?|?
    31 
    32 forall( dtype ostype | ostream( ostype ) )
    33 ostype * ?|?( 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 
    39 forall( dtype ostype | ostream( ostype ) )
    40 ostype * ?|?( 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;
     27        return write( os, &c, 1 );
    4428} // ?|?
    4529
    4630forall( dtype ostype | ostream( ostype ) )
    4731ostype * ?|?( ostype *os, int i ) {
    48         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    49         prtfmt( os, "%d", i );
    50         return os;
     32        char buffer[32];
     33        return write( os, buffer, sprintf( buffer, "%d", i ) );
    5134} // ?|?
    5235
    5336forall( dtype ostype | ostream( ostype ) )
    54 ostype * ?|?( 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;
     37ostype * ?|?( ostype *os, unsigned int i ) {
     38        char buffer[32];
     39        return write( os, buffer, sprintf( buffer, "%u", i ) );
    5840} // ?|?
    5941
    6042forall( dtype ostype | ostream( ostype ) )
    61 ostype * ?|?( 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;
     43ostype * ?|?( ostype *os, long int i ) {
     44        char buffer[32];
     45        return write( os, buffer, sprintf( buffer, "%ld", i ) );
    6546} // ?|?
    6647
    6748forall( dtype ostype | ostream( ostype ) )
    68 ostype * ?|?( 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;
     49ostype * ?|?( ostype *os, long long int i ) {
     50        char buffer[32];
     51        return write( os, buffer, sprintf( buffer, "%lld", i ) );
    7252} // ?|?
    7353
    7454forall( dtype ostype | ostream( ostype ) )
    75 ostype * ?|?( 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;
     55ostype * ?|?( ostype *os, unsigned long int i ) {
     56        char buffer[32];
     57        return write( os, buffer, sprintf( buffer, "%lu", i ) );
    7958} // ?|?
    8059
    8160forall( dtype ostype | ostream( ostype ) )
    82 ostype * ?|?( 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;
     61ostype * ?|?( ostype *os, unsigned long long int i ) {
     62        char buffer[32];
     63        return write( os, buffer, sprintf( buffer, "%llu", i ) );
    8664} // ?|?
    8765
    8866forall( dtype ostype | ostream( ostype ) )
    8967ostype * ?|?( ostype *os, float f ) {
    90         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    91         prtfmt( os, "%g", f );
    92         return os;
     68        char buffer[32];
     69        return write( os, buffer, sprintf( buffer, "%g", f ) );
    9370} // ?|?
    9471
    9572forall( dtype ostype | ostream( ostype ) )
    9673ostype * ?|?( ostype *os, double d ) {
    97         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    98         prtfmt( os, "%.*lg", DBL_DIG, d );
    99         return os;
     74        char buffer[32];
     75        return write( os, buffer, sprintf( buffer, "%.*lg", DBL_DIG, d ) );
    10076} // ?|?
    10177
    10278forall( dtype ostype | ostream( ostype ) )
    103 ostype * ?|?( 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;
     79ostype * ?|?( ostype *os, long double d ) {
     80        char buffer[32];
     81        return write( os, buffer, sprintf( buffer, "%.*Lg", LDBL_DIG, d ) );
    10782} // ?|?
    10883
    10984forall( dtype ostype | ostream( ostype ) )
    110 ostype * ?|?( ostype *os, float _Complex fc ) {
    111         os | crealf( fc );
    112         if ( cimagf( fc ) >= 0 ) os | '+';
    113         os | "" | cimagf( fc ) | 'i';
    114         return os;
     85ostype * ?|?( ostype *os, float _Complex c ) {
     86        return os | crealf( c ) | (cimagf( c ) < 0 ? "" : "+") | cimagf( c ) | 'i';
    11587} // ?|?
    11688
    11789forall( dtype ostype | ostream( ostype ) )
    118 ostype * ?|?( ostype *os, double _Complex dc ) {
    119         os | creal( dc );
    120         if ( cimag( dc ) >= 0 ) os | '+';
    121         os | "" | cimag( dc ) | 'i';
    122         return os;
     90ostype * ?|?( ostype *os, double _Complex c ) {
     91        return os | creal( c ) | (cimag( c ) < 0 ? "" : "+") | cimag( c ) | 'i';
    12392} // ?|?
    12493
    12594forall( dtype ostype | ostream( ostype ) )
    126 ostype * ?|?( ostype *os, long double _Complex ldc ) {
    127         os | creall( ldc );
    128         if ( cimagl( ldc ) >= 0 ) os | '+';
    129         os | "" | cimagl( ldc ) | 'i';
    130         return os;
     95ostype * ?|?( ostype *os, long double _Complex c ) {
     96        return os | creall( c ) | (cimagl( c ) < 0 ? "" : "+") | cimagl( c ) | 'i';
     97} // ?|?
     98
     99forall( dtype ostype | ostream( ostype ) )
     100ostype * ?|?( ostype *os, const void *p ) {
     101        char buffer[32];
     102        return write( os, buffer, sprintf( buffer, "%p", p ) );
    131103} // ?|?
    132104
    133105forall( dtype ostype | ostream( ostype ) )
    134106ostype * ?|?( ostype *os, const char *cp ) {
    135         int len = strlen( cp );
    136         // null string => no separator
    137   if ( len == 0 ) { sepOff( os ); return os; }
    138         // first character NOT spacing or special punctuation => add left separator
    139         if ( sepPrt( os ) && isspace( cp[0] ) == 0 && cp[0] != '.' && cp[0] != ',' ) {
    140                 prtfmt( os, "%s", sepGet( os ) );
    141         } // if
    142         // last character is spacing or special punctuation => turn off separator for next item
    143         unsigned int posn = len - 1;
    144         if ( isspace( cp[posn] ) || cp[posn] == ':' || cp[posn] == '$' ) {
    145                 sepOff( os );
    146         } else {
    147                 sepOn( os );
    148         } // if
    149         return write( os, cp, len );
    150 } // ?|?
    151 
    152 forall( dtype ostype | ostream( ostype ) )
    153 ostype * ?|?( ostype *os, const void *p ) {
    154         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
    155         prtfmt( os, "%p", p );
    156         return os;
     107        return write( os, cp, strlen( cp ) );
    157108} // ?|?
    158109
    159110
    160 forall( dtype ostype | ostream( ostype ) )
    161 ostype * ?|?( ostype *os, ostype * (* manip)( ostype * ) ) {
    162         return manip( os );
    163 } // ?|?
     111forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) )
     112retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
     113  return manip( os );
     114}
    164115
    165116forall( dtype ostype | ostream( ostype ) )
    166117ostype * endl( ostype * os ) {
    167         os | '\n';
     118        os | "\n";
    168119        flush( os );
    169         sepOff( os );
    170120        return os;
    171121} // endl
    172122
    173 forall( dtype ostype | ostream( ostype ) )
    174 ostype * sepOn( ostype * os ) {
    175         sepOn( os );
    176         return os;
    177 } // sepOn
    178 
    179 forall( dtype ostype | ostream( ostype ) )
    180 ostype * sepOff( ostype * os ) {
    181         sepOff( os );
    182         return os;
    183 } // sepOff
    184 
    185123//---------------------------------------
    186124
    187 forall( type elttype | writeable( elttype ), type iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
    188 void write( iteratortype begin, iteratortype end, ostype *os ) {
    189         void print( elttype i ) { os | i; }
     125forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
     126                dtype os_type | ostream( os_type ) )
     127void write( iterator_type begin, iterator_type end, os_type *os ) {
     128        void print( elt_type i ) { os | i | ' '; }
    190129        for_each( begin, end, print );
    191130} // ?|?
    192131
    193 forall( type elttype | writeable( elttype ), type iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
    194 void write_reverse( iteratortype begin, iteratortype end, ostype *os ) {
    195         void print( elttype i ) { os | i; }
     132forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
     133                dtype os_type | ostream( os_type ) )
     134void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
     135        void print( elt_type i ) { os | i | ' '; }
    196136        for_each_reverse( begin, end, print );
    197137} // ?|?
     
    200140
    201141forall( dtype istype | istream( istype ) )
    202 istype * ?|?( istype * is, char * c ) {
    203         scanfmt( is, "%c", c );
    204         return is;
     142istype * ?|?( istype *is, char *cp ) {
     143        return read( is, cp, 1 );
    205144} // ?|?
    206145
    207146forall( dtype istype | istream( istype ) )
    208 istype * ?|?( istype * is, short int * si ) {
    209         scanfmt( is, "%hd", si );
    210         return is;
     147istype * ?|?( istype *is, int *ip ) {
     148        return get( is, ip );
    211149} // ?|?
    212 
    213 forall( dtype istype | istream( istype ) )
    214 istype * ?|?( istype * is, unsigned short int * usi ) {
    215         scanfmt( is, "%hu", usi );
    216         return is;
    217 } // ?|?
    218 
    219 forall( dtype istype | istream( istype ) )
    220 istype * ?|?( istype * is, int * i ) {
    221         scanfmt( is, "%d", i );
    222         return is;
    223 } // ?|?
    224 
    225 forall( dtype istype | istream( istype ) )
    226 istype * ?|?( istype * is, unsigned int * ui ) {
    227         scanfmt( is, "%u", ui );
    228         return is;
    229 } // ?|?
    230 
    231 forall( dtype istype | istream( istype ) )
    232 istype * ?|?( istype * is, long int * li ) {
    233         scanfmt( is, "%ld", li );
    234         return is;
    235 } // ?|?
    236 
    237 forall( dtype istype | istream( istype ) )
    238 istype * ?|?( istype * is, unsigned long int * ulli ) {
    239         scanfmt( is, "%lu", ulli );
    240         return is;
    241 } // ?|?
    242 
    243 forall( dtype istype | istream( istype ) )
    244 istype * ?|?( istype * is, long long int * lli ) {
    245         scanfmt( is, "%lld", lli );
    246         return is;
    247 } // ?|?
    248 
    249 forall( dtype istype | istream( istype ) )
    250 istype * ?|?( istype * is, unsigned long long int * ulli ) {
    251         scanfmt( is, "%llu", ulli );
    252         return is;
    253 } // ?|?
    254 
    255 
    256 forall( dtype istype | istream( istype ) )
    257 istype * ?|?( istype * is, float * f ) {
    258         scanfmt( is, "%f", f );
    259         return is;
    260 } // ?|?
    261 
    262 forall( dtype istype | istream( istype ) )
    263 istype * ?|?( istype * is, double * d ) {
    264         scanfmt( is, "%lf", d );
    265         return is;
    266 } // ?|?
    267 
    268 forall( dtype istype | istream( istype ) )
    269 istype * ?|?( istype * is, long double * ld ) {
    270         scanfmt( is, "%Lf", ld );
    271         return is;
    272 } // ?|?
    273 
    274 
    275 forall( dtype istype | istream( istype ) )
    276 istype * ?|?( istype * is, float _Complex * fc ) {
    277         float re, im;
    278         scanfmt( is, "%g%gi", &re, &im );
    279         *fc = re + im * _Complex_I;
    280         return is;
    281 } // ?|?
    282 
    283 forall( dtype istype | istream( istype ) )
    284 istype * ?|?( istype * is, double _Complex * dc ) {
    285         double re, im;
    286         scanfmt( is, "%lf%lfi", &re, &im );
    287         *dc = re + im * _Complex_I;
    288         return is;
    289 } // ?|?
    290 
    291 forall( dtype istype | istream( istype ) )
    292 istype * ?|?( istype * is, long double _Complex * ldc ) {
    293         long double re, im;
    294         scanfmt( is, "%Lf%Lfi", &re, &im );
    295         *ldc = re + im * _Complex_I;
    296         return is;
    297 } // ?|?
    298 
    299 _Istream_str1 str( char * s ) { _Istream_str1 s = { s }; return s; }
    300 forall( dtype istype | istream( istype ) )
    301 istype * ?|?( istype * is, _Istream_str1 str ) {
    302         scanfmt( is, "%s", str.s );
    303         return is;
    304 } // str
    305 
    306 _Istream_str2 str( char * s, int size ) { _Istream_str2 s = { s, size }; return s; }
    307 forall( dtype istype | istream( istype ) )
    308 istype * ?|?( istype * is, _Istream_str2 str ) {
    309         char buf[16];
    310         sprintf( buf, "%%%ds", str.size );
    311         scanfmt( is, buf, str.s );
    312         return is;
    313 } // str
    314150
    315151// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.