Tiny INI parser

nini.h 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ///
  2. /// nini - Nano INI parser
  3. ///
  4. /// Written by MightyPork, 2018
  5. /// MIT license
  6. ///
  7. #ifndef INIPARSE_H
  8. #define INIPARSE_H
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. // buffer sizes
  14. #define INI_KEY_MAX 20
  15. #define INI_VALUE_MAX 30
  16. /**
  17. * INI parser callback, called for each found key-value pair.
  18. *
  19. * @param section - current section, empty string for global keys
  20. * @param key - found key (trimmed of whitespace)
  21. * @param value - value, trimmed of quotes or whitespace
  22. * @param userData - opaque user data pointer, general purpose
  23. */
  24. typedef void (*IniParserCallback)(const char *section, const char *key, const char *value, void *userData);
  25. /**
  26. * Begin parsing a stream
  27. *
  28. * @param callback - key callback to assign
  29. * @param userData - optional user data that will be passed to the callback
  30. */
  31. void ini_parse_begin(IniParserCallback callback, void *userData);
  32. /**
  33. * End parse stream.
  34. * Flushes what remains in the buffer and removes callback.
  35. *
  36. * @returns userData or NULL if none
  37. */
  38. void* ini_parse_end(void);
  39. /**
  40. * Parse a string (needn't be complete line or file)
  41. *
  42. * @param data - string to parse
  43. * @param len - string length (0 = use strlen)
  44. */
  45. void ini_parse(const char *data, size_t len);
  46. /**
  47. * Parse a complete file loaded to string
  48. *
  49. * @param text - entire file as string
  50. * @param len - file length (0 = use strlen)
  51. * @param callback - key callback
  52. * @param userData - optional user data for key callback
  53. */
  54. void ini_parse_file(const char *text, size_t len, IniParserCallback callback, void *userData);
  55. /**
  56. * Explicitly reset the parser
  57. */
  58. void ini_parse_reset(void);
  59. #endif // INIPARSE_H