| 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 : Mon Dec 7 23:08:02 2015
|
|---|
| 13 | // Update Count : 24
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "iostream.h"
|
|---|
| 17 | extern "C" {
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | #include <string.h> // strlen
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 23 | ostype * ?|?( ostype *os, char c ) {
|
|---|
| 24 | return write( os, &c, 1 );
|
|---|
| 25 | } // ?|?
|
|---|
| 26 |
|
|---|
| 27 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 28 | ostype * ?|?( ostype *os, int i ) {
|
|---|
| 29 | char buffer[32]; // larger than the largest integer
|
|---|
| 30 | sprintf( buffer, "%d", i );
|
|---|
| 31 | return write( os, buffer, strlen( buffer ) );
|
|---|
| 32 | } // ?|?
|
|---|
| 33 |
|
|---|
| 34 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 35 | ostype * ?|?( ostype *os, double d ) {
|
|---|
| 36 | char buffer[32]; // larger than the largest double
|
|---|
| 37 | sprintf( buffer, "%g", d );
|
|---|
| 38 | return write( os, buffer, strlen( buffer ) );
|
|---|
| 39 | } // ?|?
|
|---|
| 40 |
|
|---|
| 41 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 42 | ostype * ?|?( ostype *os, const char *cp ) {
|
|---|
| 43 | return write( os, cp, strlen( cp ) );
|
|---|
| 44 | } // ?|?
|
|---|
| 45 |
|
|---|
| 46 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 47 | ostype * ?|?( ostype *os, const void *p ) {
|
|---|
| 48 | char buffer[32]; // larger than the largest pointer
|
|---|
| 49 | sprintf( buffer, "%p", p );
|
|---|
| 50 | return write( os, buffer, strlen( buffer ) );
|
|---|
| 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
|
|---|
| 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 ) {
|
|---|
| 71 | os | i | ' ';
|
|---|
| 72 | }
|
|---|
| 73 | for_each( begin, end, print );
|
|---|
| 74 | } // ?|?
|
|---|
| 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 ) {
|
|---|
| 80 | void print( elt_type i ) { os | i | ' '; }
|
|---|
| 81 | for_each_reverse( begin, end, print );
|
|---|
| 82 | } // ?|?
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | forall( dtype istype | istream( istype ) )
|
|---|
| 86 | istype * ?|?( istype *is, char *cp ) {
|
|---|
| 87 | return read( is, cp, 1 );
|
|---|
| 88 | } // ?|?
|
|---|
| 89 |
|
|---|
| 90 | forall( dtype istype | istream( istype ) )
|
|---|
| 91 | istype * ?|?( istype *is, int *ip ) {
|
|---|
| 92 | char cur;
|
|---|
| 93 |
|
|---|
| 94 | // skip some whitespace
|
|---|
| 95 | do {
|
|---|
| 96 | is | &cur;
|
|---|
| 97 | if ( fail( is ) || eof( is ) ) return is;
|
|---|
| 98 | } while ( !( cur >= '0' && cur <= '9' ) );
|
|---|
| 99 |
|
|---|
| 100 | // accumulate digits
|
|---|
| 101 | *ip = 0;
|
|---|
| 102 | while ( cur >= '0' && cur <= '9' ) {
|
|---|
| 103 | *ip = *ip * 10 + ( cur - '0' );
|
|---|
| 104 | is | &cur;
|
|---|
| 105 | if ( fail( is ) || eof( is ) ) return is;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | unread( is, cur );
|
|---|
| 109 | return is;
|
|---|
| 110 | } // ?|?
|
|---|
| 111 |
|
|---|
| 112 | // Local Variables: //
|
|---|
| 113 | // tab-width: 4 //
|
|---|
| 114 | // compile-command: "cfa iostream.c" //
|
|---|
| 115 | // End: //
|
|---|