[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 | #undef __cplusplus |
---|
| 9 | extern "C" { |
---|
| 10 | #include <string.h> |
---|
| 11 | #include <ctype.h> |
---|
| 12 | #include <stdio.h> |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 16 | ostype * |
---|
| 17 | ?<<?( ostype *os, char c ) |
---|
| 18 | { |
---|
| 19 | return write( os, &c, 1 ); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 23 | ostype * |
---|
| 24 | ?<<?( ostype *os, int i ) |
---|
| 25 | { |
---|
| 26 | char buffer[20]; // larger than the largest integer |
---|
| 27 | sprintf( buffer, "%d", i ); |
---|
| 28 | return write( os, buffer, strlen( buffer ) ); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 32 | ostype * |
---|
| 33 | ?<<?( ostype *os, const char *cp ) |
---|
| 34 | { |
---|
| 35 | return write( os, cp, strlen( cp ) ); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | forall( dtype istype | istream( istype ) ) |
---|
| 39 | istype * |
---|
| 40 | ?>>?( istype *is, char *cp ) |
---|
| 41 | { |
---|
| 42 | return read( is, cp, 1 ); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | forall( dtype istype | istream( istype ) ) |
---|
| 46 | istype * |
---|
| 47 | ?>>?( istype *is, int *ip ) |
---|
| 48 | { |
---|
| 49 | char cur; |
---|
| 50 | |
---|
| 51 | // skip some whitespace |
---|
| 52 | do { |
---|
| 53 | is >> &cur; |
---|
| 54 | if( fail( is ) || eof( is ) ) return is; |
---|
| 55 | } while( !( cur >= '0' && cur <= '9' ) ); |
---|
| 56 | |
---|
| 57 | // accumulate digits |
---|
| 58 | *ip = 0; |
---|
| 59 | while( cur >= '0' && cur <= '9' ) { |
---|
| 60 | *ip = *ip * 10 + ( cur - '0' ); |
---|
| 61 | is >> &cur; |
---|
| 62 | if( fail( is ) || eof( is ) ) return is; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | unread( is, cur ); |
---|
| 66 | return is; |
---|
| 67 | } |
---|