dolphin/Source/Core/Common/IniFile.h
Lioncash de7e9557dc Common/IniFile: Make CaseInsensitiveStringCompare usable with heterogenous lookup
Previously, when performing find() operations or indexing operations on
the section map, it would need to operate on a std::string key.

This means cases like:

map.find(some_string_view)

aren't usable, which kind of sucks, especially given for most cases, we
use regular string literals to perform operations in calling code.
However, since C++14, it's possible to use heterogenous lookup to avoid
needing to construct exact key types. In otherwords, we can perform the
above or use string literals without constructing a std::string instance
around them implicitly.

We simply need to specify a member type within our comparison struct
named is_transparent, to allow std::map to perform automatic type
deduction.

We also slightly alter the algorithm to an equivalent compatible with
std::string_view (which need not be null-terminated), as strcasecmp
requires null-terminated strings.

While we're at it, we can also provide a helper function to the struct
for comparing string equality rather than only less than. This allows
removing other usages of strcasecmp in other functions, allowing for the
transition of them to std::string_view.
2019-06-16 18:20:03 -04:00

175 lines
5.2 KiB
C++

// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <algorithm>
#include <cctype>
#include <list>
#include <map>
#include <string>
#include <string_view>
#include <vector>
#include "Common/CommonTypes.h"
#include "Common/StringUtil.h"
struct CaseInsensitiveStringCompare
{
// Allow heterogenous lookup.
using is_transparent = void;
bool operator()(std::string_view a, std::string_view b) const
{
return std::lexicographical_compare(
a.begin(), a.end(), b.begin(), b.end(), [](char lhs, char rhs) {
return std::tolower(static_cast<u8>(lhs)) < std::tolower(static_cast<u8>(rhs));
});
}
static bool IsEqual(std::string_view a, std::string_view b)
{
if (a.size() != b.size())
return false;
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char lhs, char rhs) {
return std::tolower(static_cast<u8>(lhs)) == std::tolower(static_cast<u8>(rhs));
});
}
};
class IniFile
{
public:
class Section
{
friend class IniFile;
public:
Section();
explicit Section(std::string name_);
bool Exists(const std::string& key) const;
bool Delete(const std::string& key);
void Set(const std::string& key, std::string new_value);
template <typename T>
void Set(const std::string& key, T&& new_value)
{
Set(key, ValueToString(std::forward<T>(new_value)));
}
template <typename T>
void Set(const std::string& key, T&& new_value, const std::common_type_t<T>& default_value)
{
if (new_value != default_value)
Set(key, std::forward<T>(new_value));
else
Delete(key);
}
bool Get(const std::string& key, std::string* value,
const std::string& default_value = NULL_STRING) const;
template <typename T>
bool Get(const std::string& key, T* value,
const std::common_type_t<T>& default_value = {}) const
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = default_value;
return false;
}
void SetLines(std::vector<std::string> lines);
bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const;
bool operator<(const Section& other) const { return name < other.name; }
using SectionMap = std::map<std::string, std::string, CaseInsensitiveStringCompare>;
const std::string& GetName() const { return name; }
const SectionMap& GetValues() const { return values; }
bool HasLines() const { return !m_lines.empty(); }
protected:
std::string name;
std::vector<std::string> keys_order;
SectionMap values;
std::vector<std::string> m_lines;
};
IniFile();
~IniFile();
/**
* Loads sections and keys.
* @param filename filename of the ini file which should be loaded
* @param keep_current_data If true, "extends" the currently loaded list of sections and keys with
* the loaded data (and replaces existing entries). If false, existing data will be erased.
* @warning Using any other operations than "Get*" and "Exists" is untested and will behave
* unexpectedly
* @todo This really is just a hack to support having two levels of gameinis (defaults and
* user-specified) and should eventually be replaced with a less stupid system.
*/
bool Load(const std::string& filename, bool keep_current_data = false);
bool Save(const std::string& filename);
// Returns true if key exists in section
bool Exists(const std::string& sectionName, const std::string& key) const;
template <typename T>
bool GetIfExists(const std::string& sectionName, const std::string& key, T* value)
{
if (Exists(sectionName, key))
return GetOrCreateSection(sectionName)->Get(key, value);
return false;
}
template <typename T>
bool GetIfExists(const std::string& sectionName, const std::string& key, T* value, T defaultValue)
{
if (Exists(sectionName, key))
return GetOrCreateSection(sectionName)->Get(key, value, defaultValue);
else
*value = defaultValue;
return false;
}
bool GetKeys(const std::string& sectionName, std::vector<std::string>* keys) const;
void SetLines(const std::string& sectionName, const std::vector<std::string>& lines);
void SetLines(const std::string& section_name, std::vector<std::string>&& lines);
bool GetLines(const std::string& sectionName, std::vector<std::string>* lines,
const bool remove_comments = true) const;
bool DeleteKey(const std::string& sectionName, const std::string& key);
bool DeleteSection(const std::string& sectionName);
void SortSections();
Section* GetOrCreateSection(const std::string& section);
// This function is related to parsing data from lines of INI files
// It's used outside of IniFile, which is why it is exposed publicly
// In particular it is used in PostProcessing for its configuration
static void ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut);
const std::list<Section>& GetSections() const { return sections; }
private:
std::list<Section> sections;
const Section* GetSection(const std::string& section) const;
Section* GetSection(const std::string& section);
static const std::string& NULL_STRING;
};