]> sigrok.org Git - pulseview.git/commitdiff
Factored device title printing code out into pv::DeviceManager::format_device_title
authorJoel Holdsworth <redacted>
Mon, 29 Apr 2013 08:14:46 +0000 (09:14 +0100)
committerJoel Holdsworth <redacted>
Mon, 29 Apr 2013 08:27:47 +0000 (09:27 +0100)
This fixes the earlier bug where pv::DeviceManager::compare_devices
would crash if any of the device string fields were NULL.

pv/devicemanager.cpp
pv/devicemanager.h
pv/dialogs/connect.cpp
pv/toolbars/samplingbar.cpp

index 4018c0470ee031ce17f3d37664c115bb79865992..5a72b31b78563c73e3053e107d14b112c9e040b0 100644 (file)
@@ -21,7 +21,7 @@
 #include "devicemanager.h"
 
 #include <cassert>
-#include <cstring>
+#include <sstream>
 #include <stdexcept>
 #include <string>
 
@@ -83,6 +83,31 @@ list<sr_dev_inst*> DeviceManager::driver_scan(
        return driver_devices;
 }
 
+string DeviceManager::format_device_title(const sr_dev_inst *const sdi)
+{
+       ostringstream s;
+
+       assert(sdi);
+
+       if (sdi->vendor && sdi->vendor[0]) {
+               s << sdi->vendor;
+               if ((sdi->model && sdi->model[0]) ||
+                       (sdi->version && sdi->version[0]))
+                       s << ' ';
+       }
+
+       if (sdi->model && sdi->model[0]) {
+               s << sdi->model;
+               if (sdi->version && sdi->version[0])
+                       s << ' ';
+       }
+
+       if (sdi->version && sdi->version[0])
+               s << sdi->version;
+
+       return s.str();
+}
+
 void DeviceManager::init_drivers()
 {
        // Initialise all libsigrok drivers
@@ -114,22 +139,7 @@ void DeviceManager::scan_all_drivers()
 bool DeviceManager::compare_devices(const sr_dev_inst *const a,
        const sr_dev_inst *const b)
 {
-       assert(a);
-       assert(b);
-
-       const int vendor_cmp = strcasecmp(a->vendor, b->vendor);
-       if(vendor_cmp < 0)
-               return true;
-       else if(vendor_cmp > 0)
-               return false;
-
-       const int model_cmp = strcasecmp(a->model, b->model);
-       if(model_cmp < 0)
-               return true;
-       else if(model_cmp > 0)
-               return false;
-
-       return strcasecmp(a->version, b->version) < 0;
+       return format_device_title(a).compare(format_device_title(b)) < 0;
 }
 
 } // namespace pv
index 070c3cc7d94ef202e52968333d3ec608589768bc..f4be73b9274565e93832becba4e01985a54dece0 100644 (file)
@@ -24,6 +24,7 @@
 #include <glib.h>
 
 #include <list>
+#include <string>
 
 struct sr_context;
 struct sr_dev_driver;
@@ -44,6 +45,8 @@ public:
                struct sr_dev_driver *const driver,
                GSList *const drvopts = NULL);
 
+       static std::string format_device_title(const sr_dev_inst *const sdi);
+
 private:
        void init_drivers();
 
index eefd41b7db3e3af982e619c46062ade3523b3e8b..ea411f156cf33b52f4f86ed61db2ed017a7418f4 100644 (file)
@@ -160,17 +160,12 @@ void Connect::scan_pressed()
 
        g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
 
-       BOOST_FOREACH(sr_dev_inst *const sdi, devices) {
-
-               QString text;
-               if (sdi->vendor && sdi->vendor[0])
-                       text += QString("%1 ").arg(sdi->vendor);
-               if (sdi->model && sdi->model[0])
-                       text += QString("%1 ").arg(sdi->model);
-               if (sdi->version && sdi->version[0])
-                       text += QString("%1 ").arg(sdi->version);
+       BOOST_FOREACH(sr_dev_inst *const sdi, devices)
+       {
+               const string title = DeviceManager::format_device_title(sdi);
+               QString text(title.c_str());
                if (sdi->probes) {
-                       text += QString("with %1 probes").arg(
+                       text += QString(" with %1 probes").arg(
                                g_slist_length(sdi->probes));
                }
 
index 90dc6575896b16d9aa2effaf1dc5f763401362fe..0754f001e9a1828b3cd2fa66e7d8d9ee497da330 100644 (file)
 
 #include "samplingbar.h"
 
+#include <pv/devicemanager.h>
 #include <pv/dialogs/deviceoptions.h>
 
+using namespace std;
+
 namespace pv {
 namespace toolbars {
 
@@ -119,16 +122,9 @@ void SamplingBar::set_device_list(
        _device_selector.clear();
 
        BOOST_FOREACH (sr_dev_inst *sdi, devices) {
-               QString title;
-               if (sdi->vendor && sdi->vendor[0])
-                       title += sdi->vendor + QString(" ");
-               if (sdi->model && sdi->model[0])
-                       title += sdi->model + QString(" ");
-               if (sdi->version && sdi->version[0])
-                       title += sdi->version + QString(" ");
-
-               _device_selector.addItem(title, qVariantFromValue(
-                       (void*)sdi));
+               const string title = DeviceManager::format_device_title(sdi);
+               _device_selector.addItem(title.c_str(),
+                       qVariantFromValue((void*)sdi));
        }
 
        update_sample_rate_selector();