home · contact · privacy
Refactor file parsing patterns.
[plomrogue] / src / common / parse_file.h
1 /* src/common/parse_file.h
2  *
3  * Tools for parsing config files.
4  */
5
6 #ifndef PARSE_FILE_H
7 #define PARSE_FILE_H
8
9 #include <stddef.h> /* size_t */
10 #include <stdint.h> /* uint8_t */
11
12
13
14 enum parse_flags
15 {
16     EDIT_STARTED  = 0x01
17 };
18
19
20
21 /* Parse file at "path" by passing each line's first two tokens to
22  * "token_to_entry". Ignore empty line. Non-empty lines must feature at least
23  * two tokens as delimited either be whitespace or single quotes (to allow for
24  * tokens featuring whitespaces). When EOF is reached, token_to_entry() is
25  * called a last time with a first token of NULL.
26  */
27 extern void parse_file(char * path, void ( *token_to_entry) (char *, char *));
28
29 /* If "test" != 0, exit on output of "msg" and faulty line and line number as
30  * parsed by parse_file(). (Ought to be called as offspring to parse_file().)
31  */
32 extern void err_line(uint8_t test, char * msg);
33
34 /* Return next token from "line" or NULL if none is found. Tokens either a)
35  * start at the first non-whitespace character and end before the next
36  * whitespace character after that; or b) if the first non-whitespace character
37  * is a single quote followed by at least one other single quote some time later
38  * on the line, the token starts after that first single quote and ends before
39  * the second, with the next token_from_line() call starting its token search
40  * after that second quote. The only way to return an empty string (instead of
41  * NULL) as a token is to delimit the token by two succeeding single quotes.
42  * */
43 extern char * token_from_line(char * line);
44
45 /* Test for "string" to represent proper int16 (type: "i") or uint8 ("8"). */
46 extern void parsetest_int(char * string, char type);
47
48 /* Test for "string" to be of length 1 (excluding "\0" terminator). */
49 extern void parsetest_singlechar(char * string);
50
51 /* Calls err_line() with fitting message if EDIT_STARTED not set in "flags". */
52 extern void parsetest_defcontext(uint8_t flags);
53
54 /* Ensure token_from_line() does not find any more tokens on the line. */
55 extern void parsetest_too_many_values();
56
57 /* Trigger err_line() with "Unknown argument" message. */
58 extern void parse_unknown_arg();
59
60 /* If "test"!=0 call err_line() with "Declaration of ID already used" message.*/
61 extern void parse_id_uniq(int test);
62
63 /* Set "flags"=EDIT_STARTED and return pointer to new zeroed array of "size". */
64 extern char * parse_init_entry(uint8_t * flags, size_t size);
65
66 /* If "token0" fits "comparand", set "element" to value read from "token1" as
67  * string (type: "s"), char ("c") uint8 ("8") or int16 ("i"), set that element's
68  * flag to "flags" and return 1; else return 0.
69  */
70 extern uint8_t parse_val(char * token0, char * token1, char * comparand,
71                          uint8_t * flags, uint8_t set_flag, char type,
72                          char * element);
73
74 /* Check "ready_flag" is set in "flags", re-set "flags" to "ready_flag" only. */
75 extern void parse_and_reduce_to_readyflag(uint8_t * flags, uint8_t ready_flag);
76
77
78
79 #endif