source: libcfa/src/iostream.cfa @ d1a9ff5

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

change private names to start with $, change NULL to 0p

  • Property mode set to 100644
File size: 28.2 KB
RevLine 
[86bd7c1f]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[b117e0c]7// iostream.cfa --
[86bd7c1f]8//
[90c3b1c]9// Author           : Peter A. Buhr
[86bd7c1f]10// Created On       : Wed May 27 17:56:53 2015
[e24f13a]11// Last Modified By : Peter A. Buhr
[d1a9ff5]12// Last Modified On : Fri Feb  7 18:48:38 2020
13// Update Count     : 825
[86bd7c1f]14//
[51b7345]15
[58b6d1b]16#include "iostream.hfa"
[d3b7937]17
[51b7345]18extern "C" {
19#include <stdio.h>
[53a6c2a]20#include <stdbool.h>                                                                    // true/false
[200fcb3]21//#include <string.h>                                                                   // strlen, strcmp
[44574f2]22extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
[3c573e9]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)));
[52f85e0]26#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
[b2ac656]27#include <math.h>                                                                               // isfinite
[d3b7937]28#include <complex.h>                                                                    // creal, cimag
[3c573e9]29} // extern "C"
[51b7345]30
[61c7239]31
[65240bb]32//*********************************** ostream ***********************************
[61c7239]33
34
[3ce0d440]35forall( dtype ostype | ostream( ostype ) ) {
[17a1b21]36        ostype & ?|?( ostype & os, zero_t ) {
[d1a9ff5]37                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[17a1b21]38                fmt( os, "%d", 0n );
39                return os;
40        } // ?|?
41        void ?|?( ostype & os, zero_t z ) {
[65240bb]42                (ostype &)(os | z); ends( os );
[17a1b21]43        } // ?|?
44
45        ostype & ?|?( ostype & os, one_t ) {
[d1a9ff5]46                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[17a1b21]47                fmt( os, "%d", 1n );
48                return os;
49        } // ?|?
50        void ?|?( ostype & os, one_t o ) {
[65240bb]51                (ostype &)(os | o); ends( os );
[17a1b21]52        } // ?|?
53
[93c2e0a]54        ostype & ?|?( ostype & os, bool b ) {
[d1a9ff5]55                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]56                fmt( os, "%s", b ? "true" : "false" );
57                return os;
58        } // ?|?
[200fcb3]59        void ?|?( ostype & os, bool b ) {
[65240bb]60                (ostype &)(os | b); ends( os );
[200fcb3]61        } // ?|?
[51b7345]62
[200fcb3]63        ostype & ?|?( ostype & os, char c ) {
64                fmt( os, "%c", c );
[d1a9ff5]65                if ( c == '\n' ) $setNL( os, true );
[200fcb3]66                return sepOff( os );
67        } // ?|?
68        void ?|?( ostype & os, char c ) {
[65240bb]69                (ostype &)(os | c); ends( os );
[3ce0d440]70        } // ?|?
71
[200fcb3]72        ostype & ?|?( ostype & os, signed char sc ) {
[d1a9ff5]73                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[200fcb3]74                fmt( os, "%hhd", sc );
[3ce0d440]75                return os;
76        } // ?|?
[200fcb3]77        void ?|?( ostype & os, signed char sc ) {
[65240bb]78                (ostype &)(os | sc); ends( os );
[200fcb3]79        } // ?|?
[3ce0d440]80
[200fcb3]81        ostype & ?|?( ostype & os, unsigned char usc ) {
[d1a9ff5]82                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[200fcb3]83                fmt( os, "%hhu", usc );
[3ce0d440]84                return os;
85        } // ?|?
[200fcb3]86        void ?|?( ostype & os, unsigned char usc ) {
[65240bb]87                (ostype &)(os | usc); ends( os );
[200fcb3]88        } // ?|?
[3ce0d440]89
90        ostype & ?|?( ostype & os, short int si ) {
[d1a9ff5]91                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]92                fmt( os, "%hd", si );
93                return os;
94        } // ?|?
[200fcb3]95        void & ?|?( ostype & os, short int si ) {
[65240bb]96                (ostype &)(os | si); ends( os );
[200fcb3]97        } // ?|?
[3ce0d440]98
99        ostype & ?|?( ostype & os, unsigned short int usi ) {
[d1a9ff5]100                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]101                fmt( os, "%hu", usi );
102                return os;
103        } // ?|?
[200fcb3]104        void & ?|?( ostype & os, unsigned short int usi ) {
[65240bb]105                (ostype &)(os | usi); ends( os );
[200fcb3]106        } // ?|?
[3ce0d440]107
108        ostype & ?|?( ostype & os, int i ) {
[d1a9ff5]109                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]110                fmt( os, "%d", i );
111                return os;
112        } // ?|?
[200fcb3]113        void & ?|?( ostype & os, int i ) {
[65240bb]114                (ostype &)(os | i); ends( os );
[200fcb3]115        } // ?|?
[3ce0d440]116
117        ostype & ?|?( ostype & os, unsigned int ui ) {
[d1a9ff5]118                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]119                fmt( os, "%u", ui );
120                return os;
121        } // ?|?
[200fcb3]122        void & ?|?( ostype & os, unsigned int ui ) {
[65240bb]123                (ostype &)(os | ui); ends( os );
[200fcb3]124        } // ?|?
[3ce0d440]125
126        ostype & ?|?( ostype & os, long int li ) {
[d1a9ff5]127                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]128                fmt( os, "%ld", li );
129                return os;
130        } // ?|?
[200fcb3]131        void & ?|?( ostype & os, long int li ) {
[65240bb]132                (ostype &)(os | li); ends( os );
[200fcb3]133        } // ?|?
[3ce0d440]134
135        ostype & ?|?( ostype & os, unsigned long int uli ) {
[d1a9ff5]136                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]137                fmt( os, "%lu", uli );
138                return os;
139        } // ?|?
[200fcb3]140        void & ?|?( ostype & os, unsigned long int uli ) {
[65240bb]141                (ostype &)(os | uli); ends( os );
[200fcb3]142        } // ?|?
[3ce0d440]143
144        ostype & ?|?( ostype & os, long long int lli ) {
[d1a9ff5]145                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]146                fmt( os, "%lld", lli );
147                return os;
148        } // ?|?
[200fcb3]149        void & ?|?( ostype & os, long long int lli ) {
[65240bb]150                (ostype &)(os | lli); ends( os );
[200fcb3]151        } // ?|?
[3ce0d440]152
153        ostype & ?|?( ostype & os, unsigned long long int ulli ) {
[d1a9ff5]154                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]155                fmt( os, "%llu", ulli );
156                return os;
157        } // ?|?
[200fcb3]158        void & ?|?( ostype & os, unsigned long long int ulli ) {
[65240bb]159                (ostype &)(os | ulli); ends( os );
[200fcb3]160        } // ?|?
[3ce0d440]161
[e63326b]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                }
[b2ac656]175
[3ce0d440]176        ostype & ?|?( ostype & os, float f ) {
[d1a9ff5]177                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[e63326b]178                PrintWithDP( os, "%g", f );
[3ce0d440]179                return os;
180        } // ?|?
[200fcb3]181        void & ?|?( ostype & os, float f ) {
[65240bb]182                (ostype &)(os | f); ends( os );
[200fcb3]183        } // ?|?
[3ce0d440]184
185        ostype & ?|?( ostype & os, double d ) {
[d1a9ff5]186                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[e63326b]187                PrintWithDP( os, "%.*lg", d, DBL_DIG );
[3ce0d440]188                return os;
189        } // ?|?
[200fcb3]190        void & ?|?( ostype & os, double d ) {
[65240bb]191                (ostype &)(os | d); ends( os );
[200fcb3]192        } // ?|?
[3ce0d440]193
194        ostype & ?|?( ostype & os, long double ld ) {
[d1a9ff5]195                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[e63326b]196                PrintWithDP( os, "%.*Lg", ld, LDBL_DIG );
[3ce0d440]197                return os;
198        } // ?|?
[200fcb3]199        void & ?|?( ostype & os, long double ld ) {
[65240bb]200                (ostype &)(os | ld); ends( os );
[200fcb3]201        } // ?|?
[3ce0d440]202
203        ostype & ?|?( ostype & os, float _Complex fc ) {
[d1a9ff5]204                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[b2ac656]205//              os | crealf( fc ) | nonl;
[3c573e9]206                PrintWithDP( os, "%g", crealf( fc ) );
207                PrintWithDP( os, "%+g", cimagf( fc ) );
[3c5dee4]208                fmt( os, "i" );
[3ce0d440]209                return os;
210        } // ?|?
[200fcb3]211        void & ?|?( ostype & os, float _Complex fc ) {
[65240bb]212                (ostype &)(os | fc); ends( os );
[200fcb3]213        } // ?|?
[3ce0d440]214
215        ostype & ?|?( ostype & os, double _Complex dc ) {
[d1a9ff5]216                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[b2ac656]217//              os | creal( dc ) | nonl;
[3c573e9]218                PrintWithDP( os, "%.*lg", creal( dc ), DBL_DIG );
219                PrintWithDP( os, "%+.*lg", cimag( dc ), DBL_DIG );
[3c5dee4]220                fmt( os, "i" );
[3ce0d440]221                return os;
222        } // ?|?
[200fcb3]223        void & ?|?( ostype & os, double _Complex dc ) {
[65240bb]224                (ostype &)(os | dc); ends( os );
[200fcb3]225        } // ?|?
[3ce0d440]226
227        ostype & ?|?( ostype & os, long double _Complex ldc ) {
[d1a9ff5]228                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[b2ac656]229//              os | creall( ldc ) || nonl;
[3c573e9]230                PrintWithDP( os, "%.*Lg", creall( ldc ), LDBL_DIG );
231                PrintWithDP( os, "%+.*Lg", cimagl( ldc ), LDBL_DIG );
[3c5dee4]232                fmt( os, "i" );
[3ce0d440]233                return os;
234        } // ?|?
[200fcb3]235        void & ?|?( ostype & os, long double _Complex ldc ) {
[65240bb]236                (ostype &)(os | ldc); ends( os );
[200fcb3]237        } // ?|?
[3ce0d440]238
[e3fea42]239        ostype & ?|?( ostype & os, const char str[] ) {
[3ce0d440]240                enum { Open = 1, Close, OpenClose };
241                static const unsigned char mask[256] @= {
242                        // opening delimiters, no space after
243                        ['('] : Open, ['['] : Open, ['{'] : Open,
[ad72c8b]244                        ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
245                        [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
[3ce0d440]246                        // closing delimiters, no space before
247                        [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
[ad72c8b]248                        ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
[3ce0d440]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
[d1a9ff5]259                if ( $sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
260                        fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]261                } // if
262
263                // if string starts line, must reset to determine open state because separator is off
[d1a9ff5]264                $sepReset( os );                                                                // reset separator
[3ce0d440]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
[d1a9ff5]269                if ( $sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
[3ce0d440]270                        sepOn( os );
271                } else {
272                        sepOff( os );
273                } // if
[d1a9ff5]274                if ( ch == '\n' ) $setNL( os, true );                   // check *AFTER* $sepPrt call above as it resets NL flag
[3ce0d440]275                return write( os, str, len );
276        } // ?|?
[e3fea42]277
278        void ?|?( ostype & os, const char str[] ) {
[65240bb]279                (ostype &)(os | str); ends( os );
[200fcb3]280        } // ?|?
[3ce0d440]281
282//      ostype & ?|?( ostype & os, const char16_t * str ) {
[d1a9ff5]283//              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]284//              fmt( os, "%ls", str );
285//              return os;
286//      } // ?|?
[d3b7937]287
[3ce0d440]288// #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
289//      ostype & ?|?( ostype & os, const char32_t * str ) {
[d1a9ff5]290//              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]291//              fmt( os, "%ls", str );
292//              return os;
293//      } // ?|?
294// #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
[d3b7937]295
[3ce0d440]296//      ostype & ?|?( ostype & os, const wchar_t * str ) {
[d1a9ff5]297//              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]298//              fmt( os, "%ls", str );
299//              return os;
300//      } // ?|?
301
302        ostype & ?|?( ostype & os, const void * p ) {
[d1a9ff5]303                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3ce0d440]304                fmt( os, "%p", p );
305                return os;
306        } // ?|?
[200fcb3]307        void ?|?( ostype & os, const void * p ) {
[65240bb]308                (ostype &)(os | p); ends( os );
[200fcb3]309        } // ?|?
[3ce0d440]310
311        // manipulators
312        ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
[9d362a0]313                (ostype &)(manip( os ));
[200fcb3]314                return os;
315        } // ?|?
316        void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
[9d362a0]317                (ostype &)(manip( os ));
[d1a9ff5]318                if ( $getPrt( os ) ) ends( os );                                // something printed ?
319                $setPrt( os, false );                                                   // turn off
[3ce0d440]320        } // ?|?
321
322        ostype & sep( ostype & os ) {
[9d362a0]323                return (ostype &)(os | sepGet( os ));
[3ce0d440]324        } // sep
325
326        ostype & sepTuple( ostype & os ) {
[200fcb3]327                return os | sepGetTuple( os );
[3ce0d440]328        } // sepTuple
329
[200fcb3]330        ostype & nl( ostype & os ) {
[9d362a0]331                (ostype &)(os | '\n');
[d1a9ff5]332                $setPrt( os, false );                                                   // turn off
333                $setNL( os, true );
[3ce0d440]334                flush( os );
[200fcb3]335                return sepOff( os );                                                    // prepare for next line
336        } // nl
337
338        ostype & nonl( ostype & os ) {
[d1a9ff5]339                $setPrt( os, false );                                                   // turn off
[3ce0d440]340                return os;
[200fcb3]341        } // nonl
[3ce0d440]342
343        ostype & sepOn( ostype & os ) {
[200fcb3]344                sepOn( os );                                                                    // call void returning
[3ce0d440]345                return os;
346        } // sepOn
347
348        ostype & sepOff( ostype & os ) {
[200fcb3]349                sepOff( os );                                                                   // call void returning
[3ce0d440]350                return os;
351        } // sepOff
[6de9f4a]352
[3ce0d440]353        ostype & sepEnable( ostype & os ) {
[200fcb3]354                sepEnable( os );                                                                // call void returning
[3ce0d440]355                return os;
356        } // sepEnable
[b6dc097]357
[3ce0d440]358        ostype & sepDisable( ostype & os ) {
[200fcb3]359                sepDisable( os );                                                               // call void returning
[3ce0d440]360                return os;
361        } // sepDisable
[cf16f94]362
[200fcb3]363        ostype & nlOn( ostype & os ) {
364                nlOn( os );                                                                             // call void returning
365                return os;
366        } // nlOn
367
368        ostype & nlOff( ostype & os ) {
369                nlOff( os );                                                                    // call void returning
370                return os;
371        } // nlOff
372} // distribution
[cf16f94]373
[c443d1d]374// tuples
[200fcb3]375forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
376        ostype & ?|?( ostype & os, T arg, Params rest ) {
[9d362a0]377                (ostype &)(os | arg);                                                   // print first argument
[d1a9ff5]378                $sepSetCur( os, sepGetTuple( os ) );                    // switch to tuple separator
[9d362a0]379                (ostype &)(os | rest);                                                  // print remaining arguments
[d1a9ff5]380                $sepSetCur( os, sepGet( os ) );                                 // switch to regular separator
[200fcb3]381                return os;
382        } // ?|?
383        void ?|?( ostype & os, T arg, Params rest ) {
[65240bb]384                // (ostype &)(?|?( os, arg, rest )); ends( os );
[9d362a0]385                (ostype &)(os | arg);                                                   // print first argument
[d1a9ff5]386                $sepSetCur( os, sepGetTuple( os ) );                    // switch to tuple separator
[9d362a0]387                (ostype &)(os | rest);                                                  // print remaining arguments
[d1a9ff5]388                $sepSetCur( os, sepGet( os ) );                                 // switch to regular separator
[65240bb]389                ends( os );
[200fcb3]390        } // ?|?
391} // distribution
[c443d1d]392
[61c7239]393// writes the range [begin, end) to the given stream
394forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
395        void write( iterator_type begin, iterator_type end, ostype & os ) {
396                void print( elt_type i ) { os | i; }
397                for_each( begin, end, print );
398        } // ?|?
399
400        void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
401                void print( elt_type i ) { os | i; }
402                for_each_reverse( begin, end, print );
403        } // ?|?
404} // distribution
405
[65240bb]406//*********************************** manipulators ***********************************
[3c573e9]407
[65240bb]408//*********************************** integral ***********************************
[3c573e9]409
410static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
411static const char * longbin[]  = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
412
413// Default prefix for non-decimal prints is 0b, 0, 0x.
414#define IntegralFMTImpl( T, CODE, IFMTNP, IFMTP ) \
415forall( dtype ostype | ostream( ostype ) ) { \
416        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
[d1a9ff5]417                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
[3c573e9]418\
419                if ( f.base == 'b' || f.base == 'B' ) {                 /* bespoke binary format */ \
420                        int bits;                                                                                                       \
421                        if ( f.val == (T){0} ) bits = 1;                        /* force at least one bit to print */ \
422                        else bits = sizeof(long long int) * 8 - __builtin_clzll( f.val ); /* position of most significant bit */ \
423                        bits = bits > sizeof(f.val) * 8 ? sizeof(f.val) * 8 : bits; \
424                        int spaces = f.wd - bits;                                       /* can be negative */ \
425                        if ( ! f.flags.nobsdp ) { spaces -= 2; }        /* base prefix takes space */ \
426                        /* printf( "%d %d\n", bits, spaces ); */ \
427                        if ( ! f.flags.left ) {                                         /* right justified ? */ \
428                                /* Note, base prefix then zero padding or spacing then prefix. */ \
429                                if ( f.flags.pad0 || f.flags.pc ) { \
430                                        if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
431                                        if ( f.flags.pc ) spaces = f.pc - bits; \
432                                        if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
433                                } else { \
434                                        if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
435                                        if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
436                                } /* if */ \
437                        } else if ( ! f.flags.nobsdp ) { \
438                                fmt( os, "0%c", f.base ); \
439                        } /* if */ \
440                        int shift = (bits - 1) / 4 * 4; /* floor( bits - 1, 4 ) */ \
441                        typeof( f.val ) temp = f.val; \
442                        fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \
443                        for () { \
444                                shift -= 4; \
445                          if ( shift < 0 ) break; \
446                                temp = f.val; \
447                                fmt( os, "%s", longbin[(temp >> shift) & 0xf] ); \
448                        } /* for */ \
449                        if ( f.flags.left && spaces > 0 ) fmt( os, "%*s", spaces, " " ); \
450                        return os; \
451                } /* if  */ \
452\
453                char fmtstr[sizeof(IFMTP)];                                             /* sizeof includes '\0' */ \
454                if ( ! f.flags.pc ) memcpy( &fmtstr, IFMTNP, sizeof(IFMTNP) ); \
455                else memcpy( &fmtstr, IFMTP, sizeof(IFMTP) ); \
456                int star = 4;                                                                   /* position before first '*' */ \
457\
458                /* Insert flags into spaces before '*', from right to left. */ \
459                if ( ! f.flags.nobsdp ) { fmtstr[star] = '#'; star -= 1; } \
460                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
461                if ( f.flags.sign && f.base == CODE ) { fmtstr[star] = '+'; star -= 1; } \
462                if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \
463                fmtstr[star] = '%'; \
464\
465                if ( ! f.flags.pc ) {                                                   /* no precision */ \
466                        /* printf( "%s\n", &fmtstr[star] ); */ \
467                        fmtstr[sizeof(IFMTNP)-2] = f.base;                      /* sizeof includes '\0' */ \
468                        fmt( os, &fmtstr[star], f.wd, f.val ); \
469                } else {                                                                                /* precision */ \
470                        fmtstr[sizeof(IFMTP)-2] = f.base;                       /* sizeof includes '\0' */ \
471                        /* printf( "%s\n", &fmtstr[star] ); */ \
472                        fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \
473                } /* if */ \
474                return os; \
475        } /* ?|? */ \
[65240bb]476        void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
[3c573e9]477} // distribution
478
479IntegralFMTImpl( signed char, 'd', "%    *hh ", "%    *.*hh " )
480IntegralFMTImpl( unsigned char, 'u', "%    *hh ", "%    *.*hh " )
481IntegralFMTImpl( signed short int, 'd', "%    *h ", "%    *.*h " )
482IntegralFMTImpl( unsigned short int, 'u', "%    *h ", "%    *.*h " )
483IntegralFMTImpl( signed int, 'd', "%    * ", "%    *.* " )
484IntegralFMTImpl( unsigned int, 'u', "%    * ", "%    *.* " )
485IntegralFMTImpl( signed long int, 'd', "%    *l ", "%    *.*l " )
486IntegralFMTImpl( unsigned long int, 'u', "%    *l ", "%    *.*l " )
487IntegralFMTImpl( signed long long int, 'd', "%    *ll ", "%    *.*ll " )
488IntegralFMTImpl( unsigned long long int, 'u', "%    *ll ", "%    *.*ll " )
489
[65240bb]490//*********************************** floating point ***********************************
[3c573e9]491
492#define PrintWithDP2( os, format, val, ... ) \
493        { \
494                enum { size = 48 }; \
495                char buf[size]; \
496                int bufbeg = 0, i, len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \
497                if ( isfinite( val ) && (f.base != 'g' || f.pc != 0) ) { /* if number, print decimal point */ \
498                        for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E'; i += 1 ); /* decimal point or scientific ? */ \
499                        if ( i == len && ! f.flags.nobsdp ) { \
500                                if ( ! f.flags.left ) { \
501                                        buf[i] = '.'; buf[i + 1] = '\0'; \
[4f37255]502                                        if ( buf[0] == ' ' ) bufbeg = 1;        /* decimal point within width */ \
[3c573e9]503                                } else { \
504                                        for ( i = 0; i < len && buf[i] != ' '; i += 1 ); /* trailing blank ? */ \
505                                        buf[i] = '.'; \
506                                        if ( i == len ) buf[i + 1] = '\0'; \
507                                } /* if */ \
508                        } /* if */ \
509                } /* if */ \
510                fmt( os, "%s", &buf[bufbeg] ); \
511        }
512
513#define FloatingPointFMTImpl( T, DFMTNP, DFMTP ) \
514forall( dtype ostype | ostream( ostype ) ) { \
515        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
[d1a9ff5]516                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
[3c573e9]517                char fmtstr[sizeof(DFMTP)];                                             /* sizeof includes '\0' */ \
518                if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \
519                else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \
520                int star = 4;                                                                   /* position before first '*' */ \
521\
522                /* Insert flags into spaces before '*', from right to left. */ \
523                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
524                if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \
525                if ( f.flags.pad0 ) { fmtstr[star] = '0'; star -= 1; } \
526                fmtstr[star] = '%'; \
527\
528                if ( ! f.flags.pc ) {                                                   /* no precision */ \
529                        fmtstr[sizeof(DFMTNP)-2] = f.base;                      /* sizeof includes '\0' */ \
530                        /* printf( "%g %d %s\n", f.val, f.wd, &fmtstr[star]); */ \
531                        PrintWithDP2( os, &fmtstr[star], f.val, f.wd ) \
532                } else {                                                                                /* precision */ \
533                        fmtstr[sizeof(DFMTP)-2] = f.base;                       /* sizeof includes '\0' */ \
534                        /* printf( "%g %d %d %s\n", f.val, f.wd, f.pc, &fmtstr[star] ); */ \
535                        PrintWithDP2( os, &fmtstr[star], f.val, f.wd, f.pc ) \
536                } /* if */ \
537                return os; \
538        } /* ?|? */ \
[e3fea42]539\
[65240bb]540        void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
[3c573e9]541} // distribution
542
543FloatingPointFMTImpl( double, "%    * ", "%    *.* " )
544FloatingPointFMTImpl( long double, "%    *L ", "%    *.*L " )
545
[65240bb]546//*********************************** character ***********************************
[3c573e9]547
548forall( dtype ostype | ostream( ostype ) ) {
549        ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) {
[4f37255]550                if ( f.base != 'c' ) {                                                  // bespoke binary/octal/hex format
[3c573e9]551                        _Ostream_Manip(unsigned char) fmtuc @= { f.val, f.wd, f.pc, f.base, {'\0'} };
552                        fmtuc.flags.pc = f.flags.pc;
553                        fmtuc.flags.nobsdp = f.flags.nobsdp;
554//                      os | fmtuc | nonl;
555                        (ostype &)(os | fmtuc);
556                        return os;
557                } // if
558
[d1a9ff5]559                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3c573e9]560
561                #define CFMTNP "% * "
[4f37255]562                char fmtstr[sizeof(CFMTNP)];                                    // sizeof includes '\0'
[3c573e9]563                memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) );
[4f37255]564                int star = 1;                                                                   // position before first '*'
[3c573e9]565
566                // Insert flags into spaces before '*', from right to left.
567                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
568                fmtstr[star] = '%';
569
[4f37255]570                fmtstr[sizeof(CFMTNP)-2] = f.base;                              // sizeof includes '\0'
[3c573e9]571                // printf( "%d %s\n", f.wd, &fmtstr[star] );
572                fmt( os, &fmtstr[star], f.wd, f.val );
573                return os;
574        } // ?|?
[e3fea42]575
[65240bb]576        void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); }
[3c573e9]577} // distribution
578
[65240bb]579//*********************************** C string ***********************************
[3c573e9]580
581forall( dtype ostype | ostream( ostype ) ) {
582        ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) {
583                if ( ! f.val ) return os;                                               // null pointer ?
584
585                if ( f.base != 's' ) {                                                  // bespoke binary/octal/hex format
586                        _Ostream_Manip(unsigned char) fmtuc @= { 0, f.wd, f.pc, f.base, {'\0'} };
587                        fmtuc.flags.pc = f.flags.pc;
588                        fmtuc.flags.nobsdp = f.flags.nobsdp;
589                        for ( unsigned int i = 0; f.val[i] != '\0'; i += 1 ) {
590                                fmtuc.val = f.val[i];
591//                              os | fmtuc | nonl;
592                                (ostype &)(os | fmtuc);
593                        } // for
594                        return os;
595                } // if
596
[d1a9ff5]597                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
[3c573e9]598
599                #define SFMTNP "% * "
600                #define SFMTP "% *.* "
601                char fmtstr[sizeof(SFMTP)];                                             // sizeof includes '\0'
602                if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) );
603                else memcpy( &fmtstr, SFMTP, sizeof(SFMTP) );
604                int star = 1;                                                                   // position before first '*'
605
606                // Insert flags into spaces before '*', from right to left.
607                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
608                fmtstr[star] = '%';
609
610                if ( ! f.flags.pc ) {                                                   // no precision
611                        // printf( "%d %s\n", f.wd, &fmtstr[star] );
612                        fmtstr[sizeof(SFMTNP)-2] = f.base;                      // sizeof includes '\0'
613                        fmt( os, &fmtstr[star], f.wd, f.val );
614                } else {                                                                                // precision
615                        fmtstr[sizeof(SFMTP)-2] = f.base;                       // sizeof includes '\0'
616                        // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] );
617                        fmt( os, &fmtstr[star], f.wd, f.pc, f.val );
618                } // if
619                return os;
620        } // ?|?
[e3fea42]621
[65240bb]622        void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); }
[3c573e9]623} // distribution
624
625
[65240bb]626//*********************************** istream ***********************************
[3c573e9]627
[e56cfdb0]628
[3ce0d440]629forall( dtype istype | istream( istype ) ) {
[93c2e0a]630        istype & ?|?( istype & is, bool & b ) {
[3ce0d440]631                char val[6];
632                fmt( is, "%5s", val );
633                if ( strcmp( val, "true" ) == 0 ) b = true;
634                else if ( strcmp( val, "false" ) == 0 ) b = false;
635                else {
[93c2e0a]636                        fprintf( stderr, "invalid Boolean constant\n" );
[4f37255]637                        abort();                                                                        // cannot use abort stream
[3ce0d440]638                } // if
639                return is;
640        } // ?|?
641
642        istype & ?|?( istype & is, char & c ) {
[0efb269]643                char temp;
644                for () {
[4f37255]645                        fmt( is, "%c", &temp );                                         // must pass pointer through varg to fmt
[0efb269]646                        // do not overwrite parameter with newline unless appropriate
647                        if ( temp != '\n' || getANL( is ) ) { c = temp; break; }
648                        if ( eof( is ) ) break;
649                } // for
[3ce0d440]650                return is;
651        } // ?|?
652
653        istype & ?|?( istype & is, signed char & sc ) {
[3c573e9]654                fmt( is, "%hhi", &sc );
[3ce0d440]655                return is;
656        } // ?|?
657
658        istype & ?|?( istype & is, unsigned char & usc ) {
[3c573e9]659                fmt( is, "%hhi", &usc );
[3ce0d440]660                return is;
661        } // ?|?
662
663        istype & ?|?( istype & is, short int & si ) {
[3c573e9]664                fmt( is, "%hi", &si );
[3ce0d440]665                return is;
666        } // ?|?
667
668        istype & ?|?( istype & is, unsigned short int & usi ) {
[3c573e9]669                fmt( is, "%hi", &usi );
[3ce0d440]670                return is;
671        } // ?|?
672
673        istype & ?|?( istype & is, int & i ) {
[3c573e9]674                fmt( is, "%i", &i );
[3ce0d440]675                return is;
676        } // ?|?
677
678        istype & ?|?( istype & is, unsigned int & ui ) {
[3c573e9]679                fmt( is, "%i", &ui );
[3ce0d440]680                return is;
681        } // ?|?
682
683        istype & ?|?( istype & is, long int & li ) {
[3c573e9]684                fmt( is, "%li", &li );
[3ce0d440]685                return is;
686        } // ?|?
687
688        istype & ?|?( istype & is, unsigned long int & ulli ) {
[3c573e9]689                fmt( is, "%li", &ulli );
[3ce0d440]690                return is;
691        } // ?|?
692
693        istype & ?|?( istype & is, long long int & lli ) {
[3c573e9]694                fmt( is, "%lli", &lli );
[3ce0d440]695                return is;
696        } // ?|?
697
698        istype & ?|?( istype & is, unsigned long long int & ulli ) {
[3c573e9]699                fmt( is, "%lli", &ulli );
[3ce0d440]700                return is;
701        } // ?|?
702
703
704        istype & ?|?( istype & is, float & f ) {
705                fmt( is, "%f", &f );
706                return is;
707        } // ?|?
708
709        istype & ?|?( istype & is, double & d ) {
710                fmt( is, "%lf", &d );
711                return is;
712        } // ?|?
713
714        istype & ?|?( istype & is, long double & ld ) {
715                fmt( is, "%Lf", &ld );
716                return is;
717        } // ?|?
718
719
720        istype & ?|?( istype & is, float _Complex & fc ) {
721                float re, im;
[3c573e9]722                fmt( is, "%f%fi", &re, &im );
[3ce0d440]723                fc = re + im * _Complex_I;
724                return is;
725        } // ?|?
726
727        istype & ?|?( istype & is, double _Complex & dc ) {
728                double re, im;
729                fmt( is, "%lf%lfi", &re, &im );
730                dc = re + im * _Complex_I;
731                return is;
732        } // ?|?
733
734        istype & ?|?( istype & is, long double _Complex & ldc ) {
735                long double re, im;
736                fmt( is, "%Lf%Lfi", &re, &im );
737                ldc = re + im * _Complex_I;
738                return is;
739        } // ?|?
740
[e3fea42]741        // istype & ?|?( istype & is, const char fmt[] ) {
[dc5072f]742        //      fmt( is, fmt, "" );
743        //      return is;
744        // } // ?|?
[04396aa]745
746        istype & ?|?( istype & is, char * s ) {
747                fmt( is, "%s", s );
748                return is;
749        } // ?|?
750
[3ce0d440]751        // manipulators
752        istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
753                return manip( is );
754        } // ?|?
755
[200fcb3]756        istype & nl( istype & is ) {
[3c5dee4]757                fmt( is, "%*[^\n]" );                                                   // ignore characters to newline
[3ce0d440]758                return is;
[200fcb3]759        } // nl
[0efb269]760
761        istype & nlOn( istype & is ) {
762                nlOn( is );                                                                             // call void returning
763                return is;
764        } // nlOn
765
766        istype & nlOff( istype & is ) {
767                nlOff( is );                                                                    // call void returning
768                return is;
769        } // nlOff
[3ce0d440]770} // distribution
[44574f2]771
[65240bb]772//*********************************** manipulators ***********************************
[3c573e9]773
774forall( dtype istype | istream( istype ) )
[61c7239]775istype & ?|?( istype & is, _Istream_Cstr f ) {
776        // skip xxx
777        if ( ! f.s ) {
[dc5072f]778                // printf( "skip %s %d\n", f.scanset, f.wd );
[4f37255]779                if ( f.wd == -1 ) fmt( is, f.scanset, "" );             // no input arguments
[dc5072f]780                else for ( f.wd ) fmt( is, "%*c" );
[61c7239]781                return is;
782        } // if
783        size_t len = 0;
784        if ( f.scanset ) len = strlen( f.scanset );
785        char fmtstr[len + 16];
786        int start = 1;
787        fmtstr[0] = '%';
788        if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
789        if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
790        // cstr %s, %*s, %ws, %*ws
791        if ( ! f.scanset ) {
792                fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
793                // printf( "cstr %s\n", fmtstr );
794                fmt( is, fmtstr, f.s );
795                return is;
796        } // if
797        // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
798        // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
799        fmtstr[start] = '['; start += 1;
800        if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
801        strcpy( &fmtstr[start], f.scanset );                            // copy includes '\0'
802        len += start;
803        fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
804        // printf( "incl/excl %s\n", fmtstr );
805        fmt( is, fmtstr, f.s );
[3c573e9]806        return is;
[61c7239]807} // ?|?
[3c573e9]808
[86a8be5]809forall( dtype istype | istream( istype ) )
810istype & ?|?( istype & is, _Istream_Char f ) {
811        fmt( is, "%*c" );                                                                       // argument variable unused
812        return is;
813} // ?|?
814
[3c573e9]815#define InputFMTImpl( T, CODE ) \
816forall( dtype istype | istream( istype ) ) \
817istype & ?|?( istype & is, _Istream_Manip(T) f ) { \
818        enum { size = 16 }; \
819        char fmtstr[size]; \
[86a8be5]820        if ( f.wd == -1 ) { \
[3c573e9]821                snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \
822        } else { \
823                snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \
824        } /* if */ \
825        /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \
826        fmt( is, fmtstr, &f.val ); \
827        return is; \
[61c7239]828} // ?|?
[3c573e9]829
830InputFMTImpl( signed char, "hhi" )
831InputFMTImpl( unsigned char, "hhi" )
832InputFMTImpl( signed short int, "hi" )
833InputFMTImpl( unsigned short int, "hi" )
834InputFMTImpl( signed int, "i" )
835InputFMTImpl( unsigned int, "i" )
836InputFMTImpl( signed long int, "li" )
837InputFMTImpl( unsigned long int, "li" )
838InputFMTImpl( signed long long int, "lli" )
839InputFMTImpl( unsigned long long int, "lli" )
840
841InputFMTImpl( float, "f" )
842InputFMTImpl( double, "lf" )
843InputFMTImpl( long double, "Lf" )
844
[61c7239]845forall( dtype istype | istream( istype ) )
846istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) {
847        float re, im;
848        _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore };
849        is | fmtuc;
850        &fmtuc.val = &im;
851        is | fmtuc;
852        if ( ! fc.ignore ) fc.val = re + im * _Complex_I;       // re/im are uninitialized for ignore
853        return is;
854} // ?|?
855
856forall( dtype istype | istream( istype ) )
857istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
858        double re, im;
859        _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore };
860        is | fmtuc;
861        &fmtuc.val = &im;
862        is | fmtuc;
863        if ( ! dc.ignore ) dc.val = re + im * _Complex_I;       // re/im are uninitialized for ignore
864        return is;
865} // ?|?
866
867forall( dtype istype | istream( istype ) )
868istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
869        long double re, im;
870        _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore };
871        is | fmtuc;
872        &fmtuc.val = &im;
873        is | fmtuc;
874        if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I;     // re/im are uninitialized for ignore
875        return is;
876} // ?|?
[3c573e9]877
[86bd7c1f]878// Local Variables: //
879// tab-width: 4 //
[084520f]880// compile-command: "cfa iostream.cfa" //
[86bd7c1f]881// End: //
Note: See TracBrowser for help on using the repository browser.