home · contact · privacy
Made keybindings array into linked list; on the way rewrote / improved great parts...
[plomrogue] / src / readwrite.h
1 /* readwrite.h:
2  *
3  * Routines for reading and writing files.
4  */
5
6 #ifndef READWRITE_H
7 #define READWRITE_H
8
9
10
11 #include <stdio.h> /* for FILE typedef */
12 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
13 struct World;
14
15
16
17 /* Wrappers to calling from function called "f" of fopen(), fclose(), fgets()
18  * and fwrite() and calling exit_err() with appropriate error messages.
19  */
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);
24 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
25                        struct World * w, char * f);
26
27
28
29 /* Wrapper to successive call of fclose() from function called "f" on "file",
30  * then unlink() on file at "p2" if it exists, then rename() on "p1" to "p2".
31  * Used for handling atomic saving of files via temp files.
32  */
33 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
34                                      struct World * w, char * f);
35
36
37
38 /* Wrapper: Call textfile_sizes() from function called "f" to get max line
39  * length of "file", exit via exit_err() with trouble_msg()-generated error
40  * message on failure.
41  */
42 extern uint16_t get_linemax(FILE * file, struct World * w, char * f);
43
44
45
46 /* Learn from "file" the largest line length (pointed to by "linemax_p"; length
47  * includes newline chars) and (pointed to by "n_lines_p" if it is not set to
48  * NULL) the number of lines (= number of newline chars).
49  *
50  * Returns 0 on success, 1 on error of fseek() (called to return to initial file
51  * reading position).
52  */
53 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
54                               uint16_t * n_lines_p);
55
56
57
58 /* These routines for reading values "x" from / writing values to "file" ensure
59  * a defined endianness and consistent error codes: return 0 on success and 1 on
60  * fgetc()/fputc() failure.
61  */
62 extern uint8_t read_uint8(FILE * file, uint8_t * x);
63 extern uint8_t read_uint16_bigendian(FILE * file, uint16_t * x);
64 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x);
65 extern uint8_t write_uint8(uint8_t x, FILE * file);
66 extern uint8_t write_uint16_bigendian(uint16_t x, FILE * file);
67 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file);
68
69 #endif