Changeset 86bd7c1f for src/examples/fstream.c
- Timestamp:
- May 27, 2015, 8:33:19 PM (9 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:
- 00cc023
- Parents:
- 52ac3b4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/fstream.c
r52ac3b4 r86bd7c1f 1 // 2 // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo 3 // 4 // The contents of this file are covered under the licence agreement in the 5 // file "LICENCE" distributed with Cforall. 6 // 7 // fstream.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:12:33 2015 13 // Update Count : 2 14 // 15 1 16 #include "fstream.h" 2 17 … … 7 22 8 23 struct ofstream { 9 10 24 FILE *file; 25 int fail; 11 26 }; 12 27 13 28 ofstream *write( ofstream *os, const char *data, streamsize_type size ) { 14 15 fwrite( data, size, 1, os->file );16 os->fail = ferror( os->file );17 18 29 if ( ! os->fail ) { 30 fwrite( data, size, 1, os->file ); 31 os->fail = ferror( os->file ); 32 } 33 return os; 19 34 } 20 35 21 36 int fail( ofstream *os ) { 22 37 return os->fail; 23 38 } 24 39 25 40 static ofstream *make_ofstream() { 26 27 28 41 ofstream *new_stream = malloc( sizeof( ofstream ) ); 42 new_stream->fail = 0; 43 return new_stream; 29 44 } 30 45 31 46 ofstream *ofstream_stdout() { 32 33 34 47 ofstream *stdout_stream = make_ofstream(); 48 stdout_stream->file = stdout; 49 return stdout_stream; 35 50 } 36 51 37 52 ofstream *ofstream_stderr() { 38 39 40 53 ofstream *stderr_stream = make_ofstream(); 54 stderr_stream->file = stderr; 55 return stderr_stream; 41 56 } 42 57 43 58 ofstream *ofstream_fromfile( const char *name ) { 44 45 46 47 59 ofstream *file_stream = make_ofstream(); 60 file_stream->file = fopen( name, "w" ); 61 file_stream->fail = file_stream->file == 0; 62 return file_stream; 48 63 } 49 64 50 65 void ofstream_close( ofstream *os ) { 51 52 os->fail = fclose( os->file );53 54 66 if ( os->file != stdout && os->file != stderr ) { 67 os->fail = fclose( os->file ); 68 } 69 free( os ); 55 70 } 56 71 57 72 struct ifstream { 58 59 60 73 FILE *file; 74 int fail; 75 int eof; 61 76 }; 62 77 63 78 ifstream *read( ifstream *is, char *data, streamsize_type size ) { 64 65 fread( data, size, 1, is->file );66 is->fail = ferror( is->file );67 is->eof = feof( is->file );68 69 79 if ( ! is->fail && ! is->eof ) { 80 fread( data, size, 1, is->file ); 81 is->fail = ferror( is->file ); 82 is->eof = feof( is->file ); 83 } 84 return is; 70 85 } 71 86 72 87 ifstream *unread( ifstream *is, char c ) { 73 if ( ! is->fail ) { 74 if ( ! EOF == ungetc( c, is->file ) ) { 75 is->fail = 1; 88 if ( ! is->fail ) { 89 if ( ! EOF == ungetc( c, is->file ) ) { 90 is->fail = 1; 91 } 76 92 } 77 } 78 return is; 93 return is; 79 94 } 80 95 81 96 int fail( ifstream *is ) { 82 97 return is->fail; 83 98 } 84 99 85 100 int eof( ifstream *is ) { 86 101 return is->eof; 87 102 } 88 103 89 104 static ifstream *make_ifstream() { 90 91 92 93 105 ifstream *new_stream = malloc( sizeof( ifstream ) ); 106 new_stream->fail = 0; 107 new_stream->eof = 0; 108 return new_stream; 94 109 } 95 110 96 111 ifstream *ifstream_stdin() { 97 98 99 112 ifstream *stdin_stream = make_ifstream(); 113 stdin_stream->file = stdin; 114 return stdin_stream; 100 115 } 101 116 102 117 ifstream *ifstream_fromfile( const char *name ) { 103 104 105 106 118 ifstream *file_stream = make_ifstream(); 119 file_stream->file = fopen( name, "r" ); 120 file_stream->fail = file_stream->file == 0; 121 return file_stream; 107 122 } 123 124 // Local Variables: // 125 // tab-width: 4 // 126 // compile-command: "cfa fstream.c" // 127 // End: //
Note: See TracChangeset
for help on using the changeset viewer.