home · contact · privacy
Make try_fgetc() prepend clearerr() for fgetc() versions that need it.
[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). 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, char * f);
30 extern char * try_fgets(char * line, int size, FILE * file, char * f);
31
32 /* Wrapper to successive call of fclose() from function called "f" on "file",
33  * then unlink() on file at path "p2" if it exists, then rename() from path "p1"
34  * to "p2". Used for handling atomic saving of files via temp files.
35  */
36 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
37                                      char * f);
38
39 /* Return largest line length from "file" (including  newline chars). */
40 extern uint32_t textfile_width(FILE * file);
41
42
43
44 #endif