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