Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/fstream.c

    r53ba273 r6ba0659  
    77// fstream.c --
    88//
    9 // Author           : Peter A. Buhr
     9// Author           : Richard C. Bilson
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Apr  6 17:55:27 2016
    13 // Update Count     : 176
     12// Last Modified On : Wed Feb 17 14:03:05 2016
     13// Update Count     : 76
    1414//
    1515
     
    1717
    1818extern "C" {
    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
     19#include <stdio.h>
     20#include <stdlib.h>
    2521}
    2622
    27 #define IO_MSG "I/O error: "
     23struct ofstream {
     24        FILE *file;
     25};
    2826
    29 _Bool sepPrt( ofstream * os ) { return os->sepOnOff; }
    30 void sepOn( ofstream * os ) { os->sepOnOff = 1; }
    31 void sepOff( ofstream * os ) { os->sepOnOff = 0; }
    32 void sepReset( ofstream * os ) { os->sepOnOff = os->sepDefault; }
    33 void sepReset( ofstream * os, _Bool reset ) { os->sepDefault = reset; os->sepOnOff = os->sepDefault; }
    34 void sepSet( ofstream * os, const char * s ) {
    35         strncpy( &(os->separator[0]), s, separateSize - 1 );
    36         os->separator[separateSize - 1] = '\0';
    37 } // sepSet
    38 const 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
     27#define IO_MSG "I/O error "
    5128
    5229int fail( ofstream * os ) {
    53         return ferror( (FILE *)(os->file) );
     30        return ferror( os->file );
    5431} // fail
    5532
    5633int flush( ofstream * os ) {
    57         return fflush( (FILE *)(os->file) );
     34        return fflush( os->file );
    5835} // flush
    5936
    60 void 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 );
     37void 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" );
    6541                exit( EXIT_FAILURE );
    6642        } // if
    67         os->file = file;
    68         sepOff( os );
    69         sepSet( os, " " );
     43        (*os)->file = t;
    7044} // open
    7145
    7246void close( ofstream * os ) {
    73         if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
     47        if ( os->file == stdout || os->file == stderr ) return;
    7448
    75         if ( fclose( (FILE *)(os->file) ) == EOF ) {
     49        if ( fclose( os->file ) == EOF ) {
    7650                perror( IO_MSG "close output" );
    7751        } // if
    7852} // close
    7953
    80 ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
     54ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
    8155        if ( fail( os ) ) {
    8256                fprintf( stderr, "attempt write I/O on failed stream\n" );
     
    8458        } // if
    8559
    86         if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
     60        if ( fwrite( data, 1, size, os->file ) != size ) {
    8761                perror( IO_MSG "write" );
    8862                exit( EXIT_FAILURE );
     
    9165} // write
    9266
    93 int prtfmt( ofstream * os, const char fmt[], ... ) {
    94     va_list args;
     67static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
     68ofstream *sout = &soutFile;
     69static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
     70ofstream *serr = &serrFile;
    9571
    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" );
     72//---------------------------------------
     73
     74struct ifstream {
     75        FILE *file;
     76};
     77
     78int fail( ifstream * is ) {
     79        return ferror( is->file );
     80} // fail
     81
     82int eof( ifstream * is ) {
     83        return feof( is->file );
     84} // eof
     85
     86ifstream * 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" );
    10190                        exit( EXIT_FAILURE );
    10291                } // if
    10392        } // if
    104     va_end( args );
    105         return len;
    106 } // prtfmt
     93        return is;
     94} // read
    10795
    108 
    109 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 1, 0, { ' ', '\0' } };
    110 ofstream *sout = &soutFile;
    111 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 1, 0, { ' ', '\0' } };
    112 ofstream *serr = &serrFile;
    113 
    114 
    115 //---------------------------------------
    116 
    117 
    118 int fail( ifstream * is ) {
    119         return ferror( (FILE *)(is->file) );
    120 } // fail
    121 
    122 int eof( ifstream * is ) {
    123         return feof( (FILE *)(is->file) );
    124 } // eof
    125 
    126 void 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 );
    132         } // if
    133         is->file = t;
    134 } // open
    135 
    136 void 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 
    144 ifstream * read( ifstream * is, char * data, unsigned long int size ) {
     96ifstream * read( ifstream * is, char * data, streamsize_type size ) {
    14597        if ( fail( is ) ) {
    14698                fprintf( stderr, "attempt read I/O on failed stream\n" );
     
    148100        } // if
    149101
    150         if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
     102        if ( fread( data, size, 1, is->file ) == 0 ) {
    151103                perror( IO_MSG "read" );
    152104                exit( EXIT_FAILURE );
     
    161113        } // if
    162114
    163         if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
     115        if ( ungetc( c, is->file ) == EOF ) {
    164116                perror( IO_MSG "ungetc" );
    165117                exit( EXIT_FAILURE );
     
    168120} // ungetc
    169121
    170 int scanfmt( ifstream * is, const char fmt[], ... ) {
    171     va_list args;
     122void 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 );
     127        } // if
     128        (*is)->file = t;
     129} // open
    172130
    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
    180         } // if
    181     va_end( args );
    182         return len;
    183 } // prtfmt
     131void close( ifstream * is ) {
     132        if ( is->file == stdin ) return;
    184133
     134        if ( fclose( is->file ) == EOF ) {
     135                perror( IO_MSG "close input" );
     136        } // if
     137} // close
    185138
    186139static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
     
    189142// Local Variables: //
    190143// tab-width: 4 //
     144// compile-command: "cfa fstream.c" //
    191145// End: //
Note: See TracChangeset for help on using the changeset viewer.