| 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 : Wed Feb 17 14:19:56 2016
|
|---|
| 13 | // Update Count : 76
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "iostream"
|
|---|
| 17 |
|
|---|
| 18 | extern "C" {
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <string.h> // strlen
|
|---|
| 21 | #include <float.h> // DBL_DIG, LDBL_DIG
|
|---|
| 22 | #include <complex.h> // creal, cimag
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 26 | ostype * ?|?( ostype *os, char c ) {
|
|---|
| 27 | return write( os, &c, 1 );
|
|---|
| 28 | } // ?|?
|
|---|
| 29 |
|
|---|
| 30 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 31 | ostype * ?|?( ostype *os, int i ) {
|
|---|
| 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 ) );
|
|---|
| 64 | } // ?|?
|
|---|
| 65 |
|
|---|
| 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 |
|
|---|
| 72 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 73 | ostype * ?|?( ostype *os, double d ) {
|
|---|
| 74 | char buffer[32];
|
|---|
| 75 | return write( os, buffer, sprintf( buffer, "%.*lg", DBL_DIG, d ) );
|
|---|
| 76 | } // ?|?
|
|---|
| 77 |
|
|---|
| 78 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 79 | ostype * ?|?( ostype *os, long double d ) {
|
|---|
| 80 | char buffer[32];
|
|---|
| 81 | return write( os, buffer, sprintf( buffer, "%.*Lg", LDBL_DIG, d ) );
|
|---|
| 82 | } // ?|?
|
|---|
| 83 |
|
|---|
| 84 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 85 | ostype * ?|?( ostype *os, float _Complex c ) {
|
|---|
| 86 | return os | crealf( c ) | (cimagf( c ) < 0 ? "" : "+") | cimagf( c ) | 'i';
|
|---|
| 87 | } // ?|?
|
|---|
| 88 |
|
|---|
| 89 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 90 | ostype * ?|?( ostype *os, double _Complex c ) {
|
|---|
| 91 | return os | creal( c ) | (cimag( c ) < 0 ? "" : "+") | cimag( c ) | 'i';
|
|---|
| 92 | } // ?|?
|
|---|
| 93 |
|
|---|
| 94 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 95 | ostype * ?|?( ostype *os, long double _Complex c ) {
|
|---|
| 96 | return os | creall( c ) | (cimagl( c ) < 0 ? "" : "+") | cimagl( c ) | 'i';
|
|---|
| 97 | } // ?|?
|
|---|
| 98 |
|
|---|
| 99 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 100 | ostype * ?|?( ostype *os, const void *p ) {
|
|---|
| 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 ) );
|
|---|
| 108 | } // ?|?
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) )
|
|---|
| 112 | retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
|
|---|
| 113 | return manip( os );
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 117 | ostype * endl( ostype * os ) {
|
|---|
| 118 | os | "\n";
|
|---|
| 119 | flush( os );
|
|---|
| 120 | return os;
|
|---|
| 121 | } // endl
|
|---|
| 122 |
|
|---|
| 123 | //---------------------------------------
|
|---|
| 124 |
|
|---|
| 125 | forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
|
|---|
| 126 | dtype os_type | ostream( os_type ) )
|
|---|
| 127 | void write( iterator_type begin, iterator_type end, os_type *os ) {
|
|---|
| 128 | void print( elt_type i ) { os | i | ' '; }
|
|---|
| 129 | for_each( begin, end, print );
|
|---|
| 130 | } // ?|?
|
|---|
| 131 |
|
|---|
| 132 | forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
|
|---|
| 133 | dtype os_type | ostream( os_type ) )
|
|---|
| 134 | void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
|
|---|
| 135 | void print( elt_type i ) { os | i | ' '; }
|
|---|
| 136 | for_each_reverse( begin, end, print );
|
|---|
| 137 | } // ?|?
|
|---|
| 138 |
|
|---|
| 139 | //---------------------------------------
|
|---|
| 140 |
|
|---|
| 141 | forall( dtype istype | istream( istype ) )
|
|---|
| 142 | istype * ?|?( istype *is, char *cp ) {
|
|---|
| 143 | return read( is, cp, 1 );
|
|---|
| 144 | } // ?|?
|
|---|
| 145 |
|
|---|
| 146 | forall( dtype istype | istream( istype ) )
|
|---|
| 147 | istype * ?|?( istype *is, int *ip ) {
|
|---|
| 148 | return get( is, ip );
|
|---|
| 149 | } // ?|?
|
|---|
| 150 |
|
|---|
| 151 | // Local Variables: //
|
|---|
| 152 | // tab-width: 4 //
|
|---|
| 153 | // compile-command: "cfa iostream.c" //
|
|---|
| 154 | // End: //
|
|---|