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