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