Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow INIReader to read 64-bit integers #151

Merged
merged 6 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cpp/INIReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand All @@ -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, "");
Expand Down
14 changes: 11 additions & 3 deletions cpp/INIReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <map>
#include <string>
#include <cstdint>

// Visibility symbols, required for Windows DLLs
#ifndef INI_API
Expand Down Expand Up @@ -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().
Expand Down
4 changes: 3 additions & 1 deletion examples/INIReaderExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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="
Expand Down
2 changes: 1 addition & 1 deletion examples/cpptest.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Config loaded from 'test.ini': version=6, unsigned version=6, name=Bob Smith, [email protected], pi=3.14159, active=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
Has values: user.name=1, user.nose=0
Has sections: user=1, fizz=0
13 changes: 7 additions & 6 deletions examples/test.ini
Original file line number Diff line number Diff line change
@@ -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 = [email protected] ; 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 = [email protected] ; And comments (like this) ignored
active = true ; Test a boolean
pi = 3.14159 ; Test a floating point number
trillion = 1000000000000 ; Test 64-bit integers