Changeset a172972 for src/libcfa
- Timestamp:
- Feb 23, 2016, 11:20:39 AM (10 years ago)
- 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:
- ae42f2a
- Parents:
- 7528ba1 (diff), 6ce67ce (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. - Location:
- src/libcfa
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/fstream
r7528ba1 ra172972 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jan 27 23:47:41 201613 // Update Count : 312 // Last Modified On : Wed Feb 17 14:02:01 2016 13 // Update Count : 22 14 14 // 15 15 … … 19 19 #include "iostream" 20 20 21 typedef struct ofstream ofstream; 21 // implement context ostream 22 struct ofstream; 22 23 23 // implement context ostream 24 ofstream *write( ofstream *, const char *, streamsize_type ); 25 int fail( ofstream * ); 24 int fail( ofstream * os ); 25 int flush( ofstream * os ); 26 void open( ofstream ** os, const char * name, const char * mode ); 27 void close( ofstream * os ); 28 ofstream * write( ofstream * os, const char * data, streamsize_type size ); 26 29 27 ofstream *ofstream_stdout(); 28 ofstream *ofstream_stderr(); 29 ofstream *ofstream_fromfile( const char *name ); 30 void ofstream_close( ofstream *os ); 31 32 typedef struct ifstream ifstream; 30 extern ofstream * sout, * serr; 33 31 34 32 // implement context istream 35 ifstream *read( ifstream *, char *, streamsize_type ); 36 ifstream *unread( ifstream *, char ); 37 int fail( ifstream * ); 38 int eof( ifstream * ); 33 struct ifstream; 39 34 40 ifstream *ifstream_stdin(); 41 ifstream *ifstream_fromfile( const char *name ); 35 int fail( ifstream * is ); 36 int eof( ifstream * is ); 37 void open( ifstream ** is, const char * name, const char * mode ); 38 void close( ifstream * is ); 39 ifstream * get( ifstream * is, int * data ); 40 ifstream * read( ifstream * is, char * data, streamsize_type size ); 41 ifstream * ungetc( ifstream * is, char c ); 42 43 extern ifstream *sin; 42 44 43 45 #endif // __FSTREAM_H__ -
src/libcfa/fstream.c
r7528ba1 ra172972 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Jan 26 17:12:59201613 // Update Count : 612 // Last Modified On : Wed Feb 17 14:03:05 2016 13 // Update Count : 76 14 14 // 15 15 … … 23 23 struct ofstream { 24 24 FILE *file; 25 int fail;26 25 }; 27 26 28 ofstream *write( ofstream *os, const char *data, streamsize_type size ) { 29 if ( ! os->fail ) { 30 fwrite( data, size, 1, os->file ); 31 os->fail = ferror( os->file ); 27 #define IO_MSG "I/O error " 28 29 int fail( ofstream * os ) { 30 return ferror( os->file ); 31 } // fail 32 33 int flush( ofstream * os ) { 34 return fflush( os->file ); 35 } // flush 36 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" ); 41 exit( EXIT_FAILURE ); 42 } // if 43 (*os)->file = t; 44 } // open 45 46 void close( ofstream * os ) { 47 if ( os->file == stdout || os->file == stderr ) return; 48 49 if ( fclose( os->file ) == EOF ) { 50 perror( IO_MSG "close output" ); 51 } // if 52 } // close 53 54 ofstream * write( ofstream * os, const char * data, streamsize_type size ) { 55 if ( fail( os ) ) { 56 fprintf( stderr, "attempt write I/O on failed stream\n" ); 57 exit( EXIT_FAILURE ); 58 } // if 59 60 if ( fwrite( data, 1, size, os->file ) != size ) { 61 perror( IO_MSG "write" ); 62 exit( EXIT_FAILURE ); 32 63 } // if 33 64 return os; 34 65 } // write 35 66 36 int fail( ofstream *os ) { 37 return os->fail; 38 } // fail 67 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) }; 68 ofstream *sout = &soutFile; 69 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) }; 70 ofstream *serr = &serrFile; 39 71 40 static ofstream *make_ofstream() { 41 ofstream *new_stream = malloc( sizeof( ofstream ) ); 42 new_stream->fail = 0; 43 return new_stream; 44 } // make_ofstream 45 46 ofstream *ofstream_stdout() { 47 ofstream *stdout_stream = make_ofstream(); 48 stdout_stream->file = stdout; 49 return stdout_stream; 50 } // ofstream_stdout 51 52 ofstream *ofstream_stderr() { 53 ofstream *stderr_stream = make_ofstream(); 54 stderr_stream->file = stderr; 55 return stderr_stream; 56 } // ofstream_stderr 57 58 ofstream *ofstream_fromfile( const char *name ) { 59 ofstream *file_stream = make_ofstream(); 60 file_stream->file = fopen( name, "w" ); 61 file_stream->fail = file_stream->file == 0; 62 return file_stream; 63 } 64 65 void ofstream_close( ofstream *os ) { 66 if ( os->file != stdout && os->file != stderr ) { 67 os->fail = fclose( os->file ); 68 } 69 free( os ); 70 } 72 //--------------------------------------- 71 73 72 74 struct ifstream { 73 75 FILE *file; 74 int fail;75 int eof;76 76 }; 77 77 78 ifstream *read( ifstream *is, char *data, streamsize_type size ) { 79 if ( ! is->fail && ! is->eof ) { 80 fread( data, size, 1, is->file ); 81 is->fail = ferror( is->file ); 82 is->eof = feof( is->file ); 83 } 78 int fail( ifstream * is ) { 79 return ferror( is->file ); 80 } // fail 81 82 int eof( ifstream * is ) { 83 return feof( is->file ); 84 } // eof 85 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 92 } // if 84 93 return is; 85 } 94 } // read 95 96 ifstream * read( ifstream * is, char * data, streamsize_type size ) { 97 if ( fail( is ) ) { 98 fprintf( stderr, "attempt read I/O on failed stream\n" ); 99 exit( EXIT_FAILURE ); 100 } // if 101 102 if ( fread( data, size, 1, is->file ) == 0 ) { 103 perror( IO_MSG "read" ); 104 exit( EXIT_FAILURE ); 105 } // if 106 return is; 107 } // read 86 108 87 ifstream *unread( ifstream *is, char c ) { 88 if ( ! is->fail ) { 89 if ( ! EOF == ungetc( c, is->file ) ) { 90 is->fail = 1; 91 } 92 } 109 ifstream *ungetc( ifstream * is, char c ) { 110 if ( fail( is ) ) { 111 fprintf( stderr, "attempt ungetc I/O on failed stream\n" ); 112 exit( EXIT_FAILURE ); 113 } // if 114 115 if ( ungetc( c, is->file ) == EOF ) { 116 perror( IO_MSG "ungetc" ); 117 exit( EXIT_FAILURE ); 118 } // if 93 119 return is; 94 } 120 } // ungetc 95 121 96 int fail( ifstream *is ) { 97 return is->fail; 98 } 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 ); 127 } // if 128 (*is)->file = t; 129 } // open 99 130 100 int eof( ifstream *is ) { 101 return is->eof; 102 } 131 void close( ifstream * is ) { 132 if ( is->file == stdin ) return; 103 133 104 static ifstream *make_ifstream() { 105 ifstream *new_stream = malloc( sizeof( ifstream ) ); 106 new_stream->fail = 0; 107 new_stream->eof = 0; 108 return new_stream; 109 } 134 if ( fclose( is->file ) == EOF ) { 135 perror( IO_MSG "close input" ); 136 } // if 137 } // close 110 138 111 ifstream *ifstream_stdin() { 112 ifstream *stdin_stream = make_ifstream(); 113 stdin_stream->file = stdin; 114 return stdin_stream; 115 } 116 117 ifstream *ifstream_fromfile( const char *name ) { 118 ifstream *file_stream = make_ifstream(); 119 file_stream->file = fopen( name, "r" ); 120 file_stream->fail = file_stream->file == 0; 121 return file_stream; 122 } 139 static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) }; 140 ifstream *sin = &sinFile; 123 141 124 142 // Local Variables: // -
src/libcfa/iostream
r7528ba1 ra172972 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jan 29 15:50:36201613 // Update Count : 2912 // Last Modified On : Wed Feb 17 14:04:24 2016 13 // Update Count : 32 14 14 // 15 15 … … 22 22 23 23 context ostream( dtype ostype ) { 24 ostype *write( ostype *, const char *, streamsize_type );25 24 int fail( ostype * ); 25 int flush( ostype * ); 26 ostype * write( ostype *, const char *, streamsize_type ); 26 27 }; 27 28 28 context writeable( type T ) { 29 29 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, T ); … … 52 52 53 53 // writes the range [begin, end) to the given stream 54 forall( type elt_type | writeable( elt_type ), 55 type iterator_type | iterator( iterator_type, elt_type ), 56 dtype os_type | ostream( os_type ) ) 54 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), dtype os_type | ostream( os_type ) ) 57 55 void write( iterator_type begin, iterator_type end, os_type *os ); 58 56 59 forall( type elt_type | writeable( elt_type ), 60 type iterator_type | iterator( iterator_type, elt_type ), 61 dtype os_type | ostream( os_type ) ) 57 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), dtype os_type | ostream( os_type ) ) 62 58 void write_reverse( iterator_type begin, iterator_type end, os_type *os ); 63 59 64 // ******************************************************************************60 //--------------------------------------- 65 61 66 62 context istream( dtype istype ) { 67 istype *read( istype *, char *, streamsize_type );68 istype *unread( istype *, char );69 63 int fail( istype * ); 70 64 int eof( istype * ); 65 istype * get( istype *, int * ); 66 istype * read( istype *, char *, streamsize_type ); 67 istype * ungetc( istype *, char ); 71 68 }; 72 69 -
src/libcfa/iostream.c
r7528ba1 ra172972 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Feb 1 14:20:30201613 // Update Count : 6012 // Last Modified On : Wed Feb 17 14:19:56 2016 13 // Update Count : 76 14 14 // 15 15 … … 19 19 #include <stdio.h> 20 20 #include <string.h> // strlen 21 #include <float.h> // DBL_DIG, LDBL_DIG 21 22 #include <complex.h> // creal, cimag 22 23 } … … 72 73 ostype * ?|?( ostype *os, double d ) { 73 74 char buffer[32]; 74 return write( os, buffer, sprintf( buffer, "% g", d ) );75 return write( os, buffer, sprintf( buffer, "%.*lg", DBL_DIG, d ) ); 75 76 } // ?|? 76 77 … … 78 79 ostype * ?|?( ostype *os, long double d ) { 79 80 char buffer[32]; 80 return write( os, buffer, sprintf( buffer, "% Lg", d ) );81 return write( os, buffer, sprintf( buffer, "%.*Lg", LDBL_DIG, d ) ); 81 82 } // ?|? 82 83 … … 110 111 forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 111 112 retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) { 112 return manip( os);113 return manip( os ); 113 114 } 114 115 115 116 forall( dtype ostype | ostream( ostype ) ) 116 117 ostype * endl( ostype * os ) { 117 118 // flush 119 118 os | "\n"; 119 flush( os ); 120 return os; 120 121 } // endl 121 122 122 forall( type elt_type | writeable( elt_type ), 123 type iterator_type | iterator( iterator_type, elt_type ), 123 //--------------------------------------- 124 125 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), 124 126 dtype os_type | ostream( os_type ) ) 125 127 void write( iterator_type begin, iterator_type end, os_type *os ) { 126 void print( elt_type i ) { 127 os | i | ' '; 128 } 128 void print( elt_type i ) { os | i | ' '; } 129 129 for_each( begin, end, print ); 130 130 } // ?|? 131 131 132 forall( type elt_type | writeable( elt_type ), 133 type iterator_type | iterator( iterator_type, elt_type ), 132 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), 134 133 dtype os_type | ostream( os_type ) ) 135 134 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) { … … 138 137 } // ?|? 139 138 139 //--------------------------------------- 140 140 141 141 forall( dtype istype | istream( istype ) ) … … 146 146 forall( dtype istype | istream( istype ) ) 147 147 istype * ?|?( istype *is, int *ip ) { 148 char cur; 149 150 // skip some whitespace 151 do { 152 is | &cur; 153 if ( fail( is ) || eof( is ) ) return is; 154 } while ( !( cur >= '0' && cur <= '9' ) ); 155 156 // accumulate digits 157 *ip = 0; 158 while ( cur >= '0' && cur <= '9' ) { 159 *ip = *ip * 10 + ( cur - '0' ); 160 is | &cur; 161 if ( fail( is ) || eof( is ) ) return is; 162 } 163 164 unread( is, cur ); 165 return is; 148 return get( is, ip ); 166 149 } // ?|? 167 150 -
src/libcfa/stdlib.c
r7528ba1 ra172972 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Feb 5 15:41:24201613 // Update Count : 1 2812 // Last Modified On : Wed Feb 10 15:45:56 2016 13 // Update Count : 140 14 14 // 15 15 … … 23 23 #include <string.h> // memset 24 24 #include <malloc.h> // malloc_usable_size 25 #include <stdio.h>26 25 #include <math.h> // fabsf, fabs, fabsl 27 26 #include <complex.h> // _Complex_I, cabsf, cabs, cabsl … … 106 105 long int ato( const char * ptr ) { 107 106 long int li; 108 if ( sscanf( ptr, "%ld", &li ) == EOF ) {} ;// check return code107 if ( sscanf( ptr, "%ld", &li ) == EOF ) {} // check return code 109 108 return li; 110 109 } 111 110 unsigned long int ato( const char * ptr ) { 112 111 unsigned long int uli; 113 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} ;// check return code112 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} // check return code 114 113 return uli; 115 114 } 116 115 long long int ato( const char * ptr ) { 117 116 long long int lli; 118 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} ;// check return code117 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} // check return code 119 118 return lli; 120 119 } 121 120 unsigned long long int ato( const char * ptr ) { 122 121 unsigned long long int ulli; 123 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} ;// check return code122 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} // check return code 124 123 return ulli; 125 124 } 126 125 float ato( const char * ptr ) { 127 126 float f; 128 if ( sscanf( ptr, "%f", &f ) == EOF ) {} ;// check return code127 if ( sscanf( ptr, "%f", &f ) == EOF ) {} // check return code 129 128 return f; 130 129 } 131 130 double ato( const char * ptr ) { 132 131 double d; 133 if ( sscanf( ptr, "%lf", &d ) == EOF ) {} ;// check return code132 if ( sscanf( ptr, "%lf", &d ) == EOF ) {} // check return code 134 133 return d; 135 134 } 136 135 long double ato( const char * ptr ) { 137 136 long double ld; 138 printf( "FRED " ); 139 if ( sscanf( ptr, "%.32Lf", &ld ) == EOF ) {}; // check return code 137 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {} // check return code 140 138 return ld; 141 139 } 142 140 float _Complex ato( const char * ptr ) { 143 141 float re, im; 144 if ( sscanf( ptr, "%g%g ", &re, &im ) == EOF ) {};// check return code142 if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {} // check return code 145 143 return re + im * _Complex_I; 146 144 } 147 145 double _Complex ato( const char * ptr ) { 148 146 double re, im; 149 if ( sscanf( ptr, "% .16lg%.16lg", &re, &im ) == EOF ) {};// check return code147 if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {} // check return code 150 148 return re + im * _Complex_I; 151 149 } 152 150 long double _Complex ato( const char * ptr ) { 153 151 long double re, im; 154 if ( sscanf( ptr, "% .32Lg%.32Lg", &re, &im ) == EOF ) {};// check return code152 if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {} // check return code 155 153 return re + im * _Complex_I; 156 154 }
Note:
See TracChangeset
for help on using the changeset viewer.