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