OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
validate.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_COMPONENTS_FALLBACK_VALIDATE_H
2 #define OPENMW_COMPONENTS_FALLBACK_VALIDATE_H
3 
4 #include <boost/program_options.hpp>
5 
7 
8 // Parses and validates a fallback map from boost program_options.
9 // Note: for boost to pick up the validate function, you need to pull in the namespace e.g.
10 // by using namespace Fallback;
11 
12 namespace Fallback
13 {
14 
15  struct FallbackMap {
16  std::map<std::string, std::string> mMap;
17  };
18 
19  void validate(boost::any &v, std::vector<std::string> const &tokens, FallbackMap*, int)
20  {
21  if (v.empty())
22  {
23  v = boost::any(FallbackMap());
24  }
25 
26  FallbackMap *map = boost::any_cast<FallbackMap>(&v);
27 
28  for (std::vector<std::string>::const_iterator it = tokens.begin(); it != tokens.end(); ++it)
29  {
30  std::string temp = Files::EscapeHashString::processString(*it);
31  int sep = temp.find(",");
32  if (sep < 1 || sep == (int)temp.length() - 1)
33 #if (BOOST_VERSION < 104200)
34  throw boost::program_options::validation_error("invalid value");
35 #else
36  throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
37 #endif
38 
39  std::string key(temp.substr(0, sep));
40  std::string value(temp.substr(sep + 1));
41 
42  if (map->mMap.find(key) == map->mMap.end())
43  {
44  map->mMap.insert(std::make_pair(key, value));
45  }
46  }
47  }
48 }
49 
50 #endif
void validate(boost::any &v, std::vector< std::string > const &tokens, FallbackMap *, int)
Definition: validate.hpp:19
static std::string processString(const std::string &str)
Definition: escape.cpp:28
Definition: validate.hpp:15
std::map< std::string, std::string > mMap
Definition: validate.hpp:16