Ignore:
Timestamp:
Apr 15, 2016, 12:03:11 PM (9 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
29ad0ac
Parents:
c5833e8 (diff), 37f0da8 (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 gc_noraii

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/fstream.c

    rc5833e8 r0f9e4403  
    77// fstream.c --
    88//
    9 // Author           : Richard C. Bilson
     9// Author           : Peter A. Buhr
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb 17 14:03:05 2016
    13 // Update Count     : 76
     12// Last Modified On : Wed Apr  6 17:55:27 2016
     13// Update Count     : 176
    1414//
    1515
     
    1717
    1818extern "C" {
    19 #include <stdio.h>
    20 #include <stdlib.h>
     19#include <stdio.h>                                                                              // vfprintf, vfscanf
     20#include <stdlib.h>                                                                             // exit
     21#include <stdarg.h>                                                                             // varargs
     22#include <string.h>                                                                             // strlen
     23#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
     24#include <complex.h>                                                                    // creal, cimag
    2125}
    2226
    23 struct ofstream {
    24         FILE *file;
    25 };
     27#define IO_MSG "I/O error: "
    2628
    27 #define IO_MSG "I/O error "
     29_Bool sepPrt( ofstream * os ) { return os->sepOnOff; }
     30void sepOn( ofstream * os ) { os->sepOnOff = 1; }
     31void sepOff( ofstream * os ) { os->sepOnOff = 0; }
     32void sepReset( ofstream * os ) { os->sepOnOff = os->sepDefault; }
     33void sepReset( ofstream * os, _Bool reset ) { os->sepDefault = reset; os->sepOnOff = os->sepDefault; }
     34void sepSet( ofstream * os, const char * s ) {
     35        strncpy( &(os->separator[0]), s, separateSize - 1 );
     36        os->separator[separateSize - 1] = '\0';
     37} // sepSet
     38const char * sepGet( ofstream * os ) { return &(os->separator[0]); }
     39_Bool sepDisable( ofstream *os ) {
     40        _Bool temp = os->sepDefault;
     41        os->sepDefault = 0;
     42        sepReset( os );
     43        return temp;
     44} // sepDisable
     45_Bool sepEnable( ofstream *os ) {
     46        _Bool temp = os->sepDefault;
     47        os->sepDefault = 1;
     48        sepReset( os );
     49        return temp;
     50} // sepEnable
    2851
    2952int fail( ofstream * os ) {
    30         return ferror( os->file );
     53        return ferror( (FILE *)(os->file) );
    3154} // fail
    3255
    3356int flush( ofstream * os ) {
    34         return fflush( os->file );
     57        return fflush( (FILE *)(os->file) );
    3558} // flush
    3659
    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" );
     60void open( ofstream * os, const char * name, const char * mode ) {
     61        FILE *file = fopen( name, mode );
     62        if ( file == 0 ) {                                                                      // do not change unless successful
     63                fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
     64                perror( 0 );
    4165                exit( EXIT_FAILURE );
    4266        } // if
    43         (*os)->file = t;
     67        os->file = file;
     68        sepOff( os );
     69        sepSet( os, " " );
    4470} // open
    4571
    4672void close( ofstream * os ) {
    47         if ( os->file == stdout || os->file == stderr ) return;
     73        if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
    4874
    49         if ( fclose( os->file ) == EOF ) {
     75        if ( fclose( (FILE *)(os->file) ) == EOF ) {
    5076                perror( IO_MSG "close output" );
    5177        } // if
    5278} // close
    5379
    54 ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
     80ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
    5581        if ( fail( os ) ) {
    5682                fprintf( stderr, "attempt write I/O on failed stream\n" );
     
    5884        } // if
    5985
    60         if ( fwrite( data, 1, size, os->file ) != size ) {
     86        if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
    6187                perror( IO_MSG "write" );
    6288                exit( EXIT_FAILURE );
     
    6591} // write
    6692
    67 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
     93int prtfmt( ofstream * os, const char fmt[], ... ) {
     94    va_list args;
     95
     96    va_start( args, fmt );
     97    int len = vfprintf( (FILE *)(os->file), fmt, args );
     98        if ( len == EOF ) {
     99                if ( ferror( (FILE *)(os->file) ) ) {
     100                        fprintf( stderr, "invalid write\n" );
     101                        exit( EXIT_FAILURE );
     102                } // if
     103        } // if
     104    va_end( args );
     105        return len;
     106} // prtfmt
     107
     108
     109static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 1, 0, { ' ', '\0' } };
    68110ofstream *sout = &soutFile;
    69 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
     111static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 1, 0, { ' ', '\0' } };
    70112ofstream *serr = &serrFile;
     113
    71114
    72115//---------------------------------------
    73116
    74 struct ifstream {
    75         FILE *file;
    76 };
    77117
    78118int fail( ifstream * is ) {
    79         return ferror( is->file );
     119        return ferror( (FILE *)(is->file) );
    80120} // fail
    81121
    82122int eof( ifstream * is ) {
    83         return feof( is->file );
     123        return feof( (FILE *)(is->file) );
    84124} // eof
    85125
    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
     126void open( ifstream * is, const char * name, const char * mode ) {
     127        FILE *t = fopen( name, mode );
     128        if ( t == 0 ) {                                                                         // do not change unless successful
     129                fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
     130                perror( 0 );
     131                exit( EXIT_FAILURE );
    92132        } // if
    93         return is;
    94 } // read
     133        is->file = t;
     134} // open
    95135
    96 ifstream * read( ifstream * is, char * data, streamsize_type size ) {
     136void close( ifstream * is ) {
     137        if ( (FILE *)(is->file) == stdin ) return;
     138
     139        if ( fclose( (FILE *)(is->file) ) == EOF ) {
     140                perror( IO_MSG "close input" );
     141        } // if
     142} // close
     143
     144ifstream * read( ifstream * is, char * data, unsigned long int size ) {
    97145        if ( fail( is ) ) {
    98146                fprintf( stderr, "attempt read I/O on failed stream\n" );
     
    100148        } // if
    101149
    102         if ( fread( data, size, 1, is->file ) == 0 ) {
     150        if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
    103151                perror( IO_MSG "read" );
    104152                exit( EXIT_FAILURE );
     
    113161        } // if
    114162
    115         if ( ungetc( c, is->file ) == EOF ) {
     163        if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
    116164                perror( IO_MSG "ungetc" );
    117165                exit( EXIT_FAILURE );
     
    120168} // ungetc
    121169
    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 );
     170int scanfmt( ifstream * is, const char fmt[], ... ) {
     171    va_list args;
     172
     173    va_start( args, fmt );
     174    int len = vfscanf( (FILE *)(is->file), fmt, args );
     175        if ( len == EOF ) {
     176                if ( ferror( (FILE *)(is->file) ) ) {
     177                        fprintf( stderr, "invalid read\n" );
     178                        exit( EXIT_FAILURE );
     179                } // if
    127180        } // if
    128         (*is)->file = t;
    129 } // open
     181    va_end( args );
     182        return len;
     183} // prtfmt
    130184
    131 void close( ifstream * is ) {
    132         if ( is->file == stdin ) return;
    133 
    134         if ( fclose( is->file ) == EOF ) {
    135                 perror( IO_MSG "close input" );
    136         } // if
    137 } // close
    138185
    139186static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
     
    142189// Local Variables: //
    143190// tab-width: 4 //
    144 // compile-command: "cfa fstream.c" //
    145191// End: //
Note: See TracChangeset for help on using the changeset viewer.