source: src/examples/iostream.c @ 839ccbb

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

Merge branch 'master' of plg2:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 2.7 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 Nov 20 13:19:19 2015
13// Update Count     : 9
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
53forall( type elt_type | writeable( elt_type ),
54                type iterator_type | iterator( iterator_type, elt_type ),
55                dtype os_type | ostream( os_type ) )
56void write( iterator_type begin, iterator_type end, os_type *os ) {
57        void print( elt_type i ) {
58                os << i << ' ';
59        }
60        for_each( begin, end, print );
61} // ?<<?
62
63forall( type elt_type | writeable( elt_type ),
64                type iterator_type | iterator( iterator_type, elt_type ),
65                dtype os_type | ostream( os_type ) )
66void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
67        void print( elt_type i ) {
68                os << i << ' ';
69        }
70        for_each_reverse( begin, end, print );
71} // ?<<?
72
73
74forall( dtype istype | istream( istype ) )
75istype * ?>>?( istype *is, char *cp ) {
76        return read( is, cp, 1 );
77} // ?>>?
78
79forall( dtype istype | istream( istype ) )
80istype * ?>>?( istype *is, int *ip ) {
81        char cur;
82 
83        // skip some whitespace
84        do {
85                is >> &cur;
86                if ( fail( is ) || eof( is ) ) return is;
87        } while ( !( cur >= '0' && cur <= '9' ) );
88 
89        // accumulate digits
90        *ip = 0;
91        while ( cur >= '0' && cur <= '9' ) {
92                *ip = *ip * 10 + ( cur - '0' );
93                is >> &cur;
94                if ( fail( is ) || eof( is ) ) return is;
95        }
96 
97        unread( is, cur );
98        return is;
99} // ?>>?
100
101// Local Variables: //
102// tab-width: 4 //
103// compile-command: "cfa iostream.c" //
104// End: //
Note: See TracBrowser for help on using the repository browser.