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 : Sun Jul 16 21:12:03 2017
|
---|
13 | // Update Count : 398
|
---|
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 | fmt( os, "%hhd", c );
|
---|
37 | sepOff( os );
|
---|
38 | return os;
|
---|
39 | } // ?|?
|
---|
40 |
|
---|
41 | forall( dtype ostype | ostream( ostype ) )
|
---|
42 | ostype * ?|?( ostype * os, unsigned char c ) {
|
---|
43 | fmt( os, "%hhu", c );
|
---|
44 | sepOff( os );
|
---|
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 * cp ) {
|
---|
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 ( cp[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 = cp[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( cp );
|
---|
176 | ch = cp[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, cp, len );
|
---|
184 | } // ?|?
|
---|
185 |
|
---|
186 | forall( dtype ostype | ostream( ostype ) )
|
---|
187 | ostype * ?|?( ostype * os, const void * p ) {
|
---|
188 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
---|
189 | fmt( os, "%p", p );
|
---|
190 | return os;
|
---|
191 | } // ?|?
|
---|
192 |
|
---|
193 |
|
---|
194 | // tuples
|
---|
195 | forall( dtype ostype, otype T, ttype Params | ostream( ostype ) | writeable( T ) | { ostype * ?|?( ostype *, Params ); } )
|
---|
196 | ostype * ?|?( ostype * os, T arg, Params rest ) {
|
---|
197 | os | arg; // print first argument
|
---|
198 | sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
|
---|
199 | os | rest; // print remaining arguments
|
---|
200 | sepSetCur( os, sepGet( os ) ); // switch to regular separator
|
---|
201 | return os;
|
---|
202 | } // ?|?
|
---|
203 |
|
---|
204 |
|
---|
205 | // manipulators
|
---|
206 | forall( dtype ostype | ostream( ostype ) )
|
---|
207 | ostype * ?|?( ostype * os, ostype * (* manip)( ostype * ) ) {
|
---|
208 | return manip( os );
|
---|
209 | } // ?|?
|
---|
210 |
|
---|
211 | forall( dtype ostype | ostream( ostype ) )
|
---|
212 | ostype * sep( ostype * os ) {
|
---|
213 | os | sepGet( os );
|
---|
214 | return os;
|
---|
215 | } // sep
|
---|
216 |
|
---|
217 | forall( dtype ostype | ostream( ostype ) )
|
---|
218 | ostype * sepTuple( ostype * os ) {
|
---|
219 | os | sepGetTuple( os );
|
---|
220 | return os;
|
---|
221 | } // sepTuple
|
---|
222 |
|
---|
223 | forall( dtype ostype | ostream( ostype ) )
|
---|
224 | ostype * endl( ostype * os ) {
|
---|
225 | os | '\n';
|
---|
226 | setNL( os, true );
|
---|
227 | flush( os );
|
---|
228 | sepOff( os ); // prepare for next line
|
---|
229 | return os;
|
---|
230 | } // endl
|
---|
231 |
|
---|
232 | forall( dtype ostype | ostream( ostype ) )
|
---|
233 | ostype * sepOn( ostype * os ) {
|
---|
234 | sepOn( os );
|
---|
235 | return os;
|
---|
236 | } // sepOn
|
---|
237 |
|
---|
238 | forall( dtype ostype | ostream( ostype ) )
|
---|
239 | ostype * sepOff( ostype * os ) {
|
---|
240 | sepOff( os );
|
---|
241 | return os;
|
---|
242 | } // sepOff
|
---|
243 |
|
---|
244 | forall( dtype ostype | ostream( ostype ) )
|
---|
245 | ostype * sepEnable( ostype * os ) {
|
---|
246 | sepEnable( os );
|
---|
247 | return os;
|
---|
248 | } // sepEnable
|
---|
249 |
|
---|
250 | forall( dtype ostype | ostream( ostype ) )
|
---|
251 | ostype * sepDisable( ostype * os ) {
|
---|
252 | sepDisable( os );
|
---|
253 | return os;
|
---|
254 | } // sepDisable
|
---|
255 |
|
---|
256 | //---------------------------------------
|
---|
257 |
|
---|
258 | forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
|
---|
259 | void write( iteratortype begin, iteratortype end, ostype * os ) {
|
---|
260 | void print( elttype i ) { os | i; }
|
---|
261 | for_each( begin, end, print );
|
---|
262 | } // ?|?
|
---|
263 |
|
---|
264 | forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
|
---|
265 | void write_reverse( iteratortype begin, iteratortype end, ostype * os ) {
|
---|
266 | void print( elttype i ) { os | i; }
|
---|
267 | for_each_reverse( begin, end, print );
|
---|
268 | } // ?|?
|
---|
269 |
|
---|
270 | //---------------------------------------
|
---|
271 |
|
---|
272 | forall( dtype istype | istream( istype ) )
|
---|
273 | istype * ?|?( istype * is, char * c ) {
|
---|
274 | fmt( is, "%c", c );
|
---|
275 | return is;
|
---|
276 | } // ?|?
|
---|
277 |
|
---|
278 | forall( dtype istype | istream( istype ) )
|
---|
279 | istype * ?|?( istype * is, short int * si ) {
|
---|
280 | fmt( is, "%hd", si );
|
---|
281 | return is;
|
---|
282 | } // ?|?
|
---|
283 |
|
---|
284 | forall( dtype istype | istream( istype ) )
|
---|
285 | istype * ?|?( istype * is, unsigned short int * usi ) {
|
---|
286 | fmt( is, "%hu", usi );
|
---|
287 | return is;
|
---|
288 | } // ?|?
|
---|
289 |
|
---|
290 | forall( dtype istype | istream( istype ) )
|
---|
291 | istype * ?|?( istype * is, int * i ) {
|
---|
292 | fmt( is, "%d", i );
|
---|
293 | return is;
|
---|
294 | } // ?|?
|
---|
295 |
|
---|
296 | forall( dtype istype | istream( istype ) )
|
---|
297 | istype * ?|?( istype * is, unsigned int * ui ) {
|
---|
298 | fmt( is, "%u", ui );
|
---|
299 | return is;
|
---|
300 | } // ?|?
|
---|
301 |
|
---|
302 | forall( dtype istype | istream( istype ) )
|
---|
303 | istype * ?|?( istype * is, long int * li ) {
|
---|
304 | fmt( is, "%ld", li );
|
---|
305 | return is;
|
---|
306 | } // ?|?
|
---|
307 |
|
---|
308 | forall( dtype istype | istream( istype ) )
|
---|
309 | istype * ?|?( istype * is, unsigned long int * ulli ) {
|
---|
310 | fmt( is, "%lu", ulli );
|
---|
311 | return is;
|
---|
312 | } // ?|?
|
---|
313 |
|
---|
314 | forall( dtype istype | istream( istype ) )
|
---|
315 | istype * ?|?( istype * is, long long int * lli ) {
|
---|
316 | fmt( is, "%lld", lli );
|
---|
317 | return is;
|
---|
318 | } // ?|?
|
---|
319 |
|
---|
320 | forall( dtype istype | istream( istype ) )
|
---|
321 | istype * ?|?( istype * is, unsigned long long int * ulli ) {
|
---|
322 | fmt( is, "%llu", ulli );
|
---|
323 | return is;
|
---|
324 | } // ?|?
|
---|
325 |
|
---|
326 |
|
---|
327 | forall( dtype istype | istream( istype ) )
|
---|
328 | istype * ?|?( istype * is, float * f ) {
|
---|
329 | fmt( is, "%f", f );
|
---|
330 | return is;
|
---|
331 | } // ?|?
|
---|
332 |
|
---|
333 | forall( dtype istype | istream( istype ) )
|
---|
334 | istype * ?|?( istype * is, double * d ) {
|
---|
335 | fmt( is, "%lf", d );
|
---|
336 | return is;
|
---|
337 | } // ?|?
|
---|
338 |
|
---|
339 | forall( dtype istype | istream( istype ) )
|
---|
340 | istype * ?|?( istype * is, long double * ld ) {
|
---|
341 | fmt( is, "%Lf", ld );
|
---|
342 | return is;
|
---|
343 | } // ?|?
|
---|
344 |
|
---|
345 |
|
---|
346 | forall( dtype istype | istream( istype ) )
|
---|
347 | istype * ?|?( istype * is, float _Complex * fc ) {
|
---|
348 | float re, im;
|
---|
349 | fmt( is, "%g%gi", &re, &im );
|
---|
350 | *fc = re + im * _Complex_I;
|
---|
351 | return is;
|
---|
352 | } // ?|?
|
---|
353 |
|
---|
354 | forall( dtype istype | istream( istype ) )
|
---|
355 | istype * ?|?( istype * is, double _Complex * dc ) {
|
---|
356 | double re, im;
|
---|
357 | fmt( is, "%lf%lfi", &re, &im );
|
---|
358 | *dc = re + im * _Complex_I;
|
---|
359 | return is;
|
---|
360 | } // ?|?
|
---|
361 |
|
---|
362 | forall( dtype istype | istream( istype ) )
|
---|
363 | istype * ?|?( istype * is, long double _Complex * ldc ) {
|
---|
364 | long double re, im;
|
---|
365 | fmt( is, "%Lf%Lfi", &re, &im );
|
---|
366 | *ldc = re + im * _Complex_I;
|
---|
367 | return is;
|
---|
368 | } // ?|?
|
---|
369 |
|
---|
370 | _Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
|
---|
371 | forall( dtype istype | istream( istype ) )
|
---|
372 | istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
|
---|
373 | fmt( is, "%s", cstr.s );
|
---|
374 | return is;
|
---|
375 | } // cstr
|
---|
376 |
|
---|
377 | _Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
|
---|
378 | forall( dtype istype | istream( istype ) )
|
---|
379 | istype * ?|?( istype * is, _Istream_cstrC cstr ) {
|
---|
380 | char buf[16];
|
---|
381 | sprintf( buf, "%%%ds", cstr.size );
|
---|
382 | fmt( is, buf, cstr.s );
|
---|
383 | return is;
|
---|
384 | } // cstr
|
---|
385 |
|
---|
386 | // Local Variables: //
|
---|
387 | // tab-width: 4 //
|
---|
388 | // compile-command: "cfa iostream.c" //
|
---|
389 | // End: //
|
---|