1 /* src/common/parse_file.h
3 * Tools for parsing config files.
9 #include <stddef.h> /* size_t */
10 #include <stdint.h> /* uint8_t */
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.
27 extern void parse_file(char * path, void ( *token_to_entry) (char *, char *));
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.
32 extern void set_err_line_options(char * intro, char * line, uint32_t count,
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.
38 extern uint8_t err_line(uint8_t test, char * msg);
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.
49 extern char * token_from_line(char * line);
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.
54 extern uint8_t parsetest_int(char * string, char type);
56 /* Test for "string" to be of length 1 (excluding "\0"). Return 1 on failure. */
57 extern uint8_t parsetest_singlechar(char * string);
59 /* Calls err_line() with fitting message if EDIT_STARTED not set in "flags". */
60 extern void parsetest_defcontext(uint8_t flags);
62 /* Ensure token_from_line() does not find any more tokens on the line. */
63 extern void parsetest_too_many_values();
65 /* Trigger err_line() with "Unknown argument" message. */
66 extern void parse_unknown_arg();
68 /* If "test"!=0 call err_line() with "Declaration of ID already used" message.*/
69 extern void parse_id_uniq(int test);
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);
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.
78 extern uint8_t parse_val(char * token0, char * token1, char * comparand,
79 char type, char * element);
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,
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);