source: libcfa/src/iostream.cfa @ 200fcb3

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

add auto newline to sout, change endl to nl

  • Property mode set to 100644
File size: 14.4 KB
Line 
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//
7// iostream.c --
8//
9// Author           : Peter A. Buhr
10// Created On       : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Dec 11 22:02:03 2018
13// Update Count     : 546
14//
15
16#include "iostream.hfa"
17
18extern "C" {
19#include <stdio.h>
20#include <stdbool.h>                                                                    // true/false
21//#include <string.h>                                                                   // strlen, strcmp
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)));
24#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
25#include <complex.h>                                                                    // creal, cimag
26}
27
28forall( dtype ostype | ostream( ostype ) ) {
29        ostype & ?|?( ostype & os, bool b ) {
30                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
31                fmt( os, "%s", b ? "true" : "false" );
32                return os;
33        } // ?|?
34        void ?|?( ostype & os, bool b ) {
35                (ostype)(os | b); if ( getANL( os ) ) nl( os );
36        } // ?|?
37
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 ) {
44                (ostype)(os | c); if ( getANL( os ) ) nl( os );
45        } // ?|?
46
47        ostype & ?|?( ostype & os, signed char sc ) {
48                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
49                fmt( os, "%hhd", sc );
50                return os;
51        } // ?|?
52        void ?|?( ostype & os, signed char sc ) {
53                (ostype)(os | sc); if ( getANL( os ) ) nl( os );
54        } // ?|?
55
56        ostype & ?|?( ostype & os, unsigned char usc ) {
57                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
58                fmt( os, "%hhu", usc );
59                return os;
60        } // ?|?
61        void ?|?( ostype & os, unsigned char usc ) {
62                (ostype)(os | usc); if ( getANL( os ) ) nl( os );
63        } // ?|?
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        } // ?|?
70        void & ?|?( ostype & os, short int si ) {
71                (ostype)(os | si); if ( getANL( os ) ) nl( os );
72        } // ?|?
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        } // ?|?
79        void & ?|?( ostype & os, unsigned short int usi ) {
80                (ostype)(os | usi); if ( getANL( os ) ) nl( os );
81        } // ?|?
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        } // ?|?
88        void & ?|?( ostype & os, int i ) {
89                (ostype)(os | i); if ( getANL( os ) ) nl( os );
90        } // ?|?
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        } // ?|?
97        void & ?|?( ostype & os, unsigned int ui ) {
98                (ostype)(os | ui); if ( getANL( os ) ) nl( os );
99        } // ?|?
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        } // ?|?
106        void & ?|?( ostype & os, long int li ) {
107                (ostype)(os | li); if ( getANL( os ) ) nl( os );
108        } // ?|?
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        } // ?|?
115        void & ?|?( ostype & os, unsigned long int uli ) {
116                (ostype)(os | uli); if ( getANL( os ) ) nl( os );
117        } // ?|?
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        } // ?|?
124        void & ?|?( ostype & os, long long int lli ) {
125                (ostype)(os | lli); if ( getANL( os ) ) nl( os );
126        } // ?|?
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        } // ?|?
133        void & ?|?( ostype & os, unsigned long long int ulli ) {
134                (ostype)(os | ulli); if ( getANL( os ) ) nl( os );
135        } // ?|?
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        } // ?|?
142        void & ?|?( ostype & os, float f ) {
143                (ostype)(os | f); if ( getANL( os ) ) nl( os );
144        } // ?|?
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        } // ?|?
151        void & ?|?( ostype & os, double d ) {
152                (ostype)(os | d); if ( getANL( os ) ) nl( os );
153        } // ?|?
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        } // ?|?
160        void & ?|?( ostype & os, long double ld ) {
161                (ostype)(os | ld); if ( getANL( os ) ) nl( os );
162        } // ?|?
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        } // ?|?
169        void & ?|?( ostype & os, float _Complex fc ) {
170                (ostype)(os | fc); if ( getANL( os ) ) nl( os );
171        } // ?|?
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        } // ?|?
178        void & ?|?( ostype & os, double _Complex dc ) {
179                (ostype)(os | dc); if ( getANL( os ) ) nl( os );
180        } // ?|?
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        } // ?|?
187        void & ?|?( ostype & os, long double _Complex ldc ) {
188                (ostype)(os | ldc); if ( getANL( os ) ) nl( os );
189        } // ?|?
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,
196                        ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
197                        [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
198                        // closing delimiters, no space before
199                        [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
200                        ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
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        } // ?|?
229        void ?|?( ostype & os, const char * str ) {
230                (ostype)(os | str); if ( getANL( os ) ) nl( os );
231        } // ?|?
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//      } // ?|?
238
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 )
246
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        } // ?|?
258        void ?|?( ostype & os, const void * p ) {
259                (ostype)(os | p); if ( getANL( os ) ) nl( os );
260        } // ?|?
261
262        // manipulators
263        ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
264                (ostype)(manip( os ));
265                setNonl( os, false );                                                   // ignore nonl in middle
266                return os;
267        } // ?|?
268        void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
269                (ostype)(manip( os ));
270                if ( getANL( os ) && ! getNonl( os ) ) nl( os ); // ignore nl if nonl at end
271                setNonl( os, false );
272        } // ?|?
273
274        ostype & sep( ostype & os ) {
275                return (ostype)(os | sepGet( os ));
276        } // sep
277
278        ostype & sepTuple( ostype & os ) {
279                return os | sepGetTuple( os );
280        } // sepTuple
281
282        ostype & nl( ostype & os ) {
283                (ostype)(os | '\n');
284                setNL( os, true );
285                flush( os );
286                return sepOff( os );                                                    // prepare for next line
287        } // nl
288
289        ostype & nonl( ostype & os ) {
290                setNonl( os, true );                                                    // indicate nonl manipulator
291                return os;
292        } // nonl
293
294        ostype & sepOn( ostype & os ) {
295                sepOn( os );                                                                    // call void returning
296                return os;
297        } // sepOn
298
299        ostype & sepOff( ostype & os ) {
300                sepOff( os );                                                                   // call void returning
301                return os;
302        } // sepOff
303
304        ostype & sepEnable( ostype & os ) {
305                sepEnable( os );                                                                // call void returning
306                return os;
307        } // sepEnable
308
309        ostype & sepDisable( ostype & os ) {
310                sepDisable( os );                                                               // call void returning
311                return os;
312        } // sepDisable
313
314        ostype & nlOn( ostype & os ) {
315                nlOn( os );                                                                             // call void returning
316                return os;
317        } // nlOn
318
319        ostype & nlOff( ostype & os ) {
320                nlOff( os );                                                                    // call void returning
321                return os;
322        } // nlOff
323} // distribution
324
325// tuples
326forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
327        ostype & ?|?( ostype & os, T arg, Params rest ) {
328                (ostype)(os | arg);                                                             // print first argument
329                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
330                (ostype)(os | rest);                                                    // print remaining arguments
331                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
332                return os;
333        } // ?|?
334        void ?|?( ostype & os, T arg, Params rest ) {
335//              (ostype)(?|?( os, arg, rest )); if ( getANL( os ) ) nl( os );
336                (ostype)(os | arg);                                                             // print first argument
337                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
338                (ostype)(os | rest);                                                    // print remaining arguments
339                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
340                if ( getANL( os ) ) nl( os );
341        } // ?|?
342} // distribution
343
344//---------------------------------------
345
346// writes the range [begin, end) to the given stream
347forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
348        void write( iterator_type begin, iterator_type end, ostype & os ) {
349                void print( elt_type i ) { os | i; }
350                for_each( begin, end, print );
351        } // ?|?
352
353        void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
354                void print( elt_type i ) { os | i; }
355                for_each_reverse( begin, end, print );
356        } // ?|?
357} // distribution
358
359//---------------------------------------
360
361forall( dtype istype | istream( istype ) ) {
362        istype & ?|?( istype & is, bool & b ) {
363                char val[6];
364                fmt( is, "%5s", val );
365                if ( strcmp( val, "true" ) == 0 ) b = true;
366                else if ( strcmp( val, "false" ) == 0 ) b = false;
367                else {
368                        fprintf( stderr, "invalid Boolean constant\n" );
369                        abort();
370                } // if
371                return is;
372        } // ?|?
373
374        istype & ?|?( istype & is, char & c ) {
375                fmt( is, "%c", &c );                                                    // must pass pointer through varg to fmt
376                return is;
377        } // ?|?
378
379        istype & ?|?( istype & is, signed char & sc ) {
380                fmt( is, "%hhd", &sc );
381                return is;
382        } // ?|?
383
384        istype & ?|?( istype & is, unsigned char & usc ) {
385                fmt( is, "%hhu", &usc );
386                return is;
387        } // ?|?
388
389        istype & ?|?( istype & is, short int & si ) {
390                fmt( is, "%hd", &si );
391                return is;
392        } // ?|?
393
394        istype & ?|?( istype & is, unsigned short int & usi ) {
395                fmt( is, "%hu", &usi );
396                return is;
397        } // ?|?
398
399        istype & ?|?( istype & is, int & i ) {
400                fmt( is, "%d", &i );
401                return is;
402        } // ?|?
403
404        istype & ?|?( istype & is, unsigned int & ui ) {
405                fmt( is, "%u", &ui );
406                return is;
407        } // ?|?
408
409        istype & ?|?( istype & is, long int & li ) {
410                fmt( is, "%ld", &li );
411                return is;
412        } // ?|?
413
414        istype & ?|?( istype & is, unsigned long int & ulli ) {
415                fmt( is, "%lu", &ulli );
416                return is;
417        } // ?|?
418
419        istype & ?|?( istype & is, long long int & lli ) {
420                fmt( is, "%lld", &lli );
421                return is;
422        } // ?|?
423
424        istype & ?|?( istype & is, unsigned long long int & ulli ) {
425                fmt( is, "%llu", &ulli );
426                return is;
427        } // ?|?
428
429
430        istype & ?|?( istype & is, float & f ) {
431                fmt( is, "%f", &f );
432                return is;
433        } // ?|?
434
435        istype & ?|?( istype & is, double & d ) {
436                fmt( is, "%lf", &d );
437                return is;
438        } // ?|?
439
440        istype & ?|?( istype & is, long double & ld ) {
441                fmt( is, "%Lf", &ld );
442                return is;
443        } // ?|?
444
445
446        istype & ?|?( istype & is, float _Complex & fc ) {
447                float re, im;
448                fmt( is, "%g%gi", &re, &im );
449                fc = re + im * _Complex_I;
450                return is;
451        } // ?|?
452
453        istype & ?|?( istype & is, double _Complex & dc ) {
454                double re, im;
455                fmt( is, "%lf%lfi", &re, &im );
456                dc = re + im * _Complex_I;
457                return is;
458        } // ?|?
459
460        istype & ?|?( istype & is, long double _Complex & ldc ) {
461                long double re, im;
462                fmt( is, "%Lf%Lfi", &re, &im );
463                ldc = re + im * _Complex_I;
464                return is;
465        } // ?|?
466
467
468        // manipulators
469        istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
470                return manip( is );
471        } // ?|?
472
473        istype & nl( istype & is ) {
474                fmt( is, "%*[ \t\f\n\r\v]" );                                   // ignore whitespace
475                return is;
476        } // nl
477} // distribution
478
479_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
480forall( dtype istype | istream( istype ) )
481istype & ?|?( istype & is, _Istream_cstrUC cstr ) {
482        fmt( is, "%s", cstr.s );
483        return is;
484} // cstr
485
486_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
487forall( dtype istype | istream( istype ) )
488istype & ?|?( istype & is, _Istream_cstrC cstr ) {
489        char buf[16];
490        sprintf( buf, "%%%ds", cstr.size );
491        fmt( is, buf, cstr.s );
492        return is;
493} // cstr
494
495// Local Variables: //
496// tab-width: 4 //
497// compile-command: "cfa iostream.cfa" //
498// End: //
Note: See TracBrowser for help on using the repository browser.