3 * Routines for reading and writing files.
11 #include <stdio.h> /* for FILE typedef */
12 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
17 /* Wrappers to calling from function called "f" of fopen(), fclose(), fgets()
18 * and fwrite() and calling exit_err() with appropriate error messages.
20 extern FILE * try_fopen(char * path, char * mode, struct World * w, char * f);
21 extern void try_fclose(FILE * file, struct World * w, char * f);
22 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
23 struct World * w, char * f);
27 /* Wrapper to calling fgets() from function called "f". The return code of
28 * fgets() is returned unless it is NULL *and* ferror() indicates that an error
29 * occured; otherwise end of file is assumed and NULL is returned properly.
31 extern char * try_fgets(char * line, int size, FILE * file,
32 struct World * w, char * f);
36 /* Wrapper to successive call of fclose() from function called "f" on "file",
37 * then unlink() on file at "p2" if it exists, then rename() on "p1" to "p2".
38 * Used for handling atomic saving of files via temp files.
40 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
41 struct World * w, char * f);
45 /* Wrapper: Call textfile_sizes() from function called "f" to get max line
46 * length of "file", exit via exit_err() with trouble_msg()-generated error
49 extern uint16_t get_linemax(FILE * file, struct World * w, char * f);
53 /* Learn from "file" the largest line length (pointed to by "linemax_p"; length
54 * includes newline chars) and (pointed to by "n_lines_p" if it is not set to
55 * NULL) the number of lines (= number of newline chars).
57 * Returns 0 on success, 1 on error of fseek() (called to return to initial file
60 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
61 uint16_t * n_lines_p);
65 /* These routines for reading values "x" from / writing values to "file" ensure
66 * a defined endianness and consistent error codes: return 0 on success and 1 on
67 * fgetc()/fputc() failure.
69 extern uint8_t read_uint8(FILE * file, uint8_t * x);
70 extern uint8_t read_uint16_bigendian(FILE * file, uint16_t * x);
71 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x);
72 extern uint8_t write_uint8(uint8_t x, FILE * file);
73 extern uint8_t write_uint16_bigendian(uint16_t x, FILE * file);
74 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file);