[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 |
---|
[bd5b443] | 12 | // Last Modified On : Thu Feb 20 15:30:56 2020 |
---|
| 13 | // Update Count : 337 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[58b6d1b] | 18 | #include "iterator.hfa" |
---|
[e56cfdb0] | 19 | |
---|
[3c573e9] | 20 | |
---|
[65240bb] | 21 | //*********************************** ostream *********************************** |
---|
[3c573e9] | 22 | |
---|
| 23 | |
---|
[4040425] | 24 | trait ostream( dtype ostype ) { |
---|
[9ebd778] | 25 | // private |
---|
[d1a9ff5] | 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 & ); // check newline |
---|
| 32 | void $setNL( ostype &, bool ); // saw newline |
---|
| 33 | bool $getANL( ostype & ); // get auto newline (on/off) |
---|
| 34 | bool $getPrt( ostype & ); // get fmt called in output cascade |
---|
| 35 | void $setPrt( ostype &, bool ); // set fmt called in output cascade |
---|
[9ebd778] | 36 | // public |
---|
[09687aa] | 37 | void sepOn( ostype & ); // turn separator state on |
---|
| 38 | void sepOff( ostype & ); // turn separator state off |
---|
[93c2e0a] | 39 | bool sepDisable( ostype & ); // set default state to off, and return previous state |
---|
[200fcb3] | 40 | bool sepEnable( ostype & ); // set default state to on, and return previous state |
---|
| 41 | void nlOn( ostype & ); // turn auto-newline state on |
---|
| 42 | void nlOff( ostype & ); // turn auto-newline state off |
---|
[09687aa] | 43 | |
---|
| 44 | const char * sepGet( ostype & ); // get separator string |
---|
[e3fea42] | 45 | void sepSet( ostype &, const char [] ); // set separator to string (15 character maximum) |
---|
[09687aa] | 46 | const char * sepGetTuple( ostype & ); // get tuple separator string |
---|
[e3fea42] | 47 | void sepSetTuple( ostype &, const char [] ); // set tuple separator to string (15 character maximum) |
---|
[09687aa] | 48 | |
---|
[65240bb] | 49 | void ends( ostype & os ); // end of output statement |
---|
[09687aa] | 50 | int fail( ostype & ); |
---|
| 51 | int flush( ostype & ); |
---|
[e3fea42] | 52 | void open( ostype & os, const char name[], const char mode[] ); |
---|
[09687aa] | 53 | void close( ostype & os ); |
---|
[e3fea42] | 54 | ostype & write( ostype &, const char [], size_t ); |
---|
[c4b072c] | 55 | int fmt( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); |
---|
[86f384b] | 56 | }; // ostream |
---|
[90c3b1c] | 57 | |
---|
[e1780a2] | 58 | // trait writeable( otype T ) { |
---|
[09687aa] | 59 | // forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, T ); |
---|
[e1780a2] | 60 | // }; // writeable |
---|
| 61 | |
---|
| 62 | trait writeable( otype T, dtype ostype | ostream( ostype ) ) { |
---|
[09687aa] | 63 | ostype & ?|?( ostype &, T ); |
---|
[86f384b] | 64 | }; // writeable |
---|
[51b7345] | 65 | |
---|
[4e06c1e] | 66 | // implement writable for intrinsic types |
---|
[51b7345] | 67 | |
---|
[3ce0d440] | 68 | forall( dtype ostype | ostream( ostype ) ) { |
---|
[17a1b21] | 69 | ostype & ?|?( ostype &, zero_t ); |
---|
| 70 | void ?|?( ostype &, zero_t ); |
---|
| 71 | ostype & ?|?( ostype &, one_t ); |
---|
| 72 | void ?|?( ostype &, one_t ); |
---|
| 73 | |
---|
[93c2e0a] | 74 | ostype & ?|?( ostype &, bool ); |
---|
[5ea5b28] | 75 | void ?|?( ostype &, bool ); |
---|
[1e6e08de] | 76 | |
---|
[3ce0d440] | 77 | ostype & ?|?( ostype &, char ); |
---|
[5ea5b28] | 78 | void ?|?( ostype &, char ); |
---|
[3ce0d440] | 79 | ostype & ?|?( ostype &, signed char ); |
---|
[5ea5b28] | 80 | void ?|?( ostype &, signed char ); |
---|
[3ce0d440] | 81 | ostype & ?|?( ostype &, unsigned char ); |
---|
[5ea5b28] | 82 | void ?|?( ostype &, unsigned char ); |
---|
[09687aa] | 83 | |
---|
[3ce0d440] | 84 | ostype & ?|?( ostype &, short int ); |
---|
[5ea5b28] | 85 | void ?|?( ostype &, short int ); |
---|
[3ce0d440] | 86 | ostype & ?|?( ostype &, unsigned short int ); |
---|
[5ea5b28] | 87 | void ?|?( ostype &, unsigned short int ); |
---|
[3ce0d440] | 88 | ostype & ?|?( ostype &, int ); |
---|
[5ea5b28] | 89 | void ?|?( ostype &, int ); |
---|
[3ce0d440] | 90 | ostype & ?|?( ostype &, unsigned int ); |
---|
[5ea5b28] | 91 | void ?|?( ostype &, unsigned int ); |
---|
[3ce0d440] | 92 | ostype & ?|?( ostype &, long int ); |
---|
[5ea5b28] | 93 | void ?|?( ostype &, long int ); |
---|
[3ce0d440] | 94 | ostype & ?|?( ostype &, long long int ); |
---|
[5ea5b28] | 95 | void ?|?( ostype &, long long int ); |
---|
[3ce0d440] | 96 | ostype & ?|?( ostype &, unsigned long int ); |
---|
[5ea5b28] | 97 | void ?|?( ostype &, unsigned long int ); |
---|
[3ce0d440] | 98 | ostype & ?|?( ostype &, unsigned long long int ); |
---|
[5ea5b28] | 99 | void ?|?( ostype &, unsigned long long int ); |
---|
[bd5b443] | 100 | #if defined( __SIZEOF_INT128__ ) |
---|
| 101 | ostype & ?|?( ostype &, int128 ); |
---|
| 102 | void ?|?( ostype &, int128 ); |
---|
| 103 | ostype & ?|?( ostype &, unsigned int128 ); |
---|
| 104 | void ?|?( ostype &, unsigned int128 ); |
---|
| 105 | #endif // __SIZEOF_INT128__ |
---|
[09687aa] | 106 | |
---|
[65240bb] | 107 | ostype & ?|?( ostype &, float ); |
---|
| 108 | void ?|?( ostype &, float ); |
---|
[3ce0d440] | 109 | ostype & ?|?( ostype &, double ); |
---|
[5ea5b28] | 110 | void ?|?( ostype &, double ); |
---|
[3ce0d440] | 111 | ostype & ?|?( ostype &, long double ); |
---|
[5ea5b28] | 112 | void ?|?( ostype &, long double ); |
---|
[09687aa] | 113 | |
---|
[3ce0d440] | 114 | ostype & ?|?( ostype &, float _Complex ); |
---|
[5ea5b28] | 115 | void ?|?( ostype &, float _Complex ); |
---|
[3ce0d440] | 116 | ostype & ?|?( ostype &, double _Complex ); |
---|
[5ea5b28] | 117 | void ?|?( ostype &, double _Complex ); |
---|
[3ce0d440] | 118 | ostype & ?|?( ostype &, long double _Complex ); |
---|
[5ea5b28] | 119 | void ?|?( ostype &, long double _Complex ); |
---|
[09687aa] | 120 | |
---|
[e3fea42] | 121 | ostype & ?|?( ostype &, const char [] ); |
---|
| 122 | void ?|?( ostype &, const char [] ); |
---|
[3ce0d440] | 123 | // ostype & ?|?( ostype &, const char16_t * ); |
---|
[0db817e] | 124 | #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous |
---|
[3ce0d440] | 125 | // ostype & ?|?( ostype &, const char32_t * ); |
---|
[0db817e] | 126 | #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) |
---|
[3ce0d440] | 127 | // ostype & ?|?( ostype &, const wchar_t * ); |
---|
| 128 | ostype & ?|?( ostype &, const void * ); |
---|
[5ea5b28] | 129 | void ?|?( ostype &, const void * ); |
---|
[3ce0d440] | 130 | |
---|
| 131 | // manipulators |
---|
| 132 | ostype & ?|?( ostype &, ostype & (*)( ostype & ) ); |
---|
[200fcb3] | 133 | void ?|?( ostype &, ostype & (*)( ostype & ) ); |
---|
| 134 | ostype & nl( ostype & ); |
---|
| 135 | ostype & nonl( ostype & ); |
---|
[3ce0d440] | 136 | ostype & sep( ostype & ); |
---|
| 137 | ostype & sepTuple( ostype & ); |
---|
| 138 | ostype & sepOn( ostype & ); |
---|
| 139 | ostype & sepOff( ostype & ); |
---|
| 140 | ostype & sepDisable( ostype & ); |
---|
| 141 | ostype & sepEnable( ostype & ); |
---|
[200fcb3] | 142 | ostype & nlOn( ostype & ); |
---|
| 143 | ostype & nlOff( ostype & ); |
---|
[3ce0d440] | 144 | } // distribution |
---|
[cf16f94] | 145 | |
---|
[c443d1d] | 146 | // tuples |
---|
[200fcb3] | 147 | forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) { |
---|
| 148 | ostype & ?|?( ostype & os, T arg, Params rest ); |
---|
| 149 | void ?|?( ostype & os, T arg, Params rest ); |
---|
| 150 | } // distribution |
---|
[c443d1d] | 151 | |
---|
[e56cfdb0] | 152 | // writes the range [begin, end) to the given stream |
---|
[200fcb3] | 153 | forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) { |
---|
| 154 | void write( iterator_type begin, iterator_type end, ostype & os ); |
---|
| 155 | void write_reverse( iterator_type begin, iterator_type end, ostype & os ); |
---|
| 156 | } // distribution |
---|
[51b7345] | 157 | |
---|
[65240bb] | 158 | //*********************************** manipulators *********************************** |
---|
[3c573e9] | 159 | |
---|
| 160 | forall( otype T ) |
---|
| 161 | struct _Ostream_Manip { |
---|
| 162 | T val; // polymorphic base-type |
---|
[891b827] | 163 | unsigned int wd, pc; // width, precision |
---|
[3c573e9] | 164 | char base; // numeric base / floating-point style |
---|
| 165 | union { |
---|
| 166 | unsigned char all; |
---|
| 167 | struct { |
---|
| 168 | unsigned char pc:1; // precision specified |
---|
| 169 | unsigned char left:1; // left justify |
---|
| 170 | unsigned char nobsdp:1; // base prefix / decimal point |
---|
| 171 | unsigned char sign:1; // plus / minus sign |
---|
| 172 | unsigned char pad0:1; // zero pad |
---|
| 173 | } flags; |
---|
| 174 | }; |
---|
| 175 | }; // _Ostream_Manip |
---|
| 176 | |
---|
[65240bb] | 177 | //*********************************** integral *********************************** |
---|
[3c573e9] | 178 | |
---|
[61c7239] | 179 | // See 6.7.9. 19) The initialization shall occur in initializer list order, each initializer provided for a particular |
---|
| 180 | // subobject overriding any previously listed initializer for the same subobject; ***all subobjects that are not |
---|
| 181 | // initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.*** |
---|
| 182 | |
---|
[3c573e9] | 183 | #define IntegralFMTDecl( T, CODE ) \ |
---|
| 184 | static inline { \ |
---|
[61c7239] | 185 | _Ostream_Manip(T) bin( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'b', { .all : 0 } }; } \ |
---|
| 186 | _Ostream_Manip(T) oct( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'o', { .all : 0 } }; } \ |
---|
| 187 | _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'x', { .all : 0 } }; } \ |
---|
[891b827] | 188 | _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, CODE, { .all : 0 } }; } \ |
---|
| 189 | _Ostream_Manip(T) wd( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, CODE, { .flags.pc : true } }; } \ |
---|
| 190 | _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \ |
---|
| 191 | _Ostream_Manip(T) & wd( unsigned int w, unsigned char pc, _Ostream_Manip(T) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \ |
---|
[3c573e9] | 192 | _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \ |
---|
| 193 | _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \ |
---|
| 194 | _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \ |
---|
| 195 | _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \ |
---|
[61c7239] | 196 | _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, CODE, { .flags.sign : true } }; } \ |
---|
[3c573e9] | 197 | _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \ |
---|
[04396aa] | 198 | } /* distribution */ \ |
---|
[3c573e9] | 199 | forall( dtype ostype | ostream( ostype ) ) { \ |
---|
| 200 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \ |
---|
| 201 | void ?|?( ostype & os, _Ostream_Manip(T) f ); \ |
---|
[61c7239] | 202 | } // ?|? |
---|
[3c573e9] | 203 | |
---|
| 204 | IntegralFMTDecl( signed char, 'd' ) |
---|
| 205 | IntegralFMTDecl( unsigned char, 'u' ) |
---|
| 206 | IntegralFMTDecl( signed short int, 'd' ) |
---|
| 207 | IntegralFMTDecl( unsigned short int, 'u' ) |
---|
| 208 | IntegralFMTDecl( signed int, 'd' ) |
---|
| 209 | IntegralFMTDecl( unsigned int, 'u' ) |
---|
| 210 | IntegralFMTDecl( signed long int, 'd' ) |
---|
| 211 | IntegralFMTDecl( unsigned long int, 'u' ) |
---|
| 212 | IntegralFMTDecl( signed long long int, 'd' ) |
---|
| 213 | IntegralFMTDecl( unsigned long long int, 'u' ) |
---|
[bd5b443] | 214 | #if defined( __SIZEOF_INT128__ ) |
---|
| 215 | IntegralFMTDecl( int128, 'd' ) |
---|
| 216 | IntegralFMTDecl( unsigned int128, 'u' ) |
---|
| 217 | #endif |
---|
[3c573e9] | 218 | |
---|
[65240bb] | 219 | //*********************************** floating point *********************************** |
---|
[3c573e9] | 220 | |
---|
| 221 | // Default suffix for values with no fraction is "." |
---|
| 222 | #define FloatingPointFMTDecl( T ) \ |
---|
| 223 | static inline { \ |
---|
[61c7239] | 224 | _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'a', { .all : 0 } }; } \ |
---|
| 225 | _Ostream_Manip(T) sci( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'e', { .all : 0 } }; } \ |
---|
[891b827] | 226 | _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, 'f', { .all : 0 } }; } \ |
---|
| 227 | _Ostream_Manip(T) wd( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'f', { .flags.pc : true } }; } \ |
---|
| 228 | _Ostream_Manip(T) ws( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'g', { .flags.pc : true } }; } \ |
---|
| 229 | _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \ |
---|
| 230 | _Ostream_Manip(T) & wd( unsigned int w, unsigned char pc, _Ostream_Manip(T) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \ |
---|
[3c573e9] | 231 | _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \ |
---|
[61c7239] | 232 | _Ostream_Manip(T) upcase( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'G', { .all : 0 } }; } \ |
---|
[3c573e9] | 233 | _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { fmt.base -= 32; /* upper case */ return fmt; } \ |
---|
| 234 | _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \ |
---|
[61c7239] | 235 | _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.sign : true } }; } \ |
---|
[3c573e9] | 236 | _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \ |
---|
[61c7239] | 237 | _Ostream_Manip(T) nodp( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.nobsdp : true } }; } \ |
---|
[3c573e9] | 238 | _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \ |
---|
[04396aa] | 239 | } /* distribution */ \ |
---|
[3c573e9] | 240 | forall( dtype ostype | ostream( ostype ) ) { \ |
---|
| 241 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \ |
---|
| 242 | void ?|?( ostype & os, _Ostream_Manip(T) f ); \ |
---|
[61c7239] | 243 | } // ?|? |
---|
[3c573e9] | 244 | |
---|
| 245 | FloatingPointFMTDecl( double ) |
---|
| 246 | FloatingPointFMTDecl( long double ) |
---|
| 247 | |
---|
[65240bb] | 248 | //*********************************** character *********************************** |
---|
[3c573e9] | 249 | |
---|
| 250 | static inline { |
---|
[04396aa] | 251 | _Ostream_Manip(char) bin( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'b', { .all : 0 } }; } |
---|
| 252 | _Ostream_Manip(char) oct( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'o', { .all : 0 } }; } |
---|
| 253 | _Ostream_Manip(char) hex( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'x', { .all : 0 } }; } |
---|
| 254 | _Ostream_Manip(char) wd( unsigned int w, char c ) { return (_Ostream_Manip(char))@{ c, w, 0, 'c', { .all : 0 } }; } |
---|
[891b827] | 255 | _Ostream_Manip(char) & wd( unsigned int w, _Ostream_Manip(char) & fmt ) { fmt.wd = w; return fmt; } |
---|
[3c573e9] | 256 | _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; } |
---|
| 257 | _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } |
---|
| 258 | _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; } |
---|
| 259 | } // distribution |
---|
| 260 | forall( dtype ostype | ostream( ostype ) ) { |
---|
| 261 | ostype & ?|?( ostype & os, _Ostream_Manip(char) f ); |
---|
| 262 | void ?|?( ostype & os, _Ostream_Manip(char) f ); |
---|
[61c7239] | 263 | } // ?|? |
---|
[3c573e9] | 264 | |
---|
[65240bb] | 265 | //*********************************** C string *********************************** |
---|
[3c573e9] | 266 | |
---|
| 267 | static inline { |
---|
[e3fea42] | 268 | _Ostream_Manip(const char *) bin( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'b', { .all : 0 } }; } |
---|
| 269 | _Ostream_Manip(const char *) oct( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'o', { .all : 0 } }; } |
---|
| 270 | _Ostream_Manip(const char *) hex( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'x', { .all : 0 } }; } |
---|
| 271 | _Ostream_Manip(const char *) wd( unsigned int w, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, w, 0, 's', { .all : 0 } }; } |
---|
| 272 | _Ostream_Manip(const char *) wd( unsigned int w, unsigned char pc, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, w, pc, 's', { .flags.pc : true } }; } |
---|
[891b827] | 273 | _Ostream_Manip(const char *) & wd( unsigned int w, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; return fmt; } |
---|
| 274 | _Ostream_Manip(const char *) & wd( unsigned int w, unsigned char pc, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } |
---|
[3c573e9] | 275 | _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; } |
---|
| 276 | _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; } |
---|
| 277 | } // distribution |
---|
| 278 | forall( dtype ostype | ostream( ostype ) ) { |
---|
| 279 | ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ); |
---|
| 280 | void ?|?( ostype & os, _Ostream_Manip(const char *) f ); |
---|
[61c7239] | 281 | } // ?|? |
---|
[3c573e9] | 282 | |
---|
| 283 | |
---|
[65240bb] | 284 | //*********************************** istream *********************************** |
---|
[3c573e9] | 285 | |
---|
[51b7345] | 286 | |
---|
[4040425] | 287 | trait istream( dtype istype ) { |
---|
[0efb269] | 288 | void nlOn( istype & ); // read newline |
---|
| 289 | void nlOff( istype & ); // scan newline |
---|
| 290 | bool getANL( istype & ); // get scan newline (on/off) |
---|
[09687aa] | 291 | int fail( istype & ); |
---|
| 292 | int eof( istype & ); |
---|
[e3fea42] | 293 | void open( istype & is, const char name[] ); |
---|
[09687aa] | 294 | void close( istype & is ); |
---|
[9428d52] | 295 | istype & read( istype &, char *, size_t ); |
---|
[09687aa] | 296 | istype & ungetc( istype &, char ); |
---|
[c4b072c] | 297 | int fmt( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); |
---|
[86f384b] | 298 | }; // istream |
---|
[51b7345] | 299 | |
---|
[4040425] | 300 | trait readable( otype T ) { |
---|
[09687aa] | 301 | forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, T ); |
---|
[86f384b] | 302 | }; // readable |
---|
[51b7345] | 303 | |
---|
[3ce0d440] | 304 | forall( dtype istype | istream( istype ) ) { |
---|
[93c2e0a] | 305 | istype & ?|?( istype &, bool & ); |
---|
[3ce0d440] | 306 | |
---|
| 307 | istype & ?|?( istype &, char & ); |
---|
| 308 | istype & ?|?( istype &, signed char & ); |
---|
| 309 | istype & ?|?( istype &, unsigned char & ); |
---|
| 310 | |
---|
| 311 | istype & ?|?( istype &, short int & ); |
---|
| 312 | istype & ?|?( istype &, unsigned short int & ); |
---|
| 313 | istype & ?|?( istype &, int & ); |
---|
| 314 | istype & ?|?( istype &, unsigned int & ); |
---|
| 315 | istype & ?|?( istype &, long int & ); |
---|
| 316 | istype & ?|?( istype &, long long int & ); |
---|
| 317 | istype & ?|?( istype &, unsigned long int & ); |
---|
| 318 | istype & ?|?( istype &, unsigned long long int & ); |
---|
| 319 | |
---|
| 320 | istype & ?|?( istype &, float & ); |
---|
| 321 | istype & ?|?( istype &, double & ); |
---|
| 322 | istype & ?|?( istype &, long double & ); |
---|
| 323 | |
---|
| 324 | istype & ?|?( istype &, float _Complex & ); |
---|
| 325 | istype & ?|?( istype &, double _Complex & ); |
---|
| 326 | istype & ?|?( istype &, long double _Complex & ); |
---|
| 327 | |
---|
[e3fea42] | 328 | // istype & ?|?( istype &, const char [] ); |
---|
[04396aa] | 329 | istype & ?|?( istype &, char * ); |
---|
[61c7239] | 330 | |
---|
[3ce0d440] | 331 | // manipulators |
---|
| 332 | istype & ?|?( istype &, istype & (*)( istype & ) ); |
---|
[200fcb3] | 333 | istype & nl( istype & is ); |
---|
[3c5dee4] | 334 | istype & nlOn( istype & ); |
---|
| 335 | istype & nlOff( istype & ); |
---|
[3ce0d440] | 336 | } // distribution |
---|
[44574f2] | 337 | |
---|
[65240bb] | 338 | //*********************************** manipulators *********************************** |
---|
[51b7345] | 339 | |
---|
[3c573e9] | 340 | struct _Istream_Cstr { |
---|
[61c7239] | 341 | char * s; |
---|
| 342 | const char * scanset; |
---|
[3c573e9] | 343 | int wd; // width |
---|
[61c7239] | 344 | union { |
---|
| 345 | unsigned char all; |
---|
| 346 | struct { |
---|
| 347 | unsigned char ignore:1; // do not change input argument |
---|
| 348 | unsigned char inex:1; // include/exclude characters in scanset |
---|
| 349 | } flags; |
---|
| 350 | }; |
---|
| 351 | }; // _Istream_Cstr |
---|
| 352 | |
---|
[04396aa] | 353 | static inline { |
---|
| 354 | _Istream_Cstr skip( unsigned int n ) { return (_Istream_Cstr){ 0p, 0p, n, { .all : 0 } }; } |
---|
[e3fea42] | 355 | _Istream_Cstr skip( const char scanset[] ) { return (_Istream_Cstr){ 0p, scanset, -1, { .all : 0 } }; } |
---|
| 356 | _Istream_Cstr incl( const char scanset[], char * s ) { return (_Istream_Cstr){ s, scanset, -1, { .flags.inex : false } }; } |
---|
| 357 | _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; } |
---|
| 358 | _Istream_Cstr excl( const char scanset[], char * s ) { return (_Istream_Cstr){ s, scanset, -1, { .flags.inex : true } }; } |
---|
| 359 | _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; } |
---|
| 360 | _Istream_Cstr ignore( const char s[] ) { return (_Istream_Cstr)@{ s, 0p, -1, { .flags.ignore : true } }; } |
---|
[04396aa] | 361 | _Istream_Cstr & ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; } |
---|
[e3fea42] | 362 | _Istream_Cstr wdi( unsigned int w, char s[] ) { return (_Istream_Cstr)@{ s, 0p, w, { .all : 0 } }; } |
---|
[dc5072f] | 363 | _Istream_Cstr & wdi( unsigned int w, _Istream_Cstr & fmt ) { fmt.wd = w; return fmt; } |
---|
[04396aa] | 364 | } // distribution |
---|
[86a8be5] | 365 | forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Cstr f ); |
---|
| 366 | |
---|
| 367 | struct _Istream_Char { |
---|
| 368 | bool ignore; // do not change input argument |
---|
| 369 | }; // _Istream_Char |
---|
| 370 | |
---|
[04396aa] | 371 | static inline { |
---|
| 372 | _Istream_Char ignore( const char c ) { return (_Istream_Char)@{ true }; } |
---|
| 373 | _Istream_Char & ignore( _Istream_Char & fmt ) { fmt.ignore = true; return fmt; } |
---|
| 374 | } // distribution |
---|
[86a8be5] | 375 | forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Char f ); |
---|
[3c573e9] | 376 | |
---|
| 377 | forall( otype T ) |
---|
| 378 | struct _Istream_Manip { |
---|
| 379 | T & val; // polymorphic base-type |
---|
| 380 | int wd; // width |
---|
[61c7239] | 381 | bool ignore; // do not change input argument |
---|
[3c573e9] | 382 | }; // _Istream_Manip |
---|
| 383 | |
---|
| 384 | #define InputFMTDecl( T ) \ |
---|
[04396aa] | 385 | static inline { \ |
---|
| 386 | _Istream_Manip(T) ignore( const T & val ) { return (_Istream_Manip(T))@{ (T &)val, -1, true }; } \ |
---|
| 387 | _Istream_Manip(T) & ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \ |
---|
| 388 | _Istream_Manip(T) wdi( unsigned int w, T & val ) { return (_Istream_Manip(T))@{ val, w, false }; } \ |
---|
[dc5072f] | 389 | _Istream_Manip(T) & wdi( unsigned int w, _Istream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \ |
---|
[04396aa] | 390 | } /* distribution */ \ |
---|
[3c573e9] | 391 | forall( dtype istype | istream( istype ) ) { \ |
---|
| 392 | istype & ?|?( istype & is, _Istream_Manip(T) f ); \ |
---|
[61c7239] | 393 | } // ?|? |
---|
[3c573e9] | 394 | |
---|
| 395 | InputFMTDecl( signed char ) |
---|
| 396 | InputFMTDecl( unsigned char ) |
---|
| 397 | InputFMTDecl( signed short int ) |
---|
| 398 | InputFMTDecl( unsigned short int ) |
---|
| 399 | InputFMTDecl( signed int ) |
---|
| 400 | InputFMTDecl( unsigned int ) |
---|
| 401 | InputFMTDecl( signed long int ) |
---|
| 402 | InputFMTDecl( unsigned long int ) |
---|
| 403 | InputFMTDecl( signed long long int ) |
---|
| 404 | InputFMTDecl( unsigned long long int ) |
---|
| 405 | |
---|
| 406 | InputFMTDecl( float ) |
---|
| 407 | InputFMTDecl( double ) |
---|
| 408 | InputFMTDecl( long double ) |
---|
| 409 | |
---|
| 410 | InputFMTDecl( float _Complex ) |
---|
| 411 | InputFMTDecl( double _Complex ) |
---|
| 412 | InputFMTDecl( long double _Complex ) |
---|
| 413 | |
---|
| 414 | |
---|
[65240bb] | 415 | //*********************************** time *********************************** |
---|
[3c573e9] | 416 | |
---|
[10a97adb] | 417 | |
---|
[200fcb3] | 418 | #include <time_t.hfa> // Duration (constructors) / Time (constructors) |
---|
[10a97adb] | 419 | |
---|
[200fcb3] | 420 | forall( dtype ostype | ostream( ostype ) ) { |
---|
| 421 | ostype & ?|?( ostype & os, Duration dur ); |
---|
| 422 | void ?|?( ostype & os, Duration dur ); |
---|
| 423 | ostype & ?|?( ostype & os, Time time ); |
---|
| 424 | void ?|?( ostype & os, Time time ); |
---|
| 425 | } // distribution |
---|
[10a97adb] | 426 | |
---|
[86bd7c1f] | 427 | // Local Variables: // |
---|
| 428 | // tab-width: 4 // |
---|
| 429 | // End: // |
---|