Ignore:
Timestamp:
Oct 29, 2019, 4:01:24 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
773db65, 9421f3d8
Parents:
7951100 (diff), 8364209 (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' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 moved

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r7951100 rb067d9b  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jun  5 17:02:56 2018
    13 // Update Count     : 281
    14 //
    15 
    16 #include "fstream"
     12// Last Modified On : Tue Sep 10 22:19:56 2019
     13// Update Count     : 354
     14//
     15
     16#include "fstream.hfa"
    1717
    1818#include <stdio.h>                                                                              // vfprintf, vfscanf
     
    2020#include <stdarg.h>                                                                             // varargs
    2121#include <string.h>                                                                             // strlen
    22 #include <stdbool.h>                                                                    // true/false
    2322#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
    2423#include <complex.h>                                                                    // creal, cimag
    2524#include <assert.h>
     25#include <errno.h>                                                                              // errno
     26
     27
     28//*********************************** ofstream ***********************************
     29
    2630
    2731#define IO_MSG "I/O error: "
    2832
    29 void ?{}( ofstream & os, void * file, _Bool sepDefault, _Bool sepOnOff, const char * separator, const char * tupleSeparator ) {
     33void ?{}( ofstream & os, void * file ) {
    3034        os.file = file;
    31         os.sepDefault = sepDefault;
    32         os.sepOnOff = sepOnOff;
    33         sepSet( os, separator );
     35        os.sepDefault = true;
     36        os.sepOnOff = false;
     37        os.nlOnOff = true;
     38        os.prt = false;
     39        os.sawNL = false;
     40        sepSet( os, " " );
    3441        sepSetCur( os, sepGet( os ) );
    35         sepSetTuple( os, tupleSeparator );
    36 }
     42        sepSetTuple( os, ", " );
     43} // ?{}
    3744
    3845// private
    39 _Bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
     46bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
    4047void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
    41 void sepReset( ofstream & os, _Bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
     48void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
    4249const char * sepGetCur( ofstream & os ) { return os.sepCur; }
    4350void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
    44 _Bool getNL( ofstream & os ) { return os.sawNL; }
    45 void setNL( ofstream & os, _Bool state ) { os.sawNL = state; }
     51bool getNL( ofstream & os ) { return os.sawNL; }
     52void setNL( ofstream & os, bool state ) { os.sawNL = state; }
     53bool getANL( ofstream & os ) { return os.nlOnOff; }
     54bool getPrt( ofstream & os ) { return os.prt; }
     55void setPrt( ofstream & os, bool state ) { os.prt = state; }
    4656
    4757// public
     
    5060void ?{}( ofstream & os, const char * name, const char * mode ) {
    5161        open( os, name, mode );
    52 }
     62} // ?{}
     63
    5364void ?{}( ofstream & os, const char * name ) {
    5465        open( os, name, "w" );
    55 }
     66} // ?{}
    5667
    5768void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
    5869void sepOff( ofstream & os ) { os.sepOnOff = false; }
    5970
    60 _Bool sepDisable( ofstream & os ) {
    61         _Bool temp = os.sepDefault;
     71bool sepDisable( ofstream & os ) {
     72        bool temp = os.sepDefault;
    6273        os.sepDefault = false;
    6374        sepReset( os );
     
    6576} // sepDisable
    6677
    67 _Bool sepEnable( ofstream & os ) {
    68         _Bool temp = os.sepDefault;
     78bool sepEnable( ofstream & os ) {
     79        bool temp = os.sepDefault;
    6980        os.sepDefault = true;
    7081        if ( os.sepOnOff ) sepReset( os );                                      // start of line ?
    7182        return temp;
    7283} // sepEnable
     84
     85void nlOn( ofstream & os ) { os.nlOnOff = true; }
     86void nlOff( ofstream & os ) { os.nlOnOff = false; }
    7387
    7488const char * sepGet( ofstream & os ) { return os.separator; }
     
    86100} // sepSet
    87101
     102void ends( ofstream & os ) {
     103        if ( getANL( os ) ) nl( os );
     104        else setPrt( os, false );                                                       // turn off
     105        if ( &os == &exit ) exit( EXIT_FAILURE );
     106        if ( &os == &abort ) abort();
     107} // ends
     108
    88109int fail( ofstream & os ) {
    89110        return os.file == 0 || ferror( (FILE *)(os.file) );
     
    95116
    96117void open( ofstream & os, const char * name, const char * mode ) {
    97         FILE *file = fopen( name, mode );
    98         // if ( file == 0 ) {                                                                   // do not change unless successful
    99         //      fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
    100         //      perror( 0 );
    101         //      exit( EXIT_FAILURE );
    102         // } // if
    103         (os){ file, true, false, " ", ", " };
     118        FILE * file = fopen( name, mode );
     119        #ifdef __CFA_DEBUG__
     120        if ( file == 0 ) {
     121                abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
     122        } // if
     123        #endif // __CFA_DEBUG__
     124        (os){ file };
    104125} // open
    105126
     
    112133
    113134        if ( fclose( (FILE *)(os.file) ) == EOF ) {
    114                 perror( IO_MSG "close output" );
     135                abort | IO_MSG "close output" | nl | strerror( errno );
    115136        } // if
    116137} // close
     
    118139ofstream & write( ofstream & os, const char * data, size_t size ) {
    119140        if ( fail( os ) ) {
    120                 fprintf( stderr, "attempt write I/O on failed stream\n" );
    121                 exit( EXIT_FAILURE );
     141                abort | IO_MSG "attempt write I/O on failed stream";
    122142        } // if
    123143
    124144        if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
    125                 perror( IO_MSG "write" );
    126                 exit( EXIT_FAILURE );
     145                abort | IO_MSG "write" | nl | strerror( errno );
    127146        } // if
    128147        return os;
     
    135154        if ( len == EOF ) {
    136155                if ( ferror( (FILE *)(os.file) ) ) {
    137                         fprintf( stderr, "invalid write\n" );
    138                         exit( EXIT_FAILURE );
     156                        abort | IO_MSG "invalid write";
    139157                } // if
    140158        } // if
    141159        va_end( args );
    142160
     161        setPrt( os, true );                                                                     // called in output cascade
    143162        sepReset( os );                                                                         // reset separator
    144163        return len;
    145164} // fmt
    146165
    147 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " };
    148 ofstream & sout = soutFile;
    149 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " };
    150 ofstream & serr = serrFile;
    151 
    152 
    153 //---------------------------------------
     166static ofstream soutFile = { (FILE *)stdout };
     167ofstream & sout = soutFile, & stdout = soutFile;
     168static ofstream serrFile = { (FILE *)stderr };
     169ofstream & serr = serrFile, & stderr = serrFile;
     170
     171static ofstream exitFile = { (FILE *)stdout };
     172ofstream & exit = exitFile;
     173static ofstream abortFile = { (FILE *)stderr };
     174ofstream & abort = abortFile;
     175
     176
     177//*********************************** ifstream ***********************************
     178
    154179
    155180// private
    156181void ?{}( ifstream & is, void * file ) {
    157182        is.file = file;
    158 }
     183        is.nlOnOff = false;
     184} // ?{}
    159185
    160186// public
     
    163189void ?{}( ifstream & is, const char * name, const char * mode ) {
    164190        open( is, name, mode );
    165 }
     191} // ?{}
     192
    166193void ?{}( ifstream & is, const char * name ) {
    167194        open( is, name, "r" );
    168 }
     195} // ?{}
     196
     197void nlOn( ifstream & os ) { os.nlOnOff = true; }
     198void nlOff( ifstream & os ) { os.nlOnOff = false; }
     199bool getANL( ifstream & os ) { return os.nlOnOff; }
    169200
    170201int fail( ifstream & is ) {
     
    177208
    178209void open( ifstream & is, const char * name, const char * mode ) {
    179         FILE *file = fopen( name, mode );
    180         // if ( file == 0 ) {                                                                   // do not change unless successful
    181         //      fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
    182         //      perror( 0 );
    183         //      exit( EXIT_FAILURE );
    184         // } // if
     210        FILE * file = fopen( name, mode );
     211        #ifdef __CFA_DEBUG__
     212        if ( file == 0 ) {
     213                abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
     214        } // if
     215        #endif // __CFA_DEBUG__
    185216        is.file = file;
    186217} // open
     
    194225
    195226        if ( fclose( (FILE *)(is.file) ) == EOF ) {
    196                 perror( IO_MSG "close input" );
     227                abort | IO_MSG "close input" | nl | strerror( errno );
    197228        } // if
    198229} // close
     
    200231ifstream & read( ifstream & is, char * data, size_t size ) {
    201232        if ( fail( is ) ) {
    202                 fprintf( stderr, "attempt read I/O on failed stream\n" );
    203                 exit( EXIT_FAILURE );
     233                abort | IO_MSG "attempt read I/O on failed stream";
    204234        } // if
    205235
    206236        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
    207                 perror( IO_MSG "read" );
    208                 exit( EXIT_FAILURE );
     237                abort | IO_MSG "read" | nl | strerror( errno );
    209238        } // if
    210239        return is;
     
    213242ifstream &ungetc( ifstream & is, char c ) {
    214243        if ( fail( is ) ) {
    215                 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
    216                 exit( EXIT_FAILURE );
     244                abort | IO_MSG "attempt ungetc I/O on failed stream";
    217245        } // if
    218246
    219247        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
    220                 perror( IO_MSG "ungetc" );
    221                 exit( EXIT_FAILURE );
     248                abort | IO_MSG "ungetc" | nl | strerror( errno );
    222249        } // if
    223250        return is;
     
    231258        if ( len == EOF ) {
    232259                if ( ferror( (FILE *)(is.file) ) ) {
    233                         fprintf( stderr, "invalid read\n" );
    234                         exit( EXIT_FAILURE );
     260                        abort | IO_MSG "invalid read";
    235261                } // if
    236262        } // if
     
    239265} // fmt
    240266
    241 
    242 static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
    243 ifstream & sin = sinFile;
     267static ifstream sinFile = { (FILE *)stdin };
     268ifstream & sin = sinFile, & stdin = sinFile;
    244269
    245270// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.