[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 |
---|
[1a7203d] | 12 | // Last Modified On : Sun Oct 13 10:49:40 2024 |
---|
| 13 | // Update Count : 765 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[58b6d1b] | 18 | #include "iterator.hfa" |
---|
[2f34fde] | 19 | #include "Exception.hfa" |
---|
[e56cfdb0] | 20 | |
---|
[8d321f9] | 21 | // *********************************** ostream *********************************** |
---|
[3c573e9] | 22 | |
---|
[8a97248] | 23 | forall( ostype & ) |
---|
| 24 | trait basic_ostream { |
---|
[9ebd778] | 25 | // private |
---|
[6c5d92f] | 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 |
---|
[d0cfcbe1] | 31 | bool getNL$( ostype & ); // get newline |
---|
| 32 | bool setNL$( ostype &, bool ); // set newline |
---|
[6c5d92f] | 33 | bool getANL$( ostype & ); // get auto newline (on/off) |
---|
[d0cfcbe1] | 34 | bool setANL$( ostype &, bool ); // set auto newline (on/off), and return previous state |
---|
[6c5d92f] | 35 | bool getPrt$( ostype & ); // get fmt called in output cascade |
---|
[d0cfcbe1] | 36 | bool setPrt$( ostype &, bool ); // set fmt called in output cascade |
---|
[9ebd778] | 37 | // public |
---|
[200fcb3] | 38 | void nlOn( ostype & ); // turn auto-newline state on |
---|
| 39 | void nlOff( ostype & ); // turn auto-newline state off |
---|
[09687aa] | 40 | |
---|
[f5d9c37] | 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 |
---|
[09687aa] | 45 | const char * sepGet( ostype & ); // get separator string |
---|
[e3fea42] | 46 | void sepSet( ostype &, const char [] ); // set separator to string (15 character maximum) |
---|
[09687aa] | 47 | const char * sepGetTuple( ostype & ); // get tuple separator string |
---|
[e3fea42] | 48 | void sepSetTuple( ostype &, const char [] ); // set tuple separator to string (15 character maximum) |
---|
[09687aa] | 49 | |
---|
[85d8153] | 50 | void ends( ostype & ); // end of output statement |
---|
| 51 | int fmt( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); |
---|
| 52 | }; // basic_ostream |
---|
[a0bd9a2] | 53 | |
---|
[8a97248] | 54 | forall( ostype & | basic_ostream( ostype ) ) |
---|
| 55 | trait ostream { |
---|
[85d8153] | 56 | bool fail( ostype & ); // operation failed? |
---|
[00e9be9] | 57 | void clear( ostype & ); |
---|
| 58 | int flush( ostype & ); |
---|
[85d8153] | 59 | void open( ostype &, const char name[], const char mode[] ); |
---|
| 60 | void close( ostype & ); |
---|
[e3fea42] | 61 | ostype & write( ostype &, const char [], size_t ); |
---|
[86f384b] | 62 | }; // ostream |
---|
[90c3b1c] | 63 | |
---|
[8a97248] | 64 | // forall( T ) |
---|
| 65 | // trait writeable { |
---|
[fd54fef] | 66 | // forall( ostype & | ostream( ostype ) ) ostype & ?|?( ostype &, T ); |
---|
[e1780a2] | 67 | // }; // writeable |
---|
| 68 | |
---|
[8a97248] | 69 | forall( T, ostype & | ostream( ostype ) ) |
---|
[dc9dd94] | 70 | trait writeable { |
---|
[09687aa] | 71 | ostype & ?|?( ostype &, T ); |
---|
[86f384b] | 72 | }; // writeable |
---|
[51b7345] | 73 | |
---|
[4e06c1e] | 74 | // implement writable for intrinsic types |
---|
[51b7345] | 75 | |
---|
[94d2544] | 76 | #define OSTYPE_VOID( T ) void ?|?( ostype &, T ) |
---|
[b12e4ad] | 77 | #define OSTYPE_VOID_IMPL( os, T ) \ |
---|
[94d2544] | 78 | void ?|?( ostype & os, T t ) { \ |
---|
| 79 | (ostype &)(os | t); ends( os ); \ |
---|
| 80 | } // ?|? |
---|
| 81 | |
---|
[85d8153] | 82 | forall( ostype & | basic_ostream( ostype ) ) { |
---|
[93c2e0a] | 83 | ostype & ?|?( ostype &, bool ); |
---|
[94d2544] | 84 | OSTYPE_VOID( bool ); |
---|
[1e6e08de] | 85 | |
---|
[3ce0d440] | 86 | ostype & ?|?( ostype &, char ); |
---|
[94d2544] | 87 | OSTYPE_VOID( char ); |
---|
[3ce0d440] | 88 | ostype & ?|?( ostype &, signed char ); |
---|
[94d2544] | 89 | OSTYPE_VOID( signed char ); |
---|
[3ce0d440] | 90 | ostype & ?|?( ostype &, unsigned char ); |
---|
[94d2544] | 91 | OSTYPE_VOID( unsigned char ); |
---|
[09687aa] | 92 | |
---|
[3ce0d440] | 93 | ostype & ?|?( ostype &, short int ); |
---|
[94d2544] | 94 | OSTYPE_VOID( short int ); |
---|
[3ce0d440] | 95 | ostype & ?|?( ostype &, unsigned short int ); |
---|
[94d2544] | 96 | OSTYPE_VOID( unsigned short int ); |
---|
[3ce0d440] | 97 | ostype & ?|?( ostype &, int ); |
---|
[94d2544] | 98 | OSTYPE_VOID( int ); |
---|
[3ce0d440] | 99 | ostype & ?|?( ostype &, unsigned int ); |
---|
[94d2544] | 100 | OSTYPE_VOID( unsigned int ); |
---|
[3ce0d440] | 101 | ostype & ?|?( ostype &, long int ); |
---|
[94d2544] | 102 | OSTYPE_VOID( long int ); |
---|
[3ce0d440] | 103 | ostype & ?|?( ostype &, long long int ); |
---|
[94d2544] | 104 | OSTYPE_VOID( long long int ); |
---|
[3ce0d440] | 105 | ostype & ?|?( ostype &, unsigned long int ); |
---|
[94d2544] | 106 | OSTYPE_VOID( unsigned long int ); |
---|
[3ce0d440] | 107 | ostype & ?|?( ostype &, unsigned long long int ); |
---|
[94d2544] | 108 | OSTYPE_VOID( unsigned long long int ); |
---|
[ef3ac46] | 109 | #if defined( __SIZEOF_INT128__ ) |
---|
[bd5b443] | 110 | ostype & ?|?( ostype &, int128 ); |
---|
[94d2544] | 111 | OSTYPE_VOID( int128 ); |
---|
[bd5b443] | 112 | ostype & ?|?( ostype &, unsigned int128 ); |
---|
[94d2544] | 113 | OSTYPE_VOID( unsigned int128 ); |
---|
[ef3ac46] | 114 | #endif // __SIZEOF_INT128__ |
---|
[09687aa] | 115 | |
---|
[65240bb] | 116 | ostype & ?|?( ostype &, float ); |
---|
[94d2544] | 117 | OSTYPE_VOID( float ); |
---|
[3ce0d440] | 118 | ostype & ?|?( ostype &, double ); |
---|
[94d2544] | 119 | OSTYPE_VOID( double ); |
---|
[3ce0d440] | 120 | ostype & ?|?( ostype &, long double ); |
---|
[94d2544] | 121 | OSTYPE_VOID( long double ); |
---|
[09687aa] | 122 | |
---|
[3ce0d440] | 123 | ostype & ?|?( ostype &, float _Complex ); |
---|
[94d2544] | 124 | OSTYPE_VOID( float _Complex ); |
---|
[3ce0d440] | 125 | ostype & ?|?( ostype &, double _Complex ); |
---|
[94d2544] | 126 | OSTYPE_VOID( double _Complex ); |
---|
[3ce0d440] | 127 | ostype & ?|?( ostype &, long double _Complex ); |
---|
[94d2544] | 128 | OSTYPE_VOID( long double _Complex ); |
---|
[09687aa] | 129 | |
---|
[e3fea42] | 130 | ostype & ?|?( ostype &, const char [] ); |
---|
[94d2544] | 131 | OSTYPE_VOID( const char [] ); |
---|
[e7a8f65] | 132 | // ostype & ?|?( ostype &, const char16_t [] ); |
---|
[ef3ac46] | 133 | #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous |
---|
[e7a8f65] | 134 | // ostype & ?|?( ostype &, const char32_t [] ); |
---|
[ef3ac46] | 135 | #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) |
---|
[e7a8f65] | 136 | // ostype & ?|?( ostype &, const wchar_t [] ); |
---|
[3ce0d440] | 137 | ostype & ?|?( ostype &, const void * ); |
---|
[94d2544] | 138 | OSTYPE_VOID( const void * ); |
---|
[3ce0d440] | 139 | |
---|
[0a2e0e21] | 140 | // FIX-ME: does not work so using macros |
---|
[c635047] | 141 | // forall( T | { ostype & ?|?( ostype &, T ); } ) |
---|
| 142 | // void ?|?( ostype & os, T ); |
---|
| 143 | |
---|
[3ce0d440] | 144 | // manipulators |
---|
| 145 | ostype & ?|?( ostype &, ostype & (*)( ostype & ) ); |
---|
[94d2544] | 146 | OSTYPE_VOID( ostype & (*)( ostype & ) ); |
---|
[f5d9c37] | 147 | |
---|
[200fcb3] | 148 | ostype & nl( ostype & ); |
---|
| 149 | ostype & nonl( ostype & ); |
---|
[f5d9c37] | 150 | ostype & nlOn( ostype & ); |
---|
| 151 | ostype & nlOff( ostype & ); |
---|
| 152 | |
---|
| 153 | ostype & sepVal( ostype & ); |
---|
| 154 | ostype & sepTupleVal( ostype & ); |
---|
[3ce0d440] | 155 | ostype & sep( ostype & ); |
---|
[f5d9c37] | 156 | ostype & nosep( ostype & ); |
---|
[3ce0d440] | 157 | ostype & sepOn( ostype & ); |
---|
| 158 | ostype & sepOff( ostype & ); |
---|
[85d8153] | 159 | } // distribution |
---|
| 160 | |
---|
[c443d1d] | 161 | // tuples |
---|
[c0363be] | 162 | forall( ostype &, T, List ... | writeable( T, ostype ) | { ostype & ?|?( ostype &, List ); } ) { |
---|
| 163 | ostype & ?|?( ostype & os, T arg, List rest ); |
---|
| 164 | void ?|?( ostype & os, T arg, List rest ); |
---|
[200fcb3] | 165 | } // distribution |
---|
[c443d1d] | 166 | |
---|
[e56cfdb0] | 167 | // writes the range [begin, end) to the given stream |
---|
[fd54fef] | 168 | forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) { |
---|
[200fcb3] | 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 |
---|
[51b7345] | 172 | |
---|
[8d321f9] | 173 | // *********************************** manipulators *********************************** |
---|
[3c573e9] | 174 | |
---|
[a0bd9a2] | 175 | struct _Ostream_Flags { |
---|
[dc9dd94] | 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 |
---|
[a0bd9a2] | 183 | }; |
---|
| 184 | |
---|
[96ef156] | 185 | // FIXME: Should be an anonymous inner union of _Ostream_Manip. |
---|
| 186 | // Hoisting manually to work around warning of #294. |
---|
| 187 | union _Ostream_Manip_Mode { |
---|
| 188 | unsigned char all; |
---|
| 189 | _Ostream_Flags flags; |
---|
| 190 | }; |
---|
| 191 | |
---|
[fd54fef] | 192 | forall( T ) |
---|
[3c573e9] | 193 | struct _Ostream_Manip { |
---|
| 194 | T val; // polymorphic base-type |
---|
[424dfc4] | 195 | int wd, pc; // width, precision: signed for computations |
---|
[3c573e9] | 196 | char base; // numeric base / floating-point style |
---|
[96ef156] | 197 | inline _Ostream_Manip_Mode; |
---|
[3c573e9] | 198 | }; // _Ostream_Manip |
---|
| 199 | |
---|
[8d321f9] | 200 | // *********************************** integral *********************************** |
---|
[3c573e9] | 201 | |
---|
[94d2544] | 202 | #define INTEGRAL_FMT_DECL( T, CODE ) \ |
---|
[3c573e9] | 203 | static inline { \ |
---|
[1a7203d] | 204 | _Ostream_Manip(T) bin( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'b', { .all = 0 } }; } \ |
---|
| 205 | _Ostream_Manip(T) oct( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; } \ |
---|
| 206 | _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; } \ |
---|
| 207 | _Ostream_Manip(T) wd( unsigned int wd, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = 0, .base = CODE, { .all = 0 } }; } \ |
---|
| 208 | _Ostream_Manip(T) wd( unsigned int wd, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = pc, .base = CODE, { .flags.pc = true } }; } \ |
---|
[8abe4090] | 209 | _Ostream_Manip(T) & wd( unsigned int wd, _Ostream_Manip(T) & fmt ) { fmt.wd = wd; return fmt; } \ |
---|
| 210 | _Ostream_Manip(T) & wd( unsigned int wd, unsigned int pc, _Ostream_Manip(T) & fmt ) { fmt.wd = wd; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \ |
---|
[3c573e9] | 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; } \ |
---|
[1a7203d] | 215 | _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = CODE, { .flags.sign = true } }; } \ |
---|
[3c573e9] | 216 | _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \ |
---|
[04396aa] | 217 | } /* distribution */ \ |
---|
[85d8153] | 218 | forall( ostype & | basic_ostream( ostype ) ) { \ |
---|
[3c573e9] | 219 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \ |
---|
[c635047] | 220 | OSTYPE_VOID( _Ostream_Manip(T) ); \ |
---|
[61c7239] | 221 | } // ?|? |
---|
[3c573e9] | 222 | |
---|
[94d2544] | 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' ) |
---|
[bd5b443] | 233 | #if defined( __SIZEOF_INT128__ ) |
---|
[94d2544] | 234 | INTEGRAL_FMT_DECL( int128, 'd' ) |
---|
| 235 | INTEGRAL_FMT_DECL( unsigned int128, 'u' ) |
---|
[2b22b5c4] | 236 | #endif // __SIZEOF_INT128__ |
---|
[3c573e9] | 237 | |
---|
[8d321f9] | 238 | // *********************************** floating point *********************************** |
---|
[3c573e9] | 239 | |
---|
| 240 | // Default suffix for values with no fraction is "." |
---|
[94d2544] | 241 | #define FLOATING_POINT_FMT_DECL( T ) \ |
---|
[3c573e9] | 242 | static inline { \ |
---|
[1a7203d] | 243 | _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'a', { .all = 0 } }; } \ |
---|
| 244 | _Ostream_Manip(T) sci( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'e', { .all = 0 } }; } \ |
---|
| 245 | _Ostream_Manip(T) eng( T val ) { return (_Ostream_Manip(T))@{ .val = val, 1, -1, .base = 'g', { .flags.eng = true } }; } \ |
---|
| 246 | _Ostream_Manip(T) wd( unsigned int wd, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = 0, .base = 'g', { .all = 0 } }; } \ |
---|
| 247 | _Ostream_Manip(T) wd( unsigned int wd, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = pc, .base = 'f', { .flags.pc = true } }; } \ |
---|
| 248 | _Ostream_Manip(T) ws( unsigned int wd, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = pc, .base = 'g', { .flags.pc = true } }; } \ |
---|
[8abe4090] | 249 | _Ostream_Manip(T) & wd( unsigned int wd, _Ostream_Manip(T) & fmt ) { if ( fmt.flags.eng ) fmt.base = 'f'; fmt.wd = wd; return fmt; } \ |
---|
| 250 | _Ostream_Manip(T) & wd( unsigned int wd, unsigned int pc, _Ostream_Manip(T) & fmt ) { \ |
---|
| 251 | if ( fmt.flags.eng ) fmt.base = 'f'; fmt.wd = wd; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \ |
---|
| 252 | _Ostream_Manip(T) & ws( unsigned int wd, unsigned int pc, _Ostream_Manip(T) & fmt ) { fmt.wd = wd; 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; } \ |
---|
[1a7203d] | 254 | _Ostream_Manip(T) upcase( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = '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; } \ |
---|
[1a7203d] | 257 | _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'g', { .flags.sign = true } }; } \ |
---|
[3c573e9] | 258 | _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \ |
---|
[1a7203d] | 259 | _Ostream_Manip(T) nodp( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'g', { .flags.nobsdp = true } }; } \ |
---|
[3c573e9] | 260 | _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \ |
---|
[1a7203d] | 261 | _Ostream_Manip(T) unit( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'g', { .flags.nobsdp = true } }; } \ |
---|
[fd4c009] | 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 { |
---|
[1a7203d] | 275 | _Ostream_Manip(char) bin( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'b', { .all = 0 } }; } |
---|
| 276 | _Ostream_Manip(char) oct( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; } |
---|
| 277 | _Ostream_Manip(char) hex( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; } |
---|
| 278 | _Ostream_Manip(char) wd( unsigned int wd, char c ) { return (_Ostream_Manip(char))@{ c, wd, 0, .base = 'c', { .all = 0 } }; } |
---|
[8abe4090] | 279 | _Ostream_Manip(char) & wd( unsigned int wd, _Ostream_Manip(char) & fmt ) { fmt.wd = wd; 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 { |
---|
[1a7203d] | 292 | _Ostream_Manip(const char *) bin( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 'b', { .all = 0 } }; } |
---|
| 293 | _Ostream_Manip(const char *) oct( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; } |
---|
| 294 | _Ostream_Manip(const char *) hex( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; } |
---|
| 295 | _Ostream_Manip(const char *) wd( unsigned int wd, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, wd, 0, .base = 's', { .all = 0 } }; } |
---|
| 296 | _Ostream_Manip(const char *) wd( unsigned int wd, unsigned int pc, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, .wd = wd, .pc = pc, .base = 's', { .flags.pc = true } }; } |
---|
[8abe4090] | 297 | _Ostream_Manip(const char *) & wd( unsigned int wd, _Ostream_Manip(const char *) & fmt ) { fmt.wd = wd; return fmt; } |
---|
| 298 | _Ostream_Manip(const char *) & wd( unsigned int wd, unsigned int pc, _Ostream_Manip(const char *) & fmt ) { fmt.wd = wd; 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 | |
---|
[8d321f9] | 307 | // *********************************** istream *********************************** |
---|
[3c573e9] | 308 | |
---|
[8a97248] | 309 | forall( istype & ) |
---|
| 310 | trait basic_istream { |
---|
[c8371b5] | 311 | // private |
---|
| 312 | bool getANL$( istype & ); // get scan newline (on/off) |
---|
[d0cfcbe1] | 313 | bool setANL$( istype &, bool ); // set scan newline (on/off) |
---|
[c8371b5] | 314 | // public |
---|
[0efb269] | 315 | void nlOn( istype & ); // read newline |
---|
| 316 | void nlOff( istype & ); // scan newline |
---|
[ef3ac46] | 317 | int fmt( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); |
---|
[a1a1f37d] | 318 | istype & ungetc( char, istype & ); |
---|
[00e9be9] | 319 | bool eof( istype & ); |
---|
[e0dc038] | 320 | void clear( istype & ); |
---|
[ef3ac46] | 321 | }; // basic_istream |
---|
| 322 | |
---|
[8a97248] | 323 | forall( istype & | basic_istream( istype ) ) |
---|
| 324 | trait istream { |
---|
[ef3ac46] | 325 | bool fail( istype & ); |
---|
[e0dc038] | 326 | void open( istype & is, const char name[], const char mode[] ); |
---|
[e3fea42] | 327 | void open( istype & is, const char name[] ); |
---|
[09687aa] | 328 | void close( istype & is ); |
---|
[00e9be9] | 329 | istype & read( istype &, char [], size_t ); |
---|
[86f384b] | 330 | }; // istream |
---|
[51b7345] | 331 | |
---|
[8a97248] | 332 | forall( T ) |
---|
| 333 | trait readable { |
---|
[fd54fef] | 334 | forall( istype & | istream( istype ) ) istype & ?|?( istype &, T ); |
---|
[86f384b] | 335 | }; // readable |
---|
[51b7345] | 336 | |
---|
[ef3ac46] | 337 | forall( istype & | basic_istream( istype ) ) { |
---|
[93c2e0a] | 338 | istype & ?|?( istype &, bool & ); |
---|
[3ce0d440] | 339 | |
---|
| 340 | istype & ?|?( istype &, char & ); |
---|
| 341 | istype & ?|?( istype &, signed char & ); |
---|
| 342 | istype & ?|?( istype &, unsigned char & ); |
---|
| 343 | |
---|
| 344 | istype & ?|?( istype &, short int & ); |
---|
| 345 | istype & ?|?( istype &, unsigned short int & ); |
---|
| 346 | istype & ?|?( istype &, int & ); |
---|
| 347 | istype & ?|?( istype &, unsigned int & ); |
---|
| 348 | istype & ?|?( istype &, long int & ); |
---|
| 349 | istype & ?|?( istype &, unsigned long int & ); |
---|
[21baa40] | 350 | istype & ?|?( istype &, long long int & ); |
---|
[3ce0d440] | 351 | istype & ?|?( istype &, unsigned long long int & ); |
---|
[ef3ac46] | 352 | #if defined( __SIZEOF_INT128__ ) |
---|
[21baa40] | 353 | istype & ?|?( istype &, int128 & ); |
---|
| 354 | istype & ?|?( istype &, unsigned int128 & ); |
---|
[ef3ac46] | 355 | #endif // __SIZEOF_INT128__ |
---|
[3ce0d440] | 356 | |
---|
| 357 | istype & ?|?( istype &, float & ); |
---|
| 358 | istype & ?|?( istype &, double & ); |
---|
| 359 | istype & ?|?( istype &, long double & ); |
---|
| 360 | |
---|
| 361 | istype & ?|?( istype &, float _Complex & ); |
---|
| 362 | istype & ?|?( istype &, double _Complex & ); |
---|
| 363 | istype & ?|?( istype &, long double _Complex & ); |
---|
| 364 | |
---|
[c015e2d] | 365 | // This is too restrictive as it prevents building a format in a string and using that format. |
---|
| 366 | // inline istype & ?|?( istype &, char [] ) { // possible error, too restrictive to change |
---|
| 367 | // _Static_assert( false, "reading a character array without a maximum length is unsafe. Use input manipulator \"wdi( N, s )\", where \"char s[N]\" or fmt( s )." ); |
---|
| 368 | // } |
---|
| 369 | istype & ?|?( istype &, const char [] ); // match text |
---|
[61c7239] | 370 | |
---|
[3ce0d440] | 371 | // manipulators |
---|
| 372 | istype & ?|?( istype &, istype & (*)( istype & ) ); |
---|
[200fcb3] | 373 | istype & nl( istype & is ); |
---|
[3c5dee4] | 374 | istype & nlOn( istype & ); |
---|
| 375 | istype & nlOff( istype & ); |
---|
[ef3ac46] | 376 | } // distribution |
---|
| 377 | |
---|
[2f34fde] | 378 | // *********************************** exceptions *********************************** |
---|
| 379 | |
---|
[3ac5fd8] | 380 | ExceptionDecl( end_of_file ); // read encounters end of file |
---|
[211def2] | 381 | ExceptionDecl( missing_data ); // read finds no appropriate data |
---|
| 382 | ExceptionDecl( cstring_length ); // character string size exceeded |
---|
| 383 | ExceptionDecl( data_range ); // value too large for numerical type |
---|
[2f34fde] | 384 | |
---|
[8d321f9] | 385 | // *********************************** manipulators *********************************** |
---|
[51b7345] | 386 | |
---|
[e0dc038] | 387 | // skip does not compose with other C string manipulators. |
---|
[0f107e4] | 388 | struct _Istream_Cskip { |
---|
| 389 | const char * scanset; |
---|
| 390 | unsigned wd; // scan width |
---|
| 391 | }; // _Istream_Cskip |
---|
| 392 | |
---|
| 393 | static inline { |
---|
[1a7203d] | 394 | _Istream_Cskip skip( const char scanset[] ) { return (_Istream_Cskip)@{ .scanset = scanset, .wd = 0 }; } |
---|
| 395 | _Istream_Cskip skip( unsigned int wd ) { return (_Istream_Cskip)@{ .scanset = 0p, .wd = wd }; } |
---|
[0f107e4] | 396 | } // distribution |
---|
| 397 | |
---|
[38de914] | 398 | struct _Istream_str_base { |
---|
[e7a8f65] | 399 | union { |
---|
| 400 | const char * scanset; |
---|
[4aae2bd] | 401 | char delimiters[3]; // [0] => left, [1] => right |
---|
[e7a8f65] | 402 | }; |
---|
[3c573e9] | 403 | int wd; // width |
---|
[61c7239] | 404 | union { |
---|
| 405 | unsigned char all; |
---|
| 406 | struct { |
---|
| 407 | unsigned char ignore:1; // do not change input argument |
---|
| 408 | unsigned char inex:1; // include/exclude characters in scanset |
---|
[baa1d5d] | 409 | unsigned char delimiter:1; // delimit character(s) |
---|
[2f34fde] | 410 | unsigned char rwd:1; // read width |
---|
[61c7239] | 411 | } flags; |
---|
| 412 | }; |
---|
[38de914] | 413 | }; // _Istream_str_base |
---|
| 414 | |
---|
[5764204] | 415 | struct _Istream_Cwidth { |
---|
[38de914] | 416 | char * s; |
---|
| 417 | inline _Istream_str_base; |
---|
[211def2] | 418 | }; // _Istream_Cwidth |
---|
[61c7239] | 419 | |
---|
[5764204] | 420 | // Restrict nesting of input manipulators to those combinations that make sense. |
---|
| 421 | |
---|
[8c13ca8] | 422 | struct _Istream_Cquoted { |
---|
[5764204] | 423 | _Istream_Cwidth cstr; |
---|
[8c13ca8] | 424 | }; // _Istream_Cquoted |
---|
| 425 | |
---|
[211def2] | 426 | struct _Istream_Cstr { |
---|
| 427 | _Istream_Cwidth cstr; |
---|
| 428 | }; // _Istream_Cstr |
---|
| 429 | |
---|
[04396aa] | 430 | static inline { |
---|
[5764204] | 431 | // width must include room for null terminator, (gcc) scanf does not allow a 0 width => wd > 1 (1 char and null) and rd > 0 (1 char); |
---|
| 432 | _Istream_Cwidth wdi( unsigned int wd, char s[] ) { |
---|
[211def2] | 433 | if ( wd <= 1 ) throwResume ExceptionInst( cstring_length ); // minimum 1 character and null terminator |
---|
[1a7203d] | 434 | return (_Istream_Cwidth)@{ .s = s, { {.scanset = 0p}, .wd = wd, {.all = 0} } }; |
---|
[5764204] | 435 | } |
---|
| 436 | _Istream_Cwidth wdi( unsigned int wd, unsigned int rwd, char s[] ) { |
---|
[211def2] | 437 | if ( wd <= 1 || wd <= rwd ) throwResume ExceptionInst( cstring_length ); // minimum 1 character, null terminator, plus subset |
---|
[1a7203d] | 438 | return (_Istream_Cwidth)@{ .s = s, { {.scanset = 0p}, .wd = rwd, {.flags.rwd = true} } }; |
---|
[5ad2c6c7] | 439 | } |
---|
[211def2] | 440 | _Istream_Cstr & getline( _Istream_Cwidth & f, const char delimiter = '\n' ) { |
---|
| 441 | f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Cstr &)f; |
---|
| 442 | } |
---|
[baa1d5d] | 443 | _Istream_Cquoted quoted( char & ch, const char Ldelimiter = '\'', const char Rdelimiter = '\0' ) { |
---|
[1a7203d] | 444 | return (_Istream_Cquoted)@{ { .s = &ch, { {.delimiters = { Ldelimiter, Rdelimiter, '\1' }}, .wd = 1, {.flags.rwd = true} } } }; |
---|
[baa1d5d] | 445 | } |
---|
[5764204] | 446 | _Istream_Cquoted & quoted( _Istream_Cwidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) { |
---|
| 447 | f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0'; |
---|
| 448 | return (_Istream_Cquoted &)f; |
---|
[8c13ca8] | 449 | } |
---|
[5764204] | 450 | _Istream_Cstr & incl( const char scanset[], _Istream_Cwidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Cstr &)f; } |
---|
| 451 | _Istream_Cstr & excl( const char scanset[], _Istream_Cwidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Cstr &)f; } |
---|
[1a7203d] | 452 | _Istream_Cstr ignore( const char s[] ) { return (_Istream_Cstr)@{ { .s = (char *)s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } } }; } |
---|
[5764204] | 453 | _Istream_Cstr & ignore( _Istream_Cwidth & f ) { f.flags.ignore = true; return (_Istream_Cstr &)f; } |
---|
| 454 | _Istream_Cquoted & ignore( _Istream_Cquoted & f ) { f.cstr.flags.ignore = true; return (_Istream_Cquoted &)f; } |
---|
| 455 | _Istream_Cstr & ignore( _Istream_Cstr & f ) { f.cstr.flags.ignore = true; return (_Istream_Cstr &)f; } |
---|
[04396aa] | 456 | } // distribution |
---|
[8c13ca8] | 457 | |
---|
[ef3ac46] | 458 | forall( istype & | basic_istream( istype ) ) { |
---|
[8c13ca8] | 459 | istype & ?|?( istype & is, _Istream_Cskip f ); |
---|
| 460 | istype & ?|?( istype & is, _Istream_Cquoted f ); |
---|
[5764204] | 461 | istype & ?|?( istype & is, _Istream_Cstr f ); |
---|
[211def2] | 462 | static inline istype & ?|?( istype & is, _Istream_Cwidth f ) { return is | *(_Istream_Cstr *)&f; } |
---|
[8c13ca8] | 463 | } // distribution |
---|
[86a8be5] | 464 | |
---|
[96ef156] | 465 | // FIXME: `| sized(T)` seems to be working around a bug, but it is logically unnecessary. |
---|
| 466 | // Including sized(T) causes a warning that is telling us to get rid of it. |
---|
| 467 | #pragma GCC diagnostic push |
---|
| 468 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
---|
| 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 |
---|
[96ef156] | 475 | #pragma GCC diagnostic pop |
---|
[3c573e9] | 476 | |
---|
[94d2544] | 477 | #define INPUT_FMT_DECL( T ) \ |
---|
[04396aa] | 478 | static inline { \ |
---|
[1a7203d] | 479 | _Istream_Manip(T) wdi( unsigned int wd, T & val ) { return (_Istream_Manip(T))@{ .val = val, .wd = wd, .ignore = false }; } \ |
---|
| 480 | _Istream_Manip(T) ignore( const T & val ) { return (_Istream_Manip(T))@{ .val = (T &)val, .wd = -1, .ignore = true }; } \ |
---|
[04396aa] | 481 | _Istream_Manip(T) & ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \ |
---|
| 482 | } /* distribution */ \ |
---|
[ef3ac46] | 483 | forall( istype & | basic_istream( istype ) ) { \ |
---|
[3c573e9] | 484 | istype & ?|?( istype & is, _Istream_Manip(T) f ); \ |
---|
[61c7239] | 485 | } // ?|? |
---|
[3c573e9] | 486 | |
---|
[baa1d5d] | 487 | INPUT_FMT_DECL( char ) |
---|
[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 | |
---|
[8d321f9] | 507 | // *********************************** time *********************************** |
---|
[3c573e9] | 508 | |
---|
[200fcb3] | 509 | #include <time_t.hfa> // Duration (constructors) / Time (constructors) |
---|
[10a97adb] | 510 | |
---|
[fd54fef] | 511 | forall( ostype & | ostream( ostype ) ) { |
---|
[200fcb3] | 512 | ostype & ?|?( ostype & os, Duration dur ); |
---|
[c635047] | 513 | OSTYPE_VOID( Duration ); |
---|
[200fcb3] | 514 | ostype & ?|?( ostype & os, Time time ); |
---|
[c635047] | 515 | OSTYPE_VOID( Time ); |
---|
[200fcb3] | 516 | } // distribution |
---|
[10a97adb] | 517 | |
---|
[eae8b37] | 518 | forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial(E) ) |
---|
| 519 | istype & ?|?( istype &, E & ); |
---|
| 520 | |
---|
| 521 | forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { |
---|
| 522 | ostype & ?|?( ostype &, E ); |
---|
| 523 | OSTYPE_VOID( E ); |
---|
| 524 | } |
---|
| 525 | |
---|
[86bd7c1f] | 526 | // Local Variables: // |
---|
| 527 | // tab-width: 4 // |
---|
| 528 | // End: // |
---|