source: translator/examples/fstream.c @ 51b7345

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

initial commit

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[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
6extern "C" {
7#include <stdio.h>
8#include <stdlib.h>
9}
10
11struct ofstream
12{
13  FILE *file;
14  int fail;
15};
16
17ofstream *
18write( 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
27int
28fail( ofstream *os )
29{
30  return os->fail;
31}
32
33static ofstream*
34make_ofstream()
35{
36  ofstream *new_stream = malloc( sizeof( ofstream ) );
37  new_stream->fail = 0;
38  return new_stream;
39}
40
41ofstream *
42ofstream_stdout()
43{
44  ofstream *stdout_stream = make_ofstream();
45  stdout_stream->file = stdout;
46  return stdout_stream;
47}
48
49ofstream *
50ofstream_stderr()
51{
52  ofstream *stderr_stream = make_ofstream();
53  stderr_stream->file = stderr;
54  return stderr_stream;
55}
56
57ofstream *
58ofstream_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
66void
67ofstream_close( ofstream *os )
68{
69  if( os->file != stdout && os->file != stderr ) {
70    os->fail = fclose( os->file );
71  }
72  free( os );
73}
74
75struct ifstream
76{
77  FILE *file;
78  int fail;
79  int eof;
80};
81
82ifstream *
83read( 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 
93ifstream *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
103int fail( ifstream *is )
104{
105  return is->fail;
106}
107
108int eof( ifstream *is )
109{
110  return is->eof;
111}
112
113static ifstream*
114make_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
122ifstream *ifstream_stdin()
123{
124  ifstream *stdin_stream = make_ifstream();
125  stdin_stream->file = stdin;
126  return stdin_stream;
127}
128
129ifstream *
130ifstream_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
Note: See TracBrowser for help on using the repository browser.