INIReader.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Read an INI file into easy-to-access name/value pairs.
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (C) 2009-2020, Ben Hoyt
  4. // inih and INIReader are released under the New BSD license (see LICENSE.txt).
  5. // Go to the project home page for more info:
  6. //
  7. // https://github.com/benhoyt/inih
  8. #ifndef INIREADER_H
  9. #define INIREADER_H
  10. #include <map>
  11. #include <string>
  12. #include <cstdint>
  13. // Visibility symbols, required for Windows DLLs
  14. #ifndef INI_API
  15. #if defined _WIN32 || defined __CYGWIN__
  16. # ifdef INI_SHARED_LIB
  17. # ifdef INI_SHARED_LIB_BUILDING
  18. # define INI_API __declspec(dllexport)
  19. # else
  20. # define INI_API __declspec(dllimport)
  21. # endif
  22. # else
  23. # define INI_API
  24. # endif
  25. #else
  26. # if defined(__GNUC__) && __GNUC__ >= 4
  27. # define INI_API __attribute__ ((visibility ("default")))
  28. # else
  29. # define INI_API
  30. # endif
  31. #endif
  32. #endif
  33. // Read an INI file into easy-to-access name/value pairs. (Note that I've gone
  34. // for simplicity here rather than speed, but it should be pretty decent.)
  35. class INIReader
  36. {
  37. public:
  38. // Construct INIReader and parse given filename. See ini.h for more info
  39. // about the parsing.
  40. INI_API explicit INIReader(const std::string& filename);
  41. // Construct INIReader and parse given buffer. See ini.h for more info
  42. // about the parsing.
  43. INI_API explicit INIReader(const char *buffer, size_t buffer_size);
  44. // Return the result of ini_parse(), i.e., 0 on success, line number of
  45. // first error on parse error, or -1 on file open error.
  46. INI_API int ParseError() const;
  47. // Get a string value from INI file, returning default_value if not found.
  48. INI_API std::string Get(const std::string& section, const std::string& name,
  49. const std::string& default_value) const;
  50. // Get a string value from INI file, returning default_value if not found,
  51. // empty, or contains only whitespace.
  52. INI_API std::string GetString(const std::string& section, const std::string& name,
  53. const std::string& default_value) const;
  54. // Get an integer (long) value from INI file, returning default_value if
  55. // not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
  56. INI_API long GetInteger(const std::string& section, const std::string& name, long default_value) const;
  57. // Get a 64-bit integer (int64_t) value from INI file, returning default_value if
  58. // not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
  59. INI_API int64_t GetInteger64(const std::string& section, const std::string& name, int64_t default_value) const;
  60. // Get an unsigned integer (unsigned long) value from INI file, returning default_value if
  61. // not found or not a valid unsigned integer (decimal "1234", or hex "0x4d2").
  62. INI_API unsigned long GetUnsigned(const std::string& section, const std::string& name, unsigned long default_value) const;
  63. // Get an unsigned 64-bit integer (uint64_t) value from INI file, returning default_value if
  64. // not found or not a valid unsigned integer (decimal "1234", or hex "0x4d2").
  65. INI_API uint64_t GetUnsigned64(const std::string& section, const std::string& name, uint64_t default_value) const;
  66. // Get a real (floating point double) value from INI file, returning
  67. // default_value if not found or not a valid floating point value
  68. // according to strtod().
  69. INI_API double GetReal(const std::string& section, const std::string& name, double default_value) const;
  70. // Get a boolean value from INI file, returning default_value if not found or if
  71. // not a valid true/false value. Valid true values are "true", "yes", "on", "1",
  72. // and valid false values are "false", "no", "off", "0" (not case sensitive).
  73. INI_API bool GetBoolean(const std::string& section, const std::string& name, bool default_value) const;
  74. // Return true if the given section exists (section must contain at least
  75. // one name=value pair).
  76. INI_API bool HasSection(const std::string& section) const;
  77. // Return true if a value exists with the given section and field names.
  78. INI_API bool HasValue(const std::string& section, const std::string& name) const;
  79. private:
  80. int _error;
  81. std::map<std::string, std::string> _values;
  82. static std::string MakeKey(const std::string& section, const std::string& name);
  83. static int ValueHandler(void* user, const char* section, const char* name,
  84. const char* value);
  85. };
  86. #endif // INIREADER_H