[86bd7c1f] | 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
|
---|
[6ba0659] | 12 | // Last Modified On : Wed Feb 17 14:03:05 2016
|
---|
| 13 | // Update Count : 76
|
---|
[86bd7c1f] | 14 | //
|
---|
| 15 |
|
---|
[d3b7937] | 16 | #include "fstream"
|
---|
[51b73452] | 17 |
|
---|
| 18 | extern "C" {
|
---|
| 19 | #include <stdio.h>
|
---|
| 20 | #include <stdlib.h>
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[134b86a] | 23 | struct ofstream {
|
---|
[86bd7c1f] | 24 | FILE *file;
|
---|
[51b73452] | 25 | };
|
---|
| 26 |
|
---|
[6ba0659] | 27 | #define IO_MSG "I/O error "
|
---|
| 28 |
|
---|
| 29 | int fail( ofstream * os ) {
|
---|
| 30 | return ferror( os->file );
|
---|
| 31 | } // fail
|
---|
| 32 |
|
---|
| 33 | int flush( ofstream * os ) {
|
---|
| 34 | return fflush( os->file );
|
---|
| 35 | } // flush
|
---|
| 36 |
|
---|
| 37 | void open( ofstream ** os, const char * name, const char * mode ) {
|
---|
| 38 | FILE *t = fopen( name, mode );
|
---|
| 39 | if ( t == 0 ) { // do not change unless successful
|
---|
| 40 | perror( IO_MSG "open output" );
|
---|
| 41 | exit( EXIT_FAILURE );
|
---|
| 42 | } // if
|
---|
| 43 | (*os)->file = t;
|
---|
| 44 | } // open
|
---|
| 45 |
|
---|
| 46 | void close( ofstream * os ) {
|
---|
| 47 | if ( os->file == stdout || os->file == stderr ) return;
|
---|
| 48 |
|
---|
| 49 | if ( fclose( os->file ) == EOF ) {
|
---|
| 50 | perror( IO_MSG "close output" );
|
---|
| 51 | } // if
|
---|
| 52 | } // close
|
---|
| 53 |
|
---|
| 54 | ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
|
---|
| 55 | if ( fail( os ) ) {
|
---|
| 56 | fprintf( stderr, "attempt write I/O on failed stream\n" );
|
---|
| 57 | exit( EXIT_FAILURE );
|
---|
| 58 | } // if
|
---|
| 59 |
|
---|
| 60 | if ( fwrite( data, 1, size, os->file ) != size ) {
|
---|
| 61 | perror( IO_MSG "write" );
|
---|
| 62 | exit( EXIT_FAILURE );
|
---|
[839ccbb] | 63 | } // if
|
---|
[86bd7c1f] | 64 | return os;
|
---|
[839ccbb] | 65 | } // write
|
---|
[51b73452] | 66 |
|
---|
[6ba0659] | 67 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
|
---|
| 68 | ofstream *sout = &soutFile;
|
---|
| 69 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
|
---|
| 70 | ofstream *serr = &serrFile;
|
---|
[51b73452] | 71 |
|
---|
[6ba0659] | 72 | //---------------------------------------
|
---|
[51b73452] | 73 |
|
---|
[134b86a] | 74 | struct ifstream {
|
---|
[86bd7c1f] | 75 | FILE *file;
|
---|
[51b73452] | 76 | };
|
---|
| 77 |
|
---|
[6ba0659] | 78 | int fail( ifstream * is ) {
|
---|
| 79 | return ferror( is->file );
|
---|
| 80 | } // fail
|
---|
| 81 |
|
---|
| 82 | int eof( ifstream * is ) {
|
---|
| 83 | return feof( is->file );
|
---|
| 84 | } // eof
|
---|
| 85 |
|
---|
| 86 | ifstream * get( ifstream * is, int * data ) {
|
---|
| 87 | if ( fscanf( is->file, "%d", data ) == EOF ) {
|
---|
| 88 | if ( ferror( is->file ) ) {
|
---|
| 89 | fprintf( stderr, "invalid int read\n" );
|
---|
| 90 | exit( EXIT_FAILURE );
|
---|
| 91 | } // if
|
---|
| 92 | } // if
|
---|
[86bd7c1f] | 93 | return is;
|
---|
[6ba0659] | 94 | } // read
|
---|
| 95 |
|
---|
| 96 | ifstream * read( ifstream * is, char * data, streamsize_type size ) {
|
---|
| 97 | if ( fail( is ) ) {
|
---|
| 98 | fprintf( stderr, "attempt read I/O on failed stream\n" );
|
---|
| 99 | exit( EXIT_FAILURE );
|
---|
| 100 | } // if
|
---|
| 101 |
|
---|
| 102 | if ( fread( data, size, 1, is->file ) == 0 ) {
|
---|
| 103 | perror( IO_MSG "read" );
|
---|
| 104 | exit( EXIT_FAILURE );
|
---|
| 105 | } // if
|
---|
[86bd7c1f] | 106 | return is;
|
---|
[6ba0659] | 107 | } // read
|
---|
| 108 |
|
---|
| 109 | ifstream *ungetc( ifstream * is, char c ) {
|
---|
| 110 | if ( fail( is ) ) {
|
---|
| 111 | fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
|
---|
| 112 | exit( EXIT_FAILURE );
|
---|
| 113 | } // if
|
---|
[51b73452] | 114 |
|
---|
[6ba0659] | 115 | if ( ungetc( c, is->file ) == EOF ) {
|
---|
| 116 | perror( IO_MSG "ungetc" );
|
---|
| 117 | exit( EXIT_FAILURE );
|
---|
| 118 | } // if
|
---|
| 119 | return is;
|
---|
| 120 | } // ungetc
|
---|
[51b73452] | 121 |
|
---|
[6ba0659] | 122 | void open( ifstream ** is, const char * name, const char * mode ) {
|
---|
| 123 | FILE *t = fopen( name, mode );
|
---|
| 124 | if ( t == 0 ) { // do not change unless successful
|
---|
| 125 | perror( IO_MSG "open input" );
|
---|
| 126 | exit( EXIT_FAILURE );
|
---|
| 127 | } // if
|
---|
| 128 | (*is)->file = t;
|
---|
| 129 | } // open
|
---|
[51b73452] | 130 |
|
---|
[6ba0659] | 131 | void close( ifstream * is ) {
|
---|
| 132 | if ( is->file == stdin ) return;
|
---|
[51b73452] | 133 |
|
---|
[6ba0659] | 134 | if ( fclose( is->file ) == EOF ) {
|
---|
| 135 | perror( IO_MSG "close input" );
|
---|
| 136 | } // if
|
---|
| 137 | } // close
|
---|
[51b73452] | 138 |
|
---|
[6ba0659] | 139 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
|
---|
| 140 | ifstream *sin = &sinFile;
|
---|
[86bd7c1f] | 141 |
|
---|
| 142 | // Local Variables: //
|
---|
| 143 | // tab-width: 4 //
|
---|
| 144 | // compile-command: "cfa fstream.c" //
|
---|
| 145 | // End: //
|
---|