From ef9684dd86d9d9365fef6e214b230931176803f9 Mon Sep 17 00:00:00 2001 From: Nat <95653800+natcat256@users.noreply.github.com> Date: Wed, 5 Jul 2023 11:24:40 -0400 Subject: [PATCH 1/6] Add INIReader::GetBigInteger and INIReader::GetBigUnsigned --- cpp/INIReader.cpp | 20 ++++++++++++++++++++ cpp/INIReader.h | 13 ++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/cpp/INIReader.cpp b/cpp/INIReader.cpp index 24e3fda..9f0945e 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 long long INIReader::GetBigInteger(const std::string& section, const std::string& name, long long default_value) const +{ + string valstr = Get(section, name, ""); + const char* value = valstr.c_str(); + char* end; + // This parses "1234" (decimal) and also "0x4D2" (hex) + long long 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 unsigned long long INIReader::GetBigUnsigned(const std::string& section, const std::string& name, unsigned long long default_value) const +{ + string valstr = Get(section, name, ""); + const char* value = valstr.c_str(); + char* end; + // This parses "1234" (decimal) and also "0x4D2" (hex) + unsigned long long 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..2d23097 100644 --- a/cpp/INIReader.h +++ b/cpp/INIReader.h @@ -66,11 +66,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 big integer (long long) value from INI file, returning default_value if + // not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2"). + INI_API long long GetBigInteger(const std::string& section, const std::string& name, long long 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 a big unsigned integer (unsigned long long) value from INI file, returning default_value if + // not found or not a valid unsigned integer (decimal "1234", "-1234", or hex "0x4d2"). + INI_API unsigned long long GetBigUnsigned(const std::string& section, const std::string& name, unsigned long long 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(). From e800f222f91a9050bc5e6a3b46bf59d01db3b181 Mon Sep 17 00:00:00 2001 From: Nat <95653800+natcat256@users.noreply.github.com> Date: Wed, 5 Jul 2023 12:06:51 -0400 Subject: [PATCH 2/6] Use INIReader::GetBigInteger and INIReader::GetBigUnsigned in examples --- examples/INIReaderExample.cpp | 4 +++- examples/cpptest.txt | 2 +- examples/test.ini | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/INIReaderExample.cpp b/examples/INIReaderExample.cpp index 54871ae..8e6aa52 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) << ", big=" + << reader.GetBigInteger("user", "big", -1) << ", unsigned big=" + << reader.GetBigUnsigned("user", "big", -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..6cd7e85 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, big=2147483648, unsigned big=2147483648, 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..0a4725c 100644 --- a/examples/test.ini +++ b/examples/test.ini @@ -8,3 +8,4 @@ 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 +big = 2147483648 ; Test a big integer From 0bc41b98f3c28f00c1c264c3520418656e764641 Mon Sep 17 00:00:00 2001 From: Nat <95653800+natcat256@users.noreply.github.com> Date: Thu, 6 Jul 2023 10:02:12 -0400 Subject: [PATCH 3/6] Use a more suitable test value for 64-bit integers --- examples/INIReaderExample.cpp | 6 +++--- examples/cpptest.txt | 2 +- examples/test.ini | 14 +++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/INIReaderExample.cpp b/examples/INIReaderExample.cpp index 8e6aa52..d123579 100644 --- a/examples/INIReaderExample.cpp +++ b/examples/INIReaderExample.cpp @@ -13,9 +13,9 @@ int main() } std::cout << "Config loaded from 'test.ini': version=" << reader.GetInteger("protocol", "version", -1) << ", unsigned version=" - << reader.GetUnsigned("protocol", "version", -1) << ", big=" - << reader.GetBigInteger("user", "big", -1) << ", unsigned big=" - << reader.GetBigUnsigned("user", "big", -1) << ", name=" + << reader.GetUnsigned("protocol", "version", -1) << ", trillion=" + << reader.GetBigInteger("user", "trillion", -1) << ", unsigned trillion=" + << reader.GetBigUnsigned("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 6cd7e85..cbfc070 100644 --- a/examples/cpptest.txt +++ b/examples/cpptest.txt @@ -1,3 +1,3 @@ -Config loaded from 'test.ini': version=6, unsigned version=6, big=2147483648, unsigned big=2147483648, 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 0a4725c..65a4cdf 100644 --- a/examples/test.ini +++ b/examples/test.ini @@ -1,11 +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 -big = 2147483648 ; Test a big integer +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 From 89bb14ede1ade983c694afaef5e6d9900dd3b896 Mon Sep 17 00:00:00 2001 From: Nat <95653800+natcat256@users.noreply.github.com> Date: Thu, 6 Jul 2023 21:14:39 -0400 Subject: [PATCH 4/6] Use clearer method names and fixed-width types for reading 64-bit integers --- cpp/INIReader.cpp | 8 ++++---- cpp/INIReader.h | 8 ++++---- examples/INIReaderExample.cpp | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cpp/INIReader.cpp b/cpp/INIReader.cpp index 9f0945e..ebe458b 100644 --- a/cpp/INIReader.cpp +++ b/cpp/INIReader.cpp @@ -56,13 +56,13 @@ long INIReader::GetInteger(const string& section, const string& name, long defau return end > value ? n : default_value; } -INI_API long long INIReader::GetBigInteger(const std::string& section, const std::string& name, long long default_value) const +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) - long long n = strtoll(value, &end, 0); + int64_t n = strtoll(value, &end, 0); return end > value ? n : default_value; } @@ -76,13 +76,13 @@ unsigned long INIReader::GetUnsigned(const string& section, const string& name, return end > value ? n : default_value; } -INI_API unsigned long long INIReader::GetBigUnsigned(const std::string& section, const std::string& name, unsigned long long default_value) const +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) - unsigned long long n = strtoull(value, &end, 0); + uint64_t n = strtoull(value, &end, 0); return end > value ? n : default_value; } diff --git a/cpp/INIReader.h b/cpp/INIReader.h index 2d23097..e9955bc 100644 --- a/cpp/INIReader.h +++ b/cpp/INIReader.h @@ -66,17 +66,17 @@ 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 a big integer (long long) value from INI file, returning default_value if + // 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 long long GetBigInteger(const std::string& section, const std::string& name, long long default_value) const; + 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 a big unsigned integer (unsigned long long) value from INI file, returning default_value if + // 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", "-1234", or hex "0x4d2"). - INI_API unsigned long long GetBigUnsigned(const std::string& section, const std::string& name, unsigned long long default_value) const; + 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 diff --git a/examples/INIReaderExample.cpp b/examples/INIReaderExample.cpp index d123579..debe5d6 100644 --- a/examples/INIReaderExample.cpp +++ b/examples/INIReaderExample.cpp @@ -14,8 +14,8 @@ int main() std::cout << "Config loaded from 'test.ini': version=" << reader.GetInteger("protocol", "version", -1) << ", unsigned version=" << reader.GetUnsigned("protocol", "version", -1) << ", trillion=" - << reader.GetBigInteger("user", "trillion", -1) << ", unsigned trillion=" - << reader.GetBigUnsigned("user", "trillion", -1) << ", name=" + << 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=" From f67d58760c5e8116e7681967632dcc1f7779d5f3 Mon Sep 17 00:00:00 2001 From: Nat <95653800+natcat256@users.noreply.github.com> Date: Thu, 6 Jul 2023 21:20:08 -0400 Subject: [PATCH 5/6] Include `` in INIReader.h Fixed-width type definitions were already accessible with `` included, but it's nice to include this header anyways. --- cpp/INIReader.h | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/INIReader.h b/cpp/INIReader.h index e9955bc..aa36257 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 From 0168be7e773981455066ec4d8f549d7504ab6a09 Mon Sep 17 00:00:00 2001 From: Nat <95653800+natcat256@users.noreply.github.com> Date: Thu, 6 Jul 2023 21:56:07 -0400 Subject: [PATCH 6/6] Remove negative value from unsigned integer example --- cpp/INIReader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/INIReader.h b/cpp/INIReader.h index aa36257..45e3fcd 100644 --- a/cpp/INIReader.h +++ b/cpp/INIReader.h @@ -76,7 +76,7 @@ class INIReader 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", "-1234", or hex "0x4d2"). + // 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