home · contact · privacy
4192dac046ee7190a8976e3253fe5b5b2f25f6fc
[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 lines. 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 /* Set err_line() options: "intro" message, char array used to store analyzed
30  * lines ("line"), line start "count", whether to "exit" on error message.
31  */
32 extern void set_err_line_options(char * intro, char * line, uint32_t count,
33                                  uint8_t exit);
34
35 /* If "test", output "msg", faulty line, its number and exit if so defined by
36  * set_err_line_options(), else return 1 on "test" and 0 if "test" is 0.
37  */
38 extern uint8_t err_line(uint8_t test, char * msg);
39
40 /* Return next token from "line" or NULL if none is found. Tokens either a)
41  * start at the first non-whitespace character and end before the next
42  * whitespace character after that; or b) if the first non-whitespace character
43  * is a single quote followed by at least one other single quote some time later
44  * on the line, the token starts after that first single quote and ends before
45  * the second, with the next token_from_line() call starting its token search
46  * after that second quote. The only way to return an empty string (instead of
47  * NULL) as a token is to delimit the token by two succeeding single quotes.
48  * */
49 extern char * token_from_line(char * line);
50
51 /* Test for "string" to represent proper int16 (type: "i"), uint8 ("8"), uint16
52  * ("u") or uint32 ("U"). Returns 0 if proper value, else >0.
53  */
54 extern uint8_t parsetest_int(char * string, char type);
55
56 /* Test for "string" to be of length 1 (excluding "\0"). Return 1 on failure. */
57 extern uint8_t parsetest_singlechar(char * string);
58
59 /* Calls err_line() with fitting message if EDIT_STARTED not set in "flags". */
60 extern void parsetest_defcontext(uint8_t flags);
61
62 /* Ensure token_from_line() does not find any more tokens on the line. */
63 extern void parsetest_too_many_values();
64
65 /* Trigger err_line() with "Unknown argument" message. */
66 extern void parse_unknown_arg();
67
68 /* If "test"!=0 call err_line() with "Declaration of ID already used" message.*/
69 extern void parse_id_uniq(int test);
70
71 /* Set "flags"=EDIT_STARTED and return pointer to new zeroed array of "size". */
72 extern char * parse_init_entry(uint8_t * flags, size_t size);
73
74 /* If "token0" fits "comparand", set "element" to value read from "token1" as
75  * string (type: "s"), char ("c") uint8 ("8"), uint16 ("u"), uint32 ("U") or
76  * int16 ("i"), and return 1; else 0.
77  */
78 extern uint8_t parse_val(char * token0, char * token1, char * comparand,
79                          char type, char * element);
80
81 /* Wrapper to parse_val() that sets "flags" to "flags"|"set_flag" on success. */
82 extern uint8_t parse_flagval(char * token0, char * token1, char * comparand,
83                              uint8_t * flags, uint8_t set_flag, char type,
84                              char * element);
85
86 /* Check "ready_flag" is set in "flags", re-set "flags" to "ready_flag" only. */
87 extern void parse_and_reduce_to_readyflag(uint8_t * flags, uint8_t ready_flag);
88
89
90
91 #endif