Changeset 42dcae7 for translator/examples/fstream.c
- Timestamp:
- Nov 25, 2014, 9:16:10 AM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 3848e0e
- Parents:
- d11f789
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/examples/fstream.c
rd11f789 r42dcae7 1 // "cfa -E fstream.c > fstream_out.c"2 // "cfa -c -o fstream.o fstream.c"3 4 1 #include "fstream.h" 5 2 … … 14 11 }; 15 12 16 ofstream * 17 if ( !os->fail ) {13 ofstream *write( ofstream *os, const char *data, streamsize_type size ) { 14 if ( ! os->fail ) { 18 15 fwrite( data, size, 1, os->file ); 19 16 os->fail = ferror( os->file ); … … 26 23 } 27 24 28 static ofstream * 25 static ofstream *make_ofstream() { 29 26 ofstream *new_stream = malloc( sizeof( ofstream ) ); 30 27 new_stream->fail = 0; … … 32 29 } 33 30 34 ofstream * 31 ofstream *ofstream_stdout() { 35 32 ofstream *stdout_stream = make_ofstream(); 36 33 stdout_stream->file = stdout; … … 38 35 } 39 36 40 ofstream * 37 ofstream *ofstream_stderr() { 41 38 ofstream *stderr_stream = make_ofstream(); 42 39 stderr_stream->file = stderr; … … 44 41 } 45 42 46 ofstream * 43 ofstream *ofstream_fromfile( const char *name ) { 47 44 ofstream *file_stream = make_ofstream(); 48 45 file_stream->file = fopen( name, "w" ); … … 52 49 53 50 void ofstream_close( ofstream *os ) { 54 if ( os->file != stdout && os->file != stderr ) {51 if ( os->file != stdout && os->file != stderr ) { 55 52 os->fail = fclose( os->file ); 56 53 } … … 64 61 }; 65 62 66 ifstream * 67 if ( !is->fail && !is->eof ) {63 ifstream *read( ifstream *is, char *data, streamsize_type size ) { 64 if ( ! is->fail && ! is->eof ) { 68 65 fread( data, size, 1, is->file ); 69 66 is->fail = ferror( is->file ); … … 74 71 75 72 ifstream *unread( ifstream *is, char c ) { 76 if ( !is->fail ) {77 if (EOF == ungetc( c, is->file ) ) {73 if ( ! is->fail ) { 74 if ( ! EOF == ungetc( c, is->file ) ) { 78 75 is->fail = 1; 79 76 } … … 90 87 } 91 88 92 static ifstream * 89 static ifstream *make_ifstream() { 93 90 ifstream *new_stream = malloc( sizeof( ifstream ) ); 94 91 new_stream->fail = 0; … … 97 94 } 98 95 99 ifstream * 96 ifstream *ifstream_stdin() { 100 97 ifstream *stdin_stream = make_ifstream(); 101 98 stdin_stream->file = stdin; … … 103 100 } 104 101 105 ifstream * 102 ifstream *ifstream_fromfile( const char *name ) { 106 103 ifstream *file_stream = make_ifstream(); 107 104 file_stream->file = fopen( name, "r" );
Note: See TracChangeset
for help on using the changeset viewer.