source: src/libcfa/iostream.c @ 5721a6d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 5721a6d was 5721a6d, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

correctly set type for complex constants, consolidate function name tables, add offsetof, refactor printing complex constants to use basic types

  • Property mode set to 100644
File size: 4.5 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//
9// Author           : Richard C. Bilson
10// Created On       : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
[5721a6d]12// Last Modified On : Mon Feb  1 14:20:30 2016
13// Update Count     : 60
[86bd7c1f]14//
[51b7345]15
[d3b7937]16#include "iostream"
17
[51b7345]18extern "C" {
19#include <stdio.h>
[839ccbb]20#include <string.h>                                                                             // strlen
[d3b7937]21#include <complex.h>                                                                    // creal, cimag
[51b7345]22}
23
24forall( dtype ostype | ostream( ostype ) )
[cf16f94]25ostype * ?|?( ostype *os, char c ) {
[86bd7c1f]26        return write( os, &c, 1 );
[cf16f94]27} // ?|?
[51b7345]28
29forall( dtype ostype | ostream( ostype ) )
[cf16f94]30ostype * ?|?( ostype *os, int i ) {
[784deab]31        char buffer[32];
32        return write( os, buffer, sprintf( buffer, "%d", i ) );
33} // ?|?
34
35forall( dtype ostype | ostream( ostype ) )
36ostype * ?|?( ostype *os, unsigned int i ) {
37        char buffer[32];
38        return write( os, buffer, sprintf( buffer, "%u", i ) );
39} // ?|?
40
41forall( dtype ostype | ostream( ostype ) )
42ostype * ?|?( ostype *os, long int i ) {
43        char buffer[32];
44        return write( os, buffer, sprintf( buffer, "%ld", i ) );
45} // ?|?
46
47forall( dtype ostype | ostream( ostype ) )
48ostype * ?|?( ostype *os, long long int i ) {
49        char buffer[32];
50        return write( os, buffer, sprintf( buffer, "%lld", i ) );
51} // ?|?
52
53forall( dtype ostype | ostream( ostype ) )
54ostype * ?|?( ostype *os, unsigned long int i ) {
55        char buffer[32];
56        return write( os, buffer, sprintf( buffer, "%lu", i ) );
57} // ?|?
58
59forall( dtype ostype | ostream( ostype ) )
60ostype * ?|?( ostype *os, unsigned long long int i ) {
61        char buffer[32];
62        return write( os, buffer, sprintf( buffer, "%llu", i ) );
[cf16f94]63} // ?|?
[51b7345]64
[d3b7937]65forall( dtype ostype | ostream( ostype ) )
66ostype * ?|?( ostype *os, float f ) {
67        char buffer[32];
68        return write( os, buffer, sprintf( buffer, "%g", f ) );
69} // ?|?
70
[51b7345]71forall( dtype ostype | ostream( ostype ) )
[cf16f94]72ostype * ?|?( ostype *os, double d ) {
[784deab]73        char buffer[32];
74        return write( os, buffer, sprintf( buffer, "%g", d ) );
[cf16f94]75} // ?|?
[134b86a]76
77forall( dtype ostype | ostream( ostype ) )
[784deab]78ostype * ?|?( ostype *os, long double d ) {
79        char buffer[32];
80        return write( os, buffer, sprintf( buffer, "%Lg", d ) );
[cf16f94]81} // ?|?
[51b7345]82
[d3b7937]83forall( dtype ostype | ostream( ostype ) )
84ostype * ?|?( ostype *os, float _Complex c ) {
[5721a6d]85        return os | crealf( c ) | (cimagf( c ) < 0 ? "" : "+") | cimagf( c ) | 'i';
[d3b7937]86} // ?|?
87
88forall( dtype ostype | ostream( ostype ) )
89ostype * ?|?( ostype *os, double _Complex c ) {
[5721a6d]90        return os | creal( c ) | (cimag( c ) < 0 ? "" : "+") | cimag( c ) | 'i';
[d3b7937]91} // ?|?
92
93forall( dtype ostype | ostream( ostype ) )
94ostype * ?|?( ostype *os, long double _Complex c ) {
[5721a6d]95        return os | creall( c ) | (cimagl( c ) < 0 ? "" : "+") | cimagl( c ) | 'i';
[d3b7937]96} // ?|?
97
[e56cfdb0]98forall( dtype ostype | ostream( ostype ) )
[cf16f94]99ostype * ?|?( ostype *os, const void *p ) {
[784deab]100        char buffer[32];
101        return write( os, buffer, sprintf( buffer, "%p", p ) );
102} // ?|?
103
104forall( dtype ostype | ostream( ostype ) )
105ostype * ?|?( ostype *os, const char *cp ) {
106        return write( os, cp, strlen( cp ) );
[cf16f94]107} // ?|?
108
109
110forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 
111retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
112  return manip(os);
113}
114
115forall( dtype ostype | ostream( ostype ) ) 
116ostype * endl( ostype * os ) {
117  os | "\n";
118  // flush
119  return os;
120} // endl
[e56cfdb0]121
122forall( type elt_type | writeable( elt_type ),
123                type iterator_type | iterator( iterator_type, elt_type ),
124                dtype os_type | ostream( os_type ) )
125void write( iterator_type begin, iterator_type end, os_type *os ) {
126        void print( elt_type i ) {
[cf16f94]127                os | i | ' ';
[e56cfdb0]128        }
129        for_each( begin, end, print );
[cf16f94]130} // ?|?
[e56cfdb0]131
132forall( type elt_type | writeable( elt_type ),
133                type iterator_type | iterator( iterator_type, elt_type ),
134                dtype os_type | ostream( os_type ) )
135void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
[cf16f94]136        void print( elt_type i ) { os | i | ' '; }
[e56cfdb0]137        for_each_reverse( begin, end, print );
[cf16f94]138} // ?|?
[e56cfdb0]139
140
[51b7345]141forall( dtype istype | istream( istype ) )
[cf16f94]142istype * ?|?( istype *is, char *cp ) {
[86bd7c1f]143        return read( is, cp, 1 );
[cf16f94]144} // ?|?
[51b7345]145
146forall( dtype istype | istream( istype ) )
[cf16f94]147istype * ?|?( istype *is, int *ip ) {
[86bd7c1f]148        char cur;
[51b7345]149 
[86bd7c1f]150        // skip some whitespace
151        do {
[cf16f94]152                is | &cur;
[86bd7c1f]153                if ( fail( is ) || eof( is ) ) return is;
154        } while ( !( cur >= '0' && cur <= '9' ) );
[51b7345]155 
[86bd7c1f]156        // accumulate digits
157        *ip = 0;
158        while ( cur >= '0' && cur <= '9' ) {
159                *ip = *ip * 10 + ( cur - '0' );
[cf16f94]160                is | &cur;
[86bd7c1f]161                if ( fail( is ) || eof( is ) ) return is;
162        }
[51b7345]163 
[86bd7c1f]164        unread( is, cur );
165        return is;
[cf16f94]166} // ?|?
[86bd7c1f]167
168// Local Variables: //
169// tab-width: 4 //
170// compile-command: "cfa iostream.c" //
171// End: //
Note: See TracBrowser for help on using the repository browser.