Split Section to its own header and add convenience accessors

This commit is contained in:
Vinnie Falco
2014-09-16 16:19:17 -07:00
parent f87a6ccc7a
commit 89a51e5b91
7 changed files with 226 additions and 115 deletions

View File

@@ -2438,6 +2438,9 @@
<ClCompile Include="..\..\src\ripple\core\impl\LoadMonitor.cpp">
<ExcludedFromBuild>True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\core\impl\Section.cpp">
<ExcludedFromBuild>True</ExcludedFromBuild>
</ClCompile>
<ClInclude Include="..\..\src\ripple\core\Job.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\core\JobQueue.h">
@@ -2454,6 +2457,8 @@
</ClInclude>
<ClInclude Include="..\..\src\ripple\core\LoadMonitor.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\core\Section.h">
</ClInclude>
<ClCompile Include="..\..\src\ripple\data\crypto\Base58Data.cpp">
<ExcludedFromBuild>True</ExcludedFromBuild>
</ClCompile>

View File

@@ -3495,6 +3495,9 @@
<ClCompile Include="..\..\src\ripple\core\impl\LoadMonitor.cpp">
<Filter>ripple\core\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\core\impl\Section.cpp">
<Filter>ripple\core\impl</Filter>
</ClCompile>
<ClInclude Include="..\..\src\ripple\core\Job.h">
<Filter>ripple\core</Filter>
</ClInclude>
@@ -3519,6 +3522,9 @@
<ClInclude Include="..\..\src\ripple\core\LoadMonitor.h">
<Filter>ripple\core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\core\Section.h">
<Filter>ripple\core</Filter>
</ClInclude>
<ClCompile Include="..\..\src\ripple\data\crypto\Base58Data.cpp">
<Filter>ripple\data\crypto</Filter>
</ClCompile>

View File

@@ -20,8 +20,9 @@
#ifndef RIPPLE_CORE_CONFIG_H_INCLUDED
#define RIPPLE_CORE_CONFIG_H_INCLUDED
#include <ripple/unity/json.h>
#include <ripple/core/Section.h>
#include <ripple/data/protocol/RippleAddress.h>
#include <ripple/unity/json.h>
#include <beast/http/URL.h>
#include <beast/net/IPEndpoint.h>
#include <beast/module/core/files/File.h>
@@ -63,38 +64,6 @@ parseKeyValueSection (IniFileSections& secSource,
//------------------------------------------------------------------------------
/** Holds a collection of configuration values.
A configuration file contains zero or more sections.
*/
class Section
{
private:
std::vector <std::string> lines_;
std::map <std::string, std::string, beast::ci_less> map_;
public:
/** Create an empty section. */
Section() = default;
/** Append a set of lines to this section.
Parsable key/value pairs are also added to the map.
*/
void
append (std::vector <std::string> const& lines);
/** Returns `true` if a key with the given name exists. */
bool
exists (std::string const& name) const;
/** Retrieve a key/value pair.
@return A pair with bool `true` if the string was found.
*/
std::pair <std::string, bool>
find (std::string const& name) const;
};
//------------------------------------------------------------------------------
/** Holds unparsed configuration information.
The raw data sections are processed with intermediate parsers specific
to each module instead of being all parsed in a central location.
@@ -130,30 +99,6 @@ protected:
//------------------------------------------------------------------------------
/** Retrieve a key/value pair from a section.
@return The value string converted to T if it exists
and can be parsed, or else defaultValue.
*/
template <class T>
T
get (Section const& section,
std::string const& name, T const& defaultValue = T{})
{
auto const result = section.find (name);
if (! result.second)
return defaultValue;
try
{
return boost::lexical_cast <T> (result.first);
}
catch(...)
{
}
return defaultValue;
}
//------------------------------------------------------------------------------
// VFALCO TODO Replace these with beast "unsigned long long" generators
// VFALCO NOTE Apparently these are used elsewhere. Make them constants in the config
// or in the Application

135
src/ripple/core/Section.h Normal file
View File

@@ -0,0 +1,135 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_CORE_SECTION_H_INCLUDED
#define RIPPLE_CORE_SECTION_H_INCLUDED
#include <beast/utility/ci_char_traits.h>
#include <boost/lexical_cast.hpp>
#include <map>
#include <string>
#include <utility>
#include <vector>
namespace ripple {
/** Holds a collection of configuration values.
A configuration file contains zero or more sections.
*/
class Section
{
private:
std::vector <std::string> lines_;
std::map <std::string, std::string, beast::ci_less> map_;
public:
/** Create an empty section. */
Section() = default;
/** Append a set of lines to this section.
Parsable key/value pairs are also added to the map.
*/
void
append (std::vector <std::string> const& lines);
/** Returns `true` if a key with the given name exists. */
bool
exists (std::string const& name) const;
/** Retrieve a key/value pair.
@return A pair with bool `true` if the string was found.
*/
std::pair <std::string, bool>
find (std::string const& name) const;
};
/** Set a value from a configuration Section
If the named value is not found, the variable is unchanged.
@return `true` if value was set.
*/
template <class T>
bool
set (T& target, std::string const& name, Section const& section)
{
auto const result = section.find (name);
if (! result.second)
return false;
try
{
target = boost::lexical_cast <T> (result.first);
return true;
}
catch(...)
{
}
return false;
}
/** Set a value from a configuration Section
If the named value is not found, the variable is assigned the default.
@return `true` if named value was found in the Section.
*/
template <class T>
bool
set (T& target, T const& defaultValue,
std::string const& name, Section const& section)
{
auto const result = section.find (name);
if (! result.second)
return false;
try
{
// VFALCO TODO Use try_lexical_convert (boost 1.56.0)
target = boost::lexical_cast <T> (result.first);
return true;
}
catch(...)
{
target = defaultValue;
}
return false;
}
/** Retrieve a key/value pair from a section.
@return The value string converted to T if it exists
and can be parsed, or else defaultValue.
*/
// NOTE This routine might be more clumsy than the previous two
template <class T>
T
get (Section const& section,
std::string const& name, T const& defaultValue = T{})
{
auto const result = section.find (name);
if (! result.second)
return defaultValue;
try
{
return boost::lexical_cast <T> (result.first);
}
catch(...)
{
}
return defaultValue;
}
} // ripple
#endif

View File

@@ -215,64 +215,6 @@ parseAddresses (OutputSequence& out, InputIterator first, InputIterator last,
}
}
//------------------------------------------------------------------------------
//
// Section
//
//------------------------------------------------------------------------------
void
Section::append (std::vector <std::string> const& lines)
{
// <key> '=' <value>
static boost::regex const re1 (
"^" // start of line
"(?:\\s*)" // whitespace (optonal)
"([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 const& line : lines)
{
boost::smatch match;
lines_.push_back (line);
if (boost::regex_match (line, match, re1))
{
auto const result = map_.emplace (
std::make_pair (match[1], match[2]));
#if 0
if (! result.second)
{
// If we decide on how to merge values we can do it here.
}
beast::debug_ostream log;
//log << "\"" << match[1] << "\" = \"" << match[2] << "\"";
#endif
}
}
}
bool
Section::exists (std::string const& name) const
{
return map_.find (name) != map_.end();
}
std::pair <std::string, bool>
Section::find (std::string const& name) const
{
auto const iter = map_.find (name);
if (iter == map_.end())
return {{}, false};
return {iter->second, true};
}
//------------------------------------------------------------------------------
//
// BasicConfig

View File

@@ -0,0 +1,77 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/core/Section.h>
#include <boost/regex.hpp>
namespace ripple {
void
Section::append (std::vector <std::string> const& lines)
{
// <key> '=' <value>
static boost::regex const re1 (
"^" // start of line
"(?:\\s*)" // whitespace (optonal)
"([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 const& line : lines)
{
boost::smatch match;
lines_.push_back (line);
if (boost::regex_match (line, match, re1))
{
/*auto const result =*/ map_.emplace (
std::make_pair (match[1], match[2]));
#if 0
if (! result.second)
{
// If we decide on how to merge values we can do it here.
}
beast::debug_ostream log;
//log << "\"" << match[1] << "\" = \"" << match[2] << "\"";
#endif
}
}
}
bool
Section::exists (std::string const& name) const
{
return map_.find (name) != map_.end();
}
std::pair <std::string, bool>
Section::find (std::string const& name) const
{
auto const iter = map_.find (name);
if (iter == map_.end())
return {{}, false};
return {iter->second, true};
}
} // ripple

View File

@@ -25,3 +25,4 @@
#include <ripple/core/impl/LoadMonitor.cpp>
#include <ripple/core/impl/Job.cpp>
#include <ripple/core/impl/JobQueue.cpp>
#include <ripple/core/impl/Section.cpp>