summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Müller <mmueller@gnuradio.org>2021-06-08 23:15:41 +0200
committerJeff Long <willcode4@gmail.com>2021-06-10 20:48:06 -0400
commit16d1b0215d59c52009b09867fbfce0b4c3585add (patch)
treeab325d34bff825d531c0f84ddf4b74c87a6b649d
parent0705a715dea306c4d53301095f93b0f7077c4a61 (diff)
soapy: less surprising bool cast
Soapy has its own way of casting strings to bools, and it special-cases "0" and "0.0" and misses out on the opportunity to be consistent with many other ways of specifying 0 as number. I'm trying to upstream the same change to SoapSDR, in order to keep things consistent. But this is worth having in GNU Radio, either way, since GNU Radio has its own user expectations, which might even differ a bit from these of SoapySDR users. Signed-off-by: Marcus Müller <mmueller@gnuradio.org>
-rw-r--r--gr-soapy/python/soapy/bindings/soapy_common.cc24
1 files changed, 12 insertions, 12 deletions
diff --git a/gr-soapy/python/soapy/bindings/soapy_common.cc b/gr-soapy/python/soapy/bindings/soapy_common.cc
index 98d59be032..911ec7209e 100644
--- a/gr-soapy/python/soapy/bindings/soapy_common.cc
+++ b/gr-soapy/python/soapy/bindings/soapy_common.cc
@@ -11,6 +11,8 @@
#include <SoapySDR/Types.hpp>
#include <SoapySDR/Version.h>
+#include <stdexcept>
+#include <string>
// SoapySDR doesn't have an API define for SettingToString, so we need
// to check the version. 0.8 is the first tagged version to have this
@@ -49,22 +51,20 @@ static inline T string_to_setting(const std::string& str)
}
// Copied from SoapySDR 0.8
+//! convert empty and "false" strings to false, integers to their truthness
template <>
inline bool string_to_setting<bool>(const std::string& str)
{
- if (str == SOAPY_SDR_TRUE)
- return true;
- if (str == SOAPY_SDR_FALSE)
- return false;
-
- // zeros and empty strings are false
- if (str == "0")
- return false;
- if (str == "0.0")
- return false;
- if (str == "")
+ if (str.empty() || str == SOAPY_SDR_FALSE) {
return false;
-
+ }
+ if (str == SOAPY_SDR_TRUE) {
+ return true;
+ }
+ try {
+ return static_cast<bool>(std::stod(str));
+ } catch (std::invalid_argument& e) {
+ }
// other values are true
return true;
}