Ignore:
Timestamp:
Mar 3, 2016, 1:28:56 PM (10 years ago)
Author:
Aaron Moss <a3moss@…>
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, string, with_gc
Children:
3627356
Parents:
9d7b3ea (diff), 4040425 (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 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/fstream.c

    r9d7b3ea r36ebd03  
    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 : Mon Feb 29 18:41:10 2016
     13// Update Count     : 162
    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}
    22 
    23 struct ofstream {
    24         FILE *file;
    25 };
    2626
    2727#define IO_MSG "I/O error "
    2828
     29_Bool sepPrt( ofstream * os ) { return os->separate == 1; }
     30void sepOn( ofstream * os ) { if ( os->separate != 2 ) os->separate = 1; }
     31void sepOff( ofstream * os ) { if ( os->separate != 2 ) os->separate = 0; }
     32void sepSet( ofstream * os, const char * s ) {
     33        strncpy( &(os->separator[0]), s, separateSize - 1 );
     34        os->separator[separateSize - 1] = '\0';
     35} // sepSet
     36const char * sepGet( ofstream * os ) { return &(os->separator[0]); }
     37void sepDisable( ofstream *os ) { os->separate = 2; }
     38void sepEnable( ofstream *os ) { os->separate = 0; }
     39
    2940int fail( ofstream * os ) {
    30         return ferror( os->file );
     41        return ferror( (FILE *)(os->file) );
    3142} // fail
    3243
    3344int flush( ofstream * os ) {
    34         return fflush( os->file );
     45        return fflush( (FILE *)(os->file) );
    3546} // flush
    3647
    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
     48void open( ofstream * os, const char * name, const char * mode ) {
     49        FILE *file = fopen( name, mode );
     50        if ( file == 0 ) {                                                                      // do not change unless successful
    4051                perror( IO_MSG "open output" );
    4152                exit( EXIT_FAILURE );
    4253        } // if
    43         (*os)->file = t;
     54        os->file = file;
     55        sepOff( os );
     56        sepSet( os, " " );
    4457} // open
    4558
    4659void close( ofstream * os ) {
    47         if ( os->file == stdout || os->file == stderr ) return;
     60        if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
    4861
    49         if ( fclose( os->file ) == EOF ) {
     62        if ( fclose( (FILE *)(os->file) ) == EOF ) {
    5063                perror( IO_MSG "close output" );
    5164        } // if
    5265} // close
    5366
    54 ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
     67ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
    5568        if ( fail( os ) ) {
    5669                fprintf( stderr, "attempt write I/O on failed stream\n" );
     
    5871        } // if
    5972
    60         if ( fwrite( data, 1, size, os->file ) != size ) {
     73        if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
    6174                perror( IO_MSG "write" );
    6275                exit( EXIT_FAILURE );
     
    6578} // write
    6679
    67 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
     80int prtfmt( ofstream * os, const char fmt[], ... ) {
     81    va_list args;
     82
     83    va_start( args, fmt );
     84    int len = vfprintf( (FILE *)(os->file), fmt, args );
     85        if ( len == EOF ) {
     86                if ( ferror( (FILE *)(os->file) ) ) {
     87                        fprintf( stderr, "invalid write\n" );
     88                        exit( EXIT_FAILURE );
     89                } // if
     90        } // if
     91    va_end( args );
     92        return len;
     93} // prtfmt
     94
     95
     96static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 0, { ' ', '\0' } };
    6897ofstream *sout = &soutFile;
    69 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
     98static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 0, { ' ', '\0' } };
    7099ofstream *serr = &serrFile;
     100
    71101
    72102//---------------------------------------
    73103
    74 struct ifstream {
    75         FILE *file;
    76 };
    77104
    78105int fail( ifstream * is ) {
    79         return ferror( is->file );
     106        return ferror( (FILE *)(is->file) );
    80107} // fail
    81108
    82109int eof( ifstream * is ) {
    83         return feof( is->file );
     110        return feof( (FILE *)(is->file) );
    84111} // eof
    85112
    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
     113void open( ifstream * is, const char * name, const char * mode ) {
     114        FILE *t = fopen( name, mode );
     115        if ( t == 0 ) {                                                                         // do not change unless successful
     116                perror( IO_MSG "open input" );
     117                exit( EXIT_FAILURE );
    92118        } // if
    93         return is;
    94 } // read
     119        is->file = t;
     120} // open
    95121
    96 ifstream * read( ifstream * is, char * data, streamsize_type size ) {
     122void close( ifstream * is ) {
     123        if ( (FILE *)(is->file) == stdin ) return;
     124
     125        if ( fclose( (FILE *)(is->file) ) == EOF ) {
     126                perror( IO_MSG "close input" );
     127        } // if
     128} // close
     129
     130ifstream * read( ifstream * is, char * data, unsigned long int size ) {
    97131        if ( fail( is ) ) {
    98132                fprintf( stderr, "attempt read I/O on failed stream\n" );
     
    100134        } // if
    101135
    102         if ( fread( data, size, 1, is->file ) == 0 ) {
     136        if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
    103137                perror( IO_MSG "read" );
    104138                exit( EXIT_FAILURE );
     
    113147        } // if
    114148
    115         if ( ungetc( c, is->file ) == EOF ) {
     149        if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
    116150                perror( IO_MSG "ungetc" );
    117151                exit( EXIT_FAILURE );
     
    120154} // ungetc
    121155
    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 );
     156int scanfmt( ifstream * is, const char fmt[], ... ) {
     157    va_list args;
     158
     159    va_start( args, fmt );
     160    int len = vfscanf( (FILE *)(is->file), fmt, args );
     161        if ( len == EOF ) {
     162                if ( ferror( (FILE *)(is->file) ) ) {
     163                        fprintf( stderr, "invalid read\n" );
     164                        exit( EXIT_FAILURE );
     165                } // if
    127166        } // if
    128         (*is)->file = t;
    129 } // open
     167    va_end( args );
     168        return len;
     169} // prtfmt
    130170
    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
    138171
    139172static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
Note: See TracChangeset for help on using the changeset viewer.