source: libcfa/src/iostream.cfa @ 5ea5b28

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

update iostream

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