home · contact · privacy
Removed unused code in readwrite library.
[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.) Only use multiples of 8 greater or equal 32 for
18  * "size", so that storage inside uint32_t is possible.
19  */
20 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size);
21 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size);
22
23
24
25 extern FILE * try_fopen(char * path, char * mode, char * f)
26 {
27     char * msg1 = "Trouble in ";
28     char * msg2 = " with fopen() (mode '";
29     char * msg3 = "') on path '";
30     char * msg4 = "'.";
31     uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
32                     + strlen(f) + strlen(path) + strlen(mode) + 1;
33     char msg[size];
34     sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
35     FILE * file_p = fopen(path, mode);
36     exit_err(NULL == file_p, msg);
37     return file_p;
38 }
39
40
41
42 extern void try_fclose(FILE * file, char * f)
43 {
44     exit_trouble(fclose(file), f, "fclose()");
45 }
46
47
48
49 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
50 {
51     char * test = fgets(line, linemax, file);
52     exit_trouble(NULL == test && ferror(file), f, "fgets()");
53     return test;
54 }
55
56
57
58 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
59                        char * f)
60 {
61     exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
62 }
63
64
65
66 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
67                                      char * f)
68 {
69     try_fclose(file, f);
70     char * msg1 = "Trouble in ";
71     char * msg4 = "'.";
72     if (!access(p2, F_OK))
73     {
74         char * msg2 = " with unlink() on path '";
75         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
76                         + strlen(f) + strlen(p2) + 1;
77         char msg[size];
78         sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
79         exit_err(unlink(p2), msg);
80     }
81     char * msg2 = " with rename() from '";
82     char * msg3 = "' to '";
83     uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
84                     + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
85     char msg[size];
86     sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
87     exit_err(rename(p1, p2), msg);
88 }
89
90
91
92 extern uint16_t get_linemax(FILE * file, char * f)
93 {
94     uint16_t linemax;
95     exit_trouble(textfile_sizes(file, &linemax, NULL), f, "textfile_sizes()");
96     return linemax;
97 }
98
99
100
101 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
102                               uint16_t * n_lines_p)
103 {
104     int c = 0;
105     uint16_t c_count = 0;
106     uint16_t n_lines = 0;
107     uint16_t linemax = 0;
108     while (1)
109     {
110         c = fgetc(file);
111         if (EOF == c)
112         {
113             break;
114         }
115         c_count++;
116         if ('\n' == c)
117         {
118             if (c_count > linemax)
119             {
120                 linemax = c_count;
121             }
122             c_count = 0;
123             if (n_lines_p)
124             {
125                 n_lines++;
126             }
127         }
128     }
129     if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
130     {                                /* line / lack newline chars.            */
131         linemax = c_count;
132     }
133
134     if (-1 == fseek(file, 0, SEEK_SET))
135     {
136         return 1;
137     }
138     * linemax_p = linemax;
139     if (n_lines_p)
140     {
141         * n_lines_p = n_lines;
142     }
143     return 0;
144 }
145
146
147
148 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size)
149 {
150     * x = 0;
151     int16_t bitshift = size - 8;
152     int test;
153     for (; bitshift >= 0; bitshift = bitshift - 8)
154     {
155         test = fgetc(file);
156         if (EOF == test)
157         {
158             return 1;
159         }
160         * x = * x + ((uint32_t) test << bitshift);
161     }
162     return 0;
163 }
164
165
166
167 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size)
168 {
169     int16_t bitshift = size - 8;
170     for (; bitshift >= 0; bitshift = bitshift - 8)
171     {
172         if (EOF == fputc((x >> bitshift) & 0xFF, file))
173         {
174             return 1;
175         }
176     }
177     return 0;
178 }
179
180
181
182 extern uint8_t read_uint8(FILE * file, uint8_t * x)
183 {
184     /* Since read_uintX_bigendian() works on -- and zeroes -- four bytes, direct
185      * work on values of fewer bytes would corrupt immediate neighbor values.
186      */
187     uint32_t y = * x;
188     uint8_t err = read_uintX_bigendian(file, &y, 8);
189     * x = (uint8_t) y;
190     return err;
191 }
192
193
194
195 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x)
196 {
197     return read_uintX_bigendian(file, x, 32);
198 }
199
200
201
202 extern uint8_t write_uint8(uint8_t x, FILE * file)
203 {
204     return write_uintX_bigendian(file, x, 8);
205 }
206
207
208
209 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file)
210 {
211     return write_uintX_bigendian(file, x, 32);
212 }