Skip to content

Commit 9cecf06

Browse files
authored
Allow INIReader to read 64-bit integers (#151)
Add INIReader::GetInteger64 and INIReader::GetUnsigned64
1 parent d6e9d1b commit 9cecf06

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

cpp/INIReader.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ long INIReader::GetInteger(const string& section, const string& name, long defau
5656
return end > value ? n : default_value;
5757
}
5858

59+
INI_API int64_t INIReader::GetInteger64(const std::string& section, const std::string& name, int64_t default_value) const
60+
{
61+
string valstr = Get(section, name, "");
62+
const char* value = valstr.c_str();
63+
char* end;
64+
// This parses "1234" (decimal) and also "0x4D2" (hex)
65+
int64_t n = strtoll(value, &end, 0);
66+
return end > value ? n : default_value;
67+
}
68+
5969
unsigned long INIReader::GetUnsigned(const string& section, const string& name, unsigned long default_value) const
6070
{
6171
string valstr = Get(section, name, "");
@@ -66,6 +76,16 @@ unsigned long INIReader::GetUnsigned(const string& section, const string& name,
6676
return end > value ? n : default_value;
6777
}
6878

79+
INI_API uint64_t INIReader::GetUnsigned64(const std::string& section, const std::string& name, uint64_t default_value) const
80+
{
81+
string valstr = Get(section, name, "");
82+
const char* value = valstr.c_str();
83+
char* end;
84+
// This parses "1234" (decimal) and also "0x4D2" (hex)
85+
uint64_t n = strtoull(value, &end, 0);
86+
return end > value ? n : default_value;
87+
}
88+
6989
double INIReader::GetReal(const string& section, const string& name, double default_value) const
7090
{
7191
string valstr = Get(section, name, "");

cpp/INIReader.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <map>
1616
#include <string>
17+
#include <cstdint>
1718

1819
// Visibility symbols, required for Windows DLLs
1920
#ifndef INI_API
@@ -66,11 +67,18 @@ class INIReader
6667
// not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
6768
INI_API long GetInteger(const std::string& section, const std::string& name, long default_value) const;
6869

69-
// Get an unsigned integer (unsigned long) value from INI file,
70-
// returning default_value if not found or not a valid integer
71-
// (decimal "1234", or hex "0x4d2").
70+
// Get a 64-bit integer (int64_t) value from INI file, returning default_value if
71+
// not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
72+
INI_API int64_t GetInteger64(const std::string& section, const std::string& name, int64_t default_value) const;
73+
74+
// Get an unsigned integer (unsigned long) value from INI file, returning default_value if
75+
// not found or not a valid unsigned integer (decimal "1234", or hex "0x4d2").
7276
INI_API unsigned long GetUnsigned(const std::string& section, const std::string& name, unsigned long default_value) const;
7377

78+
// Get an unsigned 64-bit integer (uint64_t) value from INI file, returning default_value if
79+
// not found or not a valid unsigned integer (decimal "1234", or hex "0x4d2").
80+
INI_API uint64_t GetUnsigned64(const std::string& section, const std::string& name, uint64_t default_value) const;
81+
7482
// Get a real (floating point double) value from INI file, returning
7583
// default_value if not found or not a valid floating point value
7684
// according to strtod().

examples/INIReaderExample.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ int main()
1313
}
1414
std::cout << "Config loaded from 'test.ini': version="
1515
<< reader.GetInteger("protocol", "version", -1) << ", unsigned version="
16-
<< reader.GetUnsigned("protocol", "version", -1) << ", name="
16+
<< reader.GetUnsigned("protocol", "version", -1) << ", trillion="
17+
<< reader.GetInteger64("user", "trillion", -1) << ", unsigned trillion="
18+
<< reader.GetUnsigned64("user", "trillion", -1) << ", name="
1719
<< reader.Get("user", "name", "UNKNOWN") << ", email="
1820
<< reader.Get("user", "email", "UNKNOWN") << ", pi="
1921
<< reader.GetReal("user", "pi", -1) << ", active="

examples/cpptest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Config loaded from 'test.ini': version=6, unsigned version=6, name=Bob Smith, [email protected], pi=3.14159, active=1
1+
Config loaded from 'test.ini': version=6, unsigned version=6, trillion=1000000000000, unsigned trillion=1000000000000, name=Bob Smith, [email protected], pi=3.14159, active=1
22
Has values: user.name=1, user.nose=0
33
Has sections: user=1, fizz=0

examples/test.ini

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
; Test config file for ini_example.c and INIReaderTest.cpp
22

3-
[protocol] ; Protocol configuration
4-
version=6 ; IPv6
3+
[protocol] ; Protocol configuration
4+
version=6 ; IPv6
55

66
[user]
7-
name = Bob Smith ; Spaces around '=' are stripped
8-
email = [email protected] ; And comments (like this) ignored
9-
active = true ; Test a boolean
10-
pi = 3.14159 ; Test a floating point number
7+
name = Bob Smith ; Spaces around '=' are stripped
8+
email = [email protected] ; And comments (like this) ignored
9+
active = true ; Test a boolean
10+
pi = 3.14159 ; Test a floating point number
11+
trillion = 1000000000000 ; Test 64-bit integers

0 commit comments

Comments
 (0)