From 81d7ece0122dd853f18b9c041ac3cf0c52ad5847 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Thu, 3 Jan 2013 22:29:43 -0500 Subject: allow fuzzy matching on devices --- pulse.cc | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'pulse.cc') diff --git a/pulse.cc b/pulse.cc index 3a248de..f6b633b 100644 --- a/pulse.cc +++ b/pulse.cc @@ -151,12 +151,23 @@ Card* PulseClient::GetCard(const uint32_t& index) { Card* PulseClient::GetCard(const string& name) { long val; + vector res; if (xstrtol(name.c_str(), &val) == 0) return GetCard(val); for (Card& card : cards_) { - if (card.name_ == name) return &card; + if (card.name_.find(name) != string::npos) res.push_back(card); } - return nullptr; + + switch (res.size()) { + case 0: + return nullptr; + case 1: + break; + default: + warnx("warning: ambiguous result for '%s', using '%s'", + name.c_str(), res[0].name_.c_str()); + } + return &res[0]; } Card* PulseClient::GetCard(const Device& device) { @@ -176,12 +187,24 @@ Device* PulseClient::get_device(vector& devices, Device* PulseClient::get_device(vector& devices, const string& name) { long val; + vector res; if (xstrtol(name.c_str(), &val) == 0) return get_device(devices, val); for (Device& device : devices) { - if (device.name_ == name) return &device; + if (device.name_.find(name) != string::npos) res.push_back(device); } - return nullptr; + + switch (res.size()) { + case 0: + return nullptr; + case 1: + break; + default: + warnx("warning: ambiguous result for '%s', using '%s'", + name.c_str(), res[0].name_.c_str()); + } + + return &res[0]; } Device* PulseClient::GetDevice(const uint32_t& index, enum DeviceType type) { -- cgit v1.2.3