source: libcfa/src/iostream.cfa @ 86a8be5

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

not wd with char type, and update test appropriately

  • Property mode set to 100644
File size: 27.9 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.cfa --
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 : Mon Jun 10 18:06:16 2019
13// Update Count     : 809
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 size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
23extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
24extern char *strcpy (char *__restrict __dest, const char *__restrict __src) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));
25extern void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));
26#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
27#include <math.h>                                                                               // isfinite
28#include <complex.h>                                                                    // creal, cimag
29} // extern "C"
30
31
32//*********************************** Ostream ***********************************
33
34
35forall( dtype ostype | ostream( ostype ) ) {
36        ostype & ?|?( ostype & os, zero_t ) {
37                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
38                fmt( os, "%d", 0n );
39                return os;
40        } // ?|?
41        void ?|?( ostype & os, zero_t z ) {
42                (ostype &)(os | z); nl( os );
43        } // ?|?
44
45        ostype & ?|?( ostype & os, one_t ) {
46                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
47                fmt( os, "%d", 1n );
48                return os;
49        } // ?|?
50        void ?|?( ostype & os, one_t o ) {
51                (ostype &)(os | o); nl( os );
52        } // ?|?
53
54        ostype & ?|?( ostype & os, bool b ) {
55                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
56                fmt( os, "%s", b ? "true" : "false" );
57                return os;
58        } // ?|?
59        void ?|?( ostype & os, bool b ) {
60                (ostype &)(os | b); nl( os );
61        } // ?|?
62
63        ostype & ?|?( ostype & os, char c ) {
64                fmt( os, "%c", c );
65                if ( c == '\n' ) setNL( os, true );
66                return sepOff( os );
67        } // ?|?
68        void ?|?( ostype & os, char c ) {
69                (ostype &)(os | c); nl( os );
70        } // ?|?
71
72        ostype & ?|?( ostype & os, signed char sc ) {
73                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
74                fmt( os, "%hhd", sc );
75                return os;
76        } // ?|?
77        void ?|?( ostype & os, signed char sc ) {
78                (ostype &)(os | sc); nl( os );
79        } // ?|?
80
81        ostype & ?|?( ostype & os, unsigned char usc ) {
82                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
83                fmt( os, "%hhu", usc );
84                return os;
85        } // ?|?
86        void ?|?( ostype & os, unsigned char usc ) {
87                (ostype &)(os | usc); nl( os );
88        } // ?|?
89
90        ostype & ?|?( ostype & os, short int si ) {
91                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
92                fmt( os, "%hd", si );
93                return os;
94        } // ?|?
95        void & ?|?( ostype & os, short int si ) {
96                (ostype &)(os | si); nl( os );
97        } // ?|?
98
99        ostype & ?|?( ostype & os, unsigned short int usi ) {
100                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
101                fmt( os, "%hu", usi );
102                return os;
103        } // ?|?
104        void & ?|?( ostype & os, unsigned short int usi ) {
105                (ostype &)(os | usi); nl( os );
106        } // ?|?
107
108        ostype & ?|?( ostype & os, int i ) {
109                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
110                fmt( os, "%d", i );
111                return os;
112        } // ?|?
113        void & ?|?( ostype & os, int i ) {
114                (ostype &)(os | i); nl( os );
115        } // ?|?
116
117        ostype & ?|?( ostype & os, unsigned int ui ) {
118                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
119                fmt( os, "%u", ui );
120                return os;
121        } // ?|?
122        void & ?|?( ostype & os, unsigned int ui ) {
123                (ostype &)(os | ui); nl( os );
124        } // ?|?
125
126        ostype & ?|?( ostype & os, long int li ) {
127                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
128                fmt( os, "%ld", li );
129                return os;
130        } // ?|?
131        void & ?|?( ostype & os, long int li ) {
132                (ostype &)(os | li); nl( os );
133        } // ?|?
134
135        ostype & ?|?( ostype & os, unsigned long int uli ) {
136                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
137                fmt( os, "%lu", uli );
138                return os;
139        } // ?|?
140        void & ?|?( ostype & os, unsigned long int uli ) {
141                (ostype &)(os | uli); nl( os );
142        } // ?|?
143
144        ostype & ?|?( ostype & os, long long int lli ) {
145                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
146                fmt( os, "%lld", lli );
147                return os;
148        } // ?|?
149        void & ?|?( ostype & os, long long int lli ) {
150                (ostype &)(os | lli); nl( os );
151        } // ?|?
152
153        ostype & ?|?( ostype & os, unsigned long long int ulli ) {
154                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
155                fmt( os, "%llu", ulli );
156                return os;
157        } // ?|?
158        void & ?|?( ostype & os, unsigned long long int ulli ) {
159                (ostype &)(os | ulli); nl( os );
160        } // ?|?
161
162        #define PrintWithDP( os, format, val, ... ) \
163                { \
164                        enum { size = 48 }; \
165                        char buf[size]; \
166                        int len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \
167                        fmt( os, "%s", buf ); \
168                        if ( isfinite( val ) ) {                                        /* if number, always print decimal point */ \
169                                for ( int i = 0;; i += 1 ) { \
170                                        if ( i == len ) { fmt( os, "." ); break; } \
171                                        if ( buf[i] == '.' ) break; \
172                                } /* for */ \
173                        } /* if */ \
174                }
175
176        ostype & ?|?( ostype & os, float f ) {
177                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
178                PrintWithDP( os, "%g", f );
179                return os;
180        } // ?|?
181        void & ?|?( ostype & os, float f ) {
182                (ostype &)(os | f); nl( os );
183        } // ?|?
184
185        ostype & ?|?( ostype & os, double d ) {
186                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
187                PrintWithDP( os, "%.*lg", d, DBL_DIG );
188                return os;
189        } // ?|?
190        void & ?|?( ostype & os, double d ) {
191                (ostype &)(os | d); nl( os );
192        } // ?|?
193
194        ostype & ?|?( ostype & os, long double ld ) {
195                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
196                PrintWithDP( os, "%.*Lg", ld, LDBL_DIG );
197                return os;
198        } // ?|?
199        void & ?|?( ostype & os, long double ld ) {
200                (ostype &)(os | ld); nl( os );
201        } // ?|?
202
203        ostype & ?|?( ostype & os, float _Complex fc ) {
204                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
205//              os | crealf( fc ) | nonl;
206                PrintWithDP( os, "%g", crealf( fc ) );
207                PrintWithDP( os, "%+g", cimagf( fc ) );
208                fmt( os, "i" );
209                return os;
210        } // ?|?
211        void & ?|?( ostype & os, float _Complex fc ) {
212                (ostype &)(os | fc); nl( os );
213        } // ?|?
214
215        ostype & ?|?( ostype & os, double _Complex dc ) {
216                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
217//              os | creal( dc ) | nonl;
218                PrintWithDP( os, "%.*lg", creal( dc ), DBL_DIG );
219                PrintWithDP( os, "%+.*lg", cimag( dc ), DBL_DIG );
220                fmt( os, "i" );
221                return os;
222        } // ?|?
223        void & ?|?( ostype & os, double _Complex dc ) {
224                (ostype &)(os | dc); nl( os );
225        } // ?|?
226
227        ostype & ?|?( ostype & os, long double _Complex ldc ) {
228                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
229//              os | creall( ldc ) || nonl;
230                PrintWithDP( os, "%.*Lg", creall( ldc ), LDBL_DIG );
231                PrintWithDP( os, "%+.*Lg", cimagl( ldc ), 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// writes the range [begin, end) to the given stream
398forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
399        void write( iterator_type begin, iterator_type end, ostype & os ) {
400                void print( elt_type i ) { os | i; }
401                for_each( begin, end, print );
402        } // ?|?
403
404        void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
405                void print( elt_type i ) { os | i; }
406                for_each_reverse( begin, end, print );
407        } // ?|?
408} // distribution
409
410//*********************************** Manipulators ***********************************
411
412//*********************************** Integral ***********************************
413
414static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
415static const char * longbin[]  = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
416
417// Default prefix for non-decimal prints is 0b, 0, 0x.
418#define IntegralFMTImpl( T, CODE, IFMTNP, IFMTP ) \
419forall( dtype ostype | ostream( ostype ) ) { \
420        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
421                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \
422\
423                if ( f.base == 'b' || f.base == 'B' ) {                 /* bespoke binary format */ \
424                        int bits;                                                                                                       \
425                        if ( f.val == (T){0} ) bits = 1;                        /* force at least one bit to print */ \
426                        else bits = sizeof(long long int) * 8 - __builtin_clzll( f.val ); /* position of most significant bit */ \
427                        bits = bits > sizeof(f.val) * 8 ? sizeof(f.val) * 8 : bits; \
428                        int spaces = f.wd - bits;                                       /* can be negative */ \
429                        if ( ! f.flags.nobsdp ) { spaces -= 2; }        /* base prefix takes space */ \
430                        /* printf( "%d %d\n", bits, spaces ); */ \
431                        if ( ! f.flags.left ) {                                         /* right justified ? */ \
432                                /* Note, base prefix then zero padding or spacing then prefix. */ \
433                                if ( f.flags.pad0 || f.flags.pc ) { \
434                                        if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
435                                        if ( f.flags.pc ) spaces = f.pc - bits; \
436                                        if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
437                                } else { \
438                                        if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
439                                        if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
440                                } /* if */ \
441                        } else if ( ! f.flags.nobsdp ) { \
442                                fmt( os, "0%c", f.base ); \
443                        } /* if */ \
444                        int shift = (bits - 1) / 4 * 4; /* floor( bits - 1, 4 ) */ \
445                        typeof( f.val ) temp = f.val; \
446                        fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \
447                        for () { \
448                                shift -= 4; \
449                          if ( shift < 0 ) break; \
450                                temp = f.val; \
451                                fmt( os, "%s", longbin[(temp >> shift) & 0xf] ); \
452                        } /* for */ \
453                        if ( f.flags.left && spaces > 0 ) fmt( os, "%*s", spaces, " " ); \
454                        return os; \
455                } /* if  */ \
456\
457                char fmtstr[sizeof(IFMTP)];                                             /* sizeof includes '\0' */ \
458                if ( ! f.flags.pc ) memcpy( &fmtstr, IFMTNP, sizeof(IFMTNP) ); \
459                else memcpy( &fmtstr, IFMTP, sizeof(IFMTP) ); \
460                int star = 4;                                                                   /* position before first '*' */ \
461\
462                /* Insert flags into spaces before '*', from right to left. */ \
463                if ( ! f.flags.nobsdp ) { fmtstr[star] = '#'; star -= 1; } \
464                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
465                if ( f.flags.sign && f.base == CODE ) { fmtstr[star] = '+'; star -= 1; } \
466                if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \
467                fmtstr[star] = '%'; \
468\
469                if ( ! f.flags.pc ) {                                                   /* no precision */ \
470                        /* printf( "%s\n", &fmtstr[star] ); */ \
471                        fmtstr[sizeof(IFMTNP)-2] = f.base;                      /* sizeof includes '\0' */ \
472                        fmt( os, &fmtstr[star], f.wd, f.val ); \
473                } else {                                                                                /* precision */ \
474                        fmtstr[sizeof(IFMTP)-2] = f.base;                       /* sizeof includes '\0' */ \
475                        /* printf( "%s\n", &fmtstr[star] ); */ \
476                        fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \
477                } /* if */ \
478                return os; \
479        } /* ?|? */ \
480        void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); nl( os ); } \
481} // distribution
482
483IntegralFMTImpl( signed char, 'd', "%    *hh ", "%    *.*hh " )
484IntegralFMTImpl( unsigned char, 'u', "%    *hh ", "%    *.*hh " )
485IntegralFMTImpl( signed short int, 'd', "%    *h ", "%    *.*h " )
486IntegralFMTImpl( unsigned short int, 'u', "%    *h ", "%    *.*h " )
487IntegralFMTImpl( signed int, 'd', "%    * ", "%    *.* " )
488IntegralFMTImpl( unsigned int, 'u', "%    * ", "%    *.* " )
489IntegralFMTImpl( signed long int, 'd', "%    *l ", "%    *.*l " )
490IntegralFMTImpl( unsigned long int, 'u', "%    *l ", "%    *.*l " )
491IntegralFMTImpl( signed long long int, 'd', "%    *ll ", "%    *.*ll " )
492IntegralFMTImpl( unsigned long long int, 'u', "%    *ll ", "%    *.*ll " )
493
494//*********************************** Floating Point ***********************************
495
496#define PrintWithDP2( os, format, val, ... ) \
497        { \
498                enum { size = 48 }; \
499                char buf[size]; \
500                int bufbeg = 0, i, len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \
501                if ( isfinite( val ) && (f.base != 'g' || f.pc != 0) ) { /* if number, print decimal point */ \
502                        for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E'; i += 1 ); /* decimal point or scientific ? */ \
503                        if ( i == len && ! f.flags.nobsdp ) { \
504                                if ( ! f.flags.left ) { \
505                                        buf[i] = '.'; buf[i + 1] = '\0'; \
506                                        if ( buf[0] == ' ' ) bufbeg = 1; /* decimal point within width */ \
507                                } else { \
508                                        for ( i = 0; i < len && buf[i] != ' '; i += 1 ); /* trailing blank ? */ \
509                                        buf[i] = '.'; \
510                                        if ( i == len ) buf[i + 1] = '\0'; \
511                                } /* if */ \
512                        } /* if */ \
513                } /* if */ \
514                fmt( os, "%s", &buf[bufbeg] ); \
515        }
516
517#define FloatingPointFMTImpl( T, DFMTNP, DFMTP ) \
518forall( dtype ostype | ostream( ostype ) ) { \
519        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
520                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \
521                char fmtstr[sizeof(DFMTP)];                                             /* sizeof includes '\0' */ \
522                if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \
523                else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \
524                int star = 4;                                                                   /* position before first '*' */ \
525\
526                /* Insert flags into spaces before '*', from right to left. */ \
527                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
528                if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \
529                if ( f.flags.pad0 ) { fmtstr[star] = '0'; star -= 1; } \
530                fmtstr[star] = '%'; \
531\
532                if ( ! f.flags.pc ) {                                                   /* no precision */ \
533                        fmtstr[sizeof(DFMTNP)-2] = f.base;                      /* sizeof includes '\0' */ \
534                        /* printf( "%g %d %s\n", f.val, f.wd, &fmtstr[star]); */ \
535                        PrintWithDP2( os, &fmtstr[star], f.val, f.wd ) \
536                } else {                                                                                /* precision */ \
537                        fmtstr[sizeof(DFMTP)-2] = f.base;                       /* sizeof includes '\0' */ \
538                        /* printf( "%g %d %d %s\n", f.val, f.wd, f.pc, &fmtstr[star] ); */ \
539                        PrintWithDP2( os, &fmtstr[star], f.val, f.wd, f.pc ) \
540                } /* if */ \
541                return os; \
542        } /* ?|? */ \
543        void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); nl( os ); } \
544} // distribution
545
546FloatingPointFMTImpl( double, "%    * ", "%    *.* " )
547FloatingPointFMTImpl( long double, "%    *L ", "%    *.*L " )
548
549//*********************************** Character ***********************************
550
551forall( dtype ostype | ostream( ostype ) ) {
552        ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) {
553                if ( f.base != 'c' ) {                                                          // bespoke binary/octal/hex format
554                        _Ostream_Manip(unsigned char) fmtuc @= { f.val, f.wd, f.pc, f.base, {'\0'} };
555                        fmtuc.flags.pc = f.flags.pc;
556                        fmtuc.flags.nobsdp = f.flags.nobsdp;
557//                      os | fmtuc | nonl;
558                        (ostype &)(os | fmtuc);
559                        return os;
560                } // if
561
562                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
563
564                #define CFMTNP "% * "
565                char fmtstr[sizeof(CFMTNP)];                                            // sizeof includes '\0'
566                memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) );
567                int star = 1;                                                                           // position before first '*'
568
569                // Insert flags into spaces before '*', from right to left.
570                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
571                fmtstr[star] = '%';
572
573                fmtstr[sizeof(CFMTNP)-2] = f.base;                                      // sizeof includes '\0'
574                // printf( "%d %s\n", f.wd, &fmtstr[star] );
575                fmt( os, &fmtstr[star], f.wd, f.val );
576                return os;
577        } // ?|?
578        void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); nl( os ); }
579} // distribution
580
581//*********************************** C String ***********************************
582
583forall( dtype ostype | ostream( ostype ) ) {
584        ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) {
585                if ( ! f.val ) return os;                                               // null pointer ?
586
587                if ( f.base != 's' ) {                                                  // bespoke binary/octal/hex format
588                        _Ostream_Manip(unsigned char) fmtuc @= { 0, f.wd, f.pc, f.base, {'\0'} };
589                        fmtuc.flags.pc = f.flags.pc;
590                        fmtuc.flags.nobsdp = f.flags.nobsdp;
591                        for ( unsigned int i = 0; f.val[i] != '\0'; i += 1 ) {
592                                fmtuc.val = f.val[i];
593//                              os | fmtuc | nonl;
594                                (ostype &)(os | fmtuc);
595                        } // for
596                        return os;
597                } // if
598
599                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
600
601                #define SFMTNP "% * "
602                #define SFMTP "% *.* "
603                char fmtstr[sizeof(SFMTP)];                                             // sizeof includes '\0'
604                if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) );
605                else memcpy( &fmtstr, SFMTP, sizeof(SFMTP) );
606                int star = 1;                                                                   // position before first '*'
607
608                // Insert flags into spaces before '*', from right to left.
609                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
610                fmtstr[star] = '%';
611
612                if ( ! f.flags.pc ) {                                                   // no precision
613                        // printf( "%d %s\n", f.wd, &fmtstr[star] );
614                        fmtstr[sizeof(SFMTNP)-2] = f.base;                      // sizeof includes '\0'
615                        fmt( os, &fmtstr[star], f.wd, f.val );
616                } else {                                                                                // precision
617                        fmtstr[sizeof(SFMTP)-2] = f.base;                       // sizeof includes '\0'
618                        // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] );
619                        fmt( os, &fmtstr[star], f.wd, f.pc, f.val );
620                } // if
621                return os;
622        } // ?|?
623        void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); nl( os ); }
624} // distribution
625
626
627//*********************************** Istream ***********************************
628
629
630forall( dtype istype | istream( istype ) ) {
631        istype & ?|?( istype & is, bool & b ) {
632                char val[6];
633                fmt( is, "%5s", val );
634                if ( strcmp( val, "true" ) == 0 ) b = true;
635                else if ( strcmp( val, "false" ) == 0 ) b = false;
636                else {
637                        fprintf( stderr, "invalid Boolean constant\n" );
638                        abort();
639                } // if
640                return is;
641        } // ?|?
642
643        istype & ?|?( istype & is, char & c ) {
644                char temp;
645                for () {
646                        fmt( is, "%c", &temp );                                                 // must pass pointer through varg to fmt
647                        // do not overwrite parameter with newline unless appropriate
648                        if ( temp != '\n' || getANL( is ) ) { c = temp; break; }
649                        if ( eof( is ) ) break;
650                } // for
651                return is;
652        } // ?|?
653
654        istype & ?|?( istype & is, signed char & sc ) {
655                fmt( is, "%hhi", &sc );
656                return is;
657        } // ?|?
658
659        istype & ?|?( istype & is, unsigned char & usc ) {
660                fmt( is, "%hhi", &usc );
661                return is;
662        } // ?|?
663
664        istype & ?|?( istype & is, short int & si ) {
665                fmt( is, "%hi", &si );
666                return is;
667        } // ?|?
668
669        istype & ?|?( istype & is, unsigned short int & usi ) {
670                fmt( is, "%hi", &usi );
671                return is;
672        } // ?|?
673
674        istype & ?|?( istype & is, int & i ) {
675                fmt( is, "%i", &i );
676                return is;
677        } // ?|?
678
679        istype & ?|?( istype & is, unsigned int & ui ) {
680                fmt( is, "%i", &ui );
681                return is;
682        } // ?|?
683
684        istype & ?|?( istype & is, long int & li ) {
685                fmt( is, "%li", &li );
686                return is;
687        } // ?|?
688
689        istype & ?|?( istype & is, unsigned long int & ulli ) {
690                fmt( is, "%li", &ulli );
691                return is;
692        } // ?|?
693
694        istype & ?|?( istype & is, long long int & lli ) {
695                fmt( is, "%lli", &lli );
696                return is;
697        } // ?|?
698
699        istype & ?|?( istype & is, unsigned long long int & ulli ) {
700                fmt( is, "%lli", &ulli );
701                return is;
702        } // ?|?
703
704
705        istype & ?|?( istype & is, float & f ) {
706                fmt( is, "%f", &f );
707                return is;
708        } // ?|?
709
710        istype & ?|?( istype & is, double & d ) {
711                fmt( is, "%lf", &d );
712                return is;
713        } // ?|?
714
715        istype & ?|?( istype & is, long double & ld ) {
716                fmt( is, "%Lf", &ld );
717                return is;
718        } // ?|?
719
720
721        istype & ?|?( istype & is, float _Complex & fc ) {
722                float re, im;
723                fmt( is, "%f%fi", &re, &im );
724                fc = re + im * _Complex_I;
725                return is;
726        } // ?|?
727
728        istype & ?|?( istype & is, double _Complex & dc ) {
729                double re, im;
730                fmt( is, "%lf%lfi", &re, &im );
731                dc = re + im * _Complex_I;
732                return is;
733        } // ?|?
734
735        istype & ?|?( istype & is, long double _Complex & ldc ) {
736                long double re, im;
737                fmt( is, "%Lf%Lfi", &re, &im );
738                ldc = re + im * _Complex_I;
739                return is;
740        } // ?|?
741
742        // manipulators
743        istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
744                return manip( is );
745        } // ?|?
746
747        istype & nl( istype & is ) {
748                fmt( is, "%*[^\n]" );                                                   // ignore characters to newline
749                return is;
750        } // nl
751
752        istype & nlOn( istype & is ) {
753                nlOn( is );                                                                             // call void returning
754                return is;
755        } // nlOn
756
757        istype & nlOff( istype & is ) {
758                nlOff( is );                                                                    // call void returning
759                return is;
760        } // nlOff
761} // distribution
762
763//*********************************** Manipulators ***********************************
764
765forall( dtype istype | istream( istype ) )
766istype & ?|?( istype & is, _Istream_Cstr f ) {
767        // skip xxx
768        if ( ! f.s ) {
769                // printf( "skip %s\n", f.scanset );
770                fmt( is, f.scanset, "" );                                               // no input arguments
771                return is;
772        } // if
773        size_t len = 0;
774        if ( f.scanset ) len = strlen( f.scanset );
775        char fmtstr[len + 16];
776        int start = 1;
777        fmtstr[0] = '%';
778        if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
779        if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
780        // cstr %s, %*s, %ws, %*ws
781        if ( ! f.scanset ) {
782                fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
783                // printf( "cstr %s\n", fmtstr );
784                fmt( is, fmtstr, f.s );
785                return is;
786        } // if
787        // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
788        // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
789        fmtstr[start] = '['; start += 1;
790        if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
791        strcpy( &fmtstr[start], f.scanset );                            // copy includes '\0'
792        len += start;
793        fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
794        // printf( "incl/excl %s\n", fmtstr );
795        fmt( is, fmtstr, f.s );
796        return is;
797} // ?|?
798
799forall( dtype istype | istream( istype ) )
800istype & ?|?( istype & is, _Istream_Char f ) {
801        fmt( is, "%*c" );                                                                       // argument variable unused
802        return is;
803} // ?|?
804
805#define InputFMTImpl( T, CODE ) \
806forall( dtype istype | istream( istype ) ) \
807istype & ?|?( istype & is, _Istream_Manip(T) f ) { \
808        enum { size = 16 }; \
809        char fmtstr[size]; \
810        if ( f.wd == -1 ) { \
811                snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \
812        } else { \
813                snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \
814        } /* if */ \
815        /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \
816        fmt( is, fmtstr, &f.val ); \
817        return is; \
818} // ?|?
819
820InputFMTImpl( signed char, "hhi" )
821InputFMTImpl( unsigned char, "hhi" )
822InputFMTImpl( signed short int, "hi" )
823InputFMTImpl( unsigned short int, "hi" )
824InputFMTImpl( signed int, "i" )
825InputFMTImpl( unsigned int, "i" )
826InputFMTImpl( signed long int, "li" )
827InputFMTImpl( unsigned long int, "li" )
828InputFMTImpl( signed long long int, "lli" )
829InputFMTImpl( unsigned long long int, "lli" )
830
831InputFMTImpl( float, "f" )
832InputFMTImpl( double, "lf" )
833InputFMTImpl( long double, "Lf" )
834
835forall( dtype istype | istream( istype ) )
836istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) {
837        float re, im;
838        _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore };
839        is | fmtuc;
840        &fmtuc.val = &im;
841        is | fmtuc;
842        if ( ! fc.ignore ) fc.val = re + im * _Complex_I;       // re/im are uninitialized for ignore
843        return is;
844} // ?|?
845
846forall( dtype istype | istream( istype ) )
847istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
848        double re, im;
849        _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore };
850        is | fmtuc;
851        &fmtuc.val = &im;
852        is | fmtuc;
853        if ( ! dc.ignore ) dc.val = re + im * _Complex_I;       // re/im are uninitialized for ignore
854        return is;
855} // ?|?
856
857forall( dtype istype | istream( istype ) )
858istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
859        long double re, im;
860        _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore };
861        is | fmtuc;
862        &fmtuc.val = &im;
863        is | fmtuc;
864        if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I;     // re/im are uninitialized for ignore
865        return is;
866} // ?|?
867
868// Local Variables: //
869// tab-width: 4 //
870// compile-command: "cfa iostream.cfa" //
871// End: //
Note: See TracBrowser for help on using the repository browser.