home · contact · privacy
Use not f_name variable but __func__, standardize function name writing.
[plomrogue] / src / common / readwrite.h
1 /* src/common/readwrite.h:
2  *
3  * Routines for reading and writing files.
4  */
5
6 #ifndef READWRITE_H
7 #define READWRITE_H
8
9 #include <stdint.h> /* uint8_t, uint32_t */
10 #include <stdio.h> /* FILE */
11
12
13
14 /* Wrappers to fopen(), fclose(), fgets() and fwrite() from function called "f",
15  * calling exit_err() upon error with appropriate error messages.
16  */
17 extern FILE * try_fopen(char * path, char * mode, const char * f);
18 extern void try_fclose(FILE * file, const char * f);
19 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
20                        const char * f);
21 extern void try_fputc(uint8_t c, FILE * file, const char * f);
22
23 /* Wrapper to calling fgetc() and fgets() from function "f". The return code is
24  * returned unless ferror() indicates an error (i.e. to signify an end of file,
25  * fgetc() may return an EOF and fgets() a NULL). try_fgetc() calls clearerr()
26  * on "file" before fgetc(), because some Unixes fgetc() remember old EOFs and
27  * only return those until explicitely cleared.
28  */
29 extern int try_fgetc(FILE * file, const char * f);
30 extern char * try_fgets(char * line, int size, FILE * file, const char * f);
31
32 /* Write to "path_tmp" "path" + "_tmp" and return a new file at that "path_tmp"
33  * open for writing. "path_tmp" is malloc()'d, must be freed externally.
34 */
35 extern FILE * atomic_write_start(char * path, char ** path_tmp);
36
37 /* Finish atomic writing started in atomic_write_start(). Wraps successive calls
38  * of fclose() on "file", then unlink() on file at path "path" if it exists,
39  * then rename() from "path_tmp" to "path", then free() on "path_tmp".
40  */
41 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp);
42
43 /* Check for temp file leftover of atomic writing of "path", abort if found. */
44 extern void detect_atomic_leftover(char * path);
45
46 /* Return largest line length from "file" (including  newline chars). */
47 extern uint32_t textfile_width(FILE * file);
48
49
50
51 #endif