4 #include <stdio.h> /* for FILE typedef, fopen(), fgetc(), fputc(), fseek(),
5 * sprintf(), fwrite(), ferror()
7 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
8 #include <string.h> /* for strlen()*/
9 #include <unistd.h> /* for unlink() */
10 #include "rexit.h" /* for exit_err() */
11 #include "misc.h" /* for trouble_msg() */
16 /* Read/write "x" from/to "file" as bigendian representation of "size" bits. On
17 * failure, return 1, else 0. (As of of now, all extern read/write functions
18 * build on top of these.)
20 * Only use multiples of 8 greater or equal 32 for "size", so that storage
21 * inside uint32_t is possible. Originally a bit number check prefaced the code
22 * of both functions. It was removed as redundant due to all possible "size"
23 * values being hardcoded into the library (i.e. in all extern functions calling
24 * / wrapping around either function). If this ever changes, (re-)insert:
26 * if (0 == size || size > 32 || 0 != size % 8)
31 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size);
32 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size);
36 extern FILE * try_fopen(char * path, char * mode, struct World * w, char * f)
38 char * msg1 = "Trouble in ";
39 char * msg2 = " with fopen() (mode '";
40 char * msg3 = "') on path '";
42 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
43 + strlen(f) + strlen(path) + strlen(mode) + 1;
45 sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
46 FILE * file_p = fopen(path, mode);
47 exit_err(NULL == file_p, w, msg);
53 extern void try_fclose(FILE * file, struct World * w, char * f)
55 char * msg = trouble_msg(w, f, "fclose()");
56 exit_err(fclose(file), w, msg);
62 extern char * try_fgets(char * line, int linemax, FILE * file,
63 struct World * w, char * f)
65 char * msg = trouble_msg(w, f, "fgets()");
66 char * test = fgets(line, linemax, file);
67 exit_err(NULL == test && ferror(file), w, msg);
74 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
75 struct World * w, char * f)
77 char * msg = trouble_msg(w, f, "fwrite()");
78 exit_err(0 == fwrite(ptr, size, nmemb, stream), w, msg);
84 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
85 struct World * w, char * f)
87 try_fclose(file, w, f);
88 char * msg1 = "Trouble in ";
90 if (!access(p2, F_OK))
92 char * msg2 = " with unlink() on path '";
93 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
94 + strlen(f) + strlen(p2) + 1;
96 sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
97 exit_err(unlink(p2), w, msg);
99 char * msg2 = " with rename() from '";
100 char * msg3 = "' to '";
101 uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
102 + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
104 sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
105 exit_err(rename(p1, p2), w, msg);
110 extern uint16_t get_linemax(FILE * file, struct World * w, char * f)
112 char * msg = trouble_msg(w, f, "textfile_sizes()");
114 exit_err(textfile_sizes(file, &linemax, NULL), w, msg);
121 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
122 uint16_t * n_lines_p)
125 uint16_t c_count = 0;
126 uint16_t n_lines = 0;
127 uint16_t linemax = 0;
138 if (c_count > linemax)
149 if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
150 { /* line / lack newline chars. */
154 if (-1 == fseek(file, 0, SEEK_SET))
158 * linemax_p = linemax;
161 * n_lines_p = n_lines;
168 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size)
171 int16_t bitshift = size - 8;
173 for (; bitshift >= 0; bitshift = bitshift - 8)
180 * x = * x + ((uint32_t) test << bitshift);
187 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size)
189 int16_t bitshift = size - 8;
190 for (; bitshift >= 0; bitshift = bitshift - 8)
192 if (EOF == fputc((x >> bitshift) & 0xFF, file))
202 extern uint8_t read_uint8(FILE * file, uint8_t * x)
204 /* Since read_uintX_bigendian() works on -- and zeroes -- four bytes, direct
205 * work on values of fewer bytes would corrupt immediate neighbor values.
208 uint8_t err = read_uintX_bigendian(file, &y, 8);
215 extern uint8_t read_uint16_bigendian(FILE * file, uint16_t * x)
217 /* See read_uint8() introductory code comment for rationale. */
219 uint8_t err = read_uintX_bigendian(file, &y, 16);
226 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x)
228 return read_uintX_bigendian(file, x, 32);
233 extern uint8_t write_uint8(uint8_t x, FILE * file)
235 return write_uintX_bigendian(file, x, 8);
240 extern uint8_t write_uint16_bigendian(uint16_t x, FILE * file)
242 return write_uintX_bigendian(file, x, 16);
247 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file)
249 return write_uintX_bigendian(file, x, 32);