ini.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* inih -- simple .INI file parser
  2. inih is released under the New BSD license (see LICENSE.txt). Go to the project
  3. home page for more info:
  4. https://github.com/benhoyt/inih
  5. 2019-06-15
  6. */
  7. #ifndef __INI_H__
  8. #define __INI_H__
  9. /* Make this header file easier to include in C++ code */
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #include <stdio.h>
  14. /* Nonzero if ini_handler callback should accept lineno parameter. */
  15. #ifndef INI_HANDLER_LINENO
  16. #define INI_HANDLER_LINENO 0
  17. #endif
  18. /* Typedef for prototype of handler function. */
  19. #if INI_HANDLER_LINENO
  20. typedef int (*ini_handler)(void* user, const char* section,
  21. const char* name, const char* value,
  22. int lineno);
  23. #else
  24. typedef int (*ini_handler)(void* user, const char* section,
  25. const char* name, const char* value);
  26. #endif
  27. /* Typedef for prototype of fgets-style reader function. */
  28. typedef char* (*ini_reader)(char* str, int num, void* stream);
  29. /* Parse given INI-style file. May have [section]s, name=value pairs
  30. (whitespace stripped), and comments starting with ';' (semicolon). Section
  31. is "" if name=value pair parsed before any section heading. name:value
  32. pairs are also supported as a concession to Python's configparser.
  33. For each name=value pair parsed, call handler function with given user
  34. pointer as well as section, name, and value (data only valid for duration
  35. of handler call). Handler should return nonzero on success, zero on error.
  36. Returns 0 on success, line number of first error on parse error (doesn't
  37. stop on first error), -1 on file open error, or -2 on memory allocation
  38. error (only when INI_USE_STACK is zero).
  39. */
  40. int ini_parse(const char* filename, ini_handler handler, void* user);
  41. /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
  42. close the file when it's finished -- the caller must do that. */
  43. int ini_parse_file(FILE* file, ini_handler handler, void* user);
  44. /* Same as ini_parse(), but takes an ini_reader function pointer instead of
  45. filename. Used for implementing custom or string-based I/O (see also
  46. ini_parse_string). */
  47. int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
  48. void* user);
  49. /* Same as ini_parse(), but takes a zero-terminated string with the INI data
  50. instead of a file. Useful for parsing INI data from a network socket or
  51. already in memory. */
  52. int ini_parse_string(const char* string, ini_handler handler, void* user);
  53. /* Nonzero to allow multi-line value parsing, in the style of Python's
  54. configparser. If allowed, ini_parse() will call the handler with the same
  55. name for each subsequent line parsed. */
  56. #ifndef INI_ALLOW_MULTILINE
  57. #define INI_ALLOW_MULTILINE 1
  58. #endif
  59. /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
  60. the file. See https://github.com/benhoyt/inih/issues/21 */
  61. #ifndef INI_ALLOW_BOM
  62. #define INI_ALLOW_BOM 1
  63. #endif
  64. /* Chars that begin a start-of-line comment. Per Python configparser, allow
  65. both ; and # comments at the start of a line by default. */
  66. #ifndef INI_START_COMMENT_PREFIXES
  67. #define INI_START_COMMENT_PREFIXES ";#"
  68. #endif
  69. /* Nonzero to allow inline comments (with valid inline comment characters
  70. specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
  71. Python 3.2+ configparser behaviour. */
  72. #ifndef INI_ALLOW_INLINE_COMMENTS
  73. #define INI_ALLOW_INLINE_COMMENTS 1
  74. #endif
  75. #ifndef INI_INLINE_COMMENT_PREFIXES
  76. #define INI_INLINE_COMMENT_PREFIXES ";"
  77. #endif
  78. /* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */
  79. #ifndef INI_USE_STACK
  80. #define INI_USE_STACK 1
  81. #endif
  82. /* Maximum line length for any line in INI file (stack or heap). Note that
  83. this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */
  84. #ifndef INI_MAX_LINE
  85. #define INI_MAX_LINE 200
  86. #endif
  87. /* Nonzero to allow heap line buffer to grow via realloc(), zero for a
  88. fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is
  89. zero. */
  90. #ifndef INI_ALLOW_REALLOC
  91. #define INI_ALLOW_REALLOC 0
  92. #endif
  93. /* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK
  94. is zero. */
  95. #ifndef INI_INITIAL_ALLOC
  96. #define INI_INITIAL_ALLOC 200
  97. #endif
  98. /* Stop parsing on first error (default is to keep parsing). */
  99. #ifndef INI_STOP_ON_FIRST_ERROR
  100. #define INI_STOP_ON_FIRST_ERROR 0
  101. #endif
  102. #ifdef __cplusplus
  103. }
  104. #endif
  105. #endif /* __INI_H__ */