source: src/libcfa/iostream.c @ 22854f8

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 22854f8 was 22854f8, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

Merge branch 'master' of plg2:software/cfa/cfa-cc

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