[51b73452] | 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);
|
---|
[51b73452] | 14 | }
|
---|
| 15 |
|
---|
| 16 | forall( dtype ostype | ostream( ostype ) )
|
---|
[134b86a] | 17 | ostype * ?<<?( ostype *os, char c ) {
|
---|
| 18 | return write( os, &c, 1 );
|
---|
[51b73452] | 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 ) );
|
---|
[51b73452] | 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 ) );
|
---|
[51b73452] | 38 | }
|
---|
| 39 |
|
---|
| 40 | forall( dtype istype | istream( istype ) )
|
---|
[134b86a] | 41 | istype * ?>>?( istype *is, char *cp ) {
|
---|
| 42 | return read( is, cp, 1 );
|
---|
[51b73452] | 43 | }
|
---|
| 44 |
|
---|
| 45 | forall( dtype istype | istream( istype ) )
|
---|
[134b86a] | 46 | istype * ?>>?( istype *is, int *ip ) {
|
---|
| 47 | char cur;
|
---|
[51b73452] | 48 |
|
---|
[134b86a] | 49 | // skip some whitespace
|
---|
| 50 | do {
|
---|
| 51 | is >> &cur;
|
---|
[a32b204] | 52 | if ( fail( is ) || eof( is ) ) return is;
|
---|
| 53 | } while ( !( cur >= '0' && cur <= '9' ) );
|
---|
[51b73452] | 54 |
|
---|
[134b86a] | 55 | // accumulate digits
|
---|
| 56 | *ip = 0;
|
---|
[a32b204] | 57 | while ( cur >= '0' && cur <= '9' ) {
|
---|
[134b86a] | 58 | *ip = *ip * 10 + ( cur - '0' );
|
---|
| 59 | is >> &cur;
|
---|
[a32b204] | 60 | if ( fail( is ) || eof( is ) ) return is;
|
---|
[134b86a] | 61 | }
|
---|
[51b73452] | 62 |
|
---|
[134b86a] | 63 | unread( is, cur );
|
---|
| 64 | return is;
|
---|
[51b73452] | 65 | }
|
---|