| [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 | // | 
|---|
| [356189a] | 7 | // fstream.c -- | 
|---|
| [86bd7c1f] | 8 | // | 
|---|
| [90c3b1c] | 9 | // Author           : Peter A. Buhr | 
|---|
| [86bd7c1f] | 10 | // Created On       : Wed May 27 17:56:53 2015 | 
|---|
| [5d125e4] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [53a6c2a] | 12 | // Last Modified On : Thu Jul  6 18:38:25 2017 | 
|---|
|  | 13 | // Update Count     : 251 | 
|---|
| [86bd7c1f] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [d3b7937] | 16 | #include "fstream" | 
|---|
| [51b73452] | 17 |  | 
|---|
|  | 18 | extern "C" { | 
|---|
| [90c3b1c] | 19 | #include <stdio.h>                                                                              // vfprintf, vfscanf | 
|---|
|  | 20 | #include <stdlib.h>                                                                             // exit | 
|---|
|  | 21 | #include <stdarg.h>                                                                             // varargs | 
|---|
|  | 22 | #include <string.h>                                                                             // strlen | 
|---|
| [6152c81] | 23 | #include <stdbool.h>                                                                    // true/false | 
|---|
| [90c3b1c] | 24 | #include <float.h>                                                                              // DBL_DIG, LDBL_DIG | 
|---|
|  | 25 | #include <complex.h>                                                                    // creal, cimag | 
|---|
| [51b73452] | 26 | } | 
|---|
| [cb91437] | 27 | #include "assert" | 
|---|
| [51b73452] | 28 |  | 
|---|
| [53ba273] | 29 | #define IO_MSG "I/O error: " | 
|---|
| [6ba0659] | 30 |  | 
|---|
| [a493682] | 31 | void ?{}( ofstream & this, void * file, _Bool sepDefault, _Bool sepOnOff, const char * separator, const char * tupleSeparator ) { | 
|---|
|  | 32 | this.file = file; | 
|---|
|  | 33 | this.sepDefault = sepDefault; | 
|---|
|  | 34 | this.sepOnOff = sepOnOff; | 
|---|
|  | 35 | sepSet( &this, separator ); | 
|---|
|  | 36 | sepSetCur( &this, sepGet( &this ) ); | 
|---|
|  | 37 | sepSetTuple( &this, tupleSeparator ); | 
|---|
| [0583064b] | 38 | } | 
|---|
|  | 39 |  | 
|---|
| [9ebd778] | 40 | // private | 
|---|
| [53a6c2a] | 41 | _Bool sepPrt( ofstream * os ) { setNL( os, false ); return os->sepOnOff; } | 
|---|
| [53ba273] | 42 | void sepReset( ofstream * os ) { os->sepOnOff = os->sepDefault; } | 
|---|
|  | 43 | void sepReset( ofstream * os, _Bool reset ) { os->sepDefault = reset; os->sepOnOff = os->sepDefault; } | 
|---|
| [829c907] | 44 | const char * sepGetCur( ofstream * os ) { return os->sepCur; } | 
|---|
|  | 45 | void sepSetCur( ofstream * os, const char * sepCur ) { os->sepCur = sepCur; } | 
|---|
| [53a6c2a] | 46 | _Bool getNL( ofstream * os ) { return os->sawNL; } | 
|---|
|  | 47 | void setNL( ofstream * os, _Bool state ) { os->sawNL = state; } | 
|---|
| [829c907] | 48 |  | 
|---|
| [9ebd778] | 49 | // public | 
|---|
| [53a6c2a] | 50 | void sepOn( ofstream * os ) { os->sepOnOff = ! getNL( os ); } | 
|---|
|  | 51 | void sepOff( ofstream * os ) { os->sepOnOff = false; } | 
|---|
| [829c907] | 52 |  | 
|---|
| [53ba273] | 53 | _Bool sepDisable( ofstream *os ) { | 
|---|
|  | 54 | _Bool temp = os->sepDefault; | 
|---|
| [6152c81] | 55 | os->sepDefault = false; | 
|---|
| [53ba273] | 56 | sepReset( os ); | 
|---|
|  | 57 | return temp; | 
|---|
|  | 58 | } // sepDisable | 
|---|
| [6152c81] | 59 |  | 
|---|
| [53ba273] | 60 | _Bool sepEnable( ofstream *os ) { | 
|---|
|  | 61 | _Bool temp = os->sepDefault; | 
|---|
| [6152c81] | 62 | os->sepDefault = true; | 
|---|
| [9ebd778] | 63 | if ( os->sepOnOff ) sepReset( os );                                     // start of line ? | 
|---|
| [53ba273] | 64 | return temp; | 
|---|
|  | 65 | } // sepEnable | 
|---|
| [90c3b1c] | 66 |  | 
|---|
| [9ebd778] | 67 | const char * sepGet( ofstream * os ) { return os->separator; } | 
|---|
|  | 68 | void sepSet( ofstream * os, const char * s ) { | 
|---|
|  | 69 | assert( s ); | 
|---|
| [53a6c2a] | 70 | strncpy( os->separator, s, sepSize - 1 ); | 
|---|
|  | 71 | os->separator[sepSize - 1] = '\0'; | 
|---|
| [9ebd778] | 72 | } // sepSet | 
|---|
|  | 73 |  | 
|---|
|  | 74 | const char * sepGetTuple( ofstream * os ) { return os->tupleSeparator; } | 
|---|
|  | 75 | void sepSetTuple( ofstream * os, const char * s ) { | 
|---|
|  | 76 | assert( s ); | 
|---|
| [53a6c2a] | 77 | strncpy( os->tupleSeparator, s, sepSize - 1 ); | 
|---|
|  | 78 | os->tupleSeparator[sepSize - 1] = '\0'; | 
|---|
| [9ebd778] | 79 | } // sepSet | 
|---|
|  | 80 |  | 
|---|
| [6ba0659] | 81 | int fail( ofstream * os ) { | 
|---|
| [90c3b1c] | 82 | return ferror( (FILE *)(os->file) ); | 
|---|
| [6ba0659] | 83 | } // fail | 
|---|
|  | 84 |  | 
|---|
|  | 85 | int flush( ofstream * os ) { | 
|---|
| [90c3b1c] | 86 | return fflush( (FILE *)(os->file) ); | 
|---|
| [6ba0659] | 87 | } // flush | 
|---|
|  | 88 |  | 
|---|
| [90c3b1c] | 89 | void open( ofstream * os, const char * name, const char * mode ) { | 
|---|
|  | 90 | FILE *file = fopen( name, mode ); | 
|---|
|  | 91 | if ( file == 0 ) {                                                                      // do not change unless successful | 
|---|
| [53ba273] | 92 | fprintf( stderr, IO_MSG "open output file \"%s\", ", name ); | 
|---|
|  | 93 | perror( 0 ); | 
|---|
| [6ba0659] | 94 | exit( EXIT_FAILURE ); | 
|---|
|  | 95 | } // if | 
|---|
| [a493682] | 96 | ?{}( *os, file, true, false, " ", ", " ); | 
|---|
| [6ba0659] | 97 | } // open | 
|---|
|  | 98 |  | 
|---|
|  | 99 | void close( ofstream * os ) { | 
|---|
| [90c3b1c] | 100 | if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return; | 
|---|
| [6ba0659] | 101 |  | 
|---|
| [90c3b1c] | 102 | if ( fclose( (FILE *)(os->file) ) == EOF ) { | 
|---|
| [6ba0659] | 103 | perror( IO_MSG "close output" ); | 
|---|
| [356189a] | 104 | } // if | 
|---|
| [6ba0659] | 105 | } // close | 
|---|
|  | 106 |  | 
|---|
| [90c3b1c] | 107 | ofstream * write( ofstream * os, const char * data, unsigned long int size ) { | 
|---|
| [6ba0659] | 108 | if ( fail( os ) ) { | 
|---|
|  | 109 | fprintf( stderr, "attempt write I/O on failed stream\n" ); | 
|---|
|  | 110 | exit( EXIT_FAILURE ); | 
|---|
|  | 111 | } // if | 
|---|
|  | 112 |  | 
|---|
| [90c3b1c] | 113 | if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) { | 
|---|
| [6ba0659] | 114 | perror( IO_MSG "write" ); | 
|---|
|  | 115 | exit( EXIT_FAILURE ); | 
|---|
| [839ccbb] | 116 | } // if | 
|---|
| [86bd7c1f] | 117 | return os; | 
|---|
| [839ccbb] | 118 | } // write | 
|---|
| [51b73452] | 119 |  | 
|---|
| [829c907] | 120 | int fmt( ofstream * os, const char format[], ... ) { | 
|---|
| [5d125e4] | 121 | va_list args; | 
|---|
| [829c907] | 122 | va_start( args, format ); | 
|---|
|  | 123 | int len = vfprintf( (FILE *)(os->file), format, args ); | 
|---|
| [90c3b1c] | 124 | if ( len == EOF ) { | 
|---|
|  | 125 | if ( ferror( (FILE *)(os->file) ) ) { | 
|---|
|  | 126 | fprintf( stderr, "invalid write\n" ); | 
|---|
|  | 127 | exit( EXIT_FAILURE ); | 
|---|
|  | 128 | } // if | 
|---|
|  | 129 | } // if | 
|---|
| [5d125e4] | 130 | va_end( args ); | 
|---|
| [b72bad4f] | 131 |  | 
|---|
|  | 132 | sepReset( os );                                                                         // reset separator | 
|---|
| [90c3b1c] | 133 | return len; | 
|---|
| [829c907] | 134 | } // fmt | 
|---|
|  | 135 |  | 
|---|
| [d395012] | 136 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " }; | 
|---|
| [6ba0659] | 137 | ofstream *sout = &soutFile; | 
|---|
| [d395012] | 138 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " }; | 
|---|
| [6ba0659] | 139 | ofstream *serr = &serrFile; | 
|---|
| [51b73452] | 140 |  | 
|---|
| [90c3b1c] | 141 |  | 
|---|
| [6ba0659] | 142 | //--------------------------------------- | 
|---|
| [51b73452] | 143 |  | 
|---|
|  | 144 |  | 
|---|
| [6ba0659] | 145 | int fail( ifstream * is ) { | 
|---|
| [90c3b1c] | 146 | return ferror( (FILE *)(is->file) ); | 
|---|
| [6ba0659] | 147 | } // fail | 
|---|
|  | 148 |  | 
|---|
|  | 149 | int eof( ifstream * is ) { | 
|---|
| [90c3b1c] | 150 | return feof( (FILE *)(is->file) ); | 
|---|
| [6ba0659] | 151 | } // eof | 
|---|
|  | 152 |  | 
|---|
| [90c3b1c] | 153 | void open( ifstream * is, const char * name, const char * mode ) { | 
|---|
| [53a6c2a] | 154 | FILE *file = fopen( name, mode ); | 
|---|
|  | 155 | if ( file == 0 ) {                                                                      // do not change unless successful | 
|---|
| [53ba273] | 156 | fprintf( stderr, IO_MSG "open input file \"%s\", ", name ); | 
|---|
|  | 157 | perror( 0 ); | 
|---|
| [90c3b1c] | 158 | exit( EXIT_FAILURE ); | 
|---|
| [6ba0659] | 159 | } // if | 
|---|
| [53a6c2a] | 160 | is->file = file; | 
|---|
| [90c3b1c] | 161 | } // open | 
|---|
| [6ba0659] | 162 |  | 
|---|
| [90c3b1c] | 163 | void close( ifstream * is ) { | 
|---|
|  | 164 | if ( (FILE *)(is->file) == stdin ) return; | 
|---|
|  | 165 |  | 
|---|
|  | 166 | if ( fclose( (FILE *)(is->file) ) == EOF ) { | 
|---|
|  | 167 | perror( IO_MSG "close input" ); | 
|---|
| [356189a] | 168 | } // if | 
|---|
| [90c3b1c] | 169 | } // close | 
|---|
|  | 170 |  | 
|---|
|  | 171 | ifstream * read( ifstream * is, char * data, unsigned long int size ) { | 
|---|
| [6ba0659] | 172 | if ( fail( is ) ) { | 
|---|
|  | 173 | fprintf( stderr, "attempt read I/O on failed stream\n" ); | 
|---|
|  | 174 | exit( EXIT_FAILURE ); | 
|---|
|  | 175 | } // if | 
|---|
|  | 176 |  | 
|---|
| [90c3b1c] | 177 | if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) { | 
|---|
| [6ba0659] | 178 | perror( IO_MSG "read" ); | 
|---|
|  | 179 | exit( EXIT_FAILURE ); | 
|---|
|  | 180 | } // if | 
|---|
| [86bd7c1f] | 181 | return is; | 
|---|
| [6ba0659] | 182 | } // read | 
|---|
| [356189a] | 183 |  | 
|---|
| [6ba0659] | 184 | ifstream *ungetc( ifstream * is, char c ) { | 
|---|
|  | 185 | if ( fail( is ) ) { | 
|---|
|  | 186 | fprintf( stderr, "attempt ungetc I/O on failed stream\n" ); | 
|---|
|  | 187 | exit( EXIT_FAILURE ); | 
|---|
|  | 188 | } // if | 
|---|
| [51b73452] | 189 |  | 
|---|
| [90c3b1c] | 190 | if ( ungetc( c, (FILE *)(is->file) ) == EOF ) { | 
|---|
| [6ba0659] | 191 | perror( IO_MSG "ungetc" ); | 
|---|
|  | 192 | exit( EXIT_FAILURE ); | 
|---|
|  | 193 | } // if | 
|---|
|  | 194 | return is; | 
|---|
|  | 195 | } // ungetc | 
|---|
| [51b73452] | 196 |  | 
|---|
| [829c907] | 197 | int fmt( ifstream * is, const char format[], ... ) { | 
|---|
| [5d125e4] | 198 | va_list args; | 
|---|
| [51b73452] | 199 |  | 
|---|
| [829c907] | 200 | va_start( args, format ); | 
|---|
|  | 201 | int len = vfscanf( (FILE *)(is->file), format, args ); | 
|---|
| [90c3b1c] | 202 | if ( len == EOF ) { | 
|---|
|  | 203 | if ( ferror( (FILE *)(is->file) ) ) { | 
|---|
|  | 204 | fprintf( stderr, "invalid read\n" ); | 
|---|
|  | 205 | exit( EXIT_FAILURE ); | 
|---|
|  | 206 | } // if | 
|---|
|  | 207 | } // if | 
|---|
| [5d125e4] | 208 | va_end( args ); | 
|---|
| [90c3b1c] | 209 | return len; | 
|---|
| [829c907] | 210 | } // fmt | 
|---|
| [51b73452] | 211 |  | 
|---|
|  | 212 |  | 
|---|
| [6ba0659] | 213 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) }; | 
|---|
|  | 214 | ifstream *sin = &sinFile; | 
|---|
| [86bd7c1f] | 215 |  | 
|---|
|  | 216 | // Local Variables: // | 
|---|
|  | 217 | // tab-width: 4 // | 
|---|
|  | 218 | // End: // | 
|---|