From: Joel Holdsworth Date: Mon, 29 Apr 2013 08:14:46 +0000 (+0100) Subject: Factored device title printing code out into pv::DeviceManager::format_device_title X-Git-Tag: pulseview-0.1.0~12 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=dd63af740355865cc0659dfba74ecf4445414bc8;p=pulseview.git Factored device title printing code out into pv::DeviceManager::format_device_title This fixes the earlier bug where pv::DeviceManager::compare_devices would crash if any of the device string fields were NULL. --- diff --git a/pv/devicemanager.cpp b/pv/devicemanager.cpp index 4018c047..5a72b31b 100644 --- a/pv/devicemanager.cpp +++ b/pv/devicemanager.cpp @@ -21,7 +21,7 @@ #include "devicemanager.h" #include -#include +#include #include #include @@ -83,6 +83,31 @@ list 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 diff --git a/pv/devicemanager.h b/pv/devicemanager.h index 070c3cc7..f4be73b9 100644 --- a/pv/devicemanager.h +++ b/pv/devicemanager.h @@ -24,6 +24,7 @@ #include #include +#include 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(); diff --git a/pv/dialogs/connect.cpp b/pv/dialogs/connect.cpp index eefd41b7..ea411f15 100644 --- a/pv/dialogs/connect.cpp +++ b/pv/dialogs/connect.cpp @@ -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)); } diff --git a/pv/toolbars/samplingbar.cpp b/pv/toolbars/samplingbar.cpp index 90dc6575..0754f001 100644 --- a/pv/toolbars/samplingbar.cpp +++ b/pv/toolbars/samplingbar.cpp @@ -31,8 +31,11 @@ #include "samplingbar.h" +#include #include +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();