source: src/libcfa/iostream.c @ 82c948c

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 82c948c was 44574f2, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add _Bool read and read endl-manipulator to skip whitespace

  • Property mode set to 100644
File size: 12.9 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 : Thu Jan 25 13:09:28 2018
13// Update Count     : 467
14//
15
16#include "iostream"
17
18extern "C" {
19#include <stdio.h>
20#include <stdbool.h>                                                                    // true/false
21//#include <string.h>                                                                           // strlen, strcmp
22extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
23extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
24#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
25#include <complex.h>                                                                    // creal, cimag
26}
27
28forall( dtype ostype | ostream( ostype ) )
29ostype & ?|?( ostype & os, _Bool b ) {
30        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
31        fmt( os, "%s", b ? "true" : "false" );
32        return os;
33} // ?|?
34
35forall( dtype ostype | ostream( ostype ) )
36ostype & ?|?( ostype & os, char ch ) {
37        fmt( os, "%c", ch );
38        if ( ch == '\n' ) setNL( os, true );
39        sepOff( os );
40        return os;
41} // ?|?
42
43forall( dtype ostype | ostream( ostype ) )
44ostype & ?|?( ostype & os, signed char c ) {
45        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
46        fmt( os, "%hhd", c );
47        return os;
48} // ?|?
49
50forall( dtype ostype | ostream( ostype ) )
51ostype & ?|?( ostype & os, unsigned char c ) {
52        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
53        fmt( os, "%hhu", c );
54        return os;
55} // ?|?
56
57forall( dtype ostype | ostream( ostype ) )
58ostype & ?|?( ostype & os, short int si ) {
59        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
60        fmt( os, "%hd", si );
61        return os;
62} // ?|?
63
64forall( dtype ostype | ostream( ostype ) )
65ostype & ?|?( ostype & os, unsigned short int usi ) {
66        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
67        fmt( os, "%hu", usi );
68        return os;
69} // ?|?
70
71forall( dtype ostype | ostream( ostype ) )
72ostype & ?|?( ostype & os, int i ) {
73        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
74        fmt( os, "%d", i );
75        return os;
76} // ?|?
77
78forall( dtype ostype | ostream( ostype ) )
79ostype & ?|?( ostype & os, unsigned int ui ) {
80        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
81        fmt( os, "%u", ui );
82        return os;
83} // ?|?
84
85forall( dtype ostype | ostream( ostype ) )
86ostype & ?|?( ostype & os, long int li ) {
87        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
88        fmt( os, "%ld", li );
89        return os;
90} // ?|?
91
92forall( dtype ostype | ostream( ostype ) )
93ostype & ?|?( ostype & os, unsigned long int uli ) {
94        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
95        fmt( os, "%lu", uli );
96        return os;
97} // ?|?
98
99forall( dtype ostype | ostream( ostype ) )
100ostype & ?|?( ostype & os, long long int lli ) {
101        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
102        fmt( os, "%lld", lli );
103        return os;
104} // ?|?
105
106forall( dtype ostype | ostream( ostype ) )
107ostype & ?|?( ostype & os, unsigned long long int ulli ) {
108        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
109        fmt( os, "%llu", ulli );
110        return os;
111} // ?|?
112
113forall( dtype ostype | ostream( ostype ) )
114ostype & ?|?( ostype & os, float f ) {
115        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
116        fmt( os, "%g", f );
117        return os;
118} // ?|?
119
120forall( dtype ostype | ostream( ostype ) )
121ostype & ?|?( ostype & os, double d ) {
122        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
123        fmt( os, "%.*lg", DBL_DIG, d );
124        return os;
125} // ?|?
126
127forall( dtype ostype | ostream( ostype ) )
128ostype & ?|?( ostype & os, long double ld ) {
129        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
130        fmt( os, "%.*Lg", LDBL_DIG, ld );
131        return os;
132} // ?|?
133
134forall( dtype ostype | ostream( ostype ) )
135ostype & ?|?( ostype & os, float _Complex fc ) {
136        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
137        fmt( os, "%g%+gi", crealf( fc ), cimagf( fc ) );
138        return os;
139} // ?|?
140
141forall( dtype ostype | ostream( ostype ) )
142ostype & ?|?( ostype & os, double _Complex dc ) {
143        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
144        fmt( os, "%.*lg%+.*lgi", DBL_DIG, creal( dc ), DBL_DIG, cimag( dc ) );
145        return os;
146} // ?|?
147
148forall( dtype ostype | ostream( ostype ) )
149ostype & ?|?( ostype & os, long double _Complex ldc ) {
150        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
151        fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) );
152        return os;
153} // ?|?
154
155forall( dtype ostype | ostream( ostype ) )
156ostype & ?|?( ostype & os, const char * str ) {
157        enum { Open = 1, Close, OpenClose };
158        static const unsigned char mask[256] @= {
159                // opening delimiters, no space after
160                ['('] : Open, ['['] : Open, ['{'] : Open,
161                ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
162                [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
163                // closing delimiters, no space before
164                [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
165                ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
166                [')'] : Close, [']'] : Close, ['}'] : Close,
167                // opening-closing delimiters, no space before or after
168                ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
169                [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
170        }; // mask
171
172  if ( str[0] == '\0' ) { sepOff( os ); return os; }            // null string => no separator
173
174        // first character IS NOT spacing or closing punctuation => add left separator
175        unsigned char ch = str[0];                                                      // must make unsigned
176        if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
177                fmt( os, "%s", sepGetCur( os ) );
178        } // if
179
180        // if string starts line, must reset to determine open state because separator is off
181        sepReset( os );                                                                         // reset separator
182
183        // last character IS spacing or opening punctuation => turn off separator for next item
184        size_t len = strlen( str );
185        ch = str[len - 1];                                                                      // must make unsigned
186        if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
187                sepOn( os );
188        } else {
189                sepOff( os );
190        } // if
191        if ( ch == '\n' ) setNL( os, true );                            // check *AFTER* sepPrt call above as it resets NL flag
192        return write( os, str, len );
193} // ?|?
194
195forall( dtype ostype | ostream( ostype ) )
196ostype & ?|?( ostype & os, const char16_t * str ) {
197        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
198        fmt( os, "%ls", str );
199        return os;
200} // ?|?
201
202#if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
203forall( dtype ostype | ostream( ostype ) )
204ostype & ?|?( ostype & os, const char32_t * str ) {
205        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
206        fmt( os, "%ls", str );
207        return os;
208} // ?|?
209#endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
210
211forall( dtype ostype | ostream( ostype ) )
212ostype & ?|?( ostype & os, const wchar_t * str ) {
213        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
214        fmt( os, "%ls", str );
215        return os;
216} // ?|?
217
218forall( dtype ostype | ostream( ostype ) )
219ostype & ?|?( ostype & os, const void * p ) {
220        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
221        fmt( os, "%p", p );
222        return os;
223} // ?|?
224
225
226// tuples
227forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } )
228ostype & ?|?( ostype & os, T arg, Params rest ) {
229        os | arg;                                                                                       // print first argument
230        sepSetCur( os, sepGetTuple( os ) );                                     // switch to tuple separator
231        os | rest;                                                                                      // print remaining arguments
232        sepSetCur( os, sepGet( os ) );                                          // switch to regular separator
233        return os;
234} // ?|?
235
236
237// manipulators
238forall( dtype ostype | ostream( ostype ) )
239ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
240        return manip( os );
241} // ?|?
242
243forall( dtype ostype | ostream( ostype ) )
244ostype & sep( ostype & os ) {
245        os | sepGet( os );
246        return os;
247} // sep
248
249forall( dtype ostype | ostream( ostype ) )
250ostype & sepTuple( ostype & os ) {
251        os | sepGetTuple( os );
252        return os;
253} // sepTuple
254
255forall( dtype ostype | ostream( ostype ) )
256ostype & endl( ostype & os ) {
257        os | '\n';
258        setNL( os, true );
259        flush( os );
260        sepOff( os );                                                                           // prepare for next line
261        return os;
262} // endl
263
264forall( dtype ostype | ostream( ostype ) )
265ostype & sepOn( ostype & os ) {
266        sepOn( os );
267        return os;
268} // sepOn
269
270forall( dtype ostype | ostream( ostype ) )
271ostype & sepOff( ostype & os ) {
272        sepOff( os );
273        return os;
274} // sepOff
275
276forall( dtype ostype | ostream( ostype ) )
277ostype & sepEnable( ostype & os ) {
278        sepEnable( os );
279        return os;
280} // sepEnable
281
282forall( dtype ostype | ostream( ostype ) )
283ostype & sepDisable( ostype & os ) {
284        sepDisable( os );
285        return os;
286} // sepDisable
287
288//---------------------------------------
289
290forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
291void write( iterator_type begin, iterator_type end, ostype & os ) {
292        void print( elt_type i ) { os | i; }
293        for_each( begin, end, print );
294} // ?|?
295
296forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
297void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
298        void print( elt_type i ) { os | i; }
299        for_each_reverse( begin, end, print );
300} // ?|?
301
302//---------------------------------------
303
304forall( dtype istype | istream( istype ) )
305istype & ?|?( istype & is, _Bool & b ) {
306        char val[6];
307        fmt( is, "%5s", val );
308        if ( strcmp( val, "true" ) == 0 ) b = true;
309        else if ( strcmp( val, "false" ) == 0 ) b = false;
310        else {
311                fprintf( stderr, "invalid _Bool constant\n" );
312                abort();
313        } // if
314        return is;
315} // ?|?
316
317forall( dtype istype | istream( istype ) )
318istype & ?|?( istype & is, char & c ) {
319        fmt( is, "%c", &c );                                                            // must pass pointer through varg to fmt
320        return is;
321} // ?|?
322
323forall( dtype istype | istream( istype ) )
324istype & ?|?( istype & is, signed char & sc ) {
325        fmt( is, "%hhd", &sc );
326        return is;
327} // ?|?
328
329forall( dtype istype | istream( istype ) )
330istype & ?|?( istype & is, unsigned char & usc ) {
331        fmt( is, "%hhu", &usc );
332        return is;
333} // ?|?
334
335forall( dtype istype | istream( istype ) )
336istype & ?|?( istype & is, short int & si ) {
337        fmt( is, "%hd", &si );
338        return is;
339} // ?|?
340
341forall( dtype istype | istream( istype ) )
342istype & ?|?( istype & is, unsigned short int & usi ) {
343        fmt( is, "%hu", &usi );
344        return is;
345} // ?|?
346
347forall( dtype istype | istream( istype ) )
348istype & ?|?( istype & is, int & i ) {
349        fmt( is, "%d", &i );
350        return is;
351} // ?|?
352
353forall( dtype istype | istream( istype ) )
354istype & ?|?( istype & is, unsigned int & ui ) {
355        fmt( is, "%u", &ui );
356        return is;
357} // ?|?
358
359forall( dtype istype | istream( istype ) )
360istype & ?|?( istype & is, long int & li ) {
361        fmt( is, "%ld", &li );
362        return is;
363} // ?|?
364
365forall( dtype istype | istream( istype ) )
366istype & ?|?( istype & is, unsigned long int & ulli ) {
367        fmt( is, "%lu", &ulli );
368        return is;
369} // ?|?
370
371forall( dtype istype | istream( istype ) )
372istype & ?|?( istype & is, long long int & lli ) {
373        fmt( is, "%lld", &lli );
374        return is;
375} // ?|?
376
377forall( dtype istype | istream( istype ) )
378istype & ?|?( istype & is, unsigned long long int & ulli ) {
379        fmt( is, "%llu", &ulli );
380        return is;
381} // ?|?
382
383
384forall( dtype istype | istream( istype ) )
385istype & ?|?( istype & is, float & f ) {
386        fmt( is, "%f", &f );
387        return is;
388} // ?|?
389
390forall( dtype istype | istream( istype ) )
391istype & ?|?( istype & is, double & d ) {
392        fmt( is, "%lf", &d );
393        return is;
394} // ?|?
395
396forall( dtype istype | istream( istype ) )
397istype & ?|?( istype & is, long double & ld ) {
398        fmt( is, "%Lf", &ld );
399        return is;
400} // ?|?
401
402
403forall( dtype istype | istream( istype ) )
404istype & ?|?( istype & is, float _Complex & fc ) {
405        float re, im;
406        fmt( is, "%g%gi", &re, &im );
407        fc = re + im * _Complex_I;
408        return is;
409} // ?|?
410
411forall( dtype istype | istream( istype ) )
412istype & ?|?( istype & is, double _Complex & dc ) {
413        double re, im;
414        fmt( is, "%lf%lfi", &re, &im );
415        dc = re + im * _Complex_I;
416        return is;
417} // ?|?
418
419forall( dtype istype | istream( istype ) )
420istype & ?|?( istype & is, long double _Complex & ldc ) {
421        long double re, im;
422        fmt( is, "%Lf%Lfi", &re, &im );
423        ldc = re + im * _Complex_I;
424        return is;
425} // ?|?
426
427forall( dtype istype | istream( istype ) )
428istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
429        return manip( is );
430} // ?|?
431
432forall( dtype istype | istream( istype ) )
433istype & endl( istype & is ) {
434        fmt( is, "%*[ \t\f\n\r\v]" );                                           // ignore whitespace
435        return is;
436} // endl
437
438_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
439forall( dtype istype | istream( istype ) )
440istype & ?|?( istype & is, _Istream_cstrUC cstr ) {
441        fmt( is, "%s", cstr.s );
442        return is;
443} // cstr
444
445_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
446forall( dtype istype | istream( istype ) )
447istype & ?|?( istype & is, _Istream_cstrC cstr ) {
448        char buf[16];
449        sprintf( buf, "%%%ds", cstr.size );
450        fmt( is, buf, cstr.s );
451        return is;
452} // cstr
453
454// Local Variables: //
455// tab-width: 4 //
456// compile-command: "cfa iostream.c" //
457// End: //
Note: See TracBrowser for help on using the repository browser.