ini.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. */
  6. #ifndef __INI_H__
  7. #define __INI_H__
  8. /* Make this header file easier to include in C++ code */
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #include <stdio.h>
  13. /* Typedef for prototype of handler function. */
  14. typedef int (*ini_handler)(void* user, const char* section,
  15. const char* name, const char* value);
  16. /* Typedef for prototype of fgets-style reader function. */
  17. typedef char* (*ini_reader)(char* str, int num, void* stream);
  18. /* Parse given INI-style file. May have [section]s, name=value pairs
  19. (whitespace stripped), and comments starting with ';' (semicolon). Section
  20. is "" if name=value pair parsed before any section heading. name:value
  21. pairs are also supported as a concession to Python's configparser.
  22. For each name=value pair parsed, call handler function with given user
  23. pointer as well as section, name, and value (data only valid for duration
  24. of handler call). Handler should return nonzero on success, zero on error.
  25. Returns 0 on success, line number of first error on parse error (doesn't
  26. stop on first error), -1 on file open error, or -2 on memory allocation
  27. error (only when INI_USE_STACK is zero).
  28. */
  29. int ini_parse(const char* filename, ini_handler handler, void* user);
  30. /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
  31. close the file when it's finished -- the caller must do that. */
  32. int ini_parse_file(FILE* file, ini_handler handler, void* user);
  33. /* Same as ini_parse(), but takes an ini_reader function pointer instead of
  34. filename. Used for implementing custom or string-based I/O. */
  35. int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
  36. void* user);
  37. /* Nonzero to allow multi-line value parsing, in the style of Python's
  38. configparser. If allowed, ini_parse() will call the handler with the same
  39. name for each subsequent line parsed. */
  40. #ifndef INI_ALLOW_MULTILINE
  41. #define INI_ALLOW_MULTILINE 1
  42. #endif
  43. /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
  44. the file. See http://code.google.com/p/inih/issues/detail?id=21 */
  45. #ifndef INI_ALLOW_BOM
  46. #define INI_ALLOW_BOM 1
  47. #endif
  48. /* Nonzero to allow inline comments (with valid inline comment characters
  49. specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
  50. Python 3.2+ configparser behaviour. */
  51. #ifndef INI_ALLOW_INLINE_COMMENTS
  52. #define INI_ALLOW_INLINE_COMMENTS 1
  53. #endif
  54. #ifndef INI_INLINE_COMMENT_PREFIXES
  55. #define INI_INLINE_COMMENT_PREFIXES ";"
  56. #endif
  57. /* Nonzero to use stack, zero to use heap (malloc/free). */
  58. #ifndef INI_USE_STACK
  59. #define INI_USE_STACK 1
  60. #endif
  61. /* Stop parsing on first error (default is to keep parsing). */
  62. #ifndef INI_STOP_ON_FIRST_ERROR
  63. #define INI_STOP_ON_FIRST_ERROR 0
  64. #endif
  65. /* Maximum line length for any line in INI file. */
  66. #ifndef INI_MAX_LINE
  67. #define INI_MAX_LINE 2000
  68. #endif
  69. #ifdef __cplusplus
  70. }
  71. #endif
  72. #endif /* __INI_H__ */