[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 |
---|
[839ccbb] | 12 | // Last Modified On : Fri Nov 20 13:19:19 2015 |
---|
| 13 | // Update Count : 9 |
---|
[86bd7c1f] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "iostream.h" |
---|
| 17 | extern "C" { |
---|
| 18 | #include <stdio.h> |
---|
[839ccbb] | 19 | #include <string.h> // strlen |
---|
[51b7345] | 20 | } |
---|
| 21 | |
---|
| 22 | forall( dtype ostype | ostream( ostype ) ) |
---|
[134b86a] | 23 | ostype * ?<<?( ostype *os, char c ) { |
---|
[86bd7c1f] | 24 | return write( os, &c, 1 ); |
---|
[839ccbb] | 25 | } // ?<<? |
---|
[51b7345] | 26 | |
---|
| 27 | forall( dtype ostype | ostream( ostype ) ) |
---|
[134b86a] | 28 | ostype * ?<<?( ostype *os, int i ) { |
---|
[e56cfdb0] | 29 | char buffer[32]; // larger than the largest integer |
---|
[86bd7c1f] | 30 | sprintf( buffer, "%d", i ); |
---|
| 31 | return write( os, buffer, strlen( buffer ) ); |
---|
[839ccbb] | 32 | } // ?<<? |
---|
[51b7345] | 33 | |
---|
| 34 | forall( dtype ostype | ostream( ostype ) ) |
---|
[134b86a] | 35 | ostype * ?<<?( ostype *os, double d ) { |
---|
[e56cfdb0] | 36 | char buffer[32]; // larger than the largest double |
---|
[86bd7c1f] | 37 | sprintf( buffer, "%g", d ); |
---|
| 38 | return write( os, buffer, strlen( buffer ) ); |
---|
[839ccbb] | 39 | } // ?<<? |
---|
[134b86a] | 40 | |
---|
| 41 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 42 | ostype * ?<<?( ostype *os, const char *cp ) { |
---|
[86bd7c1f] | 43 | return write( os, cp, strlen( cp ) ); |
---|
[839ccbb] | 44 | } // ?<<? |
---|
[51b7345] | 45 | |
---|
[e56cfdb0] | 46 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 47 | ostype * ?<<?( 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 ) ); |
---|
[839ccbb] | 51 | } // ?<<? |
---|
[e56cfdb0] | 52 | |
---|
| 53 | forall( type elt_type | writeable( elt_type ), |
---|
| 54 | type iterator_type | iterator( iterator_type, elt_type ), |
---|
| 55 | dtype os_type | ostream( os_type ) ) |
---|
| 56 | void 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 ); |
---|
[839ccbb] | 61 | } // ?<<? |
---|
[e56cfdb0] | 62 | |
---|
| 63 | forall( type elt_type | writeable( elt_type ), |
---|
| 64 | type iterator_type | iterator( iterator_type, elt_type ), |
---|
| 65 | dtype os_type | ostream( os_type ) ) |
---|
| 66 | void 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 ); |
---|
[839ccbb] | 71 | } // ?<<? |
---|
[e56cfdb0] | 72 | |
---|
| 73 | |
---|
[51b7345] | 74 | forall( dtype istype | istream( istype ) ) |
---|
[134b86a] | 75 | istype * ?>>?( istype *is, char *cp ) { |
---|
[86bd7c1f] | 76 | return read( is, cp, 1 ); |
---|
[839ccbb] | 77 | } // ?>>? |
---|
[51b7345] | 78 | |
---|
| 79 | forall( dtype istype | istream( istype ) ) |
---|
[134b86a] | 80 | istype * ?>>?( istype *is, int *ip ) { |
---|
[86bd7c1f] | 81 | char cur; |
---|
[51b7345] | 82 | |
---|
[86bd7c1f] | 83 | // skip some whitespace |
---|
| 84 | do { |
---|
| 85 | is >> &cur; |
---|
| 86 | if ( fail( is ) || eof( is ) ) return is; |
---|
| 87 | } while ( !( cur >= '0' && cur <= '9' ) ); |
---|
[51b7345] | 88 | |
---|
[86bd7c1f] | 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 | } |
---|
[51b7345] | 96 | |
---|
[86bd7c1f] | 97 | unread( is, cur ); |
---|
| 98 | return is; |
---|
[839ccbb] | 99 | } // ?>>? |
---|
[86bd7c1f] | 100 | |
---|
| 101 | // Local Variables: // |
---|
| 102 | // tab-width: 4 // |
---|
| 103 | // compile-command: "cfa iostream.c" // |
---|
| 104 | // End: // |
---|