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