| 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 : Thu Nov 19 17:54:38 2015
|
|---|
| 13 | // Update Count : 4
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "iostream.h"
|
|---|
| 17 | extern "C" {
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | //#include <string.h>
|
|---|
| 20 | //#include <ctype.h>
|
|---|
| 21 | typedef long unsigned int size_t;
|
|---|
| 22 | size_t strlen(const char *s);
|
|---|
| 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]; // larger than the largest integer
|
|---|
| 33 | sprintf( buffer, "%d", i );
|
|---|
| 34 | return write( os, buffer, strlen( buffer ) );
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 38 | ostype * ?<<?( ostype *os, double d ) {
|
|---|
| 39 | char buffer[32]; // larger than the largest double
|
|---|
| 40 | sprintf( buffer, "%g", d );
|
|---|
| 41 | return write( os, buffer, strlen( buffer ) );
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 45 | ostype * ?<<?( ostype *os, const char *cp ) {
|
|---|
| 46 | return write( os, cp, strlen( cp ) );
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | forall( dtype ostype | ostream( ostype ) )
|
|---|
| 50 | ostype * ?<<?( ostype *os, const void *p ) {
|
|---|
| 51 | char buffer[32]; // larger than the largest pointer
|
|---|
| 52 | sprintf( buffer, "%p", p );
|
|---|
| 53 | return write( os, buffer, strlen( buffer ) );
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | forall( type elt_type | writeable( elt_type ),
|
|---|
| 57 | type iterator_type | iterator( iterator_type, elt_type ),
|
|---|
| 58 | dtype os_type | ostream( os_type ) )
|
|---|
| 59 | void write( iterator_type begin, iterator_type end, os_type *os ) {
|
|---|
| 60 | void print( elt_type i ) {
|
|---|
| 61 | os << i << ' ';
|
|---|
| 62 | }
|
|---|
| 63 | for_each( begin, end, print );
|
|---|
| 64 | }
|
|---|
| 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_reverse( iterator_type begin, iterator_type end, os_type *os ) {
|
|---|
| 70 | void print( elt_type i ) {
|
|---|
| 71 | os << i << ' ';
|
|---|
| 72 | }
|
|---|
| 73 | for_each_reverse( begin, end, print );
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | forall( dtype istype | istream( istype ) )
|
|---|
| 78 | istype * ?>>?( istype *is, char *cp ) {
|
|---|
| 79 | return read( is, cp, 1 );
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | forall( dtype istype | istream( istype ) )
|
|---|
| 83 | istype * ?>>?( istype *is, int *ip ) {
|
|---|
| 84 | char cur;
|
|---|
| 85 |
|
|---|
| 86 | // skip some whitespace
|
|---|
| 87 | do {
|
|---|
| 88 | is >> &cur;
|
|---|
| 89 | if ( fail( is ) || eof( is ) ) return is;
|
|---|
| 90 | } while ( !( cur >= '0' && cur <= '9' ) );
|
|---|
| 91 |
|
|---|
| 92 | // accumulate digits
|
|---|
| 93 | *ip = 0;
|
|---|
| 94 | while ( cur >= '0' && cur <= '9' ) {
|
|---|
| 95 | *ip = *ip * 10 + ( cur - '0' );
|
|---|
| 96 | is >> &cur;
|
|---|
| 97 | if ( fail( is ) || eof( is ) ) return is;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | unread( is, cur );
|
|---|
| 101 | return is;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | // Local Variables: //
|
|---|
| 105 | // tab-width: 4 //
|
|---|
| 106 | // compile-command: "cfa iostream.c" //
|
|---|
| 107 | // End: //
|
|---|