diff --git a/cpp/INIReader.cpp b/cpp/INIReader.cpp index 24e3fda..ebe458b 100644 --- a/cpp/INIReader.cpp +++ b/cpp/INIReader.cpp @@ -56,6 +56,16 @@ long INIReader::GetInteger(const string& section, const string& name, long defau return end > value ? n : default_value; } +INI_API int64_t INIReader::GetInteger64(const std::string& section, const std::string& name, int64_t default_value) const +{ + string valstr = Get(section, name, ""); + const char* value = valstr.c_str(); + char* end; + // This parses "1234" (decimal) and also "0x4D2" (hex) + int64_t n = strtoll(value, &end, 0); + return end > value ? n : default_value; +} + unsigned long INIReader::GetUnsigned(const string& section, const string& name, unsigned long default_value) const { string valstr = Get(section, name, ""); @@ -66,6 +76,16 @@ unsigned long INIReader::GetUnsigned(const string& section, const string& name, return end > value ? n : default_value; } +INI_API uint64_t INIReader::GetUnsigned64(const std::string& section, const std::string& name, uint64_t default_value) const +{ + string valstr = Get(section, name, ""); + const char* value = valstr.c_str(); + char* end; + // This parses "1234" (decimal) and also "0x4D2" (hex) + uint64_t n = strtoull(value, &end, 0); + return end > value ? n : default_value; +} + double INIReader::GetReal(const string& section, const string& name, double default_value) const { string valstr = Get(section, name, ""); diff --git a/cpp/INIReader.h b/cpp/INIReader.h index 1e0f7e5..45e3fcd 100644 --- a/cpp/INIReader.h +++ b/cpp/INIReader.h @@ -14,6 +14,7 @@ #include #include +#include // Visibility symbols, required for Windows DLLs #ifndef INI_API @@ -66,11 +67,18 @@ class INIReader // not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2"). INI_API long GetInteger(const std::string& section, const std::string& name, long default_value) const; - // Get an unsigned integer (unsigned long) value from INI file, - // returning default_value if not found or not a valid integer - // (decimal "1234", or hex "0x4d2"). + // Get a 64-bit integer (int64_t) value from INI file, returning default_value if + // not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2"). + INI_API int64_t GetInteger64(const std::string& section, const std::string& name, int64_t default_value) const; + + // Get an unsigned integer (unsigned long) value from INI file, returning default_value if + // not found or not a valid unsigned integer (decimal "1234", or hex "0x4d2"). INI_API unsigned long GetUnsigned(const std::string& section, const std::string& name, unsigned long default_value) const; + // Get an unsigned 64-bit integer (uint64_t) value from INI file, returning default_value if + // not found or not a valid unsigned integer (decimal "1234", or hex "0x4d2"). + INI_API uint64_t GetUnsigned64(const std::string& section, const std::string& name, uint64_t default_value) const; + // Get a real (floating point double) value from INI file, returning // default_value if not found or not a valid floating point value // according to strtod(). diff --git a/examples/INIReaderExample.cpp b/examples/INIReaderExample.cpp index 54871ae..debe5d6 100644 --- a/examples/INIReaderExample.cpp +++ b/examples/INIReaderExample.cpp @@ -13,7 +13,9 @@ int main() } std::cout << "Config loaded from 'test.ini': version=" << reader.GetInteger("protocol", "version", -1) << ", unsigned version=" - << reader.GetUnsigned("protocol", "version", -1) << ", name=" + << reader.GetUnsigned("protocol", "version", -1) << ", trillion=" + << reader.GetInteger64("user", "trillion", -1) << ", unsigned trillion=" + << reader.GetUnsigned64("user", "trillion", -1) << ", name=" << reader.Get("user", "name", "UNKNOWN") << ", email=" << reader.Get("user", "email", "UNKNOWN") << ", pi=" << reader.GetReal("user", "pi", -1) << ", active=" diff --git a/examples/cpptest.txt b/examples/cpptest.txt index bf083af..cbfc070 100644 --- a/examples/cpptest.txt +++ b/examples/cpptest.txt @@ -1,3 +1,3 @@ -Config loaded from 'test.ini': version=6, unsigned version=6, name=Bob Smith, email=bob@smith.com, pi=3.14159, active=1 +Config loaded from 'test.ini': version=6, unsigned version=6, trillion=1000000000000, unsigned trillion=1000000000000, name=Bob Smith, email=bob@smith.com, pi=3.14159, active=1 Has values: user.name=1, user.nose=0 Has sections: user=1, fizz=0 diff --git a/examples/test.ini b/examples/test.ini index 680c3b9..65a4cdf 100644 --- a/examples/test.ini +++ b/examples/test.ini @@ -1,10 +1,11 @@ ; Test config file for ini_example.c and INIReaderTest.cpp -[protocol] ; Protocol configuration -version=6 ; IPv6 +[protocol] ; Protocol configuration +version=6 ; IPv6 [user] -name = Bob Smith ; Spaces around '=' are stripped -email = bob@smith.com ; And comments (like this) ignored -active = true ; Test a boolean -pi = 3.14159 ; Test a floating point number +name = Bob Smith ; Spaces around '=' are stripped +email = bob@smith.com ; And comments (like this) ignored +active = true ; Test a boolean +pi = 3.14159 ; Test a floating point number +trillion = 1000000000000 ; Test 64-bit integers