parse_config.h 2.0 KB

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