Generic string -> bool parser.

This commit is contained in:
James Turner
2017-02-27 23:11:05 +00:00
parent 72341a6de4
commit 141e98564c
2 changed files with 21 additions and 1 deletions

View File

@@ -739,6 +739,19 @@ std::string error_string(int errnum)
#endif // !defined(_GNU_SOURCE)
}
bool to_bool(const std::string& s)
{
if (!strcasecmp(s.c_str(), "yes")) return true;
if (!strcasecmp(s.c_str(), "no")) return false;
if (!strcasecmp(s.c_str(), "true")) return true;
if (!strcasecmp(s.c_str(), "false")) return false;
if (s == "1") return true;
if (s == "0") return false;
SG_LOG(SG_GENERAL, SG_WARN, "Unable to parse string as boolean:" << s);
return false;
}
} // end namespace strutils
} // end namespace simgear

View File

@@ -168,7 +168,14 @@ namespace simgear {
* convert a string representing a decimal number, to an int
*/
int to_int(const std::string& s, int base = 10);
/**
* Convert a string representing a boolean, to a bool.
* Accepted values include YES, true, 0, 1, false, no, True,
*/
bool to_bool(const std::string& s);
/**
* Like strcmp(), but for dotted versions strings NN.NN.NN
* any number of terms are supported.