diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index ebe83e38..e2bc3034 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -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 diff --git a/simgear/misc/strutils.hxx b/simgear/misc/strutils.hxx index 3ab3001f..3844e7f9 100644 --- a/simgear/misc/strutils.hxx +++ b/simgear/misc/strutils.hxx @@ -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.