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