source: src/examples/fstream.c @ 86bd7c1f

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglergc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newstringwith_gc
Last change on this file since 86bd7c1f was 86bd7c1f, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

licencing: eighth groups of files

  • Property mode set to 100644
File size: 2.5 KB
Line 
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
16#include "fstream.h"
17
18extern "C" {
19#include <stdio.h>
20#include <stdlib.h>
21}
22
23struct ofstream {
24        FILE *file;
25        int fail;
26};
27
28ofstream *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;
34}
35
36int fail( ofstream *os ) {
37        return os->fail;
38}
39
40static ofstream *make_ofstream() {
41        ofstream *new_stream = malloc( sizeof( ofstream ) );
42        new_stream->fail = 0;
43        return new_stream;
44}
45
46ofstream *ofstream_stdout() {
47        ofstream *stdout_stream = make_ofstream();
48        stdout_stream->file = stdout;
49        return stdout_stream;
50}
51
52ofstream *ofstream_stderr() {
53        ofstream *stderr_stream = make_ofstream();
54        stderr_stream->file = stderr;
55        return stderr_stream;
56}
57
58ofstream *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;
63}
64
65void ofstream_close( ofstream *os ) {
66        if ( os->file != stdout && os->file != stderr ) {
67                os->fail = fclose( os->file );
68        }
69        free( os );
70}
71
72struct ifstream {
73        FILE *file;
74        int fail;
75        int eof;
76};
77
78ifstream *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;
85}
86 
87ifstream *unread( ifstream *is, char c ) {
88        if ( ! is->fail ) {
89                if ( ! EOF == ungetc( c, is->file ) ) {
90                        is->fail = 1;
91                }
92        }
93        return is;
94}
95
96int fail( ifstream *is ) {
97        return is->fail;
98}
99
100int eof( ifstream *is ) {
101        return is->eof;
102}
103
104static ifstream *make_ifstream() {
105        ifstream *new_stream = malloc( sizeof( ifstream ) );
106        new_stream->fail = 0;
107        new_stream->eof = 0;
108        return new_stream;
109}
110
111ifstream *ifstream_stdin() {
112        ifstream *stdin_stream = make_ifstream();
113        stdin_stream->file = stdin;
114        return stdin_stream;
115}
116
117ifstream *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;
122}
123
124// Local Variables: //
125// tab-width: 4 //
126// compile-command: "cfa fstream.c" //
127// End: //
Note: See TracBrowser for help on using the repository browser.