[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 | // |
---|
[3c573e9] | 7 | // iostream.hfa -- |
---|
[86bd7c1f] | 8 | // |
---|
[90c3b1c] | 9 | // Author : Peter A. Buhr |
---|
[86bd7c1f] | 10 | // Created On : Wed May 27 17:56:53 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[2f34fde] | 12 | // Last Modified On : Mon Aug 14 17:35:40 2023 |
---|
| 13 | // Update Count : 508 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[58b6d1b] | 18 | #include "iterator.hfa" |
---|
[2f34fde] | 19 | #include "Exception.hfa" |
---|
[e56cfdb0] | 20 | |
---|
[3c573e9] | 21 | |
---|
[8d321f9] | 22 | // *********************************** ostream *********************************** |
---|
[3c573e9] | 23 | |
---|
| 24 | |
---|
[8a97248] | 25 | forall( ostype & ) |
---|
| 26 | trait basic_ostream { |
---|
[9ebd778] | 27 | // private |
---|
[6c5d92f] | 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 |
---|
[9ebd778] | 38 | // public |
---|
[200fcb3] | 39 | void nlOn( ostype & ); // turn auto-newline state on |
---|
| 40 | void nlOff( ostype & ); // turn auto-newline state off |
---|
[09687aa] | 41 | |
---|
[f5d9c37] | 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 |
---|
[09687aa] | 46 | const char * sepGet( ostype & ); // get separator string |
---|
[e3fea42] | 47 | void sepSet( ostype &, const char [] ); // set separator to string (15 character maximum) |
---|
[09687aa] | 48 | const char * sepGetTuple( ostype & ); // get tuple separator string |
---|
[e3fea42] | 49 | void sepSetTuple( ostype &, const char [] ); // set tuple separator to string (15 character maximum) |
---|
[09687aa] | 50 | |
---|
[85d8153] | 51 | void ends( ostype & ); // end of output statement |
---|
| 52 | int fmt( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); |
---|
| 53 | }; // basic_ostream |
---|
[a0bd9a2] | 54 | |
---|
[8a97248] | 55 | forall( ostype & | basic_ostream( ostype ) ) |
---|
| 56 | trait ostream { |
---|
[85d8153] | 57 | bool fail( ostype & ); // operation failed? |
---|
[00e9be9] | 58 | void clear( ostype & ); |
---|
| 59 | int flush( ostype & ); |
---|
[85d8153] | 60 | void open( ostype &, const char name[], const char mode[] ); |
---|
| 61 | void close( ostype & ); |
---|
[e3fea42] | 62 | ostype & write( ostype &, const char [], size_t ); |
---|
[86f384b] | 63 | }; // ostream |
---|
[90c3b1c] | 64 | |
---|
[8a97248] | 65 | // forall( T ) |
---|
| 66 | // trait writeable { |
---|
[fd54fef] | 67 | // forall( ostype & | ostream( ostype ) ) ostype & ?|?( ostype &, T ); |
---|
[e1780a2] | 68 | // }; // writeable |
---|
| 69 | |
---|
[8a97248] | 70 | forall( T, ostype & | ostream( ostype ) ) |
---|
[dc9dd94] | 71 | trait writeable { |
---|
[09687aa] | 72 | ostype & ?|?( ostype &, T ); |
---|
[86f384b] | 73 | }; // writeable |
---|
[51b7345] | 74 | |
---|
[4e06c1e] | 75 | // implement writable for intrinsic types |
---|
[51b7345] | 76 | |
---|
[94d2544] | 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 | |
---|
[85d8153] | 83 | forall( ostype & | basic_ostream( ostype ) ) { |
---|
[93c2e0a] | 84 | ostype & ?|?( ostype &, bool ); |
---|
[94d2544] | 85 | OSTYPE_VOID( bool ); |
---|
[1e6e08de] | 86 | |
---|
[3ce0d440] | 87 | ostype & ?|?( ostype &, char ); |
---|
[94d2544] | 88 | OSTYPE_VOID( char ); |
---|
[3ce0d440] | 89 | ostype & ?|?( ostype &, signed char ); |
---|
[94d2544] | 90 | OSTYPE_VOID( signed char ); |
---|
[3ce0d440] | 91 | ostype & ?|?( ostype &, unsigned char ); |
---|
[94d2544] | 92 | OSTYPE_VOID( unsigned char ); |
---|
[09687aa] | 93 | |
---|
[3ce0d440] | 94 | ostype & ?|?( ostype &, short int ); |
---|
[94d2544] | 95 | OSTYPE_VOID( short int ); |
---|
[3ce0d440] | 96 | ostype & ?|?( ostype &, unsigned short int ); |
---|
[94d2544] | 97 | OSTYPE_VOID( unsigned short int ); |
---|
[3ce0d440] | 98 | ostype & ?|?( ostype &, int ); |
---|
[94d2544] | 99 | OSTYPE_VOID( int ); |
---|
[3ce0d440] | 100 | ostype & ?|?( ostype &, unsigned int ); |
---|
[94d2544] | 101 | OSTYPE_VOID( unsigned int ); |
---|
[3ce0d440] | 102 | ostype & ?|?( ostype &, long int ); |
---|
[94d2544] | 103 | OSTYPE_VOID( long int ); |
---|
[3ce0d440] | 104 | ostype & ?|?( ostype &, long long int ); |
---|
[94d2544] | 105 | OSTYPE_VOID( long long int ); |
---|
[3ce0d440] | 106 | ostype & ?|?( ostype &, unsigned long int ); |
---|
[94d2544] | 107 | OSTYPE_VOID( unsigned long int ); |
---|
[3ce0d440] | 108 | ostype & ?|?( ostype &, unsigned long long int ); |
---|
[94d2544] | 109 | OSTYPE_VOID( unsigned long long int ); |
---|
[ef3ac46] | 110 | #if defined( __SIZEOF_INT128__ ) |
---|
[bd5b443] | 111 | ostype & ?|?( ostype &, int128 ); |
---|
[94d2544] | 112 | OSTYPE_VOID( int128 ); |
---|
[bd5b443] | 113 | ostype & ?|?( ostype &, unsigned int128 ); |
---|
[94d2544] | 114 | OSTYPE_VOID( unsigned int128 ); |
---|
[ef3ac46] | 115 | #endif // __SIZEOF_INT128__ |
---|
[09687aa] | 116 | |
---|
[65240bb] | 117 | ostype & ?|?( ostype &, float ); |
---|
[94d2544] | 118 | OSTYPE_VOID( float ); |
---|
[3ce0d440] | 119 | ostype & ?|?( ostype &, double ); |
---|
[94d2544] | 120 | OSTYPE_VOID( double ); |
---|
[3ce0d440] | 121 | ostype & ?|?( ostype &, long double ); |
---|
[94d2544] | 122 | OSTYPE_VOID( long double ); |
---|
[09687aa] | 123 | |
---|
[3ce0d440] | 124 | ostype & ?|?( ostype &, float _Complex ); |
---|
[94d2544] | 125 | OSTYPE_VOID( float _Complex ); |
---|
[3ce0d440] | 126 | ostype & ?|?( ostype &, double _Complex ); |
---|
[94d2544] | 127 | OSTYPE_VOID( double _Complex ); |
---|
[3ce0d440] | 128 | ostype & ?|?( ostype &, long double _Complex ); |
---|
[94d2544] | 129 | OSTYPE_VOID( long double _Complex ); |
---|
[09687aa] | 130 | |
---|
[e3fea42] | 131 | ostype & ?|?( ostype &, const char [] ); |
---|
[94d2544] | 132 | OSTYPE_VOID( const char [] ); |
---|
[e7a8f65] | 133 | // ostype & ?|?( ostype &, const char16_t [] ); |
---|
[ef3ac46] | 134 | #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous |
---|
[e7a8f65] | 135 | // ostype & ?|?( ostype &, const char32_t [] ); |
---|
[ef3ac46] | 136 | #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) |
---|
[e7a8f65] | 137 | // ostype & ?|?( ostype &, const wchar_t [] ); |
---|
[3ce0d440] | 138 | ostype & ?|?( ostype &, const void * ); |
---|
[94d2544] | 139 | OSTYPE_VOID( const void * ); |
---|
[3ce0d440] | 140 | |
---|
[0a2e0e21] | 141 | // FIX-ME: does not work so using macros |
---|
[c635047] | 142 | // forall( T | { ostype & ?|?( ostype &, T ); } ) |
---|
| 143 | // void ?|?( ostype & os, T ); |
---|
| 144 | |
---|
[3ce0d440] | 145 | // manipulators |
---|
| 146 | ostype & ?|?( ostype &, ostype & (*)( ostype & ) ); |
---|
[94d2544] | 147 | OSTYPE_VOID( ostype & (*)( ostype & ) ); |
---|
[f5d9c37] | 148 | |
---|
[200fcb3] | 149 | ostype & nl( ostype & ); |
---|
| 150 | ostype & nonl( ostype & ); |
---|
[f5d9c37] | 151 | ostype & nlOn( ostype & ); |
---|
| 152 | ostype & nlOff( ostype & ); |
---|
| 153 | |
---|
| 154 | ostype & sepVal( ostype & ); |
---|
| 155 | ostype & sepTupleVal( ostype & ); |
---|
[3ce0d440] | 156 | ostype & sep( ostype & ); |
---|
[f5d9c37] | 157 | ostype & nosep( ostype & ); |
---|
[3ce0d440] | 158 | ostype & sepOn( ostype & ); |
---|
| 159 | ostype & sepOff( ostype & ); |
---|
[85d8153] | 160 | } // distribution |
---|
| 161 | |
---|
[c443d1d] | 162 | // tuples |
---|
[fd54fef] | 163 | forall( ostype &, T, Params... | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) { |
---|
[200fcb3] | 164 | ostype & ?|?( ostype & os, T arg, Params rest ); |
---|
| 165 | void ?|?( ostype & os, T arg, Params rest ); |
---|
| 166 | } // distribution |
---|
[c443d1d] | 167 | |
---|
[e56cfdb0] | 168 | // writes the range [begin, end) to the given stream |
---|
[fd54fef] | 169 | forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) { |
---|
[200fcb3] | 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 |
---|
[51b7345] | 173 | |
---|
[8d321f9] | 174 | // *********************************** manipulators *********************************** |
---|
[3c573e9] | 175 | |
---|
[a0bd9a2] | 176 | struct _Ostream_Flags { |
---|
[dc9dd94] | 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 |
---|
[a0bd9a2] | 184 | }; |
---|
| 185 | |
---|
[fd54fef] | 186 | forall( T ) |
---|
[3c573e9] | 187 | struct _Ostream_Manip { |
---|
| 188 | T val; // polymorphic base-type |
---|
[424dfc4] | 189 | int wd, pc; // width, precision: signed for computations |
---|
[3c573e9] | 190 | char base; // numeric base / floating-point style |
---|
| 191 | union { |
---|
| 192 | unsigned char all; |
---|
[a0bd9a2] | 193 | _Ostream_Flags flags; |
---|
[3c573e9] | 194 | }; |
---|
| 195 | }; // _Ostream_Manip |
---|
| 196 | |
---|
[8d321f9] | 197 | // *********************************** integral *********************************** |
---|
[3c573e9] | 198 | |
---|
[61c7239] | 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 | |
---|
[94d2544] | 203 | #define INTEGRAL_FMT_DECL( T, CODE ) \ |
---|
[3c573e9] | 204 | static inline { \ |
---|
[61c7239] | 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 } }; } \ |
---|
[891b827] | 208 | _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, CODE, { .all : 0 } }; } \ |
---|
[424dfc4] | 209 | _Ostream_Manip(T) wd( unsigned int w, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, CODE, { .flags.pc : true } }; } \ |
---|
[891b827] | 210 | _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \ |
---|
[424dfc4] | 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; } \ |
---|
[3c573e9] | 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; } \ |
---|
[61c7239] | 216 | _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, CODE, { .flags.sign : true } }; } \ |
---|
[3c573e9] | 217 | _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \ |
---|
[04396aa] | 218 | } /* distribution */ \ |
---|
[85d8153] | 219 | forall( ostype & | basic_ostream( ostype ) ) { \ |
---|
[3c573e9] | 220 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \ |
---|
[c635047] | 221 | OSTYPE_VOID( _Ostream_Manip(T) ); \ |
---|
[61c7239] | 222 | } // ?|? |
---|
[3c573e9] | 223 | |
---|
[94d2544] | 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' ) |
---|
[bd5b443] | 234 | #if defined( __SIZEOF_INT128__ ) |
---|
[94d2544] | 235 | INTEGRAL_FMT_DECL( int128, 'd' ) |
---|
| 236 | INTEGRAL_FMT_DECL( unsigned int128, 'u' ) |
---|
[2b22b5c4] | 237 | #endif // __SIZEOF_INT128__ |
---|
[3c573e9] | 238 | |
---|
[8d321f9] | 239 | // *********************************** floating point *********************************** |
---|
[3c573e9] | 240 | |
---|
| 241 | // Default suffix for values with no fraction is "." |
---|
[94d2544] | 242 | #define FLOATING_POINT_FMT_DECL( T ) \ |
---|
[3c573e9] | 243 | static inline { \ |
---|
[61c7239] | 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 } }; } \ |
---|
[fd4c009] | 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 } }; } \ |
---|
[424dfc4] | 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 } }; } \ |
---|
[fd4c009] | 250 | _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { if ( fmt.flags.eng ) fmt.base = 'f'; fmt.wd = w; return fmt; } \ |
---|
[424dfc4] | 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; } \ |
---|
[3c573e9] | 253 | _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \ |
---|
[61c7239] | 254 | _Ostream_Manip(T) upcase( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'G', { .all : 0 } }; } \ |
---|
[3c573e9] | 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; } \ |
---|
[61c7239] | 257 | _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.sign : true } }; } \ |
---|
[3c573e9] | 258 | _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \ |
---|
[61c7239] | 259 | _Ostream_Manip(T) nodp( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.nobsdp : true } }; } \ |
---|
[3c573e9] | 260 | _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \ |
---|
[fd4c009] | 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; } \ |
---|
[04396aa] | 263 | } /* distribution */ \ |
---|
[85d8153] | 264 | forall( ostype & | basic_ostream( ostype ) ) { \ |
---|
[3c573e9] | 265 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \ |
---|
[c635047] | 266 | OSTYPE_VOID( _Ostream_Manip(T) ); \ |
---|
[61c7239] | 267 | } // ?|? |
---|
[3c573e9] | 268 | |
---|
[94d2544] | 269 | FLOATING_POINT_FMT_DECL( double ) |
---|
| 270 | FLOATING_POINT_FMT_DECL( long double ) |
---|
[3c573e9] | 271 | |
---|
[8d321f9] | 272 | // *********************************** character *********************************** |
---|
[3c573e9] | 273 | |
---|
| 274 | static inline { |
---|
[04396aa] | 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 } }; } |
---|
[891b827] | 279 | _Ostream_Manip(char) & wd( unsigned int w, _Ostream_Manip(char) & fmt ) { fmt.wd = w; return fmt; } |
---|
[3c573e9] | 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 |
---|
[85d8153] | 284 | forall( ostype & | basic_ostream( ostype ) ) { |
---|
[3c573e9] | 285 | ostype & ?|?( ostype & os, _Ostream_Manip(char) f ); |
---|
[c635047] | 286 | OSTYPE_VOID( _Ostream_Manip(char) ); \ |
---|
[61c7239] | 287 | } // ?|? |
---|
[3c573e9] | 288 | |
---|
[8d321f9] | 289 | // *********************************** C string *********************************** |
---|
[3c573e9] | 290 | |
---|
| 291 | static inline { |
---|
[e3fea42] | 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 } }; } |
---|
[424dfc4] | 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 } }; } |
---|
[891b827] | 297 | _Ostream_Manip(const char *) & wd( unsigned int w, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; return fmt; } |
---|
[424dfc4] | 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; } |
---|
[3c573e9] | 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 |
---|
[85d8153] | 302 | forall( ostype & | basic_ostream( ostype ) ) { |
---|
[3c573e9] | 303 | ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ); |
---|
[c635047] | 304 | OSTYPE_VOID( _Ostream_Manip(const char *) ); \ |
---|
[61c7239] | 305 | } // ?|? |
---|
[3c573e9] | 306 | |
---|
| 307 | |
---|
[8d321f9] | 308 | // *********************************** istream *********************************** |
---|
[3c573e9] | 309 | |
---|
[51b7345] | 310 | |
---|
[94d2544] | 311 | #define ISTYPE_VOID( T ) void ?|?( istype &, T ) |
---|
| 312 | #define ISTYPE_VOID_IMPL( T ) \ |
---|
[c635047] | 313 | void ?|?( istype & is, T t ) { \ |
---|
[94d2544] | 314 | (istype &)(is | t); ends( is ); \ |
---|
| 315 | } // ?|? |
---|
| 316 | |
---|
[8a97248] | 317 | forall( istype & ) |
---|
| 318 | trait basic_istream { |
---|
[c8371b5] | 319 | // private |
---|
| 320 | bool getANL$( istype & ); // get scan newline (on/off) |
---|
| 321 | // public |
---|
[0efb269] | 322 | void nlOn( istype & ); // read newline |
---|
| 323 | void nlOff( istype & ); // scan newline |
---|
[e474cf09] | 324 | void ends( istype & os ); // end of output statement |
---|
[ef3ac46] | 325 | int fmt( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); |
---|
| 326 | istype & ungetc( istype &, char ); |
---|
[00e9be9] | 327 | bool eof( istype & ); |
---|
[ef3ac46] | 328 | }; // basic_istream |
---|
| 329 | |
---|
[8a97248] | 330 | forall( istype & | basic_istream( istype ) ) |
---|
| 331 | trait istream { |
---|
[ef3ac46] | 332 | bool fail( istype & ); |
---|
[00e9be9] | 333 | void clear( istype & ); |
---|
[e3fea42] | 334 | void open( istype & is, const char name[] ); |
---|
[09687aa] | 335 | void close( istype & is ); |
---|
[00e9be9] | 336 | istype & read( istype &, char [], size_t ); |
---|
[86f384b] | 337 | }; // istream |
---|
[51b7345] | 338 | |
---|
[8a97248] | 339 | forall( T ) |
---|
| 340 | trait readable { |
---|
[fd54fef] | 341 | forall( istype & | istream( istype ) ) istype & ?|?( istype &, T ); |
---|
[86f384b] | 342 | }; // readable |
---|
[51b7345] | 343 | |
---|
[ef3ac46] | 344 | forall( istype & | basic_istream( istype ) ) { |
---|
[93c2e0a] | 345 | istype & ?|?( istype &, bool & ); |
---|
[94d2544] | 346 | ISTYPE_VOID( bool & ); |
---|
[3ce0d440] | 347 | |
---|
| 348 | istype & ?|?( istype &, char & ); |
---|
[94d2544] | 349 | ISTYPE_VOID( char & ); |
---|
[3ce0d440] | 350 | istype & ?|?( istype &, signed char & ); |
---|
[94d2544] | 351 | ISTYPE_VOID( signed char & ); |
---|
[3ce0d440] | 352 | istype & ?|?( istype &, unsigned char & ); |
---|
[94d2544] | 353 | ISTYPE_VOID( unsigned char & ); |
---|
[3ce0d440] | 354 | |
---|
| 355 | istype & ?|?( istype &, short int & ); |
---|
[94d2544] | 356 | ISTYPE_VOID( short int & ); |
---|
[3ce0d440] | 357 | istype & ?|?( istype &, unsigned short int & ); |
---|
[94d2544] | 358 | ISTYPE_VOID( unsigned short int & ); |
---|
[3ce0d440] | 359 | istype & ?|?( istype &, int & ); |
---|
[94d2544] | 360 | ISTYPE_VOID( int & ); |
---|
[3ce0d440] | 361 | istype & ?|?( istype &, unsigned int & ); |
---|
[94d2544] | 362 | ISTYPE_VOID( unsigned int & ); |
---|
[3ce0d440] | 363 | istype & ?|?( istype &, long int & ); |
---|
[94d2544] | 364 | ISTYPE_VOID( long int & ); |
---|
[3ce0d440] | 365 | istype & ?|?( istype &, unsigned long int & ); |
---|
[94d2544] | 366 | ISTYPE_VOID( unsigned long int & ); |
---|
[21baa40] | 367 | istype & ?|?( istype &, long long int & ); |
---|
[94d2544] | 368 | ISTYPE_VOID( long long int & ); |
---|
[3ce0d440] | 369 | istype & ?|?( istype &, unsigned long long int & ); |
---|
[94d2544] | 370 | ISTYPE_VOID( unsigned long long int & ); |
---|
[ef3ac46] | 371 | #if defined( __SIZEOF_INT128__ ) |
---|
[21baa40] | 372 | istype & ?|?( istype &, int128 & ); |
---|
[94d2544] | 373 | ISTYPE_VOID( int128 & ); |
---|
[21baa40] | 374 | istype & ?|?( istype &, unsigned int128 & ); |
---|
[94d2544] | 375 | ISTYPE_VOID( unsigned int128 & ); |
---|
[ef3ac46] | 376 | #endif // __SIZEOF_INT128__ |
---|
[3ce0d440] | 377 | |
---|
| 378 | istype & ?|?( istype &, float & ); |
---|
[94d2544] | 379 | ISTYPE_VOID( float & ); |
---|
[3ce0d440] | 380 | istype & ?|?( istype &, double & ); |
---|
[94d2544] | 381 | ISTYPE_VOID( double & ); |
---|
[3ce0d440] | 382 | istype & ?|?( istype &, long double & ); |
---|
[94d2544] | 383 | ISTYPE_VOID( long double & ); |
---|
[3ce0d440] | 384 | |
---|
| 385 | istype & ?|?( istype &, float _Complex & ); |
---|
[94d2544] | 386 | ISTYPE_VOID( float _Complex & ); |
---|
[3ce0d440] | 387 | istype & ?|?( istype &, double _Complex & ); |
---|
[94d2544] | 388 | ISTYPE_VOID( double _Complex & ); |
---|
[3ce0d440] | 389 | istype & ?|?( istype &, long double _Complex & ); |
---|
[94d2544] | 390 | ISTYPE_VOID( long double _Complex & ); |
---|
[3ce0d440] | 391 | |
---|
[e3fea42] | 392 | // istype & ?|?( istype &, const char [] ); |
---|
[e7a8f65] | 393 | // istype & ?|?( istype &, char [] ); |
---|
| 394 | // ISTYPE_VOID( char [] ); |
---|
[61c7239] | 395 | |
---|
[3ce0d440] | 396 | // manipulators |
---|
| 397 | istype & ?|?( istype &, istype & (*)( istype & ) ); |
---|
[94d2544] | 398 | ISTYPE_VOID( istype & (*)( istype & ) ); |
---|
[200fcb3] | 399 | istype & nl( istype & is ); |
---|
[3c5dee4] | 400 | istype & nlOn( istype & ); |
---|
| 401 | istype & nlOff( istype & ); |
---|
[ef3ac46] | 402 | } // distribution |
---|
| 403 | |
---|
[2f34fde] | 404 | |
---|
| 405 | // *********************************** exceptions *********************************** |
---|
| 406 | |
---|
| 407 | |
---|
| 408 | ExceptionDecl( cstring_length ); |
---|
| 409 | |
---|
| 410 | |
---|
[8d321f9] | 411 | // *********************************** manipulators *********************************** |
---|
[51b7345] | 412 | |
---|
[3c573e9] | 413 | struct _Istream_Cstr { |
---|
[61c7239] | 414 | char * s; |
---|
[e7a8f65] | 415 | union { |
---|
| 416 | const char * scanset; |
---|
| 417 | char delimit; |
---|
| 418 | }; |
---|
[3c573e9] | 419 | int wd; // width |
---|
[61c7239] | 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 |
---|
[e7a8f65] | 425 | unsigned char delimit:1; // delimit character |
---|
[2f34fde] | 426 | unsigned char rwd:1; // read width |
---|
[61c7239] | 427 | } flags; |
---|
| 428 | }; |
---|
| 429 | }; // _Istream_Cstr |
---|
| 430 | |
---|
[04396aa] | 431 | static inline { |
---|
[e7a8f65] | 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} }; } |
---|
[e3fea42] | 438 | _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; } |
---|
[e7a8f65] | 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} }; } |
---|
[e3fea42] | 441 | _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; } |
---|
[e7a8f65] | 442 | _Istream_Cstr ignore( char s[] ) { return (_Istream_Cstr)@{ s, {0p}, -1, {.flags.ignore : true} }; } |
---|
[04396aa] | 443 | _Istream_Cstr & ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; } |
---|
[e7a8f65] | 444 | _Istream_Cstr wdi( unsigned int wd, char s[] ) { return (_Istream_Cstr)@{ s, {0p}, wd, {.all : 0} }; } |
---|
[2f34fde] | 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 | } |
---|
[e7a8f65] | 449 | // _Istream_Cstr & wdi( unsigned int wd, _Istream_Cstr & fmt ) { fmt.wd = wd; return fmt; } |
---|
[04396aa] | 450 | } // distribution |
---|
[ef3ac46] | 451 | forall( istype & | basic_istream( istype ) ) { |
---|
[e474cf09] | 452 | istype & ?|?( istype & is, _Istream_Cstr f ); |
---|
[c635047] | 453 | ISTYPE_VOID( _Istream_Cstr ); |
---|
[e474cf09] | 454 | } |
---|
[86a8be5] | 455 | |
---|
| 456 | struct _Istream_Char { |
---|
| 457 | bool ignore; // do not change input argument |
---|
| 458 | }; // _Istream_Char |
---|
| 459 | |
---|
[04396aa] | 460 | static inline { |
---|
[2c60c644] | 461 | _Istream_Char ignore( const char ) { return (_Istream_Char)@{ true }; } |
---|
[04396aa] | 462 | _Istream_Char & ignore( _Istream_Char & fmt ) { fmt.ignore = true; return fmt; } |
---|
| 463 | } // distribution |
---|
[ef3ac46] | 464 | forall( istype & | basic_istream( istype ) ) { |
---|
[e474cf09] | 465 | istype & ?|?( istype & is, _Istream_Char f ); |
---|
[c635047] | 466 | ISTYPE_VOID( _Istream_Char ); |
---|
[e474cf09] | 467 | } |
---|
[3c573e9] | 468 | |
---|
[fd54fef] | 469 | forall( T & | sized( T ) ) |
---|
[3c573e9] | 470 | struct _Istream_Manip { |
---|
| 471 | T & val; // polymorphic base-type |
---|
| 472 | int wd; // width |
---|
[61c7239] | 473 | bool ignore; // do not change input argument |
---|
[3c573e9] | 474 | }; // _Istream_Manip |
---|
| 475 | |
---|
[94d2544] | 476 | #define INPUT_FMT_DECL( T ) \ |
---|
[04396aa] | 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; } \ |
---|
[e7a8f65] | 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; } \ |
---|
[04396aa] | 482 | } /* distribution */ \ |
---|
[ef3ac46] | 483 | forall( istype & | basic_istream( istype ) ) { \ |
---|
[3c573e9] | 484 | istype & ?|?( istype & is, _Istream_Manip(T) f ); \ |
---|
[c635047] | 485 | ISTYPE_VOID( _Istream_Manip(T) ); \ |
---|
[61c7239] | 486 | } // ?|? |
---|
[3c573e9] | 487 | |
---|
[94d2544] | 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 ) |
---|
[3c573e9] | 506 | |
---|
| 507 | |
---|
[8d321f9] | 508 | // *********************************** time *********************************** |
---|
[3c573e9] | 509 | |
---|
[10a97adb] | 510 | |
---|
[200fcb3] | 511 | #include <time_t.hfa> // Duration (constructors) / Time (constructors) |
---|
[10a97adb] | 512 | |
---|
[fd54fef] | 513 | forall( ostype & | ostream( ostype ) ) { |
---|
[200fcb3] | 514 | ostype & ?|?( ostype & os, Duration dur ); |
---|
[c635047] | 515 | OSTYPE_VOID( Duration ); |
---|
[200fcb3] | 516 | ostype & ?|?( ostype & os, Time time ); |
---|
[c635047] | 517 | OSTYPE_VOID( Time ); |
---|
[200fcb3] | 518 | } // distribution |
---|
[10a97adb] | 519 | |
---|
[86bd7c1f] | 520 | // Local Variables: // |
---|
| 521 | // tab-width: 4 // |
---|
| 522 | // End: // |
---|