source: translator/examples/fstream.c@ c8ffe20b

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since c8ffe20b was 134b86a, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

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

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[51b73452]1// "cfa -E fstream.c > fstream_out.c"
2// "cfa -c -o fstream.o fstream.c"
[134b86a]3
[51b73452]4#include "fstream.h"
5
6extern "C" {
7#include <stdio.h>
8#include <stdlib.h>
9}
10
[134b86a]11struct ofstream {
12 FILE *file;
13 int fail;
[51b73452]14};
15
[134b86a]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;
[51b73452]22}
23
[134b86a]24int fail( ofstream *os ) {
25 return os->fail;
[51b73452]26}
27
[134b86a]28static ofstream * make_ofstream() {
29 ofstream *new_stream = malloc( sizeof( ofstream ) );
30 new_stream->fail = 0;
31 return new_stream;
[51b73452]32}
33
[134b86a]34ofstream * ofstream_stdout() {
35 ofstream *stdout_stream = make_ofstream();
36 stdout_stream->file = stdout;
37 return stdout_stream;
[51b73452]38}
39
[134b86a]40ofstream * ofstream_stderr() {
41 ofstream *stderr_stream = make_ofstream();
42 stderr_stream->file = stderr;
43 return stderr_stream;
[51b73452]44}
45
[134b86a]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;
[51b73452]51}
52
[134b86a]53void ofstream_close( ofstream *os ) {
54 if( os->file != stdout && os->file != stderr ) {
55 os->fail = fclose( os->file );
56 }
57 free( os );
[51b73452]58}
59
[134b86a]60struct ifstream {
61 FILE *file;
62 int fail;
63 int eof;
[51b73452]64};
65
[134b86a]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;
[51b73452]73}
74
[134b86a]75ifstream *unread( ifstream *is, char c ) {
76 if( !is->fail ) {
77 if( EOF == ungetc( c, is->file ) ) {
78 is->fail = 1;
79 }
[51b73452]80 }
[134b86a]81 return is;
[51b73452]82}
83
[134b86a]84int fail( ifstream *is ) {
85 return is->fail;
[51b73452]86}
87
[134b86a]88int eof( ifstream *is ) {
89 return is->eof;
[51b73452]90}
91
[134b86a]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;
[51b73452]97}
98
[134b86a]99ifstream * ifstream_stdin() {
100 ifstream *stdin_stream = make_ifstream();
101 stdin_stream->file = stdin;
102 return stdin_stream;
[51b73452]103}
104
[134b86a]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;
[51b73452]110}
Note: See TracBrowser for help on using the repository browser.