ini.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* inih -- simple .INI file parser
  2. SPDX-License-Identifier: BSD-3-Clause
  3. Copyright (C) 2009-2020, 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. #if INI_HANDLER_LINENO
  41. typedef int (*ini_handler)(void* user, const char* section,
  42. const char* name, const char* value,
  43. int lineno);
  44. #else
  45. typedef int (*ini_handler)(void* user, const char* section,
  46. const char* name, const char* value);
  47. #endif
  48. /* Typedef for prototype of fgets-style reader function. */
  49. typedef char* (*ini_reader)(char* str, int num, void* stream);
  50. /* Parse given INI-style file. May have [section]s, name=value pairs
  51. (whitespace stripped), and comments starting with ';' (semicolon). Section
  52. is "" if name=value pair parsed before any section heading. name:value
  53. pairs are also supported as a concession to Python's configparser.
  54. For each name=value pair parsed, call handler function with given user
  55. pointer as well as section, name, and value (data only valid for duration
  56. of handler call). Handler should return nonzero on success, zero on error.
  57. Returns 0 on success, line number of first error on parse error (doesn't
  58. stop on first error), -1 on file open error, or -2 on memory allocation
  59. error (only when INI_USE_STACK is zero).
  60. */
  61. INI_API int ini_parse(const char* filename, ini_handler handler, void* user);
  62. /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
  63. close the file when it's finished -- the caller must do that. */
  64. INI_API int ini_parse_file(FILE* file, ini_handler handler, void* user);
  65. /* Same as ini_parse(), but takes an ini_reader function pointer instead of
  66. filename. Used for implementing custom or string-based I/O (see also
  67. ini_parse_string). */
  68. INI_API int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
  69. void* user);
  70. /* Same as ini_parse(), but takes a zero-terminated string with the INI data
  71. instead of a file. Useful for parsing INI data from a network socket or
  72. already in memory. */
  73. INI_API int ini_parse_string(const char* string, ini_handler handler, void* user);
  74. /* Nonzero to allow multi-line value parsing, in the style of Python's
  75. configparser. If allowed, ini_parse() will call the handler with the same
  76. name for each subsequent line parsed. */
  77. #ifndef INI_ALLOW_MULTILINE
  78. #define INI_ALLOW_MULTILINE 1
  79. #endif
  80. /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
  81. the file. See https://github.com/benhoyt/inih/issues/21 */
  82. #ifndef INI_ALLOW_BOM
  83. #define INI_ALLOW_BOM 1
  84. #endif
  85. /* Chars that begin a start-of-line comment. Per Python configparser, allow
  86. both ; and # comments at the start of a line by default. */
  87. #ifndef INI_START_COMMENT_PREFIXES
  88. #define INI_START_COMMENT_PREFIXES ";#"
  89. #endif
  90. /* Nonzero to allow inline comments (with valid inline comment characters
  91. specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
  92. Python 3.2+ configparser behaviour. */
  93. #ifndef INI_ALLOW_INLINE_COMMENTS
  94. #define INI_ALLOW_INLINE_COMMENTS 1
  95. #endif
  96. #ifndef INI_INLINE_COMMENT_PREFIXES
  97. #define INI_INLINE_COMMENT_PREFIXES ";"
  98. #endif
  99. /* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */
  100. #ifndef INI_USE_STACK
  101. #define INI_USE_STACK 1
  102. #endif
  103. /* Maximum line length for any line in INI file (stack or heap). Note that
  104. this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */
  105. #ifndef INI_MAX_LINE
  106. #define INI_MAX_LINE 200
  107. #endif
  108. /* Nonzero to allow heap line buffer to grow via realloc(), zero for a
  109. fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is
  110. zero. */
  111. #ifndef INI_ALLOW_REALLOC
  112. #define INI_ALLOW_REALLOC 0
  113. #endif
  114. /* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK
  115. is zero. */
  116. #ifndef INI_INITIAL_ALLOC
  117. #define INI_INITIAL_ALLOC 200
  118. #endif
  119. /* Stop parsing on first error (default is to keep parsing). */
  120. #ifndef INI_STOP_ON_FIRST_ERROR
  121. #define INI_STOP_ON_FIRST_ERROR 0
  122. #endif
  123. /* Nonzero to call the handler at the start of each new section (with
  124. name and value NULL). Default is to only call the handler on
  125. each name=value pair. */
  126. #ifndef INI_CALL_HANDLER_ON_NEW_SECTION
  127. #define INI_CALL_HANDLER_ON_NEW_SECTION 0
  128. #endif
  129. /* Nonzero to allow a name without a value (no '=' or ':' on the line) and
  130. call the handler with value NULL in this case. Default is to treat
  131. no-value lines as an error. */
  132. #ifndef INI_ALLOW_NO_VALUE
  133. #define INI_ALLOW_NO_VALUE 0
  134. #endif
  135. /* Nonzero to use custom ini_malloc, ini_free, and ini_realloc memory
  136. allocation functions (INI_USE_STACK must also be 0). These functions must
  137. have the same signatures as malloc/free/realloc and behave in a similar
  138. way. ini_realloc is only needed if INI_ALLOW_REALLOC is set. */
  139. #ifndef INI_CUSTOM_ALLOCATOR
  140. #define INI_CUSTOM_ALLOCATOR 0
  141. #endif
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* INI_H */