#ifndef OPENMW_COMPONENTS_FALLBACK_VALIDATE_H #define OPENMW_COMPONENTS_FALLBACK_VALIDATE_H #include #include // Parses and validates a fallback map from boost program_options. // Note: for boost to pick up the validate function, you need to pull in the namespace e.g. // by using namespace Fallback; namespace Fallback { struct FallbackMap { std::map mMap; }; struct EscapeFallbackMap : FallbackMap { std::map mMap; FallbackMap toFallbackMap() const { FallbackMap temp = FallbackMap(); for (std::map::const_iterator it = mMap.begin(); it != mMap.end(); ++it) temp.mMap[it->first.toStdString()] = it->second.toStdString(); return temp; } }; void validate(boost::any &v, std::vector const &tokens, FallbackMap*, int) { if (v.empty()) { v = boost::any(FallbackMap()); } FallbackMap *map = boost::any_cast(&v); for (std::vector::const_iterator it = tokens.begin(); it != tokens.end(); ++it) { int sep = it->find(","); if (sep < 1 || sep == (int)it->length() - 1) #if (BOOST_VERSION < 104200) throw boost::program_options::validation_error("invalid value"); #else throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value); #endif std::string key(it->substr(0, sep)); std::string value(it->substr(sep + 1)); if (map->mMap.find(key) == map->mMap.end()) { map->mMap.insert(std::make_pair(key, value)); } } } void validate(boost::any &v, std::vector const &tokens, EscapeFallbackMap* eFM, int a) { validate(v, tokens, (FallbackMap *)eFM, a); } } namespace Files { void validate(boost::any &v, const std::vector &tokens, Files::EscapeHashString * eHS, int a) { std::string * temp = eHS->toStdStringPtr(); boost::program_options::validate(v, tokens, temp, a); delete temp; } } #endif