source: src/libcfa/iostream.c@ d3b7937

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since d3b7937 was d3b7937, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

building runtime library (first attempt)

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