source: src/libcfa/iostream.c @ 00e80b6

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 00e80b6 was 53a6c2a, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change meaning of sepOn, and replace #if with #pragma once in include files

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