Skip to content

Commit

Permalink
Allow INIReader to read 64-bit integers (#151)
Browse files Browse the repository at this point in the history
Add INIReader::GetInteger64 and INIReader::GetUnsigned64
  • Loading branch information
mayak-dev authored Jul 7, 2023
1 parent d6e9d1b commit 9cecf06
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
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

0 comments on commit 9cecf06

Please sign in to comment.