IniFile: Handle s64/u64 values

This commit is contained in:
Lioncash
2017-02-24 22:56:33 -05:00
parent 51136681df
commit beec40f178
19 changed files with 133 additions and 40 deletions

View File

@ -2,9 +2,10 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
// see IniFile.h
#include "Common/IniFile.h"
#include <algorithm>
#include <cinttypes>
#include <cstddef>
#include <cstring>
#include <fstream>
@ -15,7 +16,6 @@
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/StringUtil.h"
void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut)
@ -73,6 +73,41 @@ void IniFile::Section::Set(const std::string& key, const std::vector<std::string
Set(key, temp);
}
void IniFile::Section::Set(const std::string& key, u32 newValue)
{
Set(key, StringFromFormat("0x%08x", newValue));
}
void IniFile::Section::Set(const std::string& key, u64 new_value)
{
Set(key, StringFromFormat("0x%016" PRIx64, new_value));
}
void IniFile::Section::Set(const std::string& key, float newValue)
{
Set(key, StringFromFormat("%#.9g", newValue));
}
void IniFile::Section::Set(const std::string& key, double newValue)
{
Set(key, StringFromFormat("%#.17g", newValue));
}
void IniFile::Section::Set(const std::string& key, int newValue)
{
Set(key, StringFromInt(newValue));
}
void IniFile::Section::Set(const std::string& key, s64 newValue)
{
Set(key, StringFromFormat("%" PRId64, newValue));
}
void IniFile::Section::Set(const std::string& key, bool newValue)
{
Set(key, StringFromBool(newValue));
}
bool IniFile::Section::Get(const std::string& key, std::string* value,
const std::string& defaultValue) const
{
@ -133,6 +168,18 @@ bool IniFile::Section::Get(const std::string& key, int* value, int defaultValue)
return false;
}
bool IniFile::Section::Get(const std::string& key, s64* value, s64 default_value) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = default_value;
return false;
}
bool IniFile::Section::Get(const std::string& key, u32* value, u32 defaultValue) const
{
std::string temp;
@ -145,6 +192,18 @@ bool IniFile::Section::Get(const std::string& key, u32* value, u32 defaultValue)
return false;
}
bool IniFile::Section::Get(const std::string& key, u64* value, u64 default_value) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = default_value;
return false;
}
bool IniFile::Section::Get(const std::string& key, bool* value, bool defaultValue) const
{
std::string temp;

View File

@ -12,7 +12,6 @@
#include "Common/CommonFuncs.h"
#include "Common/CommonTypes.h"
#include "Common/StringUtil.h"
struct CaseInsensitiveStringCompare
{
@ -37,24 +36,14 @@ public:
void Set(const std::string& key, const std::string& newValue);
void Set(const std::string& key, const std::string& newValue, const std::string& defaultValue);
void Set(const std::string& key, u32 newValue);
void Set(const std::string& key, u64 new_value);
void Set(const std::string& key, float newValue);
void Set(const std::string& key, double newValue);
void Set(const std::string& key, int newValue);
void Set(const std::string& key, s64 new_value);
void Set(const std::string& key, bool newValue);
void Set(const std::string& key, u32 newValue)
{
Set(key, StringFromFormat("0x%08x", newValue));
}
void Set(const std::string& key, float newValue)
{
Set(key, StringFromFormat("%#.9g", newValue));
}
void Set(const std::string& key, double newValue)
{
Set(key, StringFromFormat("%#.17g", newValue));
}
void Set(const std::string& key, int newValue) { Set(key, StringFromInt(newValue)); }
void Set(const std::string& key, bool newValue) { Set(key, StringFromBool(newValue)); }
template <typename T>
void Set(const std::string& key, T newValue, const T defaultValue)
{
@ -69,7 +58,9 @@ public:
bool Get(const std::string& key, std::string* value,
const std::string& defaultValue = NULL_STRING) const;
bool Get(const std::string& key, int* value, int defaultValue = 0) const;
bool Get(const std::string& key, s64* value, s64 default_value = 0) const;
bool Get(const std::string& key, u32* value, u32 defaultValue = 0) const;
bool Get(const std::string& key, u64* value, u64 default_value = 0) const;
bool Get(const std::string& key, bool* value, bool defaultValue = false) const;
bool Get(const std::string& key, float* value, float defaultValue = 0.0f) const;
bool Get(const std::string& key, double* value, double defaultValue = 0.0) const;

View File

@ -247,6 +247,25 @@ bool TryParse(const std::string& str, u32* const output)
return true;
}
bool TryParse(const std::string& str, u64* const output)
{
char* end_ptr = nullptr;
// Set errno to a clean slate
errno = 0;
u64 value = strtoull(str.c_str(), &end_ptr, 0);
if (end_ptr == nullptr || *end_ptr != '\0')
return false;
if (errno == ERANGE)
return false;
*output = value;
return true;
}
bool TryParse(const std::string& str, bool* const output)
{
float value;

View File

@ -61,6 +61,7 @@ std::string StringFromBool(bool value);
bool TryParse(const std::string& str, bool* output);
bool TryParse(const std::string& str, u32* output);
bool TryParse(const std::string& str, u64* output);
template <typename N>
static bool TryParse(const std::string& str, N* const output)