1 /*
2  * config file parser helper
3  *
4  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7  * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
8  */
9 #ifndef __INTERNAL_PARSE_CONFIG_H
10 #define __INTERNAL_PARSE_CONFIG_H
11 
12 #include <stdio.h>
13 #ifndef FAST_FUNC
14 # define FAST_FUNC
15 #endif
16 
17 /*
18  * Config file parser
19  */
20 enum {
21 	PARSE_COLLAPSE  = 0x00010000, /* treat consecutive delimiters as one */
22 	PARSE_TRIM      = 0x00020000, /* trim leading and trailing delimiters */
23 /* TODO: COLLAPSE and TRIM seem to always go in pair */
24 	PARSE_GREEDY    = 0x00040000, /* last token takes entire remainder of the line */
25 	PARSE_MIN_DIE   = 0x00100000, /* die if < min tokens found */
26 	/* keep a copy of current line */
27 	PARSE_KEEP_COPY = 0x00200000 * 0, /*ENABLE_FEATURE_CROND_D, */
28 /*	PARSE_ESCAPE    = 0x00400000,*/ /* process escape sequences in tokens */
29 	/* NORMAL is:
30 	   * remove leading and trailing delimiters and collapse
31 	     multiple delimiters into one
32 	   * warn and continue if less than mintokens delimiters found
33 	   * grab everything into last token
34 	 */
35 	PARSE_NORMAL    = PARSE_COLLAPSE | PARSE_TRIM | PARSE_GREEDY,
36 };
37 
38 typedef struct parser_t {
39 	FILE *fp; /* input file */
40 	char *data; /* pointer to data */
41 	size_t data_len; /* offset into data of begin of line */
42 	char *line; /* pointer to beginning of line */
43 	size_t line_len; /* length of line */
44 	smalluint allocated;
45 } parser_t;
46 
47 parser_t* config_open(const char *filename) FAST_FUNC attribute_hidden;
48 libc_hidden_proto(config_open)
49 int config_read(parser_t *parser, char ***tokens, unsigned flags, const char *delims) FAST_FUNC attribute_hidden;
50 libc_hidden_proto(config_read)
51 #define config_read(parser, tokens, max, min, str, flags) \
52 	config_read(parser, tokens, ((flags) | (((min) & 0xFF) << 8) | ((max) & 0xFF)), str)
53 void config_close(parser_t *parser) FAST_FUNC attribute_hidden;
54 libc_hidden_proto(config_close)
55 
56 #endif /* __INTERNAL_PARSE_CONFIG_H */
57