source: src/libcfa/iostream.c @ a772d8ab

string
Last change on this file since a772d8ab was 53ba273, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

switch from std=c99 to std=gnu99, update latex macros, refrat and extend user manual, update limits/iostream/fstream, add rational numbers

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