home · contact · privacy
Server: Remove log_help(), this should be serverd by the client.
[plomrogue] / src / common / readwrite.h
1 /* src/common/readwrite.h:
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  *
7  * Routines for reading and writing files.
8  */
9
10 #ifndef READWRITE_H
11 #define READWRITE_H
12
13 #include <stdint.h> /* uint8_t, uint32_t */
14 #include <stdio.h> /* FILE */
15
16
17
18 /* Wrappers to fopen(), fclose(), fgets() and fwrite() from function called "f",
19  * calling exit_err() upon error with appropriate error messages.
20  */
21 extern FILE * try_fopen(char * path, char * mode, const char * f);
22 extern void try_fclose(FILE * file, const char * f);
23 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
24                        const char * f);
25 extern void try_fputc(uint8_t c, FILE * file, const char * f);
26
27 /* Wrapper to calling fgetc() and fgets() from function "f". The return code is
28  * returned unless ferror() indicates an error (i.e. to signify an end of file,
29  * fgetc() may return an EOF and fgets() a NULL). try_fgetc() calls clearerr()
30  * on "file" before fgetc(), because some Unixes fgetc() remember old EOFs and
31  * only return those until explicitely cleared.
32  */
33 extern int try_fgetc(FILE * file, const char * f);
34 extern char * try_fgets(char * line, int size, FILE * file, const char * f);
35
36 /* Return "path" + suffix "_tmp". Value is malloc'd, must be freed externally.*/
37 extern char * build_temp_path(char * path);
38
39 /* Write to "path_tmp" "path" + "_tmp" and return a new file at that "path_tmp"
40  * open for writing. "path_tmp" is malloc()'d, must be freed externally.
41 */
42 extern FILE * atomic_write_start(char * path, char ** path_tmp);
43
44 /* Finish atomic writing started in atomic_write_start(). Wraps successive calls
45  * of fclose() on "file", then unlink() on file at path "path" if it exists,
46  * then rename() from "path_tmp" to "path", then free() on "path_tmp".
47  */
48 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp);
49
50 /* Check for temp file leftover of atomic writing of "path", abort if found. */
51 extern void detect_atomic_leftover(char * path);
52
53 /* Return largest line length from "file" (including  newline chars). */
54 extern uint32_t textfile_width(FILE * file);
55
56 /* Read "file" for load of non-zero bytes to put onto "queue" string. */
57 extern uint8_t read_file_into_queue(FILE * file, char ** queue);
58
59 /* Return message from "queue" (identified as \n-delimited string, with \n as
60  * \0 in the returned message; return nothing if no \n delimiter found).
61  */
62 extern char * get_message_from_queue(char ** queue);
63
64
65
66 #endif