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