File.cpp
Go to the documentation of this file.
00001 // Copyright (c) 2013-2014 by Wayne C. Gramlich all rights reserved.
00002 
00003 #include <assert.h>
00004 #include <ctype.h>
00005 
00006 #include "File.hpp"
00007 #include "String.hpp"
00008 
00009 // *File* routines:
00010 
00016 
00017 unsigned int File__byte_read(File file) {
00018     int byte = fgetc(file);
00019     assert (byte >= 0);
00020     return (unsigned int)byte;
00021 }
00022 
00028 
00029 void File__byte_write(File file, unsigned int byte) {
00030     fputc(byte, file);
00031 }
00032 
00040 
00041 int File__character_read(File in_file) {
00042     return fgetc(in_file);
00043 }
00044 
00049 
00050 void File__close(File file) {
00051     assert (fclose(file) == 0);
00052 }
00053 
00065 
00066 double File__double_attribute_read(File in_file, String_Const attribute_name) {
00067     File__string_match(in_file, " ");
00068     File__string_match(in_file, attribute_name);
00069     File__string_match(in_file, "=\"");
00070     double fraction = (double)1.0;
00071     bool have_decimal_point = (bool)0;
00072     bool negative = (bool)0;
00073     double result = (double)0.0;
00074     while (1) {
00075         int character = File__character_read(in_file);
00076         if (isdigit(character)) {
00077             if (have_decimal_point) {
00078                 fraction /= (double)10.0;
00079                 result += fraction * (double)(character - '0');
00080             } else {
00081                 result = result * 10.0 + (double)(character - '0');
00082             }
00083         } else if (character == '"') {
00084             break;
00085         } else if (character == '.') {
00086             have_decimal_point = (bool)1;
00087         } else if (character == '-') {
00088             negative = (bool)1;
00089         } else {
00090             assert(0);
00091         }
00092     }
00093     if (negative) {
00094         result = -result;
00095     }
00096     return result;
00097 }
00098 
00106 
00107 void File__format(File file, String_Const format, ...) {
00108     // Set up *variadic_arguments to start after *format*:
00109     va_list variadic_arguments;
00110     va_start(variadic_arguments, format);
00111 
00112     // Perform the format:
00113     unsigned int formatted_size = vfprintf(file, format, variadic_arguments);
00114 }
00115 
00127 
00128 float File__float_attribute_read(File in_file, String_Const attribute_name) {
00129     File__string_match(in_file, " ");
00130     File__string_match(in_file, attribute_name);
00131     File__string_match(in_file, "=\"");
00132     float fraction = (float)1.0;
00133     bool have_decimal_point = (bool)0;
00134     bool negative = (bool)0;
00135     float result = (float)0.0;
00136     while (1) {
00137         char character = File__character_read(in_file);
00138         if (isdigit(character)) {
00139             if (have_decimal_point) {
00140                 fraction /= (float)10.0;
00141                 result += fraction * (float)(character - '0');
00142             } else {
00143                 result = result * 10.0 + (float)(character - '0');
00144             }
00145         } else if (character == '"') {
00146             break;
00147         } else if (character == '.') {
00148             have_decimal_point = (bool)1;
00149         } else if (character == '-') {
00150             negative = (bool)1;
00151         } else {
00152             assert(0);
00153         }
00154     }
00155     if (negative) {
00156         result = -result;
00157     }
00158     return result;
00159 }
00160 
00165 
00166 void File__flush(File file) {
00167     (void)fflush(file);
00168 }
00169 
00170 
00182 
00183 int File__integer_attribute_read(
00184   File in_file, String_Const attribute_name) {
00185     File__string_match(in_file, " ");
00186     File__string_match(in_file, attribute_name);
00187     File__string_match(in_file, "=\"");
00188     bool negative = (bool)0;
00189     int result = 0;
00190     while (1) {
00191         char character = File__character_read(in_file);
00192         if (isdigit(character)) {
00193             result = result * 10 + (character - '0');
00194         } else if (character == '"') {
00195             break;
00196         } else if (character == '-') {
00197             negative = (bool)1;
00198         } else {
00199             assert(0);
00200         }
00201     }
00202     if (negative) {
00203         result = -result;
00204     }
00205     return result;
00206 }
00207 
00214 
00215 unsigned int File__little_endian_short_read(File file) {
00216     int low_byte = fgetc(file);
00217     assert (low_byte >= 0);
00218     int high_byte = fgetc(file);
00219     assert (high_byte >= 0);
00220     unsigned int result = ((unsigned int)high_byte << 8) | (unsigned int)low_byte;
00221     return result;
00222 }
00223 
00230 
00231 void File__little_endian_short_write(File file, unsigned int xshort) {
00232     fputc(xshort & 0xff, file);
00233     fputc((xshort >> 8) & 0xff, file);
00234 }
00235 
00242 
00243 File File__open(String_Const file_name, String_Const flags) {
00244     return fopen(file_name, flags);
00245 }
00246 
00254 
00255 void File__string_match(File in_file, String_Const pattern) {
00256     unsigned int size = String__size(pattern);
00257     for (unsigned int index = 0; index < size; index++) {
00258         char character = File__character_read(in_file);
00259         assert(character == pattern[index]);
00260     }
00261 }
00262 
00271 
00272 void File__tag_match(File in_file, String_Const tag_name) {
00273     while (1) {
00274         char character = File__character_read(in_file);
00275         if (character == '<') {
00276             break;
00277         } else if (character == ' ') {
00278             // Do nothing:
00279         } else {
00280             assert(0);
00281         }
00282     }
00283     File__string_match(in_file, tag_name);
00284 }
00285 


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Jun 6 2019 18:08:04