[51b7345] | 1 | // "cfa -E fstream.c > fstream_out.c" |
---|
| 2 | // "cfa -c -o fstream.o fstream.c" |
---|
| 3 | #include "fstream.h" |
---|
| 4 | |
---|
| 5 | #undef __cplusplus |
---|
| 6 | extern "C" { |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <stdlib.h> |
---|
| 9 | } |
---|
| 10 | |
---|
| 11 | struct ofstream |
---|
| 12 | { |
---|
| 13 | FILE *file; |
---|
| 14 | int fail; |
---|
| 15 | }; |
---|
| 16 | |
---|
| 17 | ofstream * |
---|
| 18 | write( ofstream *os, const char *data, streamsize_type size ) |
---|
| 19 | { |
---|
| 20 | if( !os->fail ) { |
---|
| 21 | fwrite( data, size, 1, os->file ); |
---|
| 22 | os->fail = ferror( os->file ); |
---|
| 23 | } |
---|
| 24 | return os; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | int |
---|
| 28 | fail( ofstream *os ) |
---|
| 29 | { |
---|
| 30 | return os->fail; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | static ofstream* |
---|
| 34 | make_ofstream() |
---|
| 35 | { |
---|
| 36 | ofstream *new_stream = malloc( sizeof( ofstream ) ); |
---|
| 37 | new_stream->fail = 0; |
---|
| 38 | return new_stream; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | ofstream * |
---|
| 42 | ofstream_stdout() |
---|
| 43 | { |
---|
| 44 | ofstream *stdout_stream = make_ofstream(); |
---|
| 45 | stdout_stream->file = stdout; |
---|
| 46 | return stdout_stream; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | ofstream * |
---|
| 50 | ofstream_stderr() |
---|
| 51 | { |
---|
| 52 | ofstream *stderr_stream = make_ofstream(); |
---|
| 53 | stderr_stream->file = stderr; |
---|
| 54 | return stderr_stream; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | ofstream * |
---|
| 58 | ofstream_fromfile( const char *name ) |
---|
| 59 | { |
---|
| 60 | ofstream *file_stream = make_ofstream(); |
---|
| 61 | file_stream->file = fopen( name, "w" ); |
---|
| 62 | file_stream->fail = file_stream->file == 0; |
---|
| 63 | return file_stream; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void |
---|
| 67 | ofstream_close( ofstream *os ) |
---|
| 68 | { |
---|
| 69 | if( os->file != stdout && os->file != stderr ) { |
---|
| 70 | os->fail = fclose( os->file ); |
---|
| 71 | } |
---|
| 72 | free( os ); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | struct ifstream |
---|
| 76 | { |
---|
| 77 | FILE *file; |
---|
| 78 | int fail; |
---|
| 79 | int eof; |
---|
| 80 | }; |
---|
| 81 | |
---|
| 82 | ifstream * |
---|
| 83 | read( ifstream *is, char *data, streamsize_type size ) |
---|
| 84 | { |
---|
| 85 | if( !is->fail && !is->eof ) { |
---|
| 86 | fread( data, size, 1, is->file ); |
---|
| 87 | is->fail = ferror( is->file ); |
---|
| 88 | is->eof = feof( is->file ); |
---|
| 89 | } |
---|
| 90 | return is; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | ifstream *unread( ifstream *is, char c ) |
---|
| 94 | { |
---|
| 95 | if( !is->fail ) { |
---|
| 96 | if( EOF == ungetc( c, is->file ) ) { |
---|
| 97 | is->fail = 1; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | return is; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | int fail( ifstream *is ) |
---|
| 104 | { |
---|
| 105 | return is->fail; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | int eof( ifstream *is ) |
---|
| 109 | { |
---|
| 110 | return is->eof; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | static ifstream* |
---|
| 114 | make_ifstream() |
---|
| 115 | { |
---|
| 116 | ifstream *new_stream = malloc( sizeof( ifstream ) ); |
---|
| 117 | new_stream->fail = 0; |
---|
| 118 | new_stream->eof = 0; |
---|
| 119 | return new_stream; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | ifstream *ifstream_stdin() |
---|
| 123 | { |
---|
| 124 | ifstream *stdin_stream = make_ifstream(); |
---|
| 125 | stdin_stream->file = stdin; |
---|
| 126 | return stdin_stream; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | ifstream * |
---|
| 130 | ifstream_fromfile( const char *name ) |
---|
| 131 | { |
---|
| 132 | ifstream *file_stream = make_ifstream(); |
---|
| 133 | file_stream->file = fopen( name, "r" ); |
---|
| 134 | file_stream->fail = file_stream->file == 0; |
---|
| 135 | return file_stream; |
---|
| 136 | } |
---|
| 137 | |
---|