home · contact · privacy
Send log messages through server out file. Includes major refactoring.
[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 bytes to put onto "queue" of "queue_size" (growing
57  * that size). May put many \0-terminated strings on the queue at once. \n chars
58  * are replaced with \0 chars. If the queue doesn't end in \0, a \o byte is
59  * appended to it. Returns 1 if reading succeeds, 0 if nothing is read.
60  */
61 extern uint8_t read_file_into_queue(FILE * file, char ** queue,
62                                     uint32_t * queue_size);
63
64 /* Cut out and return first \0-terminated string from "queue" and appropriately
65  * reduce "queue_size". Return NULL if queue is empty. Superfluous \0 bytes
66  * after the string are also cut out. Should the queue start with \0 bytes,
67  * those are cut out before returning anything after them.
68  */
69 extern char * get_message_from_queue(char ** queue, uint32_t * queue_size);
70
71
72
73 #endif