mirror of
https://github.com/XRPLF/rippled.git
synced 2026-07-30 02:20:39 +00:00
refactor: Change config section and key string literals into constants (#7095)
Co-authored-by: Bart <11445373+bthomee@users.noreply.github.com>
This commit is contained in:
183
src/libxrpl/config/BasicConfig.cpp
Normal file
183
src/libxrpl/config/BasicConfig.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
#include <xrpl/config/BasicConfig.h>
|
||||
|
||||
#include <xrpl/basics/StringUtilities.h>
|
||||
|
||||
#include <boost/regex/v5/regbase.hpp>
|
||||
#include <boost/regex/v5/regex.hpp>
|
||||
#include <boost/regex/v5/regex_match.hpp>
|
||||
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
Section::Section(std::string name) : name_(std::move(name))
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
Section::set(std::string const& key, std::string const& value)
|
||||
{
|
||||
lookup_.insert_or_assign(key, value);
|
||||
}
|
||||
|
||||
void
|
||||
Section::append(std::vector<std::string> const& lines)
|
||||
{
|
||||
// <key> '=' <value>
|
||||
static boost::regex const kRe1(
|
||||
"^" // start of line
|
||||
"(?:\\s*)" // whitespace (optional)
|
||||
"([a-zA-Z][_a-zA-Z0-9]*)" // <key>
|
||||
"(?:\\s*)" // whitespace (optional)
|
||||
"(?:=)" // '='
|
||||
"(?:\\s*)" // whitespace (optional)
|
||||
"(.*\\S+)" // <value>
|
||||
"(?:\\s*)" // whitespace (optional)
|
||||
,
|
||||
boost::regex_constants::optimize);
|
||||
|
||||
lines_.reserve(lines_.size() + lines.size());
|
||||
for (auto line : lines)
|
||||
{
|
||||
auto removeComment = [](std::string& val) -> bool {
|
||||
bool removedTrailing = false;
|
||||
auto comment = val.find('#');
|
||||
while (comment != std::string::npos)
|
||||
{
|
||||
if (comment == 0)
|
||||
{
|
||||
// entire value is a comment. In most cases, this
|
||||
// would have already been handled by the file reader
|
||||
val = "";
|
||||
break;
|
||||
}
|
||||
if (val.at(comment - 1) == '\\')
|
||||
{
|
||||
// we have an escaped comment char. Erase the escape char
|
||||
// and keep looking
|
||||
val.erase(comment - 1, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// this must be a real comment. Extract the value
|
||||
// as a substring and stop looking.
|
||||
val = trimWhitespace(val.substr(0, comment));
|
||||
removedTrailing = true;
|
||||
break;
|
||||
}
|
||||
|
||||
comment = val.find('#', comment);
|
||||
}
|
||||
return removedTrailing;
|
||||
};
|
||||
|
||||
if (removeComment(line) && !line.empty())
|
||||
hadTrailingComments_ = true;
|
||||
|
||||
if (line.empty())
|
||||
continue;
|
||||
|
||||
boost::smatch match;
|
||||
if (boost::regex_match(line, match, kRe1))
|
||||
{
|
||||
set(match[1], match[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
values_.push_back(line);
|
||||
}
|
||||
|
||||
lines_.push_back(std::move(line));
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
Section::exists(std::string const& name) const
|
||||
{
|
||||
return lookup_.contains(name);
|
||||
}
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, Section const& section)
|
||||
{
|
||||
for (auto const& [k, v] : section.lookup_)
|
||||
os << k << "=" << v << "\n";
|
||||
return os;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
bool
|
||||
BasicConfig::exists(std::string const& name) const
|
||||
{
|
||||
return map_.contains(name);
|
||||
}
|
||||
|
||||
Section&
|
||||
BasicConfig::section(std::string const& name)
|
||||
{
|
||||
return map_.emplace(name, name).first->second;
|
||||
}
|
||||
|
||||
Section const&
|
||||
BasicConfig::section(std::string const& name) const
|
||||
{
|
||||
static Section const kNone("");
|
||||
auto const iter = map_.find(name);
|
||||
if (iter == map_.end())
|
||||
return kNone;
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
void
|
||||
BasicConfig::overwrite(std::string const& section, std::string const& key, std::string const& value)
|
||||
{
|
||||
auto const result =
|
||||
map_.emplace(std::piecewise_construct, std::make_tuple(section), std::make_tuple(section));
|
||||
result.first->second.set(key, value);
|
||||
}
|
||||
|
||||
void
|
||||
BasicConfig::deprecatedClearSection(std::string const& section)
|
||||
{
|
||||
auto i = map_.find(section);
|
||||
if (i != map_.end())
|
||||
i->second = Section(section);
|
||||
}
|
||||
|
||||
void
|
||||
BasicConfig::legacy(std::string const& section, std::string value)
|
||||
{
|
||||
map_.emplace(section, section).first->second.legacy(std::move(value));
|
||||
}
|
||||
|
||||
std::string
|
||||
BasicConfig::legacy(std::string const& sectionName) const
|
||||
{
|
||||
return section(sectionName).legacy();
|
||||
}
|
||||
|
||||
void
|
||||
BasicConfig::build(IniFileSections const& ifs)
|
||||
{
|
||||
for (auto const& entry : ifs)
|
||||
{
|
||||
auto const result = map_.emplace(
|
||||
std::piecewise_construct, std::make_tuple(entry.first), std::make_tuple(entry.first));
|
||||
result.first->second.append(entry.second);
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& ss, BasicConfig const& c)
|
||||
{
|
||||
for (auto const& [k, v] : c.map_)
|
||||
ss << "[" << k << "]\n" << v;
|
||||
return ss;
|
||||
}
|
||||
|
||||
} // namespace xrpl
|
||||
Reference in New Issue
Block a user