source: translator/examples/fstream.c @ 134b86a

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 134b86a was 134b86a, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

add compiler flag to driver, update examples, fix unnamed bit fields

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