home · contact · privacy
94150f17b515b723fe70d43adfafae8be3455775
[plomrogue] / src / common / readwrite.c
1 /* src/common/readwrite.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #include "readwrite.h"
9 #include <stddef.h> /* NULL, size_t */
10 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
11 #include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
12                     * fputc(), fwrite(), fclose(), fopen(), clearerr()
13                     */
14 #include <stdlib.h> /* free() */
15 #include <string.h> /* strlen(), memcpy(), strchr() */
16 #include <unistd.h> /* access(), unlink() */
17 #include "rexit.h" /* exit_err(), exit_trouble() */
18 #include "try_malloc.h" /* try_malloc() */
19
20
21
22 extern FILE * try_fopen(char * path, char * mode, const char * f)
23 {
24     char * msg1 = "Trouble in ";
25     char * msg2 = " with fopen (mode '";
26     char * msg3 = "') on path '";
27     char * msg4 = "'.";
28     uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
29                     + strlen(f) + strlen(path) + strlen(mode) + 1;
30     char * msg = try_malloc(size, __func__);
31     int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4);
32     exit_trouble(test < 0, __func__, "sprintf");
33     FILE * file_p = fopen(path, mode);
34     exit_err(!file_p, msg);
35     free(msg);
36     return file_p;
37 }
38
39
40
41 extern void try_fclose(FILE * file, const char * f)
42 {
43     exit_trouble(fclose(file), f, "fclose");
44 }
45
46
47
48 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
49                        const char * f)
50 {
51     exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite");
52 }
53
54
55
56 extern void try_fputc(uint8_t c, FILE * file, const char * f)
57 {
58     exit_trouble(EOF == fputc(c, file), f, "fputc");
59 }
60
61
62
63 extern int try_fgetc(FILE * file, const char * f)
64 {
65     clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
66     int test = fgetc(file);
67     exit_trouble(EOF == test && ferror(file), f, "fgetc");
68     return test;
69 }
70
71
72
73 extern char * try_fgets(char * line, int linemax, FILE * file, const char * f)
74 {
75     char * test = fgets(line, linemax, file);
76     exit_trouble(!test && ferror(file), f, "fgets");
77     return test;
78 }
79
80
81
82 extern char * build_temp_path(char * path)
83 {
84     char * suffix_tmp = "_tmp";
85     uint16_t size = strlen(path) + strlen(suffix_tmp) + 1;
86     char * path_tmp = try_malloc(size, __func__);
87     int test = sprintf(path_tmp, "%s%s", path, suffix_tmp);
88     exit_trouble(test < 0, __func__, "sprintf");
89     return path_tmp;
90 }
91
92
93
94 extern FILE * atomic_write_start(char * path, char ** path_tmp)
95 {
96     *path_tmp = build_temp_path(path);
97     return try_fopen(*path_tmp, "w", __func__);
98 }
99
100
101
102 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp)
103 {
104     try_fclose(file, __func__);
105     char * msg1 = "Trouble in ";
106     char * msg4 = "'.";
107     if (!access(path, F_OK))
108     {
109         char * msg2 = " with unlink on path '";
110         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
111                         + strlen(__func__) + strlen(path) + 1;
112         char * msg = try_malloc(size, __func__);
113         int test = sprintf(msg, "%s%s%s%s%s", msg1, __func__, msg2, path, msg4);
114         exit_trouble(test < 0, __func__, "sprintf");
115         exit_err(unlink(path), msg);
116         free(msg);
117     }
118     char * msg2 = " with rename from '";
119     char * msg3 = "' to '";
120     uint16_t size =   strlen(msg1) + strlen(__func__) + strlen(msg2) +
121                     + strlen(path_tmp) + strlen(msg3) + strlen(path)
122                     + strlen(msg4) + 1;
123     char * msg = try_malloc(size, __func__);
124     int test = sprintf(msg, "%s%s%s%s%s%s%s",
125                             msg1, __func__, msg2, path_tmp, msg3, path, msg4);
126     exit_trouble(test < 0, __func__, "sprintf");
127     exit_err(rename(path_tmp, path), msg);
128     free(msg);
129     free(path_tmp);
130 }
131
132
133
134 extern void detect_atomic_leftover(char * path)
135 {
136     char * path_tmp = build_temp_path(path);
137     char * part1 = "Found file '";
138     char * part2 = "' that may be a leftover from an aborted previous attempt "
139                    "to write '";
140     char * part3 = "'. Aborting until the matter is solved by (re-)moving it.";
141     uint32_t size =   strlen(part1) + strlen(path_tmp) + strlen(part2)
142                     + strlen(path) + strlen(part3) + 1;
143     char * msg = try_malloc(size, __func__);
144     int test = sprintf(msg, "%s%s%s%s%s", part1, path_tmp, part2, path, part3);
145     exit_trouble(test < 0, __func__, "sprintf");
146     exit_err(!access(path_tmp, F_OK), msg);
147     free(msg);
148     free(path_tmp);
149 }
150
151
152
153 extern uint32_t textfile_width(FILE * file)
154 {
155     int c = 0;
156     uint32_t c_count = 0;
157     uint32_t linemax = 0;
158     while (1)
159     {
160         c = try_fgetc(file, __func__);
161         if (EOF == c)
162         {
163             break;
164         }
165         c_count++;
166         exit_trouble(UINT32_MAX==c_count, __func__, "too large text file line");
167         if ('\n' == c)
168         {
169             if (c_count > linemax)
170             {
171                 linemax = c_count;
172             }
173             c_count = 0;
174         }
175     }
176   if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
177   {                                /* line / lack newline chars.            */
178       linemax = c_count;
179    }
180    exit_trouble(-1 == fseek(file, 0, SEEK_SET), __func__, "fseek");
181    return linemax;
182 }
183
184
185
186 extern uint8_t read_file_into_queue(FILE * file, char ** queue)
187 {
188     uint8_t ret = 0;
189     int test;
190     while (EOF != (test = try_fgetc(file, __func__)))
191     {
192         ret = 1;
193         if ('\0' != test)
194         {
195             if (*queue)
196             {
197                 char * new_queue = try_malloc(strlen(*queue) + 1 + 1, __func__);
198                 memcpy(new_queue, *queue, strlen(*queue));
199                 new_queue[strlen(*queue)] = (char) test;
200                 new_queue[strlen(*queue) + 1] = '\0';
201                 free(*queue);
202                 *queue = new_queue;
203             }
204             else
205             {
206                 *queue = try_malloc(1 + 1, __func__);
207                 (*queue)[0] = (char) test;
208                 (*queue)[1] = '\0';
209             }
210         }
211     }
212     return ret;
213 }
214
215
216
217 extern char * get_message_from_queue(char ** queue)
218 {
219     if (!(*queue))
220     {
221         return NULL;
222     }
223     char * first_nl = strchr(*queue, '\n');
224     if (!first_nl)
225     {
226         return NULL;
227     }
228     char * msg = try_malloc(first_nl - (*queue) + 1, __func__);
229     memcpy(msg, *queue, first_nl - (*queue));
230     msg[first_nl - (*queue)] = '\0';
231     char * new_queue = try_malloc(strlen(first_nl + 1) + 1, __func__);
232     memcpy(new_queue, first_nl + 1, strlen(first_nl + 1) + 1);
233     free(*queue);
234     *queue = new_queue;
235     return msg;
236 }