Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r9d362a0 r0efb269  
    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 : Sat Apr 20 12:03:43 2019
     13// Update Count     : 311
    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: "
     
    3233        os.nlOnOff = nlOnOff;
    3334        os.prt = prt;
     35        os.sawNL = false;
    3436        sepSet( os, separator );
    3537        sepSetCur( os, sepGet( os ) );
     
    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__
     
    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
     
    166163void ?{}( ifstream & is, void * file ) {
    167164        is.file = file;
     165        is.nlOnOff = false;
    168166}
    169167
     
    177175        open( is, name, "r" );
    178176}
     177
     178void nlOn( ifstream & os ) { os.nlOnOff = true; }
     179void nlOff( ifstream & os ) { os.nlOnOff = false; }
     180bool getANL( ifstream & os ) { return os.nlOnOff; }
    179181
    180182int fail( ifstream & is ) {
     
    187189
    188190void open( ifstream & is, const char * name, const char * mode ) {
    189         FILE *file = fopen( name, mode );
     191        FILE * file = fopen( name, mode );
    190192        #ifdef __CFA_DEBUG__
    191193        if ( file == 0 ) {
    192                 fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
    193                 perror( 0 );
    194                 exit( EXIT_FAILURE );
     194                abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) );
    195195        } // if
    196196        #endif // __CFA_DEBUG__
     
    206206
    207207        if ( fclose( (FILE *)(is.file) ) == EOF ) {
    208                 perror( IO_MSG "close input" );
     208                abort( IO_MSG "close input %s", strerror( errno ) );
    209209        } // if
    210210} // close
     
    212212ifstream & read( ifstream & is, char * data, size_t size ) {
    213213        if ( fail( is ) ) {
    214                 fprintf( stderr, "attempt read I/O on failed stream\n" );
    215                 exit( EXIT_FAILURE );
     214                abort( "attempt read I/O on failed stream\n" );
    216215        } // if
    217216
    218217        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
    219                 perror( IO_MSG "read" );
    220                 exit( EXIT_FAILURE );
     218                abort( IO_MSG "read %s", strerror( errno ) );
    221219        } // if
    222220        return is;
     
    225223ifstream &ungetc( ifstream & is, char c ) {
    226224        if ( fail( is ) ) {
    227                 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
    228                 exit( EXIT_FAILURE );
     225                abort( "attempt ungetc I/O on failed stream\n" );
    229226        } // if
    230227
    231228        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
    232                 perror( IO_MSG "ungetc" );
    233                 exit( EXIT_FAILURE );
     229                abort( IO_MSG "ungetc %s", strerror( errno ) );
    234230        } // if
    235231        return is;
     
    243239        if ( len == EOF ) {
    244240                if ( ferror( (FILE *)(is.file) ) ) {
    245                         fprintf( stderr, "invalid read\n" );
    246                         exit( EXIT_FAILURE );
     241                        abort( "invalid read\n" );
    247242                } // if
    248243        } // if
Note: See TracChangeset for help on using the changeset viewer.