source: src/examples/iostream.c @ faf8857

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 faf8857 was cf16f94, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

create temporary return variable for return expressions, remove unnecessary code after temporary-return-variable change, fix missing lvalue qualifiers, change stream operator to |

  • Property mode set to 100644
File size: 2.9 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 Dec  7 23:08:02 2015
13// Update Count     : 24
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];                                                                        // larger than the largest integer
30        sprintf( buffer, "%d", i );
31        return write( os, buffer, strlen( buffer ) );
32} // ?|?
33
34forall( dtype ostype | ostream( ostype ) )
35ostype * ?|?( ostype *os, double d ) {
36        char buffer[32];                                                                        // larger than the largest double
37        sprintf( buffer, "%g", d );
38        return write( os, buffer, strlen( buffer ) );
39} // ?|?
40
41forall( dtype ostype | ostream( ostype ) )
42ostype * ?|?( ostype *os, const char *cp ) {
43        return write( os, cp, strlen( cp ) );
44} // ?|?
45
46forall( dtype ostype | ostream( ostype ) )
47ostype * ?|?( ostype *os, const void *p ) {
48        char buffer[32];                                                                        // larger than the largest pointer
49        sprintf( buffer, "%p", p );
50        return write( os, buffer, strlen( buffer ) );
51} // ?|?
52
53
54forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 
55retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
56  return manip(os);
57}
58
59forall( dtype ostype | ostream( ostype ) ) 
60ostype * endl( ostype * os ) {
61  os | "\n";
62  // flush
63  return os;
64} // endl
65
66forall( type elt_type | writeable( elt_type ),
67                type iterator_type | iterator( iterator_type, elt_type ),
68                dtype os_type | ostream( os_type ) )
69void write( iterator_type begin, iterator_type end, os_type *os ) {
70        void print( elt_type i ) {
71                os | i | ' ';
72        }
73        for_each( begin, end, print );
74} // ?|?
75
76forall( type elt_type | writeable( elt_type ),
77                type iterator_type | iterator( iterator_type, elt_type ),
78                dtype os_type | ostream( os_type ) )
79void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
80        void print( elt_type i ) { os | i | ' '; }
81        for_each_reverse( begin, end, print );
82} // ?|?
83
84
85forall( dtype istype | istream( istype ) )
86istype * ?|?( istype *is, char *cp ) {
87        return read( is, cp, 1 );
88} // ?|?
89
90forall( dtype istype | istream( istype ) )
91istype * ?|?( istype *is, int *ip ) {
92        char cur;
93 
94        // skip some whitespace
95        do {
96                is | &cur;
97                if ( fail( is ) || eof( is ) ) return is;
98        } while ( !( cur >= '0' && cur <= '9' ) );
99 
100        // accumulate digits
101        *ip = 0;
102        while ( cur >= '0' && cur <= '9' ) {
103                *ip = *ip * 10 + ( cur - '0' );
104                is | &cur;
105                if ( fail( is ) || eof( is ) ) return is;
106        }
107 
108        unread( is, cur );
109        return is;
110} // ?|?
111
112// Local Variables: //
113// tab-width: 4 //
114// compile-command: "cfa iostream.c" //
115// End: //
Note: See TracBrowser for help on using the repository browser.