Ignore:
Timestamp:
May 24, 2019, 10:19:41 AM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
d908563
Parents:
6a9d4b4 (diff), 292642a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r6a9d4b4 r933f32f  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Dec 24 18:33:38 2018
    13 // Update Count     : 304
     12// Last Modified On : Thu May 16 08:33:28 2019
     13// Update Count     : 328
    1414//
    1515
     
    2323#include <complex.h>                                                                    // creal, cimag
    2424#include <assert.h>
     25#include <errno.h>                                                                              // errno
    2526
    2627#define IO_MSG "I/O error: "
    2728
    28 void ?{}( ofstream & os, void * file, bool sepDefault, bool sepOnOff, bool nlOnOff, bool prt, const char * separator, const char * tupleSeparator ) {
     29void ?{}( ofstream & os, void * file ) {
    2930        os.file = file;
    30         os.sepDefault = sepDefault;
    31         os.sepOnOff = sepOnOff;
    32         os.nlOnOff = nlOnOff;
    33         os.prt = prt;
    34         sepSet( os, separator );
     31        os.sepDefault = true;
     32        os.sepOnOff = false;
     33        os.nlOnOff = true;
     34        os.prt = false;
     35        os.sawNL = false;
     36        sepSet( os, " " );
    3537        sepSetCur( os, sepGet( os ) );
    36         sepSetTuple( os, tupleSeparator );
     38        sepSetTuple( os, ", " );
    3739}
    3840
     
    102104
    103105void open( ofstream & os, const char * name, const char * mode ) {
    104         FILE *file = fopen( name, mode );
     106        FILE * file = fopen( name, mode );
    105107        #ifdef __CFA_DEBUG__
    106108        if ( file == 0 ) {
    107                 fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
    108                 perror( 0 );
    109                 exit( EXIT_FAILURE );
     109                abort( IO_MSG "open output file \"%s\", %s", name, strerror( errno ) );
    110110        } // if
    111111        #endif // __CFA_DEBUG__
    112         (os){ file, true, false, true, false, " ", ", " };
     112        (os){ file };
    113113} // open
    114114
     
    121121
    122122        if ( fclose( (FILE *)(os.file) ) == EOF ) {
    123                 perror( IO_MSG "close output" );
     123                abort( IO_MSG "close output %s", strerror( errno ) );
    124124        } // if
    125125} // close
     
    127127ofstream & write( ofstream & os, const char * data, size_t size ) {
    128128        if ( fail( os ) ) {
    129                 fprintf( stderr, "attempt write I/O on failed stream\n" );
    130                 exit( EXIT_FAILURE );
     129                abort( "attempt write I/O on failed stream\n" );
    131130        } // if
    132131
    133132        if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
    134                 perror( IO_MSG "write" );
    135                 exit( EXIT_FAILURE );
     133                abort( IO_MSG "write %s", strerror( errno ) );
    136134        } // if
    137135        return os;
     
    144142        if ( len == EOF ) {
    145143                if ( ferror( (FILE *)(os.file) ) ) {
    146                         fprintf( stderr, "invalid write\n" );
    147                         exit( EXIT_FAILURE );
     144                        abort( "invalid write\n" );
    148145                } // if
    149146        } // if
     
    155152} // fmt
    156153
    157 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, true, false, " ", ", " };
     154static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
    158155ofstream & sout = soutFile;
    159 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, true, false, " ", ", " };
     156static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
    160157ofstream & serr = serrFile;
    161158
     159// static ofstream sexitFile = { (FILE *)(&_IO_2_1_stdout_) };
     160// ofstream & sexit = sexitFile;
     161// static ofstream sabortFile = { (FILE *)(&_IO_2_1_stderr_) };
     162// ofstream & sabort = sabortFile;
     163
     164void nl( ofstream & os ) {
     165        if ( getANL( os ) ) (ofstream &)(nl( os ));                     // implementation only
     166        else setPrt( os, false );                                                       // turn off
     167}
    162168
    163169//---------------------------------------
     
    166172void ?{}( ifstream & is, void * file ) {
    167173        is.file = file;
     174        is.nlOnOff = false;
    168175}
    169176
     
    177184        open( is, name, "r" );
    178185}
     186
     187void nlOn( ifstream & os ) { os.nlOnOff = true; }
     188void nlOff( ifstream & os ) { os.nlOnOff = false; }
     189bool getANL( ifstream & os ) { return os.nlOnOff; }
    179190
    180191int fail( ifstream & is ) {
     
    187198
    188199void open( ifstream & is, const char * name, const char * mode ) {
    189         FILE *file = fopen( name, mode );
     200        FILE * file = fopen( name, mode );
    190201        #ifdef __CFA_DEBUG__
    191202        if ( file == 0 ) {
    192                 fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
    193                 perror( 0 );
    194                 exit( EXIT_FAILURE );
     203                abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) );
    195204        } // if
    196205        #endif // __CFA_DEBUG__
     
    206215
    207216        if ( fclose( (FILE *)(is.file) ) == EOF ) {
    208                 perror( IO_MSG "close input" );
     217                abort( IO_MSG "close input %s", strerror( errno ) );
    209218        } // if
    210219} // close
     
    212221ifstream & read( ifstream & is, char * data, size_t size ) {
    213222        if ( fail( is ) ) {
    214                 fprintf( stderr, "attempt read I/O on failed stream\n" );
    215                 exit( EXIT_FAILURE );
     223                abort( "attempt read I/O on failed stream\n" );
    216224        } // if
    217225
    218226        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
    219                 perror( IO_MSG "read" );
    220                 exit( EXIT_FAILURE );
     227                abort( IO_MSG "read %s", strerror( errno ) );
    221228        } // if
    222229        return is;
     
    225232ifstream &ungetc( ifstream & is, char c ) {
    226233        if ( fail( is ) ) {
    227                 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
    228                 exit( EXIT_FAILURE );
     234                abort( "attempt ungetc I/O on failed stream\n" );
    229235        } // if
    230236
    231237        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
    232                 perror( IO_MSG "ungetc" );
    233                 exit( EXIT_FAILURE );
     238                abort( IO_MSG "ungetc %s", strerror( errno ) );
    234239        } // if
    235240        return is;
     
    243248        if ( len == EOF ) {
    244249                if ( ferror( (FILE *)(is.file) ) ) {
    245                         fprintf( stderr, "invalid read\n" );
    246                         exit( EXIT_FAILURE );
     250                        abort( "invalid read\n" );
    247251                } // if
    248252        } // if
Note: See TracChangeset for help on using the changeset viewer.