File.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013-2014 by Wayne C. Gramlich all rights reserved.
2 
3 #include <assert.h>
4 #include <ctype.h>
5 
6 #include "File.hpp"
7 #include "String.hpp"
8 
9 // *File* routines:
10 
16 
17 unsigned int File__byte_read(File file) {
18  int byte = fgetc(file);
19  assert (byte >= 0);
20  return (unsigned int)byte;
21 }
22 
28 
29 void File__byte_write(File file, unsigned int byte) {
30  fputc(byte, file);
31 }
32 
40 
41 int File__character_read(File in_file) {
42  return fgetc(in_file);
43 }
44 
49 
50 void File__close(File file) {
51  assert (fclose(file) == 0);
52 }
53 
65 
66 double File__double_attribute_read(File in_file, String_Const attribute_name) {
67  File__string_match(in_file, " ");
68  File__string_match(in_file, attribute_name);
69  File__string_match(in_file, "=\"");
70  double fraction = (double)1.0;
71  bool have_decimal_point = (bool)0;
72  bool negative = (bool)0;
73  double result = (double)0.0;
74  while (1) {
75  int character = File__character_read(in_file);
76  if (isdigit(character)) {
77  if (have_decimal_point) {
78  fraction /= (double)10.0;
79  result += fraction * (double)(character - '0');
80  } else {
81  result = result * 10.0 + (double)(character - '0');
82  }
83  } else if (character == '"') {
84  break;
85  } else if (character == '.') {
86  have_decimal_point = (bool)1;
87  } else if (character == '-') {
88  negative = (bool)1;
89  } else {
90  assert(0);
91  }
92  }
93  if (negative) {
94  result = -result;
95  }
96  return result;
97 }
98 
106 
107 void File__format(File file, String_Const format, ...) {
108  // Set up *variadic_arguments to start after *format*:
109  va_list variadic_arguments;
110  va_start(variadic_arguments, format);
111 
112  // Perform the format:
113  unsigned int formatted_size = vfprintf(file, format, variadic_arguments);
114 }
115 
127 
128 float File__float_attribute_read(File in_file, String_Const attribute_name) {
129  File__string_match(in_file, " ");
130  File__string_match(in_file, attribute_name);
131  File__string_match(in_file, "=\"");
132  float fraction = (float)1.0;
133  bool have_decimal_point = (bool)0;
134  bool negative = (bool)0;
135  float result = (float)0.0;
136  while (1) {
137  char character = File__character_read(in_file);
138  if (isdigit(character)) {
139  if (have_decimal_point) {
140  fraction /= (float)10.0;
141  result += fraction * (float)(character - '0');
142  } else {
143  result = result * 10.0 + (float)(character - '0');
144  }
145  } else if (character == '"') {
146  break;
147  } else if (character == '.') {
148  have_decimal_point = (bool)1;
149  } else if (character == '-') {
150  negative = (bool)1;
151  } else {
152  assert(0);
153  }
154  }
155  if (negative) {
156  result = -result;
157  }
158  return result;
159 }
160 
165 
166 void File__flush(File file) {
167  (void)fflush(file);
168 }
169 
170 
182 
184  File in_file, String_Const attribute_name) {
185  File__string_match(in_file, " ");
186  File__string_match(in_file, attribute_name);
187  File__string_match(in_file, "=\"");
188  bool negative = (bool)0;
189  int result = 0;
190  while (1) {
191  char character = File__character_read(in_file);
192  if (isdigit(character)) {
193  result = result * 10 + (character - '0');
194  } else if (character == '"') {
195  break;
196  } else if (character == '-') {
197  negative = (bool)1;
198  } else {
199  assert(0);
200  }
201  }
202  if (negative) {
203  result = -result;
204  }
205  return result;
206 }
207 
214 
216  int low_byte = fgetc(file);
217  assert (low_byte >= 0);
218  int high_byte = fgetc(file);
219  assert (high_byte >= 0);
220  unsigned int result = ((unsigned int)high_byte << 8) | (unsigned int)low_byte;
221  return result;
222 }
223 
230 
231 void File__little_endian_short_write(File file, unsigned int xshort) {
232  fputc(xshort & 0xff, file);
233  fputc((xshort >> 8) & 0xff, file);
234 }
235 
242 
244  return fopen(file_name, flags);
245 }
246 
254 
255 void File__string_match(File in_file, String_Const pattern) {
256  unsigned int size = String__size(pattern);
257  for (unsigned int index = 0; index < size; index++) {
258  char character = File__character_read(in_file);
259  assert(character == pattern[index]);
260  }
261 }
262 
271 
272 void File__tag_match(File in_file, String_Const tag_name) {
273  while (1) {
274  char character = File__character_read(in_file);
275  if (character == '<') {
276  break;
277  } else if (character == ' ') {
278  // Do nothing:
279  } else {
280  assert(0);
281  }
282  }
283  File__string_match(in_file, tag_name);
284 }
285 
void File__byte_write(File file, unsigned int byte)
Write byte ot file.
Definition: File.cpp:29
void File__close(File file)
Closes file.
Definition: File.cpp:50
unsigned int File__little_endian_short_read(File file)
Read a little endian short (16-bits) from file.
Definition: File.cpp:215
double File__double_attribute_read(File in_file, String_Const attribute_name)
Reads in an XML attribute with a floating point value.
Definition: File.cpp:66
FILE * File
FILE is a file I/O object.
Definition: File.hpp:12
void File__string_match(File in_file, String_Const pattern)
Exactly matches pattern read from in_file.
Definition: File.cpp:255
void File__flush(File file)
Flushes file content out of internal buffers.
Definition: File.cpp:166
int File__integer_attribute_read(File in_file, String_Const attribute_name)
Reads in an XML attribute with a integer value.
Definition: File.cpp:183
void File__little_endian_short_write(File file, unsigned int xshort)
Write 16-bit xshort to file in little endian format.
Definition: File.cpp:231
void File__format(File file, String_Const format,...)
will write format out to file with all patterns that start with "%" replaced by formatted versions of...
Definition: File.cpp:107
unsigned int File__byte_read(File file)
Read a byte from file.
Definition: File.cpp:17
float File__float_attribute_read(File in_file, String_Const attribute_name)
Reads in an XML attribute with a floating point value.
Definition: File.cpp:128
File File__open(String_Const file_name, String_Const flags)
will open file_name using flags to specify read/write options.
Definition: File.cpp:243
int File__character_read(File in_file)
Return the next character read from in_file.
Definition: File.cpp:41
void File__tag_match(File in_file, String_Const tag_name)
Matchs and "XML" start tag.
Definition: File.cpp:272
const char * String_Const
Definition: String.hpp:9
unsigned int String__size(String_Const string)
Returns the size of string.
Definition: String.cpp:75


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Dec 28 2017 04:06:53