1
0
mirror of https://github.com/benhoyt/inih.git synced 2025-01-17 22:22:53 +08:00

Allow INIReader to read 64-bit integers (#151)

Add INIReader::GetInteger64 and INIReader::GetUnsigned64
This commit is contained in:
Nat 2023-07-06 22:00:45 -04:00 committed by GitHub
parent d6e9d1ba68
commit 9cecf0643d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 11 deletions

View File

@ -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, "");

View File

@ -14,6 +14,7 @@
#include <map>
#include <string>
#include <cstdint>
// Visibility symbols, required for Windows DLLs
#ifndef INI_API
@ -66,11 +67,18 @@ public:
// 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().

View File

@ -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="

View File

@ -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

View File

@ -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