source: translator/examples/fstream.c @ f7f6785

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since f7f6785 was 42dcae7, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

re-inserted remove and reorder hoisted aggregate, fixed example programs

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#include "fstream.h"
2
3extern "C" {
4#include <stdio.h>
5#include <stdlib.h>
6}
7
8struct ofstream {
9    FILE *file;
10    int fail;
11};
12
13ofstream *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
21int fail( ofstream *os ) {
22    return os->fail;
23}
24
25static ofstream *make_ofstream() {
26    ofstream *new_stream = malloc( sizeof( ofstream ) );
27    new_stream->fail = 0;
28    return new_stream;
29}
30
31ofstream *ofstream_stdout() {
32    ofstream *stdout_stream = make_ofstream();
33    stdout_stream->file = stdout;
34    return stdout_stream;
35}
36
37ofstream *ofstream_stderr() {
38    ofstream *stderr_stream = make_ofstream();
39    stderr_stream->file = stderr;
40    return stderr_stream;
41}
42
43ofstream *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
50void ofstream_close( ofstream *os ) {
51    if ( os->file != stdout && os->file != stderr ) {
52        os->fail = fclose( os->file );
53    }
54    free( os );
55}
56
57struct ifstream {
58    FILE *file;
59    int fail;
60    int eof;
61};
62
63ifstream *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 
72ifstream *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
81int fail( ifstream *is ) {
82    return is->fail;
83}
84
85int eof( ifstream *is ) {
86    return is->eof;
87}
88
89static 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
96ifstream *ifstream_stdin() {
97    ifstream *stdin_stream = make_ifstream();
98    stdin_stream->file = stdin;
99    return stdin_stream;
100}
101
102ifstream *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}
Note: See TracBrowser for help on using the repository browser.