diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index 399ed919b1..83161c7440 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -2438,6 +2438,9 @@ True + + True + @@ -2454,6 +2457,8 @@ + + True diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index 8d46b985fc..64d3f98d86 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -3495,6 +3495,9 @@ ripple\core\impl + + ripple\core\impl + ripple\core @@ -3519,6 +3522,9 @@ ripple\core + + ripple\core + ripple\data\crypto diff --git a/src/ripple/core/Config.h b/src/ripple/core/Config.h index 6abad70afb..271e6ea9d8 100644 --- a/src/ripple/core/Config.h +++ b/src/ripple/core/Config.h @@ -20,8 +20,9 @@ #ifndef RIPPLE_CORE_CONFIG_H_INCLUDED #define RIPPLE_CORE_CONFIG_H_INCLUDED -#include +#include #include +#include #include #include #include @@ -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 lines_; - std::map 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 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 - 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 -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 (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 diff --git a/src/ripple/core/Section.h b/src/ripple/core/Section.h new file mode 100644 index 0000000000..fc6cb04696 --- /dev/null +++ b/src/ripple/core/Section.h @@ -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 +#include +#include +#include +#include +#include + +namespace ripple { + +/** Holds a collection of configuration values. + A configuration file contains zero or more sections. +*/ +class Section +{ +private: + std::vector lines_; + std::map 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 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 + 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 +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 (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 +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 (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 +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 (result.first); + } + catch(...) + { + } + return defaultValue; +} + +} // ripple + +#endif + diff --git a/src/ripple/core/impl/Config.cpp b/src/ripple/core/impl/Config.cpp index 735633ac98..ab9e7e88e0 100644 --- a/src/ripple/core/impl/Config.cpp +++ b/src/ripple/core/impl/Config.cpp @@ -215,64 +215,6 @@ parseAddresses (OutputSequence& out, InputIterator first, InputIterator last, } } -//------------------------------------------------------------------------------ -// -// Section -// -//------------------------------------------------------------------------------ - -void -Section::append (std::vector const& lines) -{ - // '=' - static boost::regex const re1 ( - "^" // start of line - "(?:\\s*)" // whitespace (optonal) - "([a-zA-Z][_a-zA-Z0-9]*)" // - "(?:\\s*)" // whitespace (optional) - "(?:=)" // '=' - "(?:\\s*)" // whitespace (optional) - "(.*\\S+)" // - "(?:\\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 -Section::find (std::string const& name) const -{ - auto const iter = map_.find (name); - if (iter == map_.end()) - return {{}, false}; - return {iter->second, true}; -} - //------------------------------------------------------------------------------ // // BasicConfig diff --git a/src/ripple/core/impl/Section.cpp b/src/ripple/core/impl/Section.cpp new file mode 100644 index 0000000000..8356223aab --- /dev/null +++ b/src/ripple/core/impl/Section.cpp @@ -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 +#include + +namespace ripple { + +void +Section::append (std::vector const& lines) +{ + // '=' + static boost::regex const re1 ( + "^" // start of line + "(?:\\s*)" // whitespace (optonal) + "([a-zA-Z][_a-zA-Z0-9]*)" // + "(?:\\s*)" // whitespace (optional) + "(?:=)" // '=' + "(?:\\s*)" // whitespace (optional) + "(.*\\S+)" // + "(?:\\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 +Section::find (std::string const& name) const +{ + auto const iter = map_.find (name); + if (iter == map_.end()) + return {{}, false}; + return {iter->second, true}; +} + +} // ripple diff --git a/src/ripple/unity/core.cpp b/src/ripple/unity/core.cpp index 63650c1a1e..877fc9e4aa 100644 --- a/src/ripple/unity/core.cpp +++ b/src/ripple/unity/core.cpp @@ -25,3 +25,4 @@ #include #include #include +#include