Allow paths with trailing data, emmitting a warning

This commit is contained in:
AnyOldName3 2021-11-14 00:22:44 +00:00
parent 9be606a40d
commit 8fb0b5846e
7 changed files with 113 additions and 65 deletions

View file

@ -138,10 +138,10 @@ void mergeComposingVariables(boost::program_options::variables_map& first, boost
boost::any& firstValue = firstPosition->second.value();
const boost::any& secondValue = second[name].value();
if (firstValue.type() == typeid(Files::PathContainer))
if (firstValue.type() == typeid(Files::ReluctantPathContainer))
{
auto& firstPathContainer = boost::any_cast<Files::PathContainer&>(firstValue);
const auto& secondPathContainer = boost::any_cast<const Files::PathContainer&>(secondValue);
auto& firstPathContainer = boost::any_cast<Files::ReluctantPathContainer&>(firstValue);
const auto& secondPathContainer = boost::any_cast<const Files::ReluctantPathContainer&>(secondValue);
firstPathContainer.insert(firstPathContainer.end(), secondPathContainer.begin(), secondPathContainer.end());
}
@ -317,4 +317,22 @@ void parseConfig(std::istream& stream, boost::program_options::variables_map& va
);
}
std::istream& operator>> (std::istream& istream, ReluctantPath& reluctantPath)
{
// Read from stream using boost::filesystem::path rules, then discard anything remaining.
// This prevents boost::program_options getting upset that we've not consumed the whole stream.
istream >> static_cast<boost::filesystem::path&>(reluctantPath);
if (istream && !istream.eof() && istream.peek() != EOF)
{
std::string remainder(std::istreambuf_iterator(istream), {});
Log(Debug::Warning) << "Trailing data in path setting. Used '" << reluctantPath.string() << "' but '" << remainder << "' remained";
}
return istream;
}
PathContainer asPathContainer(const ReluctantPathContainer& reluctantPathContainer)
{
return PathContainer(reluctantPathContainer.begin(), reluctantPathContainer.end());
}
} /* namespace Cfg */

View file

@ -77,6 +77,18 @@ void parseArgs(int argc, const char* const argv[], boost::program_options::varia
void parseConfig(std::istream& stream, boost::program_options::variables_map& variables,
boost::program_options::options_description& description);
class ReluctantPath : public boost::filesystem::path
{
public:
operator boost::filesystem::path() { return *this; }
};
std::istream& operator>> (std::istream& istream, ReluctantPath& reluctantPath);
typedef std::vector<ReluctantPath> ReluctantPathContainer;
PathContainer asPathContainer(const ReluctantPathContainer& reluctantPathContainer);
} /* namespace Cfg */
#endif /* COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP */