home · contact · privacy
Refactor atomic writing to atomic_write_start() + atomic_write_finish().
[plomrogue] / src / common / readwrite.c
1 /* src/common/readwrite.c */
2
3 #include "readwrite.h"
4 #include <stddef.h> /* size_t */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
6 #include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
7                     * fputc(), fwrite(), fclose(), fopen(), clearerr()
8                     */
9 #include <stdlib.h> /* free() */
10 #include <string.h> /* strlen() */
11 #include <unistd.h> /* access(), unlink() */
12 #include "rexit.h" /* exit_err(), exit_trouble() */
13 #include "try_malloc.h" /* try_malloc() */
14
15
16
17 extern FILE * try_fopen(char * path, char * mode, char * f)
18 {
19     char * f_name = "try_fopen()";
20     char * msg1 = "Trouble in ";
21     char * msg2 = " with fopen() (mode '";
22     char * msg3 = "') on path '";
23     char * msg4 = "'.";
24     uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
25                     + strlen(f) + strlen(path) + strlen(mode) + 1;
26     char * msg = try_malloc(size, f_name);
27     int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4);
28     exit_trouble(test < 0, f_name, "sprintf()");
29     FILE * file_p = fopen(path, mode);
30     exit_err(NULL == file_p, msg);
31     free(msg);
32     return file_p;
33 }
34
35
36
37 extern void try_fclose(FILE * file, char * f)
38 {
39     exit_trouble(fclose(file), f, "fclose()");
40 }
41
42
43
44 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
45                        char * f)
46 {
47     exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
48 }
49
50
51
52 extern void try_fputc(uint8_t c, FILE * file, char * f)
53 {
54     exit_trouble(EOF == fputc(c, file), f, "fputc()");
55 }
56
57
58
59 extern int try_fgetc(FILE * file, char * f)
60 {
61     clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
62     int test = fgetc(file);
63     exit_trouble(EOF == test && ferror(file), f, "fgetc()");
64     return test;
65 }
66
67
68
69 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
70 {
71     char * test = fgets(line, linemax, file);
72     exit_trouble(NULL == test && ferror(file), f, "fgets()");
73     return test;
74 }
75
76
77
78 extern FILE * atomic_write_start(char * path, char ** path_tmp)
79 {
80     char * f_name = "atomic_write_start()";
81     char * suffix_tmp = "_tmp";
82     uint16_t size = strlen(path) + strlen(suffix_tmp) + 1;
83     *path_tmp = try_malloc(size, f_name);
84     int test = sprintf(*path_tmp, "%s%s", path, suffix_tmp);
85     exit_trouble(test < 0, f_name, "sprintf()");
86     return try_fopen(*path_tmp, "w", f_name);
87 }
88
89
90
91 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp)
92 {
93     char * f_name = "atomic_write_finish()";
94     try_fclose(file, f_name);
95     char * msg1 = "Trouble in ";
96     char * msg4 = "'.";
97     if (!access(path, F_OK))
98     {
99         char * msg2 = " with unlink() on path '";
100         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
101                         + strlen(f_name) + strlen(path) + 1;
102         char * msg = try_malloc(size, f_name);
103         int test = sprintf(msg, "%s%s%s%s%s", msg1, f_name, msg2, path, msg4);
104         exit_trouble(test < 0, f_name, "sprintf()");
105         exit_err(unlink(path), msg);
106         free(msg);
107     }
108     char * msg2 = " with rename() from '";
109     char * msg3 = "' to '";
110     uint16_t size =   strlen(msg1) + strlen(f_name) + strlen(msg2) +
111                     + strlen(path_tmp) + strlen(msg3) + strlen(path)
112                     + strlen(msg4) + 1;
113     char * msg = try_malloc(size, f_name);
114     int test = sprintf(msg, "%s%s%s%s%s%s%s",
115                             msg1, f_name, msg2, path_tmp, msg3, path, msg4);
116     exit_trouble(test < 0, f_name, "sprintf()");
117     exit_err(rename(path_tmp, path), msg);
118     free(msg);
119     free(path_tmp);
120 }
121
122
123
124 extern uint32_t textfile_width(FILE * file)
125 {
126     char * f_name = "textfile_width()";
127     int c = 0;
128     uint32_t c_count = 0;
129     uint32_t linemax = 0;
130     while (1)
131     {
132         c = try_fgetc(file, f_name);
133         if (EOF == c)
134         {
135             break;
136         }
137         c_count++;
138         exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
139         if ('\n' == c)
140         {
141             if (c_count > linemax)
142             {
143                 linemax = c_count;
144             }
145             c_count = 0;
146         }
147     }
148   if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
149   {                                /* line / lack newline chars.            */
150       linemax = c_count;
151    }
152    exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");
153    return linemax;
154 }