parse_config.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #include <stdio.h>
  11. #ifndef FAST_FUNC
  12. # define FAST_FUNC
  13. #endif
  14. /*
  15. * Config file parser
  16. */
  17. enum {
  18. PARSE_COLLAPSE = 0x00010000, /* treat consecutive delimiters as one */
  19. PARSE_TRIM = 0x00020000, /* trim leading and trailing delimiters */
  20. /* TODO: COLLAPSE and TRIM seem to always go in pair */
  21. PARSE_GREEDY = 0x00040000, /* last token takes entire remainder of the line */
  22. PARSE_MIN_DIE = 0x00100000, /* die if < min tokens found */
  23. /* keep a copy of current line */
  24. PARSE_KEEP_COPY = 0x00200000 * 0, /*ENABLE_FEATURE_CROND_D, */
  25. /* PARSE_ESCAPE = 0x00400000,*/ /* process escape sequences in tokens */
  26. /* NORMAL is:
  27. * remove leading and trailing delimiters and collapse
  28. multiple delimiters into one
  29. * warn and continue if less than mintokens delimiters found
  30. * grab everything into last token
  31. */
  32. PARSE_NORMAL = PARSE_COLLAPSE | PARSE_TRIM | PARSE_GREEDY,
  33. };
  34. typedef struct parser_t {
  35. FILE *fp; /* input file */
  36. char *data; /* pointer to data */
  37. size_t data_len; /* offset into data of begin of line */
  38. char *line; /* pointer to beginning of line */
  39. size_t line_len; /* length of line */
  40. smalluint allocated;
  41. } parser_t;
  42. parser_t* config_open(const char *filename) FAST_FUNC attribute_hidden;
  43. int config_read(parser_t *parser, char ***tokens, unsigned flags, const char *delims) FAST_FUNC attribute_hidden;
  44. #define config_read(parser, tokens, max, min, str, flags) \
  45. config_read(parser, tokens, ((flags) | (((min) & 0xFF) << 8) | ((max) & 0xFF)), str)
  46. void config_close(parser_t *parser) FAST_FUNC attribute_hidden;