]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/ConfigKey_methods.cpp
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / bindings / cxx / ConfigKey_methods.cpp
index 7381c922f3203e58a97e83d6d5a48bab3e4beae1..d899ddcc2a18d2053470a346051e6f4e5ccca27f 100644 (file)
@@ -100,6 +100,20 @@ static inline unsigned long stoul(const std::string &str)
 }
 #endif
 
+// Conversion from text to uint32_t, including a range check.
+// This is sigrok specific, _not_ part of any C++ standard library.
+static uint32_t stou32(const std::string &str)
+{
+       unsigned long ret;
+       errno = 0;
+       ret = stoul(str);
+       if (errno == ERANGE)
+               throw std::out_of_range("stou32");
+       if (ret > std::numeric_limits<uint32_t>::max())
+               throw std::out_of_range("stou32");
+       return ret;
+}
+
 Glib::VariantBase ConfigKey::parse_string(std::string value, enum sr_datatype dt)
 {
        GVariant *variant;
@@ -139,6 +153,13 @@ Glib::VariantBase ConfigKey::parse_string(std::string value, enum sr_datatype dt
                                throw Error(SR_ERR_ARG);
                        }
                        break;
+               case SR_T_UINT32:
+                       try {
+                               variant = g_variant_new_uint32(stou32(value));
+                       } catch (invalid_argument&) {
+                               throw Error(SR_ERR_ARG);
+                       }
+                       break;
                default:
                        throw Error(SR_ERR_BUG);
        }