home · contact · privacy
Make server config files more readable, their parsing more lenient.
[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, char * f);
18 extern void try_fclose(FILE * file, char * f);
19 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
20                        char * f);
21 extern void try_fputc(uint8_t c, FILE * file, 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).
26  */
27 extern int try_fgetc(FILE * file, char * f);
28 extern char * try_fgets(char * line, int size, FILE * file, char * f);
29
30 /* Wrapper to successive call of fclose() from function called "f" on "file",
31  * then unlink() on file at path "p2" if it exists, then rename() from path "p1"
32  * to "p2". Used for handling atomic saving of files via temp files.
33  */
34 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
35                                      char * f);
36
37 /* Return largest line length from "file" (including  newline chars). */
38 extern uint32_t textfile_width(FILE * file);
39
40
41
42 #endif