source: src/examples/iostream.c @ 15db1ab

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

fix recursive include bug in shadow includes, major clean of examples, add several long long routines to prelude

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