[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
|
---|
[cf16f94] | 12 | // Last Modified On : Mon Dec 7 23:08:02 2015
|
---|
| 13 | // Update Count : 24
|
---|
[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 ) {
|
---|
[e56cfdb0] | 29 | char buffer[32]; // larger than the largest integer
|
---|
[86bd7c1f] | 30 | sprintf( buffer, "%d", i );
|
---|
| 31 | return write( os, buffer, strlen( buffer ) );
|
---|
[cf16f94] | 32 | } // ?|?
|
---|
[51b73452] | 33 |
|
---|
| 34 | forall( dtype ostype | ostream( ostype ) )
|
---|
[cf16f94] | 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 ) );
|
---|
[cf16f94] | 39 | } // ?|?
|
---|
[134b86a] | 40 |
|
---|
| 41 | forall( dtype ostype | ostream( ostype ) )
|
---|
[cf16f94] | 42 | ostype * ?|?( ostype *os, const char *cp ) {
|
---|
[86bd7c1f] | 43 | return write( os, cp, strlen( cp ) );
|
---|
[cf16f94] | 44 | } // ?|?
|
---|
[51b73452] | 45 |
|
---|
[e56cfdb0] | 46 | forall( dtype ostype | ostream( ostype ) )
|
---|
[cf16f94] | 47 | ostype * ?|?( ostype *os, const void *p ) {
|
---|
[e56cfdb0] | 48 | char buffer[32]; // larger than the largest pointer
|
---|
| 49 | sprintf( buffer, "%p", p );
|
---|
| 50 | return write( os, buffer, strlen( buffer ) );
|
---|
[cf16f94] | 51 | } // ?|?
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) )
|
---|
| 55 | retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
|
---|
| 56 | return manip(os);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | forall( dtype ostype | ostream( ostype ) )
|
---|
| 60 | ostype * endl( ostype * os ) {
|
---|
| 61 | os | "\n";
|
---|
| 62 | // flush
|
---|
| 63 | return os;
|
---|
| 64 | } // endl
|
---|
[e56cfdb0] | 65 |
|
---|
| 66 | forall( type elt_type | writeable( elt_type ),
|
---|
| 67 | type iterator_type | iterator( iterator_type, elt_type ),
|
---|
| 68 | dtype os_type | ostream( os_type ) )
|
---|
| 69 | void write( iterator_type begin, iterator_type end, os_type *os ) {
|
---|
| 70 | void print( elt_type i ) {
|
---|
[cf16f94] | 71 | os | i | ' ';
|
---|
[e56cfdb0] | 72 | }
|
---|
| 73 | for_each( begin, end, print );
|
---|
[cf16f94] | 74 | } // ?|?
|
---|
[e56cfdb0] | 75 |
|
---|
| 76 | forall( type elt_type | writeable( elt_type ),
|
---|
| 77 | type iterator_type | iterator( iterator_type, elt_type ),
|
---|
| 78 | dtype os_type | ostream( os_type ) )
|
---|
| 79 | void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
|
---|
[cf16f94] | 80 | void print( elt_type i ) { os | i | ' '; }
|
---|
[e56cfdb0] | 81 | for_each_reverse( begin, end, print );
|
---|
[cf16f94] | 82 | } // ?|?
|
---|
[e56cfdb0] | 83 |
|
---|
| 84 |
|
---|
[51b73452] | 85 | forall( dtype istype | istream( istype ) )
|
---|
[cf16f94] | 86 | istype * ?|?( istype *is, char *cp ) {
|
---|
[86bd7c1f] | 87 | return read( is, cp, 1 );
|
---|
[cf16f94] | 88 | } // ?|?
|
---|
[51b73452] | 89 |
|
---|
| 90 | forall( dtype istype | istream( istype ) )
|
---|
[cf16f94] | 91 | istype * ?|?( istype *is, int *ip ) {
|
---|
[86bd7c1f] | 92 | char cur;
|
---|
[51b73452] | 93 |
|
---|
[86bd7c1f] | 94 | // skip some whitespace
|
---|
| 95 | do {
|
---|
[cf16f94] | 96 | is | &cur;
|
---|
[86bd7c1f] | 97 | if ( fail( is ) || eof( is ) ) return is;
|
---|
| 98 | } while ( !( cur >= '0' && cur <= '9' ) );
|
---|
[51b73452] | 99 |
|
---|
[86bd7c1f] | 100 | // accumulate digits
|
---|
| 101 | *ip = 0;
|
---|
| 102 | while ( cur >= '0' && cur <= '9' ) {
|
---|
| 103 | *ip = *ip * 10 + ( cur - '0' );
|
---|
[cf16f94] | 104 | is | &cur;
|
---|
[86bd7c1f] | 105 | if ( fail( is ) || eof( is ) ) return is;
|
---|
| 106 | }
|
---|
[51b73452] | 107 |
|
---|
[86bd7c1f] | 108 | unread( is, cur );
|
---|
| 109 | return is;
|
---|
[cf16f94] | 110 | } // ?|?
|
---|
[86bd7c1f] | 111 |
|
---|
| 112 | // Local Variables: //
|
---|
| 113 | // tab-width: 4 //
|
---|
| 114 | // compile-command: "cfa iostream.c" //
|
---|
| 115 | // End: //
|
---|