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() and fgets()
18 * 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_fgets(char * line, int size, FILE * file,
23 struct World * w, char * f);
27 /* Wrapper to successive call of fclose() from function called "f" on "file",
28 * then unlink() on file at "p2" if it exists, then rename() on "p1" to "p2".
29 * Used for handling atomic saving of files via temp files.
31 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
32 struct World * w, char * f);
36 /* Wrapper: Call textfile_sizes() from function called "f" to get max line
37 * length of "file", exit via exit_err() with trouble_msg()-generated error
40 extern uint16_t get_linemax(FILE * file, struct World * w, char * f);
44 /* Learn from "file" the largest line length (pointed to by "linemax_p"; length
45 * includes newline chars) and (pointed to by "n_lines_p" if it is not set to
46 * NULL) the number of lines (= number of newline chars).
48 * Returns 0 on success, 1 on error of fseek() (called to return to initial file
51 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
52 uint16_t * n_lines_p);
56 /* These routines for reading values "x" from / writing values to "file" ensure
57 * a defined endianness and consistent error codes: return 0 on success and 1 on
58 * fgetc()/fputc() failure.
60 extern uint8_t read_uint8(FILE * file, uint8_t * x);
61 extern uint8_t read_uint16_bigendian(FILE * file, uint16_t * x);
62 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x);
63 extern uint8_t write_uint8(uint8_t x, FILE * file);
64 extern uint8_t write_uint16_bigendian(uint16_t x, FILE * file);
65 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file);