Generic string -> bool parser.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user