home · contact · privacy
Replaced entire read/write_uint*_bigendian() family in readwrite library with simpler...
[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 extern FILE * try_fopen(char * path, char * mode, char * f)
16 {
17     char * msg1 = "Trouble in ";
18     char * msg2 = " with fopen() (mode '";
19     char * msg3 = "') on path '";
20     char * msg4 = "'.";
21     uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
22                     + strlen(f) + strlen(path) + strlen(mode) + 1;
23     char msg[size];
24     sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
25     FILE * file_p = fopen(path, mode);
26     exit_err(NULL == file_p, msg);
27     return file_p;
28 }
29
30
31
32 extern void try_fclose(FILE * file, char * f)
33 {
34     exit_trouble(fclose(file), f, "fclose()");
35 }
36
37
38
39 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
40                        char * f)
41 {
42     exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
43 }
44
45
46
47 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
48 {
49     char * test = fgets(line, linemax, file);
50     exit_trouble(NULL == test && ferror(file), f, "fgets()");
51     return test;
52 }
53
54
55
56 extern uint8_t try_fgetc(FILE * file, char * f)
57 {
58     int test = fgetc(file);
59     exit_trouble(EOF == test, f, "fgetc() unexpectedly hitting EOF");
60     return (uint8_t) test;
61 }
62
63
64
65 extern void try_fputc(uint8_t c, FILE * file, char * f)
66 {
67     int test = fputc(c, file);
68     exit_trouble(EOF == test, f, "fputc() hitting EOF");
69 }
70
71
72
73 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
74                                      char * f)
75 {
76     try_fclose(file, f);
77     char * msg1 = "Trouble in ";
78     char * msg4 = "'.";
79     if (!access(p2, F_OK))
80     {
81         char * msg2 = " with unlink() on path '";
82         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
83                         + strlen(f) + strlen(p2) + 1;
84         char msg[size];
85         sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
86         exit_err(unlink(p2), msg);
87     }
88     char * msg2 = " with rename() from '";
89     char * msg3 = "' to '";
90     uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
91                     + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
92     char msg[size];
93     sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
94     exit_err(rename(p1, p2), msg);
95 }
96
97
98
99 extern uint16_t get_linemax(FILE * file, char * f)
100 {
101     uint16_t linemax;
102     exit_trouble(textfile_sizes(file, &linemax, NULL), f, "textfile_sizes()");
103     return linemax;
104 }
105
106
107
108 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
109                               uint16_t * n_lines_p)
110 {
111     int c = 0;
112     uint16_t c_count = 0;
113     uint16_t n_lines = 0;
114     uint16_t linemax = 0;
115     while (1)
116     {
117         c = fgetc(file);
118         if (EOF == c)
119         {
120             break;
121         }
122         c_count++;
123         if ('\n' == c)
124         {
125             if (c_count > linemax)
126             {
127                 linemax = c_count;
128             }
129             c_count = 0;
130             if (n_lines_p)
131             {
132                 n_lines++;
133             }
134         }
135     }
136     if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
137     {                                /* line / lack newline chars.            */
138         linemax = c_count;
139     }
140
141     if (-1 == fseek(file, 0, SEEK_SET))
142     {
143         return 1;
144     }
145     * linemax_p = linemax;
146     if (n_lines_p)
147     {
148         * n_lines_p = n_lines;
149     }
150     return 0;
151 }
152
153
154
155 extern uint32_t read_uint32_bigendian(FILE * file)
156 {
157     char * f_name = "read_uint32_bigendian()";
158     uint32_t x;
159     x =       (uint32_t) try_fgetc(file, f_name) << 24;
160     x = x + ( (uint32_t) try_fgetc(file, f_name) << 16 );
161     x = x + ( (uint32_t) try_fgetc(file, f_name) <<  8 );
162     x = x +   (uint32_t) try_fgetc(file, f_name);
163     return x;
164 }
165
166
167
168 extern void write_uint32_bigendian(uint32_t x, FILE * file)
169 {
170     char * f_name = "write_uint32_bigendian()";
171     try_fputc(   x >> 24,          file, f_name);
172     try_fputc( ( x >> 16 ) & 0xFF, file, f_name);
173     try_fputc( ( x >>  8 ) & 0xFF, file, f_name);
174     try_fputc(   x         & 0xFF, file, f_name);
175 }