source: src/libcfa/iostream.c @ 44f44617

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 44f44617 was 86f384b, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

make tuple separator a separator rather than terminator

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