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 : Rob Schluntz
|
---|
12 | // Last Modified On : Thu Apr 14 16:02:09 2016
|
---|
13 | // Update Count : 278
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "iostream"
|
---|
17 |
|
---|
18 | extern "C" {
|
---|
19 | #include <stdio.h>
|
---|
20 | #include <string.h> // strlen
|
---|
21 | #include <float.h> // DBL_DIG, LDBL_DIG
|
---|
22 | #include <complex.h> // creal, cimag
|
---|
23 | #include <ctype.h> // isspace, ispunct
|
---|
24 | }
|
---|
25 |
|
---|
26 | forall( dtype ostype | ostream( ostype ) )
|
---|
27 | ostype * ?|?( ostype *os, char c ) {
|
---|
28 | prtfmt( os, "%c", c );
|
---|
29 | sepOff( os );
|
---|
30 | return os;
|
---|
31 | } // ?|?
|
---|
32 |
|
---|
33 | forall( dtype ostype | ostream( ostype ) )
|
---|
34 | ostype * ?|?( ostype *os, short int si ) {
|
---|
35 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
36 | sepReset( os );
|
---|
37 | prtfmt( os, "%hd", si );
|
---|
38 | return os;
|
---|
39 | } // ?|?
|
---|
40 |
|
---|
41 | forall( dtype ostype | ostream( ostype ) )
|
---|
42 | ostype * ?|?( ostype *os, unsigned short int usi ) {
|
---|
43 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
44 | sepReset( os );
|
---|
45 | prtfmt( os, "%hu", usi );
|
---|
46 | return os;
|
---|
47 | } // ?|?
|
---|
48 |
|
---|
49 | forall( dtype ostype | ostream( ostype ) )
|
---|
50 | ostype * ?|?( ostype *os, int i ) {
|
---|
51 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
52 | sepReset( os );
|
---|
53 | prtfmt( os, "%d", i );
|
---|
54 | return os;
|
---|
55 | } // ?|?
|
---|
56 |
|
---|
57 | forall( dtype ostype | ostream( ostype ) )
|
---|
58 | ostype * ?|?( ostype *os, unsigned int ui ) {
|
---|
59 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
60 | sepReset( os );
|
---|
61 | prtfmt( os, "%u", ui );
|
---|
62 | return os;
|
---|
63 | } // ?|?
|
---|
64 |
|
---|
65 | forall( dtype ostype | ostream( ostype ) )
|
---|
66 | ostype * ?|?( ostype *os, long int li ) {
|
---|
67 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
68 | sepReset( os );
|
---|
69 | prtfmt( os, "%ld", li );
|
---|
70 | return os;
|
---|
71 | } // ?|?
|
---|
72 |
|
---|
73 | forall( dtype ostype | ostream( ostype ) )
|
---|
74 | ostype * ?|?( ostype *os, unsigned long int uli ) {
|
---|
75 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
76 | sepReset( os );
|
---|
77 | prtfmt( os, "%lu", uli );
|
---|
78 | return os;
|
---|
79 | } // ?|?
|
---|
80 |
|
---|
81 | forall( dtype ostype | ostream( ostype ) )
|
---|
82 | ostype * ?|?( ostype *os, long long int lli ) {
|
---|
83 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
84 | sepReset( os );
|
---|
85 | prtfmt( os, "%lld", lli );
|
---|
86 | return os;
|
---|
87 | } // ?|?
|
---|
88 |
|
---|
89 | forall( dtype ostype | ostream( ostype ) )
|
---|
90 | ostype * ?|?( ostype *os, unsigned long long int ulli ) {
|
---|
91 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
92 | sepReset( os );
|
---|
93 | prtfmt( os, "%llu", ulli );
|
---|
94 | return os;
|
---|
95 | } // ?|?
|
---|
96 |
|
---|
97 | forall( dtype ostype | ostream( ostype ) )
|
---|
98 | ostype * ?|?( ostype *os, float f ) {
|
---|
99 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
100 | sepReset( os );
|
---|
101 | prtfmt( os, "%g", f );
|
---|
102 | return os;
|
---|
103 | } // ?|?
|
---|
104 |
|
---|
105 | forall( dtype ostype | ostream( ostype ) )
|
---|
106 | ostype * ?|?( ostype *os, double d ) {
|
---|
107 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
108 | sepReset( os );
|
---|
109 | prtfmt( os, "%.*lg", DBL_DIG, d );
|
---|
110 | return os;
|
---|
111 | } // ?|?
|
---|
112 |
|
---|
113 | forall( dtype ostype | ostream( ostype ) )
|
---|
114 | ostype * ?|?( ostype *os, long double ld ) {
|
---|
115 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
116 | sepReset( os );
|
---|
117 | prtfmt( os, "%.*Lg", LDBL_DIG, ld );
|
---|
118 | return os;
|
---|
119 | } // ?|?
|
---|
120 |
|
---|
121 | forall( dtype ostype | ostream( ostype ) )
|
---|
122 | ostype * ?|?( ostype *os, float _Complex fc ) {
|
---|
123 | os | crealf( fc );
|
---|
124 | _Bool temp = sepDisable( os ); // disable separators within complex value
|
---|
125 | if ( cimagf( fc ) >= 0 ) os | '+'; // negative value prints '-'
|
---|
126 | os | cimagf( fc ) | 'i';
|
---|
127 | sepReset( os, temp ); // reset separator
|
---|
128 | return os;
|
---|
129 | } // ?|?
|
---|
130 |
|
---|
131 | forall( dtype ostype | ostream( ostype ) )
|
---|
132 | ostype * ?|?( ostype *os, double _Complex dc ) {
|
---|
133 | os | creal( dc );
|
---|
134 | _Bool temp = sepDisable( os ); // disable separators within complex value
|
---|
135 | if ( cimag( dc ) >= 0 ) os | '+'; // negative value prints '-'
|
---|
136 | os | cimag( dc ) | 'i';
|
---|
137 | sepReset( os, temp ); // reset separator
|
---|
138 | return os;
|
---|
139 | } // ?|?
|
---|
140 |
|
---|
141 | forall( dtype ostype | ostream( ostype ) )
|
---|
142 | ostype * ?|?( ostype *os, long double _Complex ldc ) {
|
---|
143 | os | creall( ldc );
|
---|
144 | _Bool temp = sepDisable( os ); // disable separators within complex value
|
---|
145 | if ( cimagl( ldc ) >= 0 ) os | '+'; // negative value prints '-'
|
---|
146 | os | cimagl( ldc ) | 'i';
|
---|
147 | sepReset( os, temp ); // reset separator
|
---|
148 | return os;
|
---|
149 | } // ?|?
|
---|
150 |
|
---|
151 | forall( dtype ostype | ostream( ostype ) )
|
---|
152 | ostype * ?|?( ostype *os, const char *cp ) {
|
---|
153 | enum { Open = 1, Close, OpenClose };
|
---|
154 | static const unsigned char mask[256] = {
|
---|
155 | // opening delimiters
|
---|
156 | ['('] : Open, ['['] : Open, ['{'] : Open,
|
---|
157 | ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
|
---|
158 | // closing delimiters
|
---|
159 | [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
|
---|
160 | [')'] : Close, [']'] : Close, ['}'] : Close,
|
---|
161 | ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
|
---|
162 | // opening-closing delimiters
|
---|
163 | ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose,
|
---|
164 | ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
|
---|
165 | }; // mask
|
---|
166 |
|
---|
167 | int len = strlen( cp );
|
---|
168 | // null string => no separator
|
---|
169 | if ( len == 0 ) { sepOff( os ); return os; }
|
---|
170 | // first character IS NOT spacing or closing punctuation => add left separator
|
---|
171 | unsigned char ch = cp[0]; // must make unsigned
|
---|
172 | if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
|
---|
173 | prtfmt( os, "%s", sepGet( os ) );
|
---|
174 | } // if
|
---|
175 | // last character IS spacing or opening punctuation => turn off separator for next item
|
---|
176 | unsigned int posn = len - 1;
|
---|
177 | ch = cp[posn]; // must make unsigned
|
---|
178 | if ( mask[ ch ] == Open || mask[ ch ] == OpenClose ) {
|
---|
179 | sepOff( os );
|
---|
180 | } else {
|
---|
181 | sepOn( os );
|
---|
182 | } // if
|
---|
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 ) ) prtfmt( os, "%s", sepGet( os ) );
|
---|
189 | sepReset( os );
|
---|
190 | prtfmt( os, "%p", p );
|
---|
191 | return os;
|
---|
192 | } // ?|?
|
---|
193 |
|
---|
194 |
|
---|
195 | forall( dtype ostype | ostream( ostype ) )
|
---|
196 | ostype * ?|?( ostype *os, ostype * (* manip)( ostype * ) ) {
|
---|
197 | return manip( os );
|
---|
198 | } // ?|?
|
---|
199 |
|
---|
200 | forall( dtype ostype | ostream( ostype ) )
|
---|
201 | ostype * endl( ostype * os ) {
|
---|
202 | os | '\n';
|
---|
203 | flush( os );
|
---|
204 | sepOff( os );
|
---|
205 | return os;
|
---|
206 | } // endl
|
---|
207 |
|
---|
208 | forall( dtype ostype | ostream( ostype ) )
|
---|
209 | ostype * sepOn( ostype * os ) {
|
---|
210 | sepOn( os );
|
---|
211 | return os;
|
---|
212 | } // sepOn
|
---|
213 |
|
---|
214 | forall( dtype ostype | ostream( ostype ) )
|
---|
215 | ostype * sepOff( ostype * os ) {
|
---|
216 | sepOff( os );
|
---|
217 | return os;
|
---|
218 | } // sepOff
|
---|
219 |
|
---|
220 | forall( dtype ostype | ostream( ostype ) )
|
---|
221 | ostype * sepEnable( ostype * os ) {
|
---|
222 | sepEnable( os );
|
---|
223 | return os;
|
---|
224 | } // sepEnable
|
---|
225 |
|
---|
226 | forall( dtype ostype | ostream( ostype ) )
|
---|
227 | ostype * sepDisable( ostype * os ) {
|
---|
228 | sepDisable( os );
|
---|
229 | return os;
|
---|
230 | } // sepDisable
|
---|
231 |
|
---|
232 | //---------------------------------------
|
---|
233 |
|
---|
234 | forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
|
---|
235 | void write( iteratortype begin, iteratortype end, ostype *os ) {
|
---|
236 | void print( elttype i ) { os | i; }
|
---|
237 | for_each( begin, end, print );
|
---|
238 | } // ?|?
|
---|
239 |
|
---|
240 | forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
|
---|
241 | void write_reverse( iteratortype begin, iteratortype end, ostype *os ) {
|
---|
242 | void print( elttype i ) { os | i; }
|
---|
243 | for_each_reverse( begin, end, print );
|
---|
244 | } // ?|?
|
---|
245 |
|
---|
246 | //---------------------------------------
|
---|
247 |
|
---|
248 | forall( dtype istype | istream( istype ) )
|
---|
249 | istype * ?|?( istype * is, char * c ) {
|
---|
250 | scanfmt( is, "%c", c );
|
---|
251 | return is;
|
---|
252 | } // ?|?
|
---|
253 |
|
---|
254 | forall( dtype istype | istream( istype ) )
|
---|
255 | istype * ?|?( istype * is, short int * si ) {
|
---|
256 | scanfmt( is, "%hd", si );
|
---|
257 | return is;
|
---|
258 | } // ?|?
|
---|
259 |
|
---|
260 | forall( dtype istype | istream( istype ) )
|
---|
261 | istype * ?|?( istype * is, unsigned short int * usi ) {
|
---|
262 | scanfmt( is, "%hu", usi );
|
---|
263 | return is;
|
---|
264 | } // ?|?
|
---|
265 |
|
---|
266 | forall( dtype istype | istream( istype ) )
|
---|
267 | istype * ?|?( istype * is, int * i ) {
|
---|
268 | scanfmt( is, "%d", i );
|
---|
269 | return is;
|
---|
270 | } // ?|?
|
---|
271 |
|
---|
272 | forall( dtype istype | istream( istype ) )
|
---|
273 | istype * ?|?( istype * is, unsigned int * ui ) {
|
---|
274 | scanfmt( is, "%u", ui );
|
---|
275 | return is;
|
---|
276 | } // ?|?
|
---|
277 |
|
---|
278 | forall( dtype istype | istream( istype ) )
|
---|
279 | istype * ?|?( istype * is, long int * li ) {
|
---|
280 | scanfmt( is, "%ld", li );
|
---|
281 | return is;
|
---|
282 | } // ?|?
|
---|
283 |
|
---|
284 | forall( dtype istype | istream( istype ) )
|
---|
285 | istype * ?|?( istype * is, unsigned long int * ulli ) {
|
---|
286 | scanfmt( is, "%lu", ulli );
|
---|
287 | return is;
|
---|
288 | } // ?|?
|
---|
289 |
|
---|
290 | forall( dtype istype | istream( istype ) )
|
---|
291 | istype * ?|?( istype * is, long long int * lli ) {
|
---|
292 | scanfmt( is, "%lld", lli );
|
---|
293 | return is;
|
---|
294 | } // ?|?
|
---|
295 |
|
---|
296 | forall( dtype istype | istream( istype ) )
|
---|
297 | istype * ?|?( istype * is, unsigned long long int * ulli ) {
|
---|
298 | scanfmt( is, "%llu", ulli );
|
---|
299 | return is;
|
---|
300 | } // ?|?
|
---|
301 |
|
---|
302 |
|
---|
303 | forall( dtype istype | istream( istype ) )
|
---|
304 | istype * ?|?( istype * is, float * f ) {
|
---|
305 | scanfmt( is, "%f", f );
|
---|
306 | return is;
|
---|
307 | } // ?|?
|
---|
308 |
|
---|
309 | forall( dtype istype | istream( istype ) )
|
---|
310 | istype * ?|?( istype * is, double * d ) {
|
---|
311 | scanfmt( is, "%lf", d );
|
---|
312 | return is;
|
---|
313 | } // ?|?
|
---|
314 |
|
---|
315 | forall( dtype istype | istream( istype ) )
|
---|
316 | istype * ?|?( istype * is, long double * ld ) {
|
---|
317 | scanfmt( is, "%Lf", ld );
|
---|
318 | return is;
|
---|
319 | } // ?|?
|
---|
320 |
|
---|
321 |
|
---|
322 | forall( dtype istype | istream( istype ) )
|
---|
323 | istype * ?|?( istype * is, float _Complex * fc ) {
|
---|
324 | float re, im;
|
---|
325 | scanfmt( is, "%g%gi", &re, &im );
|
---|
326 | *fc = re + im * _Complex_I;
|
---|
327 | return is;
|
---|
328 | } // ?|?
|
---|
329 |
|
---|
330 | forall( dtype istype | istream( istype ) )
|
---|
331 | istype * ?|?( istype * is, double _Complex * dc ) {
|
---|
332 | double re, im;
|
---|
333 | scanfmt( is, "%lf%lfi", &re, &im );
|
---|
334 | *dc = re + im * _Complex_I;
|
---|
335 | return is;
|
---|
336 | } // ?|?
|
---|
337 |
|
---|
338 | forall( dtype istype | istream( istype ) )
|
---|
339 | istype * ?|?( istype * is, long double _Complex * ldc ) {
|
---|
340 | long double re, im;
|
---|
341 | scanfmt( is, "%Lf%Lfi", &re, &im );
|
---|
342 | *ldc = re + im * _Complex_I;
|
---|
343 | return is;
|
---|
344 | } // ?|?
|
---|
345 |
|
---|
346 | _Istream_cstrUC cstr( char * str ) { _Istream_cstrUC s = { str }; return s; }
|
---|
347 | forall( dtype istype | istream( istype ) )
|
---|
348 | istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
|
---|
349 | scanfmt( is, "%s", cstr.s );
|
---|
350 | return is;
|
---|
351 | } // cstr
|
---|
352 |
|
---|
353 | _Istream_cstrC cstr( char * str, int size ) { _Istream_cstrC s = { str, size }; return s; }
|
---|
354 | forall( dtype istype | istream( istype ) )
|
---|
355 | istype * ?|?( istype * is, _Istream_cstrC cstr ) {
|
---|
356 | char buf[16];
|
---|
357 | sprintf( buf, "%%%ds", cstr.size );
|
---|
358 | scanfmt( is, buf, cstr.s );
|
---|
359 | return is;
|
---|
360 | } // cstr
|
---|
361 |
|
---|
362 | // Local Variables: //
|
---|
363 | // tab-width: 4 //
|
---|
364 | // compile-command: "cfa iostream.c" //
|
---|
365 | // End: //
|
---|