| 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> | 
|---|
| 10 | //#include <string.h> | 
|---|
| 11 | //#include <ctype.h> | 
|---|
| 12 | typedef long unsigned int size_t; | 
|---|
| 13 | size_t strlen(const char *s); | 
|---|
| 14 | } | 
|---|
| 15 |  | 
|---|
| 16 | forall( dtype ostype | ostream( ostype ) ) | 
|---|
| 17 | ostype * ?<<?( ostype *os, char c ) { | 
|---|
| 18 | return write( os, &c, 1 ); | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | forall( dtype ostype | ostream( ostype ) ) | 
|---|
| 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 ) ); | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | forall( dtype ostype | ostream( ostype ) ) | 
|---|
| 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 ) ); | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | forall( dtype istype | istream( istype ) ) | 
|---|
| 41 | istype * ?>>?( istype *is, char *cp ) { | 
|---|
| 42 | return read( is, cp, 1 ); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | forall( dtype istype | istream( istype ) ) | 
|---|
| 46 | istype * ?>>?( istype *is, int *ip ) { | 
|---|
| 47 | char cur; | 
|---|
| 48 |  | 
|---|
| 49 | // skip some whitespace | 
|---|
| 50 | do { | 
|---|
| 51 | is >> &cur; | 
|---|
| 52 | if( fail( is ) || eof( is ) ) return is; | 
|---|
| 53 | } while( !( cur >= '0' && cur <= '9' ) ); | 
|---|
| 54 |  | 
|---|
| 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 | } | 
|---|
| 62 |  | 
|---|
| 63 | unread( is, cur ); | 
|---|
| 64 | return is; | 
|---|
| 65 | } | 
|---|