home · contact · privacy
78f1bffb73d4b4472c5daa699ba4220f6c797456
[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 void try_fputc(uint8_t c, FILE * file, char * f)
48 {
49     exit_trouble(EOF == fputc(c, file), f, "fputc()");
50 }
51
52
53
54 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
55 {
56     char * test = fgets(line, linemax, file);
57     exit_trouble(NULL == test && ferror(file), f, "fgets()");
58     return test;
59 }
60
61
62
63 extern int try_fgetc(FILE * file, char * f)
64 {
65     int test = fgetc(file);
66     exit_trouble(EOF == test && ferror(file), f, "fgetc()");
67     return test;
68 }
69
70
71
72 extern uint8_t try_fgetc_noeof(FILE * file, char * f)
73 {
74     char * f_name = "try_fgetc_noeof()";
75     int test = try_fgetc(file, f_name);
76     exit_trouble(EOF == test, f, "fgetc() unexpectedly hitting EOF");
77     return (uint8_t) test;
78 }
79
80
81
82 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
83                                      char * f)
84 {
85     try_fclose(file, f);
86     char * msg1 = "Trouble in ";
87     char * msg4 = "'.";
88     if (!access(p2, F_OK))
89     {
90         char * msg2 = " with unlink() on path '";
91         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
92                         + strlen(f) + strlen(p2) + 1;
93         char msg[size];
94         sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
95         exit_err(unlink(p2), msg);
96     }
97     char * msg2 = " with rename() from '";
98     char * msg3 = "' to '";
99     uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
100                     + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
101     char msg[size];
102     sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
103     exit_err(rename(p1, p2), msg);
104 }
105
106
107
108 extern uint16_t get_linemax(FILE * file, char * f)
109 {
110     uint16_t linemax;
111     exit_trouble(textfile_sizes(file, &linemax, NULL), f, "textfile_sizes()");
112     return linemax;
113 }
114
115
116
117 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
118                               uint16_t * n_lines_p)
119 {
120     char * f_name = "textfile_sizes()";
121     int c = 0;
122     uint16_t c_count = 0;
123     uint16_t n_lines = 0;
124     uint16_t linemax = 0;
125     while (1)
126     {
127         c = try_fgetc(file, f_name);
128         if (EOF == c)
129         {
130             break;
131         }
132         c_count++;
133         if ('\n' == c)
134         {
135             if (c_count > linemax)
136             {
137                 linemax = c_count;
138             }
139             c_count = 0;
140             if (n_lines_p)
141             {
142                 n_lines++;
143             }
144         }
145     }
146     if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
147     {                                /* line / lack newline chars.            */
148         linemax = c_count;
149     }
150
151     if (-1 == fseek(file, 0, SEEK_SET))
152     {
153         return 1;
154     }
155     * linemax_p = linemax;
156     if (n_lines_p)
157     {
158         * n_lines_p = n_lines;
159     }
160     return 0;
161 }
162
163
164
165 extern uint32_t read_uint32_bigendian(FILE * file)
166 {
167     char * f_name = "read_uint32_bigendian()";
168     uint32_t x;
169     x =       (uint32_t) try_fgetc_noeof(file, f_name) << 24;
170     x = x + ( (uint32_t) try_fgetc_noeof(file, f_name) << 16 );
171     x = x + ( (uint32_t) try_fgetc_noeof(file, f_name) <<  8 );
172     x = x +   (uint32_t) try_fgetc_noeof(file, f_name);
173     return x;
174 }
175
176
177
178 extern void write_uint32_bigendian(uint32_t x, FILE * file)
179 {
180     char * f_name = "write_uint32_bigendian()";
181     try_fputc(   x >> 24,          file, f_name);
182     try_fputc( ( x >> 16 ) & 0xFF, file, f_name);
183     try_fputc( ( x >>  8 ) & 0xFF, file, f_name);
184     try_fputc(   x         & 0xFF, file, f_name);
185 }