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