parse_config.h 1.9 KB

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