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