Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/fstream.c

    r90c3b1c 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 : Mon Feb 29 18:41:10 2016
    13 // Update Count     : 162
     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}
     22
     23struct ofstream {
     24        FILE *file;
     25};
    2626
    2727#define IO_MSG "I/O error "
    2828
    29 _Bool sepPrt( ofstream * os ) { return os->separate == 1; }
    30 void sepOn( ofstream * os ) { if ( os->separate != 2 ) os->separate = 1; }
    31 void sepOff( ofstream * os ) { if ( os->separate != 2 ) os->separate = 0; }
    32 void sepSet( ofstream * os, const char * s ) {
    33         strncpy( &(os->separator[0]), s, separateSize - 1 );
    34         os->separator[separateSize - 1] = '\0';
    35 } // sepSet
    36 const char * sepGet( ofstream * os ) { return &(os->separator[0]); }
    37 void sepDisable( ofstream *os ) { os->separate = 2; }
    38 void sepEnable( ofstream *os ) { os->separate = 0; }
    39 
    4029int fail( ofstream * os ) {
    41         return ferror( (FILE *)(os->file) );
     30        return ferror( os->file );
    4231} // fail
    4332
    4433int flush( ofstream * os ) {
    45         return fflush( (FILE *)(os->file) );
     34        return fflush( os->file );
    4635} // flush
    4736
    48 void open( ofstream * os, const char * name, const char * mode ) {
    49         FILE *file = fopen( name, mode );
    50         if ( file == 0 ) {                                                                      // do not change unless successful
     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
    5140                perror( IO_MSG "open output" );
    5241                exit( EXIT_FAILURE );
    5342        } // if
    54         os->file = file;
    55         sepOff( os );
    56         sepSet( os, " " );
     43        (*os)->file = t;
    5744} // open
    5845
    5946void close( ofstream * os ) {
    60         if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
     47        if ( os->file == stdout || os->file == stderr ) return;
    6148
    62         if ( fclose( (FILE *)(os->file) ) == EOF ) {
     49        if ( fclose( os->file ) == EOF ) {
    6350                perror( IO_MSG "close output" );
    6451        } // if
    6552} // close
    6653
    67 ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
     54ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
    6855        if ( fail( os ) ) {
    6956                fprintf( stderr, "attempt write I/O on failed stream\n" );
     
    7158        } // if
    7259
    73         if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
     60        if ( fwrite( data, 1, size, os->file ) != size ) {
    7461                perror( IO_MSG "write" );
    7562                exit( EXIT_FAILURE );
     
    7865} // write
    7966
    80 int prtfmt( ofstream * os, const char fmt[], ... ) {
    81     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;
    8271
    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" );
     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" );
    8890                        exit( EXIT_FAILURE );
    8991                } // if
    9092        } // if
    91     va_end( args );
    92         return len;
    93 } // prtfmt
     93        return is;
     94} // read
    9495
    95 
    96 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 0, { ' ', '\0' } };
    97 ofstream *sout = &soutFile;
    98 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 0, { ' ', '\0' } };
    99 ofstream *serr = &serrFile;
    100 
    101 
    102 //---------------------------------------
    103 
    104 
    105 int fail( ifstream * is ) {
    106         return ferror( (FILE *)(is->file) );
    107 } // fail
    108 
    109 int eof( ifstream * is ) {
    110         return feof( (FILE *)(is->file) );
    111 } // eof
    112 
    113 void 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 );
    118         } // if
    119         is->file = t;
    120 } // open
    121 
    122 void 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 
    130 ifstream * read( ifstream * is, char * data, unsigned long int size ) {
     96ifstream * read( ifstream * is, char * data, streamsize_type size ) {
    13197        if ( fail( is ) ) {
    13298                fprintf( stderr, "attempt read I/O on failed stream\n" );
     
    134100        } // if
    135101
    136         if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
     102        if ( fread( data, size, 1, is->file ) == 0 ) {
    137103                perror( IO_MSG "read" );
    138104                exit( EXIT_FAILURE );
     
    147113        } // if
    148114
    149         if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
     115        if ( ungetc( c, is->file ) == EOF ) {
    150116                perror( IO_MSG "ungetc" );
    151117                exit( EXIT_FAILURE );
     
    154120} // ungetc
    155121
    156 int scanfmt( ifstream * is, const char fmt[], ... ) {
    157     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
    158130
    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
    166         } // if
    167     va_end( args );
    168         return len;
    169 } // prtfmt
     131void close( ifstream * is ) {
     132        if ( is->file == stdin ) return;
    170133
     134        if ( fclose( is->file ) == EOF ) {
     135                perror( IO_MSG "close input" );
     136        } // if
     137} // close
    171138
    172139static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
Note: See TracChangeset for help on using the changeset viewer.