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