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