2
0

ini.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* inih -- simple .INI file parser
  2. SPDX-License-Identifier: BSD-3-Clause
  3. Copyright (C) 2009-2025, Ben Hoyt
  4. inih is released under the New BSD license (see LICENSE.txt). Go to the project
  5. home page for more info:
  6. https://github.com/benhoyt/inih
  7. */
  8. #ifndef INI_H
  9. #define INI_H
  10. /* Make this header file easier to include in C++ code */
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #include <stdio.h>
  15. /* Nonzero if ini_handler callback should accept lineno parameter. */
  16. #ifndef INI_HANDLER_LINENO
  17. #define INI_HANDLER_LINENO 0
  18. #endif
  19. /* Visibility symbols, required for Windows DLLs */
  20. #ifndef INI_API
  21. #if defined _WIN32 || defined __CYGWIN__
  22. # ifdef INI_SHARED_LIB
  23. # ifdef INI_SHARED_LIB_BUILDING
  24. # define INI_API __declspec(dllexport)
  25. # else
  26. # define INI_API __declspec(dllimport)
  27. # endif
  28. # else
  29. # define INI_API
  30. # endif
  31. #else
  32. # if defined(__GNUC__) && __GNUC__ >= 4
  33. # define INI_API __attribute__ ((visibility ("default")))
  34. # else
  35. # define INI_API
  36. # endif
  37. #endif
  38. #endif
  39. /* Typedef for prototype of handler function.
  40. Note that even though the value parameter has type "const char*", the user
  41. may cast to "char*" and modify its content, as the value is not used again
  42. after the call to ini_handler. This is not true of section and name --
  43. those must not be modified.
  44. */
  45. #if INI_HANDLER_LINENO
  46. typedef int (*ini_handler)(void* user, const char* section,
  47. const char* name, const char* value,
  48. int lineno);
  49. #else
  50. typedef int (*ini_handler)(void* user, const char* section,
  51. const char* name, const char* value);
  52. #endif
  53. /* Typedef for prototype of fgets-style reader function. */
  54. typedef char* (*ini_reader)(char* str, int num, void* stream);
  55. /* Parse given INI-style file. May have [section]s, name=value pairs
  56. (whitespace stripped), and comments starting with ';' (semicolon). Section
  57. is "" if name=value pair parsed before any section heading. name:value
  58. pairs are also supported as a concession to Python's configparser.
  59. For each name=value pair parsed, call handler function with given user
  60. pointer as well as section, name, and value (data only valid for duration
  61. of handler call). Handler should return nonzero on success, zero on error.
  62. Returns 0 on success, line number of first error on parse error (doesn't
  63. stop on first error), -1 on file open error, or -2 on memory allocation
  64. error (only when INI_USE_STACK is zero).
  65. */
  66. INI_API int ini_parse(const char* filename, ini_handler handler, void* user);
  67. /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
  68. close the file when it's finished -- the caller must do that. */
  69. INI_API int ini_parse_file(FILE* file, ini_handler handler, void* user);
  70. /* Same as ini_parse(), but takes an ini_reader function pointer instead of
  71. filename. Used for implementing custom or string-based I/O (see also
  72. ini_parse_string). */
  73. INI_API int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
  74. void* user);
  75. /* Same as ini_parse(), but takes a zero-terminated string with the INI data
  76. instead of a file. Useful for parsing INI data from a network socket or
  77. which is already in memory. */
  78. INI_API int ini_parse_string(const char* string, ini_handler handler, void* user);
  79. /* Same as ini_parse_string(), but takes a string and its length, avoiding
  80. strlen(). Useful for parsing INI data from a network socket or which is
  81. already in memory, or interfacing with C++ std::string_view. */
  82. INI_API int ini_parse_string_length(const char* string, size_t length, ini_handler handler, void* user);
  83. /* Nonzero to allow multi-line value parsing, in the style of Python's
  84. configparser. If allowed, ini_parse() will call the handler with the same
  85. name for each subsequent line parsed. */
  86. #ifndef INI_ALLOW_MULTILINE
  87. #define INI_ALLOW_MULTILINE 1
  88. #endif
  89. /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
  90. the file. See https://github.com/benhoyt/inih/issues/21 */
  91. #ifndef INI_ALLOW_BOM
  92. #define INI_ALLOW_BOM 1
  93. #endif
  94. /* Chars that begin a start-of-line comment. Per Python configparser, allow
  95. both ; and # comments at the start of a line by default. */
  96. #ifndef INI_START_COMMENT_PREFIXES
  97. #define INI_START_COMMENT_PREFIXES ";#"
  98. #endif
  99. /* Nonzero to allow inline comments (with valid inline comment characters
  100. specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
  101. Python 3.2+ configparser behaviour. */
  102. #ifndef INI_ALLOW_INLINE_COMMENTS
  103. #define INI_ALLOW_INLINE_COMMENTS 1
  104. #endif
  105. #ifndef INI_INLINE_COMMENT_PREFIXES
  106. #define INI_INLINE_COMMENT_PREFIXES ";"
  107. #endif
  108. /* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */
  109. #ifndef INI_USE_STACK
  110. #define INI_USE_STACK 1
  111. #endif
  112. /* Maximum line length for any line in INI file (stack or heap). Note that
  113. this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */
  114. #ifndef INI_MAX_LINE
  115. #define INI_MAX_LINE 200
  116. #endif
  117. /* Nonzero to allow heap line buffer to grow via realloc(), zero for a
  118. fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is
  119. zero. */
  120. #ifndef INI_ALLOW_REALLOC
  121. #define INI_ALLOW_REALLOC 0
  122. #endif
  123. /* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK
  124. is zero. */
  125. #ifndef INI_INITIAL_ALLOC
  126. #define INI_INITIAL_ALLOC 200
  127. #endif
  128. /* Stop parsing on first error (default is to keep parsing). */
  129. #ifndef INI_STOP_ON_FIRST_ERROR
  130. #define INI_STOP_ON_FIRST_ERROR 0
  131. #endif
  132. /* Nonzero to call the handler at the start of each new section (with
  133. name and value NULL). Default is to only call the handler on
  134. each name=value pair. */
  135. #ifndef INI_CALL_HANDLER_ON_NEW_SECTION
  136. #define INI_CALL_HANDLER_ON_NEW_SECTION 0
  137. #endif
  138. /* Nonzero to allow a name without a value (no '=' or ':' on the line) and
  139. call the handler with value NULL in this case. Default is to treat
  140. no-value lines as an error. */
  141. #ifndef INI_ALLOW_NO_VALUE
  142. #define INI_ALLOW_NO_VALUE 0
  143. #endif
  144. /* Nonzero to use custom ini_malloc, ini_free, and ini_realloc memory
  145. allocation functions (INI_USE_STACK must also be 0). These functions must
  146. have the same signatures as malloc/free/realloc and behave in a similar
  147. way. ini_realloc is only needed if INI_ALLOW_REALLOC is set. */
  148. #ifndef INI_CUSTOM_ALLOCATOR
  149. #define INI_CUSTOM_ALLOCATOR 0
  150. #endif
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif /* INI_H */