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