[51b7345] | 1 | // "cfa -c -o iostream.o iostream.c" |
---|
| 2 | // "cfa -v -E iostream.c > iostream_out.c" |
---|
| 3 | // "cfa -CFA iostream.c > iostream_out.c" |
---|
| 4 | // "cfa iostream_out.c" |
---|
| 5 | // "gcc32 iostream_out.c LibCfa/libcfa.a" |
---|
| 6 | |
---|
| 7 | #include "iostream.h" |
---|
| 8 | extern "C" { |
---|
| 9 | #include <stdio.h> |
---|
[134b86a] | 10 | //#include <string.h> |
---|
| 11 | //#include <ctype.h> |
---|
| 12 | typedef long unsigned int size_t; |
---|
| 13 | size_t strlen(const char *s); |
---|
[51b7345] | 14 | } |
---|
| 15 | |
---|
| 16 | forall( dtype ostype | ostream( ostype ) ) |
---|
[134b86a] | 17 | ostype * ?<<?( ostype *os, char c ) { |
---|
| 18 | return write( os, &c, 1 ); |
---|
[51b7345] | 19 | } |
---|
| 20 | |
---|
| 21 | forall( dtype ostype | ostream( ostype ) ) |
---|
[134b86a] | 22 | ostype * ?<<?( ostype *os, int i ) { |
---|
| 23 | char buffer[20]; // larger than the largest integer |
---|
| 24 | sprintf( buffer, "%d", i ); |
---|
| 25 | return write( os, buffer, strlen( buffer ) ); |
---|
[51b7345] | 26 | } |
---|
| 27 | |
---|
| 28 | forall( dtype ostype | ostream( ostype ) ) |
---|
[134b86a] | 29 | ostype * ?<<?( ostype *os, double d ) { |
---|
| 30 | char buffer[32]; // larger than the largest double |
---|
| 31 | sprintf( buffer, "%g", d ); |
---|
| 32 | return write( os, buffer, strlen( buffer ) ); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 36 | ostype * ?<<?( ostype *os, const char *cp ) { |
---|
| 37 | return write( os, cp, strlen( cp ) ); |
---|
[51b7345] | 38 | } |
---|
| 39 | |
---|
| 40 | forall( dtype istype | istream( istype ) ) |
---|
[134b86a] | 41 | istype * ?>>?( istype *is, char *cp ) { |
---|
| 42 | return read( is, cp, 1 ); |
---|
[51b7345] | 43 | } |
---|
| 44 | |
---|
| 45 | forall( dtype istype | istream( istype ) ) |
---|
[134b86a] | 46 | istype * ?>>?( istype *is, int *ip ) { |
---|
| 47 | char cur; |
---|
[51b7345] | 48 | |
---|
[134b86a] | 49 | // skip some whitespace |
---|
| 50 | do { |
---|
| 51 | is >> &cur; |
---|
| 52 | if( fail( is ) || eof( is ) ) return is; |
---|
| 53 | } while( !( cur >= '0' && cur <= '9' ) ); |
---|
[51b7345] | 54 | |
---|
[134b86a] | 55 | // accumulate digits |
---|
| 56 | *ip = 0; |
---|
| 57 | while( cur >= '0' && cur <= '9' ) { |
---|
| 58 | *ip = *ip * 10 + ( cur - '0' ); |
---|
| 59 | is >> &cur; |
---|
| 60 | if( fail( is ) || eof( is ) ) return is; |
---|
| 61 | } |
---|
[51b7345] | 62 | |
---|
[134b86a] | 63 | unread( is, cur ); |
---|
| 64 | return is; |
---|
[51b7345] | 65 | } |
---|