home · contact · privacy
Some code-stylistic improvements to rexit library; also moved exit_trouble() into it.
[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(), exit_trouble() */
11 #include "main.h"   /* for world global */
12
13
14
15 /* Read/write "x" from/to "file" as bigendian representation of "size" bits. On
16  * failure, return 1, else 0. (As of of now, all extern read/write functions
17  * build on top of these.)
18  *
19  * Only use multiples of 8 greater or equal 32 for "size", so that storage
20  * inside uint32_t is possible. Originally a bit number check prefaced the code
21  * of both functions. It was removed as redundant due to all possible "size"
22  * values being hardcoded into the library (i.e. in all extern functions calling
23  * / wrapping around either function). If this ever changes, (re-)insert:
24  *
25  *    if (0 == size || size > 32 || 0 != size % 8)
26  *    {
27  *        return 1;
28  *    }
29  */
30 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size);
31 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size);
32
33
34
35 extern FILE * try_fopen(char * path, char * mode, char * f)
36 {
37     char * msg1 = "Trouble in ";
38     char * msg2 = " with fopen() (mode '";
39     char * msg3 = "') on path '";
40     char * msg4 = "'.";
41     uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
42                     + strlen(f) + strlen(path) + strlen(mode) + 1;
43     char msg[size];
44     sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
45     FILE * file_p = fopen(path, mode);
46     exit_err(NULL == file_p, msg);
47     return file_p;
48 }
49
50
51
52 extern void try_fclose(FILE * file, char * f)
53 {
54     exit_trouble(fclose(file), f, "fclose()");
55 }
56
57
58
59 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
60 {
61     char * test = fgets(line, linemax, file);
62     exit_trouble(NULL == test && ferror(file), f, "fgets()");
63     return test;
64 }
65
66
67
68 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
69                        char * f)
70 {
71     exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
72 }
73
74
75
76 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
77                                      char * f)
78 {
79     try_fclose(file, f);
80     char * msg1 = "Trouble in ";
81     char * msg4 = "'.";
82     if (!access(p2, F_OK))
83     {
84         char * msg2 = " with unlink() on path '";
85         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
86                         + strlen(f) + strlen(p2) + 1;
87         char msg[size];
88         sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
89         exit_err(unlink(p2), msg);
90     }
91     char * msg2 = " with rename() from '";
92     char * msg3 = "' to '";
93     uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
94                     + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
95     char msg[size];
96     sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
97     exit_err(rename(p1, p2), msg);
98 }
99
100
101
102 extern uint16_t get_linemax(FILE * file, char * f)
103 {
104     uint16_t linemax;
105     exit_trouble(textfile_sizes(file, &linemax, NULL), f, "textfile_sizes()");
106     return linemax;
107 }
108
109
110
111 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
112                               uint16_t * n_lines_p)
113 {
114     int c = 0;
115     uint16_t c_count = 0;
116     uint16_t n_lines = 0;
117     uint16_t linemax = 0;
118     while (1)
119     {
120         c = fgetc(file);
121         if (EOF == c)
122         {
123             break;
124         }
125         c_count++;
126         if ('\n' == c)
127         {
128             if (c_count > linemax)
129             {
130                 linemax = c_count;
131             }
132             c_count = 0;
133             if (n_lines_p)
134             {
135                 n_lines++;
136             }
137         }
138     }
139     if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
140     {                                /* line / lack newline chars.            */
141         linemax = c_count;
142     }
143
144     if (-1 == fseek(file, 0, SEEK_SET))
145     {
146         return 1;
147     }
148     * linemax_p = linemax;
149     if (n_lines_p)
150     {
151         * n_lines_p = n_lines;
152     }
153     return 0;
154 }
155
156
157
158 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size)
159 {
160     * x = 0;
161     int16_t bitshift = size - 8;
162     int test;
163     for (; bitshift >= 0; bitshift = bitshift - 8)
164     {
165         test = fgetc(file);
166         if (EOF == test)
167         {
168             return 1;
169         }
170         * x = * x + ((uint32_t) test << bitshift);
171     }
172     return 0;
173 }
174
175
176
177 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size)
178 {
179     int16_t bitshift = size - 8;
180     for (; bitshift >= 0; bitshift = bitshift - 8)
181     {
182         if (EOF == fputc((x >> bitshift) & 0xFF, file))
183         {
184             return 1;
185         }
186     }
187     return 0;
188 }
189
190
191
192 extern uint8_t read_uint8(FILE * file, uint8_t * x)
193 {
194     /* Since read_uintX_bigendian() works on -- and zeroes -- four bytes, direct
195      * work on values of fewer bytes would corrupt immediate neighbor values.
196      */
197     uint32_t y = * x;
198     uint8_t err = read_uintX_bigendian(file, &y, 8);
199     * x = (uint8_t) y;
200     return err;
201 }
202
203
204
205 extern uint8_t read_uint16_bigendian(FILE * file, uint16_t * x)
206 {
207     /* See read_uint8() introductory code comment for rationale. */
208     uint32_t y = * x;
209     uint8_t err = read_uintX_bigendian(file, &y, 16);
210     * x = (uint16_t) y;
211     return err;
212 }
213
214
215
216 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x)
217 {
218     return read_uintX_bigendian(file, x, 32);
219 }
220
221
222
223 extern uint8_t write_uint8(uint8_t x, FILE * file)
224 {
225     return write_uintX_bigendian(file, x, 8);
226 }
227
228
229
230 extern uint8_t write_uint16_bigendian(uint16_t x, FILE * file)
231 {
232     return write_uintX_bigendian(file, x, 16);
233 }
234
235
236
237 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file)
238 {
239     return write_uintX_bigendian(file, x, 32);
240 }