home · contact · privacy
Made single World struct a global variable, fitted a lot of code to this change,...
[plomrogue] / src / readwrite.c
1 /* readwrite.c */
2
3 #include "readwrite.h"
4 #include <stdio.h>  /* for FILE typedef, fopen(), fgetc(), fputc(), fseek(),
5                      * sprintf(), fwrite(), ferror()
6                      */
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() */
12 #include "main.h"   /* for world global */
13
14
15
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.)
19  *
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:
25  *
26  *    if (0 == size || size > 32 || 0 != size % 8)
27  *    {
28  *        return 1;
29  *    }
30  */
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);
33
34
35
36 extern FILE * try_fopen(char * path, char * mode, char * f)
37 {
38     char * msg1 = "Trouble in ";
39     char * msg2 = " with fopen() (mode '";
40     char * msg3 = "') on path '";
41     char * msg4 = "'.";
42     uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
43                     + strlen(f) + strlen(path) + strlen(mode) + 1;
44     char msg[size];
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, msg);
48     return file_p;
49 }
50
51
52
53 extern void try_fclose(FILE * file, char * f)
54 {
55     char * msg = trouble_msg(f, "fclose()");
56     exit_err(fclose(file), msg);
57     free(msg);
58 }
59
60
61
62 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
63 {
64     char * msg = trouble_msg(f, "fgets()");
65     char * test = fgets(line, linemax, file);
66     exit_err(NULL == test && ferror(file), msg);
67     free(msg);
68     return test;
69 }
70
71
72
73 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
74                        char * f)
75 {
76     char * msg = trouble_msg(f, "fwrite()");
77     exit_err(0 == fwrite(ptr, size, nmemb, stream), msg);
78     free(msg);
79 }
80
81
82
83 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
84                                      char * f)
85 {
86     try_fclose(file, f);
87     char * msg1 = "Trouble in ";
88     char * msg4 = "'.";
89     if (!access(p2, F_OK))
90     {
91         char * msg2 = " with unlink() on path '";
92         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
93                         + strlen(f) + strlen(p2) + 1;
94         char msg[size];
95         sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
96         exit_err(unlink(p2), msg);
97     }
98     char * msg2 = " with rename() from '";
99     char * msg3 = "' to '";
100     uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
101                     + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
102     char msg[size];
103     sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
104     exit_err(rename(p1, p2), msg);
105 }
106
107
108
109 extern uint16_t get_linemax(FILE * file, char * f)
110 {
111     char * msg = trouble_msg(f, "textfile_sizes()");
112     uint16_t linemax;
113     exit_err(textfile_sizes(file, &linemax, NULL), msg);
114     free(msg);
115     return linemax;
116 }
117
118
119
120 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
121                               uint16_t * n_lines_p)
122 {
123     int c = 0;
124     uint16_t c_count = 0;
125     uint16_t n_lines = 0;
126     uint16_t linemax = 0;
127     while (1)
128     {
129         c = fgetc(file);
130         if (EOF == c)
131         {
132             break;
133         }
134         c_count++;
135         if ('\n' == c)
136         {
137             if (c_count > linemax)
138             {
139                 linemax = c_count;
140             }
141             c_count = 0;
142             if (n_lines_p)
143             {
144                 n_lines++;
145             }
146         }
147     }
148     if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
149     {                                /* line / lack newline chars.            */
150         linemax = c_count;
151     }
152
153     if (-1 == fseek(file, 0, SEEK_SET))
154     {
155         return 1;
156     }
157     * linemax_p = linemax;
158     if (n_lines_p)
159     {
160         * n_lines_p = n_lines;
161     }
162     return 0;
163 }
164
165
166
167 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size)
168 {
169     * x = 0;
170     int16_t bitshift = size - 8;
171     int test;
172     for (; bitshift >= 0; bitshift = bitshift - 8)
173     {
174         test = fgetc(file);
175         if (EOF == test)
176         {
177             return 1;
178         }
179         * x = * x + ((uint32_t) test << bitshift);
180     }
181     return 0;
182 }
183
184
185
186 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size)
187 {
188     int16_t bitshift = size - 8;
189     for (; bitshift >= 0; bitshift = bitshift - 8)
190     {
191         if (EOF == fputc((x >> bitshift) & 0xFF, file))
192         {
193             return 1;
194         }
195     }
196     return 0;
197 }
198
199
200
201 extern uint8_t read_uint8(FILE * file, uint8_t * x)
202 {
203     /* Since read_uintX_bigendian() works on -- and zeroes -- four bytes, direct
204      * work on values of fewer bytes would corrupt immediate neighbor values.
205      */
206     uint32_t y = * x;
207     uint8_t err = read_uintX_bigendian(file, &y, 8);
208     * x = (uint8_t) y;
209     return err;
210 }
211
212
213
214 extern uint8_t read_uint16_bigendian(FILE * file, uint16_t * x)
215 {
216     /* See read_uint8() introductory code comment for rationale. */
217     uint32_t y = * x;
218     uint8_t err = read_uintX_bigendian(file, &y, 16);
219     * x = (uint16_t) y;
220     return err;
221 }
222
223
224
225 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x)
226 {
227     return read_uintX_bigendian(file, x, 32);
228 }
229
230
231
232 extern uint8_t write_uint8(uint8_t x, FILE * file)
233 {
234     return write_uintX_bigendian(file, x, 8);
235 }
236
237
238
239 extern uint8_t write_uint16_bigendian(uint16_t x, FILE * file)
240 {
241     return write_uintX_bigendian(file, x, 16);
242 }
243
244
245
246 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file)
247 {
248     return write_uintX_bigendian(file, x, 32);
249 }