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.c -- |
---|
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 : Sat Jun 2 08:24:56 2018 |
---|
13 | // Update Count : 471 |
---|
14 | // |
---|
15 | |
---|
16 | #include "iostream" |
---|
17 | |
---|
18 | extern "C" { |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdbool.h> // true/false |
---|
21 | //#include <string.h> // strlen, strcmp |
---|
22 | extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2))); |
---|
23 | extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))); |
---|
24 | #include <float.h> // DBL_DIG, LDBL_DIG |
---|
25 | #include <complex.h> // creal, cimag |
---|
26 | } |
---|
27 | |
---|
28 | forall( dtype ostype | ostream( ostype ) ) { |
---|
29 | ostype & ?|?( ostype & os, _Bool b ) { |
---|
30 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
31 | fmt( os, "%s", b ? "true" : "false" ); |
---|
32 | return os; |
---|
33 | } // ?|? |
---|
34 | |
---|
35 | ostype & ?|?( ostype & os, char ch ) { |
---|
36 | fmt( os, "%c", ch ); |
---|
37 | if ( ch == '\n' ) setNL( os, true ); |
---|
38 | sepOff( os ); |
---|
39 | return os; |
---|
40 | } // ?|? |
---|
41 | |
---|
42 | ostype & ?|?( ostype & os, signed char c ) { |
---|
43 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
44 | fmt( os, "%hhd", c ); |
---|
45 | return os; |
---|
46 | } // ?|? |
---|
47 | |
---|
48 | ostype & ?|?( ostype & os, unsigned char c ) { |
---|
49 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
50 | fmt( os, "%hhu", c ); |
---|
51 | return os; |
---|
52 | } // ?|? |
---|
53 | |
---|
54 | ostype & ?|?( ostype & os, short int si ) { |
---|
55 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
56 | fmt( os, "%hd", si ); |
---|
57 | return os; |
---|
58 | } // ?|? |
---|
59 | |
---|
60 | ostype & ?|?( ostype & os, unsigned short int usi ) { |
---|
61 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
62 | fmt( os, "%hu", usi ); |
---|
63 | return os; |
---|
64 | } // ?|? |
---|
65 | |
---|
66 | ostype & ?|?( ostype & os, int i ) { |
---|
67 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
68 | fmt( os, "%d", i ); |
---|
69 | return os; |
---|
70 | } // ?|? |
---|
71 | |
---|
72 | ostype & ?|?( ostype & os, unsigned int ui ) { |
---|
73 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
74 | fmt( os, "%u", ui ); |
---|
75 | return os; |
---|
76 | } // ?|? |
---|
77 | |
---|
78 | ostype & ?|?( ostype & os, long int li ) { |
---|
79 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
80 | fmt( os, "%ld", li ); |
---|
81 | return os; |
---|
82 | } // ?|? |
---|
83 | |
---|
84 | ostype & ?|?( ostype & os, unsigned long int uli ) { |
---|
85 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
86 | fmt( os, "%lu", uli ); |
---|
87 | return os; |
---|
88 | } // ?|? |
---|
89 | |
---|
90 | ostype & ?|?( ostype & os, long long int lli ) { |
---|
91 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
92 | fmt( os, "%lld", lli ); |
---|
93 | return os; |
---|
94 | } // ?|? |
---|
95 | |
---|
96 | ostype & ?|?( ostype & os, unsigned long long int ulli ) { |
---|
97 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
98 | fmt( os, "%llu", ulli ); |
---|
99 | return os; |
---|
100 | } // ?|? |
---|
101 | |
---|
102 | ostype & ?|?( ostype & os, float f ) { |
---|
103 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
104 | fmt( os, "%g", f ); |
---|
105 | return os; |
---|
106 | } // ?|? |
---|
107 | |
---|
108 | ostype & ?|?( ostype & os, double d ) { |
---|
109 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
110 | fmt( os, "%.*lg", DBL_DIG, d ); |
---|
111 | return os; |
---|
112 | } // ?|? |
---|
113 | |
---|
114 | ostype & ?|?( ostype & os, long double ld ) { |
---|
115 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
116 | fmt( os, "%.*Lg", LDBL_DIG, ld ); |
---|
117 | return os; |
---|
118 | } // ?|? |
---|
119 | |
---|
120 | ostype & ?|?( ostype & os, float _Complex fc ) { |
---|
121 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
122 | fmt( os, "%g%+gi", crealf( fc ), cimagf( fc ) ); |
---|
123 | return os; |
---|
124 | } // ?|? |
---|
125 | |
---|
126 | ostype & ?|?( ostype & os, double _Complex dc ) { |
---|
127 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
128 | fmt( os, "%.*lg%+.*lgi", DBL_DIG, creal( dc ), DBL_DIG, cimag( dc ) ); |
---|
129 | return os; |
---|
130 | } // ?|? |
---|
131 | |
---|
132 | ostype & ?|?( ostype & os, long double _Complex ldc ) { |
---|
133 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
134 | fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) ); |
---|
135 | return os; |
---|
136 | } // ?|? |
---|
137 | |
---|
138 | ostype & ?|?( ostype & os, const char * str ) { |
---|
139 | enum { Open = 1, Close, OpenClose }; |
---|
140 | static const unsigned char mask[256] @= { |
---|
141 | // opening delimiters, no space after |
---|
142 | ['('] : Open, ['['] : Open, ['{'] : Open, |
---|
143 | ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open, |
---|
144 | [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open, |
---|
145 | // closing delimiters, no space before |
---|
146 | [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close, |
---|
147 | ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close, |
---|
148 | [')'] : Close, [']'] : Close, ['}'] : Close, |
---|
149 | // opening-closing delimiters, no space before or after |
---|
150 | ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose, |
---|
151 | [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace |
---|
152 | }; // mask |
---|
153 | |
---|
154 | if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator |
---|
155 | |
---|
156 | // first character IS NOT spacing or closing punctuation => add left separator |
---|
157 | unsigned char ch = str[0]; // must make unsigned |
---|
158 | if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) { |
---|
159 | fmt( os, "%s", sepGetCur( os ) ); |
---|
160 | } // if |
---|
161 | |
---|
162 | // if string starts line, must reset to determine open state because separator is off |
---|
163 | sepReset( os ); // reset separator |
---|
164 | |
---|
165 | // last character IS spacing or opening punctuation => turn off separator for next item |
---|
166 | size_t len = strlen( str ); |
---|
167 | ch = str[len - 1]; // must make unsigned |
---|
168 | if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) { |
---|
169 | sepOn( os ); |
---|
170 | } else { |
---|
171 | sepOff( os ); |
---|
172 | } // if |
---|
173 | if ( ch == '\n' ) setNL( os, true ); // check *AFTER* sepPrt call above as it resets NL flag |
---|
174 | return write( os, str, len ); |
---|
175 | } // ?|? |
---|
176 | |
---|
177 | // ostype & ?|?( ostype & os, const char16_t * str ) { |
---|
178 | // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
179 | // fmt( os, "%ls", str ); |
---|
180 | // return os; |
---|
181 | // } // ?|? |
---|
182 | |
---|
183 | // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous |
---|
184 | // ostype & ?|?( ostype & os, const char32_t * str ) { |
---|
185 | // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
186 | // fmt( os, "%ls", str ); |
---|
187 | // return os; |
---|
188 | // } // ?|? |
---|
189 | // #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) |
---|
190 | |
---|
191 | // ostype & ?|?( ostype & os, const wchar_t * str ) { |
---|
192 | // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
193 | // fmt( os, "%ls", str ); |
---|
194 | // return os; |
---|
195 | // } // ?|? |
---|
196 | |
---|
197 | ostype & ?|?( ostype & os, const void * p ) { |
---|
198 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
199 | fmt( os, "%p", p ); |
---|
200 | return os; |
---|
201 | } // ?|? |
---|
202 | |
---|
203 | |
---|
204 | // manipulators |
---|
205 | ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { |
---|
206 | return manip( os ); |
---|
207 | } // ?|? |
---|
208 | |
---|
209 | ostype & sep( ostype & os ) { |
---|
210 | os | sepGet( os ); |
---|
211 | return os; |
---|
212 | } // sep |
---|
213 | |
---|
214 | ostype & sepTuple( ostype & os ) { |
---|
215 | os | sepGetTuple( os ); |
---|
216 | return os; |
---|
217 | } // sepTuple |
---|
218 | |
---|
219 | ostype & endl( ostype & os ) { |
---|
220 | os | '\n'; |
---|
221 | setNL( os, true ); |
---|
222 | flush( os ); |
---|
223 | sepOff( os ); // prepare for next line |
---|
224 | return os; |
---|
225 | } // endl |
---|
226 | |
---|
227 | ostype & sepOn( ostype & os ) { |
---|
228 | sepOn( os ); |
---|
229 | return os; |
---|
230 | } // sepOn |
---|
231 | |
---|
232 | ostype & sepOff( ostype & os ) { |
---|
233 | sepOff( os ); |
---|
234 | return os; |
---|
235 | } // sepOff |
---|
236 | |
---|
237 | ostype & sepEnable( ostype & os ) { |
---|
238 | sepEnable( os ); |
---|
239 | return os; |
---|
240 | } // sepEnable |
---|
241 | |
---|
242 | ostype & sepDisable( ostype & os ) { |
---|
243 | sepDisable( os ); |
---|
244 | return os; |
---|
245 | } // sepDisable |
---|
246 | } // distribution |
---|
247 | |
---|
248 | |
---|
249 | // tuples |
---|
250 | forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) |
---|
251 | ostype & ?|?( ostype & os, T arg, Params rest ) { |
---|
252 | os | arg; // print first argument |
---|
253 | sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator |
---|
254 | os | rest; // print remaining arguments |
---|
255 | sepSetCur( os, sepGet( os ) ); // switch to regular separator |
---|
256 | return os; |
---|
257 | } // ?|? |
---|
258 | |
---|
259 | //--------------------------------------- |
---|
260 | |
---|
261 | // writes the range [begin, end) to the given stream |
---|
262 | forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) |
---|
263 | void write( iterator_type begin, iterator_type end, ostype & os ) { |
---|
264 | void print( elt_type i ) { os | i; } |
---|
265 | for_each( begin, end, print ); |
---|
266 | } // ?|? |
---|
267 | |
---|
268 | forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) |
---|
269 | void write_reverse( iterator_type begin, iterator_type end, ostype & os ) { |
---|
270 | void print( elt_type i ) { os | i; } |
---|
271 | for_each_reverse( begin, end, print ); |
---|
272 | } // ?|? |
---|
273 | |
---|
274 | //--------------------------------------- |
---|
275 | |
---|
276 | forall( dtype istype | istream( istype ) ) { |
---|
277 | istype & ?|?( istype & is, _Bool & b ) { |
---|
278 | char val[6]; |
---|
279 | fmt( is, "%5s", val ); |
---|
280 | if ( strcmp( val, "true" ) == 0 ) b = true; |
---|
281 | else if ( strcmp( val, "false" ) == 0 ) b = false; |
---|
282 | else { |
---|
283 | fprintf( stderr, "invalid _Bool constant\n" ); |
---|
284 | abort(); |
---|
285 | } // if |
---|
286 | return is; |
---|
287 | } // ?|? |
---|
288 | |
---|
289 | istype & ?|?( istype & is, char & c ) { |
---|
290 | fmt( is, "%c", &c ); // must pass pointer through varg to fmt |
---|
291 | return is; |
---|
292 | } // ?|? |
---|
293 | |
---|
294 | istype & ?|?( istype & is, signed char & sc ) { |
---|
295 | fmt( is, "%hhd", &sc ); |
---|
296 | return is; |
---|
297 | } // ?|? |
---|
298 | |
---|
299 | istype & ?|?( istype & is, unsigned char & usc ) { |
---|
300 | fmt( is, "%hhu", &usc ); |
---|
301 | return is; |
---|
302 | } // ?|? |
---|
303 | |
---|
304 | istype & ?|?( istype & is, short int & si ) { |
---|
305 | fmt( is, "%hd", &si ); |
---|
306 | return is; |
---|
307 | } // ?|? |
---|
308 | |
---|
309 | istype & ?|?( istype & is, unsigned short int & usi ) { |
---|
310 | fmt( is, "%hu", &usi ); |
---|
311 | return is; |
---|
312 | } // ?|? |
---|
313 | |
---|
314 | istype & ?|?( istype & is, int & i ) { |
---|
315 | fmt( is, "%d", &i ); |
---|
316 | return is; |
---|
317 | } // ?|? |
---|
318 | |
---|
319 | istype & ?|?( istype & is, unsigned int & ui ) { |
---|
320 | fmt( is, "%u", &ui ); |
---|
321 | return is; |
---|
322 | } // ?|? |
---|
323 | |
---|
324 | istype & ?|?( istype & is, long int & li ) { |
---|
325 | fmt( is, "%ld", &li ); |
---|
326 | return is; |
---|
327 | } // ?|? |
---|
328 | |
---|
329 | istype & ?|?( istype & is, unsigned long int & ulli ) { |
---|
330 | fmt( is, "%lu", &ulli ); |
---|
331 | return is; |
---|
332 | } // ?|? |
---|
333 | |
---|
334 | istype & ?|?( istype & is, long long int & lli ) { |
---|
335 | fmt( is, "%lld", &lli ); |
---|
336 | return is; |
---|
337 | } // ?|? |
---|
338 | |
---|
339 | istype & ?|?( istype & is, unsigned long long int & ulli ) { |
---|
340 | fmt( is, "%llu", &ulli ); |
---|
341 | return is; |
---|
342 | } // ?|? |
---|
343 | |
---|
344 | |
---|
345 | istype & ?|?( istype & is, float & f ) { |
---|
346 | fmt( is, "%f", &f ); |
---|
347 | return is; |
---|
348 | } // ?|? |
---|
349 | |
---|
350 | istype & ?|?( istype & is, double & d ) { |
---|
351 | fmt( is, "%lf", &d ); |
---|
352 | return is; |
---|
353 | } // ?|? |
---|
354 | |
---|
355 | istype & ?|?( istype & is, long double & ld ) { |
---|
356 | fmt( is, "%Lf", &ld ); |
---|
357 | return is; |
---|
358 | } // ?|? |
---|
359 | |
---|
360 | |
---|
361 | istype & ?|?( istype & is, float _Complex & fc ) { |
---|
362 | float re, im; |
---|
363 | fmt( is, "%g%gi", &re, &im ); |
---|
364 | fc = re + im * _Complex_I; |
---|
365 | return is; |
---|
366 | } // ?|? |
---|
367 | |
---|
368 | istype & ?|?( istype & is, double _Complex & dc ) { |
---|
369 | double re, im; |
---|
370 | fmt( is, "%lf%lfi", &re, &im ); |
---|
371 | dc = re + im * _Complex_I; |
---|
372 | return is; |
---|
373 | } // ?|? |
---|
374 | |
---|
375 | istype & ?|?( istype & is, long double _Complex & ldc ) { |
---|
376 | long double re, im; |
---|
377 | fmt( is, "%Lf%Lfi", &re, &im ); |
---|
378 | ldc = re + im * _Complex_I; |
---|
379 | return is; |
---|
380 | } // ?|? |
---|
381 | |
---|
382 | |
---|
383 | // manipulators |
---|
384 | istype & ?|?( istype & is, istype & (* manip)( istype & ) ) { |
---|
385 | return manip( is ); |
---|
386 | } // ?|? |
---|
387 | |
---|
388 | istype & endl( istype & is ) { |
---|
389 | fmt( is, "%*[ \t\f\n\r\v]" ); // ignore whitespace |
---|
390 | return is; |
---|
391 | } // endl |
---|
392 | } // distribution |
---|
393 | |
---|
394 | _Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; } |
---|
395 | forall( dtype istype | istream( istype ) ) |
---|
396 | istype & ?|?( istype & is, _Istream_cstrUC cstr ) { |
---|
397 | fmt( is, "%s", cstr.s ); |
---|
398 | return is; |
---|
399 | } // cstr |
---|
400 | |
---|
401 | _Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; } |
---|
402 | forall( dtype istype | istream( istype ) ) |
---|
403 | istype & ?|?( istype & is, _Istream_cstrC cstr ) { |
---|
404 | char buf[16]; |
---|
405 | sprintf( buf, "%%%ds", cstr.size ); |
---|
406 | fmt( is, buf, cstr.s ); |
---|
407 | return is; |
---|
408 | } // cstr |
---|
409 | |
---|
410 | // Local Variables: // |
---|
411 | // tab-width: 4 // |
---|
412 | // compile-command: "cfa iostream.c" // |
---|
413 | // End: // |
---|