source: libcfa/src/iostream.cfa @ 9d362a0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 9d362a0 was 9d362a0, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

simplify code

  • Property mode set to 100644
File size: 14.1 KB
RevLine 
[86bd7c1f]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[a5a71d0]7// iostream.c --
[86bd7c1f]8//
[90c3b1c]9// Author           : Peter A. Buhr
[86bd7c1f]10// Created On       : Wed May 27 17:56:53 2015
[e24f13a]11// Last Modified By : Peter A. Buhr
[9d362a0]12// Last Modified On : Mon Dec 24 18:33:40 2018
13// Update Count     : 589
[86bd7c1f]14//
[51b7345]15
[58b6d1b]16#include "iostream.hfa"
[d3b7937]17
[51b7345]18extern "C" {
19#include <stdio.h>
[53a6c2a]20#include <stdbool.h>                                                                    // true/false
[200fcb3]21//#include <string.h>                                                                   // strlen, strcmp
[44574f2]22extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
23extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
[52f85e0]24#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
[d3b7937]25#include <complex.h>                                                                    // creal, cimag
[51b7345]26}
27
[3ce0d440]28forall( dtype ostype | ostream( ostype ) ) {
[93c2e0a]29        ostype & ?|?( ostype & os, bool b ) {
[3ce0d440]30                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
31                fmt( os, "%s", b ? "true" : "false" );
32                return os;
33        } // ?|?
[200fcb3]34        void ?|?( ostype & os, bool b ) {
[9d362a0]35                (ostype &)(os | b); nl( os );
[200fcb3]36        } // ?|?
[51b7345]37
[200fcb3]38        ostype & ?|?( ostype & os, char c ) {
39                fmt( os, "%c", c );
40                if ( c == '\n' ) setNL( os, true );
41                return sepOff( os );
42        } // ?|?
43        void ?|?( ostype & os, char c ) {
[9d362a0]44                (ostype &)(os | c); nl( os );
[3ce0d440]45        } // ?|?
46
[200fcb3]47        ostype & ?|?( ostype & os, signed char sc ) {
[3ce0d440]48                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[200fcb3]49                fmt( os, "%hhd", sc );
[3ce0d440]50                return os;
51        } // ?|?
[200fcb3]52        void ?|?( ostype & os, signed char sc ) {
[9d362a0]53                (ostype &)(os | sc); nl( os );
[200fcb3]54        } // ?|?
[3ce0d440]55
[200fcb3]56        ostype & ?|?( ostype & os, unsigned char usc ) {
[3ce0d440]57                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[200fcb3]58                fmt( os, "%hhu", usc );
[3ce0d440]59                return os;
60        } // ?|?
[200fcb3]61        void ?|?( ostype & os, unsigned char usc ) {
[9d362a0]62                (ostype &)(os | usc); nl( os );
[200fcb3]63        } // ?|?
[3ce0d440]64
65        ostype & ?|?( ostype & os, short int si ) {
66                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
67                fmt( os, "%hd", si );
68                return os;
69        } // ?|?
[200fcb3]70        void & ?|?( ostype & os, short int si ) {
[9d362a0]71                (ostype &)(os | si); nl( os );
[200fcb3]72        } // ?|?
[3ce0d440]73
74        ostype & ?|?( ostype & os, unsigned short int usi ) {
75                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
76                fmt( os, "%hu", usi );
77                return os;
78        } // ?|?
[200fcb3]79        void & ?|?( ostype & os, unsigned short int usi ) {
[9d362a0]80                (ostype &)(os | usi); nl( os );
[200fcb3]81        } // ?|?
[3ce0d440]82
83        ostype & ?|?( ostype & os, int i ) {
84                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
85                fmt( os, "%d", i );
86                return os;
87        } // ?|?
[200fcb3]88        void & ?|?( ostype & os, int i ) {
[9d362a0]89                (ostype &)(os | i); nl( os );
[200fcb3]90        } // ?|?
[3ce0d440]91
92        ostype & ?|?( ostype & os, unsigned int ui ) {
93                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
94                fmt( os, "%u", ui );
95                return os;
96        } // ?|?
[200fcb3]97        void & ?|?( ostype & os, unsigned int ui ) {
[9d362a0]98                (ostype &)(os | ui); nl( os );
[200fcb3]99        } // ?|?
[3ce0d440]100
101        ostype & ?|?( ostype & os, long int li ) {
102                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
103                fmt( os, "%ld", li );
104                return os;
105        } // ?|?
[200fcb3]106        void & ?|?( ostype & os, long int li ) {
[9d362a0]107                (ostype &)(os | li); nl( os );
[200fcb3]108        } // ?|?
[3ce0d440]109
110        ostype & ?|?( ostype & os, unsigned long int uli ) {
111                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
112                fmt( os, "%lu", uli );
113                return os;
114        } // ?|?
[200fcb3]115        void & ?|?( ostype & os, unsigned long int uli ) {
[9d362a0]116                (ostype &)(os | uli); nl( os );
[200fcb3]117        } // ?|?
[3ce0d440]118
119        ostype & ?|?( ostype & os, long long int lli ) {
120                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
121                fmt( os, "%lld", lli );
122                return os;
123        } // ?|?
[200fcb3]124        void & ?|?( ostype & os, long long int lli ) {
[9d362a0]125                (ostype &)(os | lli); nl( os );
[200fcb3]126        } // ?|?
[3ce0d440]127
128        ostype & ?|?( ostype & os, unsigned long long int ulli ) {
129                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
130                fmt( os, "%llu", ulli );
131                return os;
132        } // ?|?
[200fcb3]133        void & ?|?( ostype & os, unsigned long long int ulli ) {
[9d362a0]134                (ostype &)(os | ulli); nl( os );
[200fcb3]135        } // ?|?
[3ce0d440]136
137        ostype & ?|?( ostype & os, float f ) {
138                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
139                fmt( os, "%g", f );
140                return os;
141        } // ?|?
[200fcb3]142        void & ?|?( ostype & os, float f ) {
[9d362a0]143                (ostype &)(os | f); nl( os );
[200fcb3]144        } // ?|?
[3ce0d440]145
146        ostype & ?|?( ostype & os, double d ) {
147                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
148                fmt( os, "%.*lg", DBL_DIG, d );
149                return os;
150        } // ?|?
[200fcb3]151        void & ?|?( ostype & os, double d ) {
[9d362a0]152                (ostype &)(os | d); nl( os );
[200fcb3]153        } // ?|?
[3ce0d440]154
155        ostype & ?|?( ostype & os, long double ld ) {
156                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
157                fmt( os, "%.*Lg", LDBL_DIG, ld );
158                return os;
159        } // ?|?
[200fcb3]160        void & ?|?( ostype & os, long double ld ) {
[9d362a0]161                (ostype &)(os | ld); nl( os );
[200fcb3]162        } // ?|?
[3ce0d440]163
164        ostype & ?|?( ostype & os, float _Complex fc ) {
165                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
166                fmt( os, "%g%+gi", crealf( fc ), cimagf( fc ) );
167                return os;
168        } // ?|?
[200fcb3]169        void & ?|?( ostype & os, float _Complex fc ) {
[9d362a0]170                (ostype &)(os | fc); nl( os );
[200fcb3]171        } // ?|?
[3ce0d440]172
173        ostype & ?|?( ostype & os, double _Complex dc ) {
174                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
175                fmt( os, "%.*lg%+.*lgi", DBL_DIG, creal( dc ), DBL_DIG, cimag( dc ) );
176                return os;
177        } // ?|?
[200fcb3]178        void & ?|?( ostype & os, double _Complex dc ) {
[9d362a0]179                (ostype &)(os | dc); nl( os );
[200fcb3]180        } // ?|?
[3ce0d440]181
182        ostype & ?|?( ostype & os, long double _Complex ldc ) {
183                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
184                fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) );
185                return os;
186        } // ?|?
[200fcb3]187        void & ?|?( ostype & os, long double _Complex ldc ) {
[9d362a0]188                (ostype &)(os | ldc); nl( os );
[200fcb3]189        } // ?|?
[3ce0d440]190
191        ostype & ?|?( ostype & os, const char * str ) {
192                enum { Open = 1, Close, OpenClose };
193                static const unsigned char mask[256] @= {
194                        // opening delimiters, no space after
195                        ['('] : Open, ['['] : Open, ['{'] : Open,
[ad72c8b]196                        ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
197                        [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
[3ce0d440]198                        // closing delimiters, no space before
199                        [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
[ad72c8b]200                        ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
[3ce0d440]201                        [')'] : Close, [']'] : Close, ['}'] : Close,
202                        // opening-closing delimiters, no space before or after
203                        ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
204                        [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
205                }; // mask
206
207          if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
208
209                // first character IS NOT spacing or closing punctuation => add left separator
210                unsigned char ch = str[0];                                              // must make unsigned
211                if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
212                        fmt( os, "%s", sepGetCur( os ) );
213                } // if
214
215                // if string starts line, must reset to determine open state because separator is off
216                sepReset( os );                                                                 // reset separator
217
218                // last character IS spacing or opening punctuation => turn off separator for next item
219                size_t len = strlen( str );
220                ch = str[len - 1];                                                              // must make unsigned
221                if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
222                        sepOn( os );
223                } else {
224                        sepOff( os );
225                } // if
226                if ( ch == '\n' ) setNL( os, true );                    // check *AFTER* sepPrt call above as it resets NL flag
227                return write( os, str, len );
228        } // ?|?
[200fcb3]229        void ?|?( ostype & os, const char * str ) {
[9d362a0]230                (ostype &)(os | str); nl( os );
[200fcb3]231        } // ?|?
[3ce0d440]232
233//      ostype & ?|?( ostype & os, const char16_t * str ) {
234//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
235//              fmt( os, "%ls", str );
236//              return os;
237//      } // ?|?
[d3b7937]238
[3ce0d440]239// #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
240//      ostype & ?|?( ostype & os, const char32_t * str ) {
241//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
242//              fmt( os, "%ls", str );
243//              return os;
244//      } // ?|?
245// #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
[d3b7937]246
[3ce0d440]247//      ostype & ?|?( ostype & os, const wchar_t * str ) {
248//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
249//              fmt( os, "%ls", str );
250//              return os;
251//      } // ?|?
252
253        ostype & ?|?( ostype & os, const void * p ) {
254                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
255                fmt( os, "%p", p );
256                return os;
257        } // ?|?
[200fcb3]258        void ?|?( ostype & os, const void * p ) {
[9d362a0]259                (ostype &)(os | p); nl( os );
[200fcb3]260        } // ?|?
[3ce0d440]261
262        // manipulators
263        ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
[9d362a0]264                (ostype &)(manip( os ));
[200fcb3]265                return os;
266        } // ?|?
267        void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
[9d362a0]268                (ostype &)(manip( os ));
269                if ( getPrt( os ) ) nl( os );                                   // something printed ?
[5ea5b28]270                setPrt( os, false );                                                    // turn off
[3ce0d440]271        } // ?|?
272
273        ostype & sep( ostype & os ) {
[9d362a0]274                return (ostype &)(os | sepGet( os ));
[3ce0d440]275        } // sep
276
277        ostype & sepTuple( ostype & os ) {
[200fcb3]278                return os | sepGetTuple( os );
[3ce0d440]279        } // sepTuple
280
[200fcb3]281        ostype & nl( ostype & os ) {
[9d362a0]282                (ostype &)(os | '\n');
[5ea5b28]283                setPrt( os, false );                                                    // turn off
[3ce0d440]284                setNL( os, true );
285                flush( os );
[200fcb3]286                return sepOff( os );                                                    // prepare for next line
287        } // nl
288
[9d362a0]289        void nl( ostype & os ) {
290                if ( getANL( os ) ) (ostype &)(nl( os ));               // implementation only
291                else setPrt( os, false );                                               // turn off
292        } // nl
293
[200fcb3]294        ostype & nonl( ostype & os ) {
[5ea5b28]295                setPrt( os, false );                                                    // turn off
[3ce0d440]296                return os;
[200fcb3]297        } // nonl
[3ce0d440]298
299        ostype & sepOn( ostype & os ) {
[200fcb3]300                sepOn( os );                                                                    // call void returning
[3ce0d440]301                return os;
302        } // sepOn
303
304        ostype & sepOff( ostype & os ) {
[200fcb3]305                sepOff( os );                                                                   // call void returning
[3ce0d440]306                return os;
307        } // sepOff
[6de9f4a]308
[3ce0d440]309        ostype & sepEnable( ostype & os ) {
[200fcb3]310                sepEnable( os );                                                                // call void returning
[3ce0d440]311                return os;
312        } // sepEnable
[b6dc097]313
[3ce0d440]314        ostype & sepDisable( ostype & os ) {
[200fcb3]315                sepDisable( os );                                                               // call void returning
[3ce0d440]316                return os;
317        } // sepDisable
[cf16f94]318
[200fcb3]319        ostype & nlOn( ostype & os ) {
320                nlOn( os );                                                                             // call void returning
321                return os;
322        } // nlOn
323
324        ostype & nlOff( ostype & os ) {
325                nlOff( os );                                                                    // call void returning
326                return os;
327        } // nlOff
328} // distribution
[cf16f94]329
[c443d1d]330// tuples
[200fcb3]331forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
332        ostype & ?|?( ostype & os, T arg, Params rest ) {
[9d362a0]333                (ostype &)(os | arg);                                                   // print first argument
[200fcb3]334                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
[9d362a0]335                (ostype &)(os | rest);                                                  // print remaining arguments
[200fcb3]336                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
337                return os;
338        } // ?|?
339        void ?|?( ostype & os, T arg, Params rest ) {
[9d362a0]340                // (ostype &)(?|?( os, arg, rest )); nl( os );
341                (ostype &)(os | arg);                                                   // print first argument
[200fcb3]342                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
[9d362a0]343                (ostype &)(os | rest);                                                  // print remaining arguments
[200fcb3]344                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
[9d362a0]345                nl( os );
[200fcb3]346        } // ?|?
347} // distribution
[c443d1d]348
[6ba0659]349//---------------------------------------
350
[3ce0d440]351// writes the range [begin, end) to the given stream
[200fcb3]352forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
353        void write( iterator_type begin, iterator_type end, ostype & os ) {
354                void print( elt_type i ) { os | i; }
355                for_each( begin, end, print );
356        } // ?|?
357
358        void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
359                void print( elt_type i ) { os | i; }
360                for_each_reverse( begin, end, print );
361        } // ?|?
362} // distribution
[e56cfdb0]363
[6ba0659]364//---------------------------------------
[e56cfdb0]365
[3ce0d440]366forall( dtype istype | istream( istype ) ) {
[93c2e0a]367        istype & ?|?( istype & is, bool & b ) {
[3ce0d440]368                char val[6];
369                fmt( is, "%5s", val );
370                if ( strcmp( val, "true" ) == 0 ) b = true;
371                else if ( strcmp( val, "false" ) == 0 ) b = false;
372                else {
[93c2e0a]373                        fprintf( stderr, "invalid Boolean constant\n" );
[3ce0d440]374                        abort();
375                } // if
376                return is;
377        } // ?|?
378
379        istype & ?|?( istype & is, char & c ) {
380                fmt( is, "%c", &c );                                                    // must pass pointer through varg to fmt
381                return is;
382        } // ?|?
383
384        istype & ?|?( istype & is, signed char & sc ) {
385                fmt( is, "%hhd", &sc );
386                return is;
387        } // ?|?
388
389        istype & ?|?( istype & is, unsigned char & usc ) {
390                fmt( is, "%hhu", &usc );
391                return is;
392        } // ?|?
393
394        istype & ?|?( istype & is, short int & si ) {
395                fmt( is, "%hd", &si );
396                return is;
397        } // ?|?
398
399        istype & ?|?( istype & is, unsigned short int & usi ) {
400                fmt( is, "%hu", &usi );
401                return is;
402        } // ?|?
403
404        istype & ?|?( istype & is, int & i ) {
405                fmt( is, "%d", &i );
406                return is;
407        } // ?|?
408
409        istype & ?|?( istype & is, unsigned int & ui ) {
410                fmt( is, "%u", &ui );
411                return is;
412        } // ?|?
413
414        istype & ?|?( istype & is, long int & li ) {
415                fmt( is, "%ld", &li );
416                return is;
417        } // ?|?
418
419        istype & ?|?( istype & is, unsigned long int & ulli ) {
420                fmt( is, "%lu", &ulli );
421                return is;
422        } // ?|?
423
424        istype & ?|?( istype & is, long long int & lli ) {
425                fmt( is, "%lld", &lli );
426                return is;
427        } // ?|?
428
429        istype & ?|?( istype & is, unsigned long long int & ulli ) {
430                fmt( is, "%llu", &ulli );
431                return is;
432        } // ?|?
433
434
435        istype & ?|?( istype & is, float & f ) {
436                fmt( is, "%f", &f );
437                return is;
438        } // ?|?
439
440        istype & ?|?( istype & is, double & d ) {
441                fmt( is, "%lf", &d );
442                return is;
443        } // ?|?
444
445        istype & ?|?( istype & is, long double & ld ) {
446                fmt( is, "%Lf", &ld );
447                return is;
448        } // ?|?
449
450
451        istype & ?|?( istype & is, float _Complex & fc ) {
452                float re, im;
453                fmt( is, "%g%gi", &re, &im );
454                fc = re + im * _Complex_I;
455                return is;
456        } // ?|?
457
458        istype & ?|?( istype & is, double _Complex & dc ) {
459                double re, im;
460                fmt( is, "%lf%lfi", &re, &im );
461                dc = re + im * _Complex_I;
462                return is;
463        } // ?|?
464
465        istype & ?|?( istype & is, long double _Complex & ldc ) {
466                long double re, im;
467                fmt( is, "%Lf%Lfi", &re, &im );
468                ldc = re + im * _Complex_I;
469                return is;
470        } // ?|?
471
472
473        // manipulators
474        istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
475                return manip( is );
476        } // ?|?
477
[200fcb3]478        istype & nl( istype & is ) {
[3ce0d440]479                fmt( is, "%*[ \t\f\n\r\v]" );                                   // ignore whitespace
480                return is;
[200fcb3]481        } // nl
[3ce0d440]482} // distribution
[44574f2]483
[e24f13a]484_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
[90c3b1c]485forall( dtype istype | istream( istype ) )
[09687aa]486istype & ?|?( istype & is, _Istream_cstrUC cstr ) {
[829c907]487        fmt( is, "%s", cstr.s );
[90c3b1c]488        return is;
[53ba273]489} // cstr
[90c3b1c]490
[e24f13a]491_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
[90c3b1c]492forall( dtype istype | istream( istype ) )
[09687aa]493istype & ?|?( istype & is, _Istream_cstrC cstr ) {
[90c3b1c]494        char buf[16];
[53ba273]495        sprintf( buf, "%%%ds", cstr.size );
[829c907]496        fmt( is, buf, cstr.s );
[90c3b1c]497        return is;
[53ba273]498} // cstr
[90c3b1c]499
[86bd7c1f]500// Local Variables: //
501// tab-width: 4 //
[084520f]502// compile-command: "cfa iostream.cfa" //
[86bd7c1f]503// End: //
Note: See TracBrowser for help on using the repository browser.