source: libcfa/src/iostream.cfa @ 0161ddf

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0161ddf was e63326b, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

clean up always printing decimal point for floating-point numbers

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