Skip to content

Commit

Permalink
Merge pull request hrydgard#19109 from hrydgard/assorted-cleanup
Browse files Browse the repository at this point in the history
Assorted cleanup
  • Loading branch information
hrydgard authored May 5, 2024
2 parents d769c32 + 0b91aa5 commit 602c37c
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 147 deletions.
57 changes: 24 additions & 33 deletions Common/Data/Format/IniFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,54 +198,54 @@ bool Section::GetKeys(std::vector<std::string> &keys) const {
return true;
}

ParsedIniLine *Section::GetLine(const char *key) {
ParsedIniLine *Section::GetLine(std::string_view key) {
for (auto &line : lines_) {
if (equalsNoCase(line.Key(), key))
return &line;
}
return nullptr;
}

const ParsedIniLine *Section::GetLine(const char* key) const {
const ParsedIniLine *Section::GetLine(std::string_view key) const {
for (auto &line : lines_) {
if (equalsNoCase(line.Key(), key))
return &line;
}
return nullptr;
}

void Section::Set(const char* key, uint32_t newValue) {
void Section::Set(std::string_view key, uint32_t newValue) {
char temp[128];
snprintf(temp, sizeof(temp), "0x%08x", newValue);
Set(key, (const char *)temp);
}

void Section::Set(const char* key, uint64_t newValue) {
void Section::Set(std::string_view key, uint64_t newValue) {
char temp[128];
snprintf(temp, sizeof(temp), "0x%016" PRIx64, newValue);
Set(key, (const char *)temp);
}

void Section::Set(const char* key, float newValue) {
void Section::Set(std::string_view key, float newValue) {
_dbg_assert_(!my_isnanorinf(newValue));
char temp[128];
snprintf(temp, sizeof(temp), "%f", newValue);
Set(key, (const char *)temp);
}

void Section::Set(const char* key, double newValue) {
void Section::Set(std::string_view key, double newValue) {
char temp[128];
snprintf(temp, sizeof(temp), "%f", newValue);
Set(key, (const char *)temp);
}

void Section::Set(const char* key, int newValue) {
void Section::Set(std::string_view key, int newValue) {
char temp[128];
snprintf(temp, sizeof(temp), "%d", newValue);
Set(key, (const char *)temp);
}

void Section::Set(const char* key, const char* newValue) {
void Section::Set(std::string_view key, const char* newValue) {
ParsedIniLine *line = GetLine(key);
if (line) {
line->SetValue(newValue);
Expand All @@ -255,15 +255,15 @@ void Section::Set(const char* key, const char* newValue) {
}
}

void Section::Set(const char* key, const std::string& newValue, const std::string& defaultValue)
void Section::Set(std::string_view key, const std::string& newValue, const std::string& defaultValue)
{
if (newValue != defaultValue)
Set(key, newValue);
else
Delete(key);
}

bool Section::Get(const char* key, std::string* value, const char* defaultValue) const {
bool Section::Get(std::string_view key, std::string* value, const char* defaultValue) const {
const ParsedIniLine *line = GetLine(key);
if (!line) {
if (defaultValue) {
Expand All @@ -276,31 +276,31 @@ bool Section::Get(const char* key, std::string* value, const char* defaultValue)
return true;
}

void Section::Set(const char* key, const float newValue, const float defaultValue)
void Section::Set(std::string_view key, const float newValue, const float defaultValue)
{
if (newValue != defaultValue)
Set(key, newValue);
else
Delete(key);
}

void Section::Set(const char* key, int newValue, int defaultValue)
void Section::Set(std::string_view key, int newValue, int defaultValue)
{
if (newValue != defaultValue)
Set(key, newValue);
else
Delete(key);
}

void Section::Set(const char* key, bool newValue, bool defaultValue)
void Section::Set(std::string_view key, bool newValue, bool defaultValue)
{
if (newValue != defaultValue)
Set(key, newValue);
else
Delete(key);
}

void Section::Set(const char* key, const std::vector<std::string>& newValues)
void Section::Set(std::string_view key, const std::vector<std::string>& newValues)
{
std::string temp;
// Join the strings with ,
Expand All @@ -319,12 +319,10 @@ void Section::AddComment(const std::string &comment) {
lines_.emplace_back(ParsedIniLine::CommentOnly("# " + comment));
}

bool Section::Get(const char* key, std::vector<std::string>& values) const
{
bool Section::Get(std::string_view key, std::vector<std::string>& values) const {
std::string temp;
bool retval = Get(key, &temp, 0);
if (!retval || temp.empty())
{
if (!retval || temp.empty()) {
return false;
}
// ignore starting , if any
Expand All @@ -333,7 +331,6 @@ bool Section::Get(const char* key, std::vector<std::string>& values) const

// split by ,
while (subStart != std::string::npos) {

// Find next ,
subEnd = temp.find_first_of(',', subStart);
if (subStart != subEnd)
Expand All @@ -347,8 +344,7 @@ bool Section::Get(const char* key, std::vector<std::string>& values) const
return true;
}

bool Section::Get(const char* key, int* value, int defaultValue) const
{
bool Section::Get(std::string_view key, int* value, int defaultValue) const {
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp, value))
Expand All @@ -357,8 +353,7 @@ bool Section::Get(const char* key, int* value, int defaultValue) const
return false;
}

bool Section::Get(const char* key, uint32_t* value, uint32_t defaultValue) const
{
bool Section::Get(std::string_view key, uint32_t* value, uint32_t defaultValue) const {
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp, value))
Expand All @@ -367,8 +362,7 @@ bool Section::Get(const char* key, uint32_t* value, uint32_t defaultValue) const
return false;
}

bool Section::Get(const char* key, uint64_t* value, uint64_t defaultValue) const
{
bool Section::Get(std::string_view key, uint64_t* value, uint64_t defaultValue) const {
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp, value))
Expand All @@ -377,8 +371,7 @@ bool Section::Get(const char* key, uint64_t* value, uint64_t defaultValue) const
return false;
}

bool Section::Get(const char* key, bool* value, bool defaultValue) const
{
bool Section::Get(std::string_view key, bool* value, bool defaultValue) const {
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp, value))
Expand All @@ -387,8 +380,7 @@ bool Section::Get(const char* key, bool* value, bool defaultValue) const
return false;
}

bool Section::Get(const char* key, float* value, float defaultValue) const
{
bool Section::Get(std::string_view key, float* value, float defaultValue) const {
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp, value))
Expand All @@ -397,8 +389,7 @@ bool Section::Get(const char* key, float* value, float defaultValue) const
return false;
}

bool Section::Get(const char* key, double* value, double defaultValue) const
{
bool Section::Get(std::string_view key, double* value, double defaultValue) const {
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp, value))
Expand All @@ -407,7 +398,7 @@ bool Section::Get(const char* key, double* value, double defaultValue) const
return false;
}

bool Section::Exists(const char *key) const {
bool Section::Exists(std::string_view key) const {
for (auto &line : lines_) {
if (equalsNoCase(key, line.Key()))
return true;
Expand All @@ -425,7 +416,7 @@ std::map<std::string, std::string> Section::ToMap() const {
return outMap;
}

bool Section::Delete(const char *key) {
bool Section::Delete(std::string_view key) {
ParsedIniLine *line = GetLine(key);
for (auto liter = lines_.begin(); liter != lines_.end(); ++liter) {
if (line == &*liter) {
Expand Down
88 changes: 29 additions & 59 deletions Common/Data/Format/IniFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,56 +54,56 @@ class Section {

public:
Section() {}
Section(const std::string& name) : name_(name) {}
Section(std::string_view name) : name_(name) {}

bool Exists(const char *key) const;
bool Delete(const char *key);
bool Exists(std::string_view key) const;
bool Delete(std::string_view key);

void Clear();

std::map<std::string, std::string> ToMap() const;

ParsedIniLine *GetLine(const char *key);
const ParsedIniLine *GetLine(const char *key) const;
ParsedIniLine *GetLine(std::string_view key);
const ParsedIniLine *GetLine(std::string_view key) const;

void Set(const char* key, const char* newValue);
void Set(const char* key, const std::string& newValue, const std::string& defaultValue);
void Set(std::string_view key, const char* newValue);
void Set(std::string_view key, const std::string& newValue, const std::string& defaultValue);

void Set(const std::string &key, const std::string &value) {
Set(key.c_str(), value.c_str());
void Set(std::string_view key, const std::string &value) {
Set(key, value.c_str());
}
bool Get(const char* key, std::string* value, const char* defaultValue) const;
bool Get(std::string_view key, std::string* value, const char* defaultValue) const;

void Set(const char* key, uint32_t newValue);
void Set(const char* key, uint64_t newValue);
void Set(const char* key, float newValue);
void Set(const char* key, const float newValue, const float defaultValue);
void Set(const char* key, double newValue);
void Set(std::string_view key, uint32_t newValue);
void Set(std::string_view key, uint64_t newValue);
void Set(std::string_view key, float newValue);
void Set(std::string_view key, const float newValue, const float defaultValue);
void Set(std::string_view key, double newValue);

void Set(const char* key, int newValue, int defaultValue);
void Set(const char* key, int newValue);
void Set(std::string_view key, int newValue, int defaultValue);
void Set(std::string_view key, int newValue);

void Set(const char* key, bool newValue, bool defaultValue);
void Set(const char* key, bool newValue) {
void Set(std::string_view key, bool newValue, bool defaultValue);
void Set(std::string_view key, bool newValue) {
Set(key, newValue ? "True" : "False");
}
void Set(const char* key, const std::vector<std::string>& newValues);
void Set(std::string_view key, const std::vector<std::string>& newValues);

// Declare without a body to make it fail to compile. This is to prevent accidentally
// setting a pointer as a bool. The failure is in the linker unfortunately, but that's better
// than accidentally succeeding in a bad way.
template<class T>
void Set(const char *key, T *ptr);
void Set(std::string_view key, T *ptr);

void AddComment(const std::string &comment);

bool Get(const char* key, int* value, int defaultValue = 0) const;
bool Get(const char* key, uint32_t* value, uint32_t defaultValue = 0) const;
bool Get(const char* key, uint64_t* value, uint64_t defaultValue = 0) const;
bool Get(const char* key, bool* value, bool defaultValue = false) const;
bool Get(const char* key, float* value, float defaultValue = false) const;
bool Get(const char* key, double* value, double defaultValue = false) const;
bool Get(const char* key, std::vector<std::string>& values) const;
bool Get(std::string_view key, int* value, int defaultValue = 0) const;
bool Get(std::string_view key, uint32_t* value, uint32_t defaultValue = 0) const;
bool Get(std::string_view key, uint64_t* value, uint64_t defaultValue = 0) const;
bool Get(std::string_view key, bool* value, bool defaultValue = false) const;
bool Get(std::string_view key, float* value, float defaultValue = false) const;
bool Get(std::string_view key, double* value, double defaultValue = false) const;
bool Get(std::string_view key, std::vector<std::string>& values) const;

// Return a list of all keys in this section
bool GetKeys(std::vector<std::string> &keys) const;
Expand Down Expand Up @@ -133,44 +133,14 @@ class IniFile {
// Returns true if key exists in section
bool Exists(const char* sectionName, const char* key) const;

// TODO: Get rid of these, in favor of the Section ones.
void Set(const char* sectionName, const char* key, const char* newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, const std::string& newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue.c_str());
}
void Set(const char* sectionName, const char* key, int newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, uint32_t newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, uint64_t newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, bool newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, const std::vector<std::string>& newValues) {
GetOrCreateSection(sectionName)->Set(key, newValues);
}

// TODO: Get rid of these, in favor of the Section ones.
// These will not create the section if it doesn't exist.
bool Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue = "");
bool Get(const char* sectionName, const char* key, int* value, int defaultValue = 0);
bool Get(const char* sectionName, const char* key, uint32_t* value, uint32_t defaultValue = 0);
bool Get(const char* sectionName, const char* key, uint64_t* value, uint64_t defaultValue = 0);
bool Get(const char* sectionName, const char* key, bool* value, bool defaultValue = false);
bool Get(const char* sectionName, const char* key, std::vector<std::string>& values);

template<typename T> bool GetIfExists(const char* sectionName, const char* key, T value)
{
if (Exists(sectionName, key))
return Get(sectionName, key, value);
return false;
}

bool GetKeys(const char* sectionName, std::vector<std::string>& keys) const;

bool DeleteKey(const char* sectionName, const char* key);
Expand Down
8 changes: 4 additions & 4 deletions Common/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ void SplitString(std::string_view str, const char delim, std::vector<std::string
}
}

static std::string ApplyHtmlEscapes(std::string str) {
static std::string ApplyHtmlEscapes(std::string_view str_view) {
struct Repl {
const char *a;
const char *b;
Expand All @@ -345,15 +345,15 @@ static std::string ApplyHtmlEscapes(std::string str) {
// Easy to add more cases.
};

std::string str(str_view);
for (const Repl &r : replacements) {
str = ReplaceAll(str, r.a, r.b);
}

return str;
}

// Meant for HTML listings and similar, so supports some HTML escapes.
void GetQuotedStrings(const std::string& str, std::vector<std::string> &output) {
void GetQuotedStrings(std::string_view str, std::vector<std::string> &output) {
size_t next = 0;
bool even = 0;
for (size_t pos = 0, len = str.length(); pos < len; ++pos) {
Expand All @@ -372,11 +372,11 @@ void GetQuotedStrings(const std::string& str, std::vector<std::string> &output)
}
}

// TODO: this is quite inefficient.
std::string ReplaceAll(std::string_view input, std::string_view src, std::string_view dest) {
size_t pos = 0;

std::string result(input);

if (src == dest)
return result;

Expand Down
2 changes: 1 addition & 1 deletion Common/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void SplitString(std::string_view str, const char delim, std::vector<std::string
// Try to avoid this when possible, in favor of the string_view version.
void SplitString(std::string_view str, const char delim, std::vector<std::string> &output);

void GetQuotedStrings(const std::string& str, std::vector<std::string>& output);
void GetQuotedStrings(std::string_view str, std::vector<std::string> &output);

std::string ReplaceAll(std::string_view input, std::string_view src, std::string_view dest);

Expand Down
Loading

0 comments on commit 602c37c

Please sign in to comment.