| 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.hfa --
 | 
|---|
| 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 Aug 14 17:35:40 2023
 | 
|---|
| 13 | // Update Count     : 508
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #pragma once
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "iterator.hfa"
 | 
|---|
| 19 | #include "Exception.hfa"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | 
 | 
|---|
| 22 | // *********************************** ostream ***********************************
 | 
|---|
| 23 | 
 | 
|---|
| 24 | 
 | 
|---|
| 25 | forall( ostype & )
 | 
|---|
| 26 | trait basic_ostream {
 | 
|---|
| 27 |         // private
 | 
|---|
| 28 |         bool sepPrt$( ostype & );                                                       // get separator state (on/off)
 | 
|---|
| 29 |         void sepReset$( ostype & );                                                     // set separator state to default state
 | 
|---|
| 30 |         void sepReset$( ostype &, bool );                                       // set separator and default state
 | 
|---|
| 31 |         const char * sepGetCur$( ostype & );                            // get current separator string
 | 
|---|
| 32 |         void sepSetCur$( ostype &, const char [] );                     // set current separator string
 | 
|---|
| 33 |         bool getNL$( ostype & );                                                        // check newline
 | 
|---|
| 34 |         void setNL$( ostype &, bool );                                          // saw newline
 | 
|---|
| 35 |         bool getANL$( ostype & );                                                       // get auto newline (on/off)
 | 
|---|
| 36 |         bool getPrt$( ostype & );                                                       // get fmt called in output cascade
 | 
|---|
| 37 |         void setPrt$( ostype &, bool );                                         // set fmt called in output cascade
 | 
|---|
| 38 |         // public
 | 
|---|
| 39 |         void nlOn( ostype & );                                                          // turn auto-newline state on
 | 
|---|
| 40 |         void nlOff( ostype & );                                                         // turn auto-newline state off
 | 
|---|
| 41 | 
 | 
|---|
| 42 |         void sep( ostype & );                                                           // turn separator state on
 | 
|---|
| 43 |         void nosep( ostype & );                                                         // turn separator state off
 | 
|---|
| 44 |         bool sepOn( ostype & );                                                         // set default state to on, and return previous state
 | 
|---|
| 45 |         bool sepOff( ostype & );                                                        // set default state to off, and return previous state
 | 
|---|
| 46 |         const char * sepGet( ostype & );                                        // get separator string
 | 
|---|
| 47 |         void sepSet( ostype &, const char [] );                         // set separator to string (15 character maximum)
 | 
|---|
| 48 |         const char * sepGetTuple( ostype & );                           // get tuple separator string
 | 
|---|
| 49 |         void sepSetTuple( ostype &, const char [] );            // set tuple separator to string (15 character maximum)
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         void ends( ostype & );                                                          // end of output statement
 | 
|---|
| 52 |         int fmt( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
 | 
|---|
| 53 | }; // basic_ostream
 | 
|---|
| 54 | 
 | 
|---|
| 55 | forall( ostype & | basic_ostream( ostype ) )
 | 
|---|
| 56 | trait ostream {
 | 
|---|
| 57 |         bool fail( ostype & );                                                          // operation failed?
 | 
|---|
| 58 |         void clear( ostype & );
 | 
|---|
| 59 |         int flush( ostype & );
 | 
|---|
| 60 |         void open( ostype &, const char name[], const char mode[] );
 | 
|---|
| 61 |         void close( ostype & );
 | 
|---|
| 62 |         ostype & write( ostype &, const char [], size_t );
 | 
|---|
| 63 | }; // ostream
 | 
|---|
| 64 | 
 | 
|---|
| 65 | // forall( T )
 | 
|---|
| 66 | // trait writeable {
 | 
|---|
| 67 | //      forall( ostype & | ostream( ostype ) ) ostype & ?|?( ostype &, T );
 | 
|---|
| 68 | // }; // writeable
 | 
|---|
| 69 | 
 | 
|---|
| 70 | forall( T, ostype & | ostream( ostype ) )
 | 
|---|
| 71 | trait writeable {
 | 
|---|
| 72 |         ostype & ?|?( ostype &, T );
 | 
|---|
| 73 | }; // writeable
 | 
|---|
| 74 | 
 | 
|---|
| 75 | // implement writable for intrinsic types
 | 
|---|
| 76 | 
 | 
|---|
| 77 | #define OSTYPE_VOID( T ) void ?|?( ostype &, T )
 | 
|---|
| 78 | #define OSTYPE_VOID_IMPL( T ) \
 | 
|---|
| 79 |         void ?|?( ostype & os, T t ) { \
 | 
|---|
| 80 |                 (ostype &)(os | t); ends( os ); \
 | 
|---|
| 81 |         } // ?|?
 | 
|---|
| 82 | 
 | 
|---|
| 83 | forall( ostype & | basic_ostream( ostype ) ) {
 | 
|---|
| 84 |         ostype & ?|?( ostype &, bool );
 | 
|---|
| 85 |         OSTYPE_VOID( bool );
 | 
|---|
| 86 | 
 | 
|---|
| 87 |         ostype & ?|?( ostype &, char );
 | 
|---|
| 88 |         OSTYPE_VOID( char );
 | 
|---|
| 89 |         ostype & ?|?( ostype &, signed char );
 | 
|---|
| 90 |         OSTYPE_VOID( signed char );
 | 
|---|
| 91 |         ostype & ?|?( ostype &, unsigned char );
 | 
|---|
| 92 |         OSTYPE_VOID( unsigned char );
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         ostype & ?|?( ostype &, short int );
 | 
|---|
| 95 |         OSTYPE_VOID( short int );
 | 
|---|
| 96 |         ostype & ?|?( ostype &, unsigned short int );
 | 
|---|
| 97 |         OSTYPE_VOID( unsigned short int );
 | 
|---|
| 98 |         ostype & ?|?( ostype &, int );
 | 
|---|
| 99 |         OSTYPE_VOID( int );
 | 
|---|
| 100 |         ostype & ?|?( ostype &, unsigned int );
 | 
|---|
| 101 |         OSTYPE_VOID( unsigned int );
 | 
|---|
| 102 |         ostype & ?|?( ostype &, long int );
 | 
|---|
| 103 |         OSTYPE_VOID( long int );
 | 
|---|
| 104 |         ostype & ?|?( ostype &, long long int );
 | 
|---|
| 105 |         OSTYPE_VOID( long long int );
 | 
|---|
| 106 |         ostype & ?|?( ostype &, unsigned long int );
 | 
|---|
| 107 |         OSTYPE_VOID( unsigned long int );
 | 
|---|
| 108 |         ostype & ?|?( ostype &, unsigned long long int );
 | 
|---|
| 109 |         OSTYPE_VOID( unsigned long long int );
 | 
|---|
| 110 |         #if defined( __SIZEOF_INT128__ )
 | 
|---|
| 111 |         ostype & ?|?( ostype &, int128 );
 | 
|---|
| 112 |         OSTYPE_VOID( int128 );
 | 
|---|
| 113 |         ostype & ?|?( ostype &, unsigned int128 );
 | 
|---|
| 114 |         OSTYPE_VOID( unsigned int128 );
 | 
|---|
| 115 |         #endif // __SIZEOF_INT128__
 | 
|---|
| 116 | 
 | 
|---|
| 117 |         ostype & ?|?( ostype &, float );
 | 
|---|
| 118 |         OSTYPE_VOID( float );
 | 
|---|
| 119 |         ostype & ?|?( ostype &, double );
 | 
|---|
| 120 |         OSTYPE_VOID( double );
 | 
|---|
| 121 |         ostype & ?|?( ostype &, long double );
 | 
|---|
| 122 |         OSTYPE_VOID( long double );
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         ostype & ?|?( ostype &, float _Complex );
 | 
|---|
| 125 |         OSTYPE_VOID( float _Complex );
 | 
|---|
| 126 |         ostype & ?|?( ostype &, double _Complex );
 | 
|---|
| 127 |         OSTYPE_VOID( double _Complex );
 | 
|---|
| 128 |         ostype & ?|?( ostype &, long double _Complex );
 | 
|---|
| 129 |         OSTYPE_VOID( long double _Complex );
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         ostype & ?|?( ostype &, const char [] );
 | 
|---|
| 132 |         OSTYPE_VOID( const char [] );
 | 
|---|
| 133 |         // ostype & ?|?( ostype &, const char16_t [] );
 | 
|---|
| 134 |         #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
 | 
|---|
| 135 |         // ostype & ?|?( ostype &, const char32_t [] );
 | 
|---|
| 136 |         #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
 | 
|---|
| 137 |         // ostype & ?|?( ostype &, const wchar_t [] );
 | 
|---|
| 138 |         ostype & ?|?( ostype &, const void * );
 | 
|---|
| 139 |         OSTYPE_VOID( const void * );
 | 
|---|
| 140 | 
 | 
|---|
| 141 |         // FIX-ME: does not work so using macros
 | 
|---|
| 142 |         // forall( T | { ostype & ?|?( ostype &, T ); } )
 | 
|---|
| 143 |         // void ?|?( ostype & os, T );
 | 
|---|
| 144 | 
 | 
|---|
| 145 |         // manipulators
 | 
|---|
| 146 |         ostype & ?|?( ostype &, ostype & (*)( ostype & ) );
 | 
|---|
| 147 |         OSTYPE_VOID( ostype & (*)( ostype & ) );
 | 
|---|
| 148 | 
 | 
|---|
| 149 |         ostype & nl( ostype & );
 | 
|---|
| 150 |         ostype & nonl( ostype & );
 | 
|---|
| 151 |         ostype & nlOn( ostype & );
 | 
|---|
| 152 |         ostype & nlOff( ostype & );
 | 
|---|
| 153 | 
 | 
|---|
| 154 |         ostype & sepVal( ostype & );
 | 
|---|
| 155 |         ostype & sepTupleVal( ostype & );
 | 
|---|
| 156 |         ostype & sep( ostype & );
 | 
|---|
| 157 |         ostype & nosep( ostype & );
 | 
|---|
| 158 |         ostype & sepOn( ostype & );
 | 
|---|
| 159 |         ostype & sepOff( ostype & );
 | 
|---|
| 160 | } // distribution
 | 
|---|
| 161 | 
 | 
|---|
| 162 | // tuples
 | 
|---|
| 163 | forall( ostype &, T, Params... | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
 | 
|---|
| 164 |         ostype & ?|?( ostype & os, T arg, Params rest );
 | 
|---|
| 165 |         void ?|?( ostype & os, T arg, Params rest );
 | 
|---|
| 166 | } // distribution
 | 
|---|
| 167 | 
 | 
|---|
| 168 | // writes the range [begin, end) to the given stream
 | 
|---|
| 169 | forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) {
 | 
|---|
| 170 |         void write( iterator_type begin, iterator_type end, ostype & os );
 | 
|---|
| 171 |         void write_reverse( iterator_type begin, iterator_type end, ostype & os );
 | 
|---|
| 172 | } // distribution
 | 
|---|
| 173 | 
 | 
|---|
| 174 | // *********************************** manipulators ***********************************
 | 
|---|
| 175 | 
 | 
|---|
| 176 | struct _Ostream_Flags {
 | 
|---|
| 177 |         unsigned char eng:1;                                                            // engineering notation
 | 
|---|
| 178 |         unsigned char neg:1;                                                            // val is negative
 | 
|---|
| 179 |         unsigned char pc:1;                                                                     // precision specified
 | 
|---|
| 180 |         unsigned char left:1;                                                           // left justify
 | 
|---|
| 181 |         unsigned char nobsdp:1;                                                         // base prefix / decimal point
 | 
|---|
| 182 |         unsigned char sign:1;                                                           // plus / minus sign
 | 
|---|
| 183 |         unsigned char pad0:1;                                                           // zero pad
 | 
|---|
| 184 | };
 | 
|---|
| 185 | 
 | 
|---|
| 186 | forall( T )
 | 
|---|
| 187 | struct _Ostream_Manip {
 | 
|---|
| 188 |         T val;                                                                                          // polymorphic base-type
 | 
|---|
| 189 |         int wd, pc;                                                                                     // width, precision: signed for computations
 | 
|---|
| 190 |         char base;                                                                                      // numeric base / floating-point style
 | 
|---|
| 191 |         union {
 | 
|---|
| 192 |                 unsigned char all;
 | 
|---|
| 193 |                 _Ostream_Flags flags;
 | 
|---|
| 194 |         };
 | 
|---|
| 195 | }; // _Ostream_Manip
 | 
|---|
| 196 | 
 | 
|---|
| 197 | // *********************************** integral ***********************************
 | 
|---|
| 198 | 
 | 
|---|
| 199 | // See 6.7.9. 19) The initialization shall occur in initializer list order, each initializer provided for a particular
 | 
|---|
| 200 | // subobject overriding any previously listed initializer for the same subobject; ***all subobjects that are not
 | 
|---|
| 201 | // initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.***
 | 
|---|
| 202 | 
 | 
|---|
| 203 | #define INTEGRAL_FMT_DECL( T, CODE ) \
 | 
|---|
| 204 | static inline { \
 | 
|---|
| 205 |         _Ostream_Manip(T) bin( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'b', { .all : 0 } }; } \
 | 
|---|
| 206 |         _Ostream_Manip(T) oct( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'o', { .all : 0 } }; } \
 | 
|---|
| 207 |         _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'x', { .all : 0 } }; } \
 | 
|---|
| 208 |         _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, CODE, { .all : 0 } }; } \
 | 
|---|
| 209 |         _Ostream_Manip(T) wd( unsigned int w, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, CODE, { .flags.pc : true } }; } \
 | 
|---|
| 210 |         _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
 | 
|---|
| 211 |         _Ostream_Manip(T) & wd( unsigned int w, unsigned int pc, _Ostream_Manip(T) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
 | 
|---|
| 212 |         _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
 | 
|---|
| 213 |         _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \
 | 
|---|
| 214 |         _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
 | 
|---|
| 215 |         _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
 | 
|---|
| 216 |         _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, CODE, { .flags.sign : true } }; } \
 | 
|---|
| 217 |         _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
 | 
|---|
| 218 | } /* distribution */ \
 | 
|---|
| 219 | forall( ostype & | basic_ostream( ostype ) ) { \
 | 
|---|
| 220 |         ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
 | 
|---|
| 221 |         OSTYPE_VOID( _Ostream_Manip(T) ); \
 | 
|---|
| 222 | } // ?|?
 | 
|---|
| 223 | 
 | 
|---|
| 224 | INTEGRAL_FMT_DECL( signed char, 'd' )
 | 
|---|
| 225 | INTEGRAL_FMT_DECL( unsigned char, 'u' )
 | 
|---|
| 226 | INTEGRAL_FMT_DECL( signed short int, 'd' )
 | 
|---|
| 227 | INTEGRAL_FMT_DECL( unsigned short int, 'u' )
 | 
|---|
| 228 | INTEGRAL_FMT_DECL( signed int, 'd' )
 | 
|---|
| 229 | INTEGRAL_FMT_DECL( unsigned int, 'u' )
 | 
|---|
| 230 | INTEGRAL_FMT_DECL( signed long int, 'd' )
 | 
|---|
| 231 | INTEGRAL_FMT_DECL( unsigned long int, 'u' )
 | 
|---|
| 232 | INTEGRAL_FMT_DECL( signed long long int, 'd' )
 | 
|---|
| 233 | INTEGRAL_FMT_DECL( unsigned long long int, 'u' )
 | 
|---|
| 234 | #if defined( __SIZEOF_INT128__ )
 | 
|---|
| 235 | INTEGRAL_FMT_DECL( int128, 'd' )
 | 
|---|
| 236 | INTEGRAL_FMT_DECL( unsigned int128, 'u' )
 | 
|---|
| 237 | #endif // __SIZEOF_INT128__
 | 
|---|
| 238 | 
 | 
|---|
| 239 | // *********************************** floating point ***********************************
 | 
|---|
| 240 | 
 | 
|---|
| 241 | // Default suffix for values with no fraction is "."
 | 
|---|
| 242 | #define FLOATING_POINT_FMT_DECL( T ) \
 | 
|---|
| 243 | static inline { \
 | 
|---|
| 244 |         _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'a', { .all : 0 } }; } \
 | 
|---|
| 245 |         _Ostream_Manip(T) sci( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'e', { .all : 0 } }; } \
 | 
|---|
| 246 |         _Ostream_Manip(T) eng( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.eng : true } }; } \
 | 
|---|
| 247 |         _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, 'g', { .all : 0 } }; } \
 | 
|---|
| 248 |         _Ostream_Manip(T) wd( unsigned int w, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'f', { .flags.pc : true } }; } \
 | 
|---|
| 249 |         _Ostream_Manip(T) ws( unsigned int w, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'g', { .flags.pc : true } }; } \
 | 
|---|
| 250 |         _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { if ( fmt.flags.eng ) fmt.base = 'f'; fmt.wd = w; return fmt; } \
 | 
|---|
| 251 |         _Ostream_Manip(T) & wd( unsigned int w, unsigned int pc, _Ostream_Manip(T) & fmt ) { if ( fmt.flags.eng ) fmt.base = 'f'; fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
 | 
|---|
| 252 |         _Ostream_Manip(T) & ws( unsigned int w, unsigned int pc, _Ostream_Manip(T) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
 | 
|---|
| 253 |         _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
 | 
|---|
| 254 |         _Ostream_Manip(T) upcase( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'G', { .all : 0 } }; } \
 | 
|---|
| 255 |         _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { fmt.base -= 32; /* upper case */ return fmt; } \
 | 
|---|
| 256 |         _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
 | 
|---|
| 257 |         _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.sign : true } }; } \
 | 
|---|
| 258 |         _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
 | 
|---|
| 259 |         _Ostream_Manip(T) nodp( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.nobsdp : true } }; } \
 | 
|---|
| 260 |         _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
 | 
|---|
| 261 |         _Ostream_Manip(T) unit( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.nobsdp : true } }; } \
 | 
|---|
| 262 |         _Ostream_Manip(T) & unit( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
 | 
|---|
| 263 | } /* distribution */ \
 | 
|---|
| 264 | forall( ostype & | basic_ostream( ostype ) ) { \
 | 
|---|
| 265 |         ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
 | 
|---|
| 266 |         OSTYPE_VOID( _Ostream_Manip(T) ); \
 | 
|---|
| 267 | } // ?|?
 | 
|---|
| 268 | 
 | 
|---|
| 269 | FLOATING_POINT_FMT_DECL( double )
 | 
|---|
| 270 | FLOATING_POINT_FMT_DECL( long double )
 | 
|---|
| 271 | 
 | 
|---|
| 272 | // *********************************** character ***********************************
 | 
|---|
| 273 | 
 | 
|---|
| 274 | static inline {
 | 
|---|
| 275 |         _Ostream_Manip(char) bin( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'b', { .all : 0 } }; }
 | 
|---|
| 276 |         _Ostream_Manip(char) oct( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'o', { .all : 0 } }; }
 | 
|---|
| 277 |         _Ostream_Manip(char) hex( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'x', { .all : 0 } }; }
 | 
|---|
| 278 |         _Ostream_Manip(char) wd( unsigned int w, char c ) { return (_Ostream_Manip(char))@{ c, w, 0, 'c', { .all : 0 } }; }
 | 
|---|
| 279 |         _Ostream_Manip(char) & wd( unsigned int w, _Ostream_Manip(char) & fmt ) { fmt.wd = w; return fmt; }
 | 
|---|
| 280 |         _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; }
 | 
|---|
| 281 |         _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
 | 
|---|
| 282 |         _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
 | 
|---|
| 283 | } // distribution
 | 
|---|
| 284 | forall( ostype & | basic_ostream( ostype ) ) {
 | 
|---|
| 285 |         ostype & ?|?( ostype & os, _Ostream_Manip(char) f );
 | 
|---|
| 286 |         OSTYPE_VOID( _Ostream_Manip(char) ); \
 | 
|---|
| 287 | } // ?|?
 | 
|---|
| 288 | 
 | 
|---|
| 289 | // *********************************** C string ***********************************
 | 
|---|
| 290 | 
 | 
|---|
| 291 | static inline {
 | 
|---|
| 292 |         _Ostream_Manip(const char *) bin( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'b', { .all : 0 } }; }
 | 
|---|
| 293 |         _Ostream_Manip(const char *) oct( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'o', { .all : 0 } }; }
 | 
|---|
| 294 |         _Ostream_Manip(const char *) hex( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'x', { .all : 0 } }; }
 | 
|---|
| 295 |         _Ostream_Manip(const char *) wd( unsigned int w, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, w, 0, 's', { .all : 0 } }; }
 | 
|---|
| 296 |         _Ostream_Manip(const char *) wd( unsigned int w, unsigned int pc, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, w, pc, 's', { .flags.pc : true } }; }
 | 
|---|
| 297 |         _Ostream_Manip(const char *) & wd( unsigned int w, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; return fmt; }
 | 
|---|
| 298 |         _Ostream_Manip(const char *) & wd( unsigned int w, unsigned int pc, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
 | 
|---|
| 299 |         _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; }
 | 
|---|
| 300 |         _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
 | 
|---|
| 301 | } // distribution
 | 
|---|
| 302 | forall( ostype & | basic_ostream( ostype ) ) {
 | 
|---|
| 303 |         ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f );
 | 
|---|
| 304 |         OSTYPE_VOID( _Ostream_Manip(const char *) ); \
 | 
|---|
| 305 | } // ?|?
 | 
|---|
| 306 | 
 | 
|---|
| 307 | 
 | 
|---|
| 308 | // *********************************** istream ***********************************
 | 
|---|
| 309 | 
 | 
|---|
| 310 | 
 | 
|---|
| 311 | #define ISTYPE_VOID( T ) void ?|?( istype &, T )
 | 
|---|
| 312 | #define ISTYPE_VOID_IMPL( T ) \
 | 
|---|
| 313 |         void ?|?( istype & is, T t ) { \
 | 
|---|
| 314 |                 (istype &)(is | t); ends( is ); \
 | 
|---|
| 315 |         } // ?|?
 | 
|---|
| 316 | 
 | 
|---|
| 317 | forall( istype & )
 | 
|---|
| 318 | trait basic_istream {
 | 
|---|
| 319 |         // private
 | 
|---|
| 320 |         bool getANL$( istype & );                                                       // get scan newline (on/off)
 | 
|---|
| 321 |         // public
 | 
|---|
| 322 |         void nlOn( istype & );                                                          // read newline
 | 
|---|
| 323 |         void nlOff( istype & );                                                         // scan newline
 | 
|---|
| 324 |         void ends( istype & os );                                                       // end of output statement
 | 
|---|
| 325 |         int fmt( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
 | 
|---|
| 326 |         istype & ungetc( istype &, char );
 | 
|---|
| 327 |         bool eof( istype & );
 | 
|---|
| 328 | }; // basic_istream
 | 
|---|
| 329 | 
 | 
|---|
| 330 | forall( istype & | basic_istream( istype ) )
 | 
|---|
| 331 | trait istream {
 | 
|---|
| 332 |         bool fail( istype & );
 | 
|---|
| 333 |         void clear( istype & );
 | 
|---|
| 334 |         void open( istype & is, const char name[] );
 | 
|---|
| 335 |         void close( istype & is );
 | 
|---|
| 336 |         istype & read( istype &, char [], size_t );
 | 
|---|
| 337 | }; // istream
 | 
|---|
| 338 | 
 | 
|---|
| 339 | forall( T )
 | 
|---|
| 340 | trait readable {
 | 
|---|
| 341 |         forall( istype & | istream( istype ) ) istype & ?|?( istype &, T );
 | 
|---|
| 342 | }; // readable
 | 
|---|
| 343 | 
 | 
|---|
| 344 | forall( istype & | basic_istream( istype ) ) {
 | 
|---|
| 345 |         istype & ?|?( istype &, bool & );
 | 
|---|
| 346 |         ISTYPE_VOID( bool & );
 | 
|---|
| 347 | 
 | 
|---|
| 348 |         istype & ?|?( istype &, char & );
 | 
|---|
| 349 |         ISTYPE_VOID( char & );
 | 
|---|
| 350 |         istype & ?|?( istype &, signed char & );
 | 
|---|
| 351 |         ISTYPE_VOID( signed char & );
 | 
|---|
| 352 |         istype & ?|?( istype &, unsigned char & );
 | 
|---|
| 353 |         ISTYPE_VOID( unsigned char & );
 | 
|---|
| 354 | 
 | 
|---|
| 355 |         istype & ?|?( istype &, short int & );
 | 
|---|
| 356 |         ISTYPE_VOID( short int & );
 | 
|---|
| 357 |         istype & ?|?( istype &, unsigned short int & );
 | 
|---|
| 358 |         ISTYPE_VOID( unsigned short int & );
 | 
|---|
| 359 |         istype & ?|?( istype &, int & );
 | 
|---|
| 360 |         ISTYPE_VOID( int & );
 | 
|---|
| 361 |         istype & ?|?( istype &, unsigned int & );
 | 
|---|
| 362 |         ISTYPE_VOID( unsigned int & );
 | 
|---|
| 363 |         istype & ?|?( istype &, long int & );
 | 
|---|
| 364 |         ISTYPE_VOID( long int & );
 | 
|---|
| 365 |         istype & ?|?( istype &, unsigned long int & );
 | 
|---|
| 366 |         ISTYPE_VOID( unsigned long int & );
 | 
|---|
| 367 |         istype & ?|?( istype &, long long int & );
 | 
|---|
| 368 |         ISTYPE_VOID( long long int & );
 | 
|---|
| 369 |         istype & ?|?( istype &, unsigned long long int & );
 | 
|---|
| 370 |         ISTYPE_VOID( unsigned long long int & );
 | 
|---|
| 371 |         #if defined( __SIZEOF_INT128__ )
 | 
|---|
| 372 |         istype & ?|?( istype &, int128 & );
 | 
|---|
| 373 |         ISTYPE_VOID( int128 & );
 | 
|---|
| 374 |         istype & ?|?( istype &, unsigned int128 & );
 | 
|---|
| 375 |         ISTYPE_VOID( unsigned int128 & );
 | 
|---|
| 376 |         #endif // __SIZEOF_INT128__
 | 
|---|
| 377 | 
 | 
|---|
| 378 |         istype & ?|?( istype &, float & );
 | 
|---|
| 379 |         ISTYPE_VOID( float & );
 | 
|---|
| 380 |         istype & ?|?( istype &, double & );
 | 
|---|
| 381 |         ISTYPE_VOID( double & );
 | 
|---|
| 382 |         istype & ?|?( istype &, long double & );
 | 
|---|
| 383 |         ISTYPE_VOID( long double & );
 | 
|---|
| 384 | 
 | 
|---|
| 385 |         istype & ?|?( istype &, float _Complex & );
 | 
|---|
| 386 |         ISTYPE_VOID( float _Complex & );
 | 
|---|
| 387 |         istype & ?|?( istype &, double _Complex & );
 | 
|---|
| 388 |         ISTYPE_VOID( double _Complex & );
 | 
|---|
| 389 |         istype & ?|?( istype &, long double _Complex & );
 | 
|---|
| 390 |         ISTYPE_VOID( long double _Complex & );
 | 
|---|
| 391 | 
 | 
|---|
| 392 | //      istype & ?|?( istype &, const char [] );
 | 
|---|
| 393 | //      istype & ?|?( istype &, char [] );
 | 
|---|
| 394 | //      ISTYPE_VOID( char [] );
 | 
|---|
| 395 | 
 | 
|---|
| 396 |         // manipulators
 | 
|---|
| 397 |         istype & ?|?( istype &, istype & (*)( istype & ) );
 | 
|---|
| 398 |         ISTYPE_VOID( istype & (*)( istype & ) );
 | 
|---|
| 399 |         istype & nl( istype & is );
 | 
|---|
| 400 |         istype & nlOn( istype & );
 | 
|---|
| 401 |         istype & nlOff( istype & );
 | 
|---|
| 402 | } // distribution
 | 
|---|
| 403 | 
 | 
|---|
| 404 | 
 | 
|---|
| 405 | // *********************************** exceptions ***********************************
 | 
|---|
| 406 | 
 | 
|---|
| 407 | 
 | 
|---|
| 408 | ExceptionDecl( cstring_length );
 | 
|---|
| 409 | 
 | 
|---|
| 410 | 
 | 
|---|
| 411 | // *********************************** manipulators ***********************************
 | 
|---|
| 412 | 
 | 
|---|
| 413 | struct _Istream_Cstr {
 | 
|---|
| 414 |         char * s;
 | 
|---|
| 415 |         union {
 | 
|---|
| 416 |                 const char * scanset;
 | 
|---|
| 417 |                 char delimit;
 | 
|---|
| 418 |         };
 | 
|---|
| 419 |         int wd;                                                                                         // width
 | 
|---|
| 420 |         union {
 | 
|---|
| 421 |                 unsigned char all;
 | 
|---|
| 422 |                 struct {
 | 
|---|
| 423 |                         unsigned char ignore:1;                                         // do not change input argument
 | 
|---|
| 424 |                         unsigned char inex:1;                                           // include/exclude characters in scanset
 | 
|---|
| 425 |                         unsigned char delimit:1;                                        // delimit character
 | 
|---|
| 426 |                         unsigned char rwd:1;                                            // read width
 | 
|---|
| 427 |                 } flags;
 | 
|---|
| 428 |         };
 | 
|---|
| 429 | }; // _Istream_Cstr
 | 
|---|
| 430 | 
 | 
|---|
| 431 | static inline {
 | 
|---|
| 432 |         _Istream_Cstr skip( const char scanset[] ) { return (_Istream_Cstr){ 0p, {scanset}, -1, {.all : 0} }; }
 | 
|---|
| 433 |         _Istream_Cstr skip( unsigned int wd ) { return (_Istream_Cstr){ 0p, {0p}, wd, {.all : 0} }; }
 | 
|---|
| 434 |         _Istream_Cstr & getline( _Istream_Cstr & fmt ) { fmt.delimit = '\n'; fmt.flags.delimit = true; return fmt; }
 | 
|---|
| 435 |         _Istream_Cstr & getline( const char delimit, _Istream_Cstr & fmt ) { fmt.delimit = delimit; fmt.flags.delimit = true; return fmt; }
 | 
|---|
| 436 | //      _Istream_Cstr incl( const char scanset[], char s[] ) { return (_Istream_Cstr){ s, {scanset}, -1, { .flags.inex : false } }; }
 | 
|---|
| 437 | //      _Istream_Cstr incl( const char scanset[], unsigned int wd, char s[] ) { return (_Istream_Cstr){ s, {scanset}, wd, {.flags.inex : false} }; }
 | 
|---|
| 438 |         _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
 | 
|---|
| 439 | //      _Istream_Cstr excl( const char scanset[], char s[] ) { return (_Istream_Cstr){ s, {scanset}, -1, { .flags.inex : true } }; }
 | 
|---|
| 440 | //      _Istream_Cstr excl( const char scanset[], unsigned int wd, char s[] ) { return (_Istream_Cstr){ s, {scanset}, wd, {.flags.inex : true} }; }
 | 
|---|
| 441 |         _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
 | 
|---|
| 442 |         _Istream_Cstr ignore( char s[] ) { return (_Istream_Cstr)@{ s, {0p}, -1, {.flags.ignore : true} }; }
 | 
|---|
| 443 |         _Istream_Cstr & ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; }
 | 
|---|
| 444 |         _Istream_Cstr wdi( unsigned int wd, char s[] ) { return (_Istream_Cstr)@{ s, {0p}, wd, {.all : 0} }; }
 | 
|---|
| 445 |         _Istream_Cstr wdi( unsigned int wd, unsigned int rwd, char s[] ) {
 | 
|---|
| 446 |                 if ( wd <= rwd ) throw (cstring_length){ &cstring_length_vt };
 | 
|---|
| 447 |                 return (_Istream_Cstr)@{ s, {0p}, rwd, {.flags.rwd : true} };
 | 
|---|
| 448 |         }
 | 
|---|
| 449 | //      _Istream_Cstr & wdi( unsigned int wd, _Istream_Cstr & fmt ) { fmt.wd = wd; return fmt; }
 | 
|---|
| 450 | } // distribution
 | 
|---|
| 451 | forall( istype & | basic_istream( istype ) ) {
 | 
|---|
| 452 |         istype & ?|?( istype & is, _Istream_Cstr f );
 | 
|---|
| 453 |         ISTYPE_VOID( _Istream_Cstr );
 | 
|---|
| 454 | }
 | 
|---|
| 455 | 
 | 
|---|
| 456 | struct _Istream_Char {
 | 
|---|
| 457 |         bool ignore;                                                                            // do not change input argument
 | 
|---|
| 458 | }; // _Istream_Char
 | 
|---|
| 459 | 
 | 
|---|
| 460 | static inline {
 | 
|---|
| 461 |         _Istream_Char ignore( const char ) { return (_Istream_Char)@{ true }; }
 | 
|---|
| 462 |         _Istream_Char & ignore( _Istream_Char & fmt ) { fmt.ignore = true; return fmt; }
 | 
|---|
| 463 | } // distribution
 | 
|---|
| 464 | forall( istype & | basic_istream( istype ) ) {
 | 
|---|
| 465 |         istype & ?|?( istype & is, _Istream_Char f );
 | 
|---|
| 466 |         ISTYPE_VOID( _Istream_Char );
 | 
|---|
| 467 | }
 | 
|---|
| 468 | 
 | 
|---|
| 469 | forall( T & | sized( T ) )
 | 
|---|
| 470 | struct _Istream_Manip {
 | 
|---|
| 471 |         T & val;                                                                                        // polymorphic base-type
 | 
|---|
| 472 |         int wd;                                                                                         // width
 | 
|---|
| 473 |         bool ignore;                                                                            // do not change input argument
 | 
|---|
| 474 | }; // _Istream_Manip
 | 
|---|
| 475 | 
 | 
|---|
| 476 | #define INPUT_FMT_DECL( T ) \
 | 
|---|
| 477 | static inline { \
 | 
|---|
| 478 |         _Istream_Manip(T) ignore( const T & val ) { return (_Istream_Manip(T))@{ (T &)val, -1, true }; } \
 | 
|---|
| 479 |         _Istream_Manip(T) & ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \
 | 
|---|
| 480 |         _Istream_Manip(T) wdi( unsigned int wd, T & val ) { return (_Istream_Manip(T))@{ val, wd, false }; } \
 | 
|---|
| 481 |         _Istream_Manip(T) & wdi( unsigned int wd, _Istream_Manip(T) & fmt ) { fmt.wd = wd; return fmt; } \
 | 
|---|
| 482 | } /* distribution */ \
 | 
|---|
| 483 | forall( istype & | basic_istream( istype ) ) { \
 | 
|---|
| 484 |         istype & ?|?( istype & is, _Istream_Manip(T) f ); \
 | 
|---|
| 485 |         ISTYPE_VOID( _Istream_Manip(T) ); \
 | 
|---|
| 486 | } // ?|?
 | 
|---|
| 487 | 
 | 
|---|
| 488 | INPUT_FMT_DECL( signed char )
 | 
|---|
| 489 | INPUT_FMT_DECL( unsigned char )
 | 
|---|
| 490 | INPUT_FMT_DECL( signed short int )
 | 
|---|
| 491 | INPUT_FMT_DECL( unsigned short int )
 | 
|---|
| 492 | INPUT_FMT_DECL( signed int )
 | 
|---|
| 493 | INPUT_FMT_DECL( unsigned int )
 | 
|---|
| 494 | INPUT_FMT_DECL( signed long int )
 | 
|---|
| 495 | INPUT_FMT_DECL( unsigned long int )
 | 
|---|
| 496 | INPUT_FMT_DECL( signed long long int )
 | 
|---|
| 497 | INPUT_FMT_DECL( unsigned long long int )
 | 
|---|
| 498 | 
 | 
|---|
| 499 | INPUT_FMT_DECL( float )
 | 
|---|
| 500 | INPUT_FMT_DECL( double )
 | 
|---|
| 501 | INPUT_FMT_DECL( long double )
 | 
|---|
| 502 | 
 | 
|---|
| 503 | INPUT_FMT_DECL( float _Complex )
 | 
|---|
| 504 | INPUT_FMT_DECL( double _Complex )
 | 
|---|
| 505 | INPUT_FMT_DECL( long double _Complex )
 | 
|---|
| 506 | 
 | 
|---|
| 507 | 
 | 
|---|
| 508 | // *********************************** time ***********************************
 | 
|---|
| 509 | 
 | 
|---|
| 510 | 
 | 
|---|
| 511 | #include <time_t.hfa>                                                                   // Duration (constructors) / Time (constructors)
 | 
|---|
| 512 | 
 | 
|---|
| 513 | forall( ostype & | ostream( ostype ) ) {
 | 
|---|
| 514 |         ostype & ?|?( ostype & os, Duration dur );
 | 
|---|
| 515 |         OSTYPE_VOID( Duration );
 | 
|---|
| 516 |         ostype & ?|?( ostype & os, Time time );
 | 
|---|
| 517 |         OSTYPE_VOID( Time );
 | 
|---|
| 518 | } // distribution
 | 
|---|
| 519 | 
 | 
|---|
| 520 | // Local Variables: //
 | 
|---|
| 521 | // tab-width: 4 //
 | 
|---|
| 522 | // End: //
 | 
|---|