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