Change compilation error to runtime exception and decide that inheriting from std::string may have been a bad idea in C++

coverity_scan^2
AnyOldName3 9 years ago
parent 7697406467
commit fbe6dc9704

@ -69,8 +69,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
{ {
// Create a local alias for brevity // Create a local alias for brevity
namespace bpo = boost::program_options; namespace bpo = boost::program_options;
typedef Files::EscapeHashString string; typedef std::vector<Files::EscapeHashString> StringsVector;
typedef std::vector<string> StringsVector;
bpo::options_description desc("Syntax: openmw <options>\nAllowed options"); bpo::options_description desc("Syntax: openmw <options>\nAllowed options");
@ -80,16 +79,16 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
("data", bpo::value<Files::PathContainer>()->default_value(Files::PathContainer(), "data") ("data", bpo::value<Files::PathContainer>()->default_value(Files::PathContainer(), "data")
->multitoken()->composing(), "set data directories (later directories have higher priority)") ->multitoken()->composing(), "set data directories (later directories have higher priority)")
("data-local", bpo::value<string>()->default_value(""), ("data-local", bpo::value<Files::EscapeHashString>()->default_value(""),
"set local data directory (highest priority)") "set local data directory (highest priority)")
("fallback-archive", bpo::value<StringsVector>()->default_value(StringsVector(), "fallback-archive") ("fallback-archive", bpo::value<StringsVector>()->default_value(StringsVector(), "fallback-archive")
->multitoken(), "set fallback BSA archives (later archives have higher priority)") ->multitoken(), "set fallback BSA archives (later archives have higher priority)")
("resources", bpo::value<string>()->default_value("resources"), ("resources", bpo::value<Files::EscapeHashString>()->default_value("resources"),
"set resources directory") "set resources directory")
("start", bpo::value<std::string>()->default_value(""), ("start", bpo::value<Files::EscapeHashString>()->default_value(""),
"set initial cell") "set initial cell")
("content", bpo::value<StringsVector>()->default_value(StringsVector(), "") ("content", bpo::value<StringsVector>()->default_value(StringsVector(), "")
@ -110,7 +109,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
("script-console", bpo::value<bool>()->implicit_value(true) ("script-console", bpo::value<bool>()->implicit_value(true)
->default_value(false), "enable console-only script functionality") ->default_value(false), "enable console-only script functionality")
("script-run", bpo::value<string>()->default_value(""), ("script-run", bpo::value<Files::EscapeHashString>()->default_value(""),
"select a file containing a list of console commands that is executed on startup") "select a file containing a list of console commands that is executed on startup")
("script-warn", bpo::value<int>()->implicit_value (1) ("script-warn", bpo::value<int>()->implicit_value (1)
@ -126,7 +125,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
("script-blacklist-use", bpo::value<bool>()->implicit_value(true) ("script-blacklist-use", bpo::value<bool>()->implicit_value(true)
->default_value(true), "enable script blacklisting") ->default_value(true), "enable script blacklisting")
("load-savegame", bpo::value<string>()->default_value(""), ("load-savegame", bpo::value<Files::EscapeHashString>()->default_value(""),
"load a save game file on game startup (specify an absolute filename or a filename relative to the current working directory)") "load a save game file on game startup (specify an absolute filename or a filename relative to the current working directory)")
("skip-menu", bpo::value<bool>()->implicit_value(true) ("skip-menu", bpo::value<bool>()->implicit_value(true)
@ -138,7 +137,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
("fs-strict", bpo::value<bool>()->implicit_value(true) ("fs-strict", bpo::value<bool>()->implicit_value(true)
->default_value(false), "strict file system handling (no case folding)") ->default_value(false), "strict file system handling (no case folding)")
( "encoding", bpo::value<string>()-> ("encoding", bpo::value<Files::EscapeHashString>()->
default_value("win1252"), default_value("win1252"),
"Character encoding used in OpenMW game messages:\n" "Character encoding used in OpenMW game messages:\n"
"\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages\n" "\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages\n"
@ -242,7 +241,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
engine.setScriptConsoleMode (variables["script-console"].as<bool>()); engine.setScriptConsoleMode (variables["script-console"].as<bool>());
engine.setStartupScript (variables["script-run"].as<std::string>()); engine.setStartupScript (variables["script-run"].as<std::string>());
engine.setWarningsMode (variables["script-warn"].as<int>()); engine.setWarningsMode (variables["script-warn"].as<int>());
engine.setScriptBlacklist (string::toStdStringVector(variables["script-blacklist"].as<StringsVector>())); engine.setScriptBlacklist (Files::EscapeHashString::toStdStringVector(variables["script-blacklist"].as<StringsVector>()));
engine.setScriptBlacklistUse (variables["script-blacklist-use"].as<bool>()); engine.setScriptBlacklistUse (variables["script-blacklist-use"].as<bool>());
engine.setSaveGameFile (variables["load-savegame"].as<std::string>()); engine.setSaveGameFile (variables["load-savegame"].as<std::string>());

@ -12,11 +12,11 @@
namespace Fallback namespace Fallback
{ {
struct FallbackMap { struct FallbackMap {
std::map<std::string, std::string> mMap; std::map<std::string, std::string> mMap;
}; };
struct EscapeFallbackMap : FallbackMap struct EscapeFallbackMap : FallbackMap
{ {
std::map<Files::EscapeHashString, Files::EscapeHashString> mMap; std::map<Files::EscapeHashString, Files::EscapeHashString> mMap;
@ -29,34 +29,34 @@ namespace Fallback
} }
}; };
void validate(boost::any &v, std::vector<std::string> const &tokens, FallbackMap*, int) void validate(boost::any &v, std::vector<std::string> const &tokens, FallbackMap*, int)
{ {
if(v.empty()) if (v.empty())
{ {
v = boost::any(FallbackMap()); v = boost::any(FallbackMap());
} }
FallbackMap *map = boost::any_cast<FallbackMap>(&v); FallbackMap *map = boost::any_cast<FallbackMap>(&v);
for(std::vector<std::string>::const_iterator it=tokens.begin(); it != tokens.end(); ++it) for (std::vector<std::string>::const_iterator it = tokens.begin(); it != tokens.end(); ++it)
{ {
int sep = it->find(","); int sep = it->find(",");
if(sep < 1 || sep == (int)it->length()-1) if (sep < 1 || sep == (int)it->length() - 1)
#if (BOOST_VERSION < 104200) #if (BOOST_VERSION < 104200)
throw boost::program_options::validation_error("invalid value"); throw boost::program_options::validation_error("invalid value");
#else #else
throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value); throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
#endif #endif
std::string key(it->substr(0,sep)); std::string key(it->substr(0, sep));
std::string value(it->substr(sep+1)); std::string value(it->substr(sep + 1));
if(map->mMap.find(key) == map->mMap.end()) if (map->mMap.find(key) == map->mMap.end())
{ {
map->mMap.insert(std::make_pair (key,value)); map->mMap.insert(std::make_pair(key, value));
} }
} }
} }
void validate(boost::any &v, std::vector<std::string> const &tokens, EscapeFallbackMap* eFM, int a) void validate(boost::any &v, std::vector<std::string> const &tokens, EscapeFallbackMap* eFM, int a)
{ {
@ -64,4 +64,13 @@ namespace Fallback
} }
} }
namespace Files {
void validate(boost::any &v, const std::vector<std::string> &tokens, Files::EscapeHashString * eHS, int a)
{
std::string * temp = eHS->toStdStringPtr();
boost::program_options::validate(v, tokens, temp, a);
delete temp;
}
}
#endif #endif

@ -285,6 +285,12 @@ std::string EscapeHashString::toStdString() const
return std::string(* this); return std::string(* this);
} }
std::string * EscapeHashString::toStdStringPtr() const
{
std::string * ret = new std::string(*this);
return ret;
}
const boost::filesystem::path& ConfigurationManager::getGlobalPath() const const boost::filesystem::path& ConfigurationManager::getGlobalPath() const
{ {
return mFixedPath.getGlobalConfigPath(); return mFixedPath.getGlobalConfigPath();

@ -107,6 +107,7 @@ class EscapeHashString : public std::string
EscapeHashString(InputIterator first, InputIterator last); EscapeHashString(InputIterator first, InputIterator last);
std::string toStdString() const; std::string toStdString() const;
std::string * toStdStringPtr() const;
}; };
} /* namespace Cfg */ } /* namespace Cfg */

Loading…
Cancel
Save