linuxdcpp-team team mailing list archive
-
linuxdcpp-team team
-
Mailing list archive
-
Message #04953
[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2721: group string matching methods at the same place (used by ADLS & user matching for now)
------------------------------------------------------------
revno: 2721
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Thu 2011-12-15 18:08:18 +0100
message:
group string matching methods at the same place (used by ADLS & user matching for now)
added:
dcpp/StringMatch.cpp
dcpp/StringMatch.h
modified:
dcpp/ADLSearch.cpp
dcpp/ADLSearch.h
dcpp/UserMatch.cpp
dcpp/UserMatch.h
--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk
Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/ADLSearch.cpp'
--- dcpp/ADLSearch.cpp 2011-09-30 11:33:12 +0000
+++ dcpp/ADLSearch.cpp 2011-12-15 17:08:18 +0000
@@ -26,10 +26,8 @@
#include "ClientManager.h"
#include "File.h"
-#include "LogManager.h"
#include "QueueManager.h"
#include "SimpleXML.h"
-#include "StringTokenizer.h"
namespace dcpp {
@@ -103,47 +101,16 @@
}
bool ADLSearch::isRegEx() const {
- return boost::get<boost::regex>(&v);
+ return match.getMethod() == StringMatch::REGEX;
}
void ADLSearch::setRegEx(bool b) {
- if(b)
- v = boost::regex();
- else
- v = StringSearch::List();
+ match.setMethod(b ? StringMatch::REGEX : StringMatch::PARTIAL);
}
-struct Prepare : boost::static_visitor<> {
- Prepare(const string& s_) : s(s_) { }
-
- void operator()(StringSearch::List& stringSearches) const {
- // Prepare quick search of substrings
- stringSearches.clear();
-
- // Split into substrings
- StringTokenizer<string> st(s, ' ');
- for(auto i = st.getTokens().begin(), iend = st.getTokens().end(); i != iend; ++i) {
- if(!i->empty()) {
- // Add substring search
- stringSearches.push_back(StringSearch(*i));
- }
- }
- }
-
- void operator()(boost::regex& r) const {
- try {
- r.assign(s);
- } catch(const std::runtime_error&) {
- LogManager::getInstance()->message(str(F_("Invalid ADL Search regular expression: %1%") % s));
- }
- }
-
-private:
- const string& s;
-};
-
void ADLSearch::prepare(ParamMap& params) {
- boost::apply_visitor(Prepare(Util::formatParams(searchString, params)), v);
+ match.pattern = Util::formatParams(searchString, params);
+ match.prepare();
}
bool ADLSearch::matchesFile(const string& f, const string& fp, int64_t size) {
@@ -186,34 +153,8 @@
return searchAll(d);
}
-struct SearchAll : boost::static_visitor<bool> {
- SearchAll(const string& s_) : s(s_) { }
-
- bool operator()(StringSearch::List& stringSearches) const {
- // Match all substrings
- for(auto i = stringSearches.begin(), iend = stringSearches.end(); i != iend; ++i) {
- if(!i->match(s)) {
- return false;
- }
- }
- return !stringSearches.empty();
- }
-
- bool operator()(boost::regex& r) const {
- try {
- return !r.empty() && boost::regex_search(s, r);
- } catch(const std::runtime_error&) {
- // most likely a stack overflow, ignore...
- return false;
- }
- }
-
-private:
- const string& s;
-};
-
bool ADLSearch::searchAll(const string& s) {
- return boost::apply_visitor(SearchAll(s), v);
+ return match.match(s);
}
ADLSearchManager::ADLSearchManager() : user(UserPtr(), Util::emptyString) {
=== modified file 'dcpp/ADLSearch.h'
--- dcpp/ADLSearch.h 2011-09-30 11:33:12 +0000
+++ dcpp/ADLSearch.h 2011-12-15 17:08:18 +0000
@@ -24,16 +24,11 @@
#ifndef DCPLUSPLUS_DCPP_A_D_L_SEARCH_H
#define DCPLUSPLUS_DCPP_A_D_L_SEARCH_H
-#include <boost/regex.hpp>
-#include <boost/variant.hpp>
-
-#include "Util.h"
-
+#include "DirectoryListing.h"
#include "SettingsManager.h"
-
-#include "StringSearch.h"
#include "Singleton.h"
-#include "DirectoryListing.h"
+#include "StringMatch.h"
+#include "Util.h"
namespace dcpp {
@@ -94,7 +89,7 @@
private:
friend class ADLSearchManager;
- boost::variant<StringSearch::List, boost::regex> v;
+ StringMatch match;
/// Prepare search
void prepare(ParamMap& params);
=== added file 'dcpp/StringMatch.cpp'
--- dcpp/StringMatch.cpp 1970-01-01 00:00:00 +0000
+++ dcpp/StringMatch.cpp 2011-12-15 17:08:18 +0000
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2001-2011 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "stdinc.h"
+#include "StringMatch.h"
+
+#include "format.h"
+#include "LogManager.h"
+#include "StringTokenizer.h"
+
+namespace dcpp {
+
+StringMatch::Method StringMatch::getMethod() const {
+ return boost::get<StringSearch::List>(&search) ? PARTIAL : boost::get<string>(&search) ? EXACT : REGEX;
+}
+
+void StringMatch::setMethod(Method method) {
+ switch(method) {
+ case PARTIAL: search = StringSearch::List(); break;
+ case EXACT: search = string(); break;
+ case REGEX: search = boost::regex(); break;
+ }
+}
+
+bool StringMatch::operator==(const StringMatch& rhs) const {
+ return pattern == rhs.pattern && getMethod() == rhs.getMethod();
+}
+
+struct Prepare : boost::static_visitor<bool> {
+ Prepare(const string& pattern) : pattern(pattern) { }
+
+ bool operator()(StringSearch::List& s) const {
+ s.clear();
+ StringTokenizer<string> st(pattern, ' ');
+ for(auto i = st.getTokens().cbegin(), iend = st.getTokens().cend(); i != iend; ++i) {
+ if(!i->empty()) {
+ s.push_back(StringSearch(*i));
+ }
+ }
+ return true;
+ }
+
+ bool operator()(string& s) const {
+ s = pattern;
+ return true;
+ }
+
+ bool operator()(boost::regex& r) const {
+ try {
+ r.assign(pattern);
+ return true;
+ } catch(const std::runtime_error&) {
+ LogManager::getInstance()->message(str(F_("Invalid regular expression: %1%") % pattern));
+ return false;
+ }
+ }
+
+private:
+ const string& pattern;
+};
+
+bool StringMatch::prepare() {
+ return !pattern.empty() && boost::apply_visitor(Prepare(pattern), search);
+}
+
+struct Match : boost::static_visitor<bool> {
+ Match(const string& str) : str(str) { }
+
+ bool operator()(const StringSearch::List& s) const {
+ for(auto i = s.cbegin(), iend = s.cend(); i != iend; ++i) {
+ if(!i->match(str)) {
+ return false;
+ }
+ }
+ return !s.empty();
+ }
+
+ bool operator()(const string& s) const {
+ return str == s;
+ }
+
+ bool operator()(const boost::regex& r) const {
+ try {
+ return !r.empty() && boost::regex_search(str, r);
+ } catch(const std::runtime_error&) {
+ // most likely a stack overflow, ignore...
+ return false;
+ }
+ }
+
+private:
+ const string& str;
+};
+
+bool StringMatch::match(const string& str) const {
+ return boost::apply_visitor(Match(str), search);
+}
+
+} // namespace dcpp
=== added file 'dcpp/StringMatch.h'
--- dcpp/StringMatch.h 1970-01-01 00:00:00 +0000
+++ dcpp/StringMatch.h 2011-12-15 17:08:18 +0000
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2001-2011 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef DCPLUSPLUS_DCPP_STRING_MATCH_H
+#define DCPLUSPLUS_DCPP_STRING_MATCH_H
+
+#include "forward.h"
+#include "StringSearch.h"
+
+#include <string>
+
+#include <boost/regex.hpp>
+#include <boost/variant.hpp>
+
+namespace dcpp {
+
+using std::string;
+
+/** Provides ways of matching a pattern against strings. */
+struct StringMatch {
+ enum Method {
+ PARTIAL, /// case-insensitive pattern matching (multiple patterns separated with spaces)
+ EXACT, /// case-sensitive, character-for-character equality
+ REGEX, /// regular expression
+
+ METHOD_LAST
+ };
+
+ string pattern;
+
+ Method getMethod() const;
+ void setMethod(Method method);
+
+ bool operator==(const StringMatch& rhs) const;
+
+ bool prepare();
+ bool match(const string& str) const;
+
+private:
+ boost::variant<StringSearch::List, string, boost::regex> search;
+};
+
+} // namespace dcpp
+
+#endif
=== modified file 'dcpp/UserMatch.cpp'
--- dcpp/UserMatch.cpp 2011-12-10 18:51:47 +0000
+++ dcpp/UserMatch.cpp 2011-12-15 17:08:18 +0000
@@ -21,85 +21,12 @@
#include "Client.h"
#include "FavoriteManager.h"
-#include "format.h"
-#include "LogManager.h"
#include "OnlineUser.h"
namespace dcpp {
-UserMatch::Rule::Method UserMatch::Rule::getMethod() const {
- return boost::get<StringSearch>(&search) ? PARTIAL : boost::get<string>(&search) ? EXACT : REGEX;
-}
-
-void UserMatch::Rule::setMethod(Method method) {
- switch(method) {
- case PARTIAL: search = StringSearch(); break;
- case EXACT: search = string(); break;
- case REGEX: search = boost::regex(); break;
- }
-}
-
bool UserMatch::Rule::operator==(const Rule& rhs) const {
- return field == rhs.field && pattern == rhs.pattern && getMethod() == rhs.getMethod();
-}
-
-struct Prepare : boost::static_visitor<bool> {
- Prepare(const string& pattern) : pattern(pattern) { }
-
- bool operator()(StringSearch& s) const {
- s = pattern;
- return true;
- }
-
- bool operator()(string& s) const {
- s = pattern;
- return true;
- }
-
- bool operator()(boost::regex& r) const {
- try {
- r.assign(pattern);
- return true;
- } catch(const std::runtime_error&) {
- LogManager::getInstance()->message(str(F_("Invalid user matching regular expression: %1%") % pattern));
- return false;
- }
- }
-
-private:
- const string& pattern;
-};
-
-bool UserMatch::Rule::prepare() {
- return !pattern.empty() && boost::apply_visitor(Prepare(pattern), search);
-}
-
-struct Match : boost::static_visitor<bool> {
- Match(const string& str) : str(str) { }
-
- bool operator()(const StringSearch& s) const {
- return s.match(str);
- }
-
- bool operator()(const string& s) const {
- return str == s;
- }
-
- bool operator()(const boost::regex& r) const {
- try {
- return !r.empty() && boost::regex_search(str, r);
- } catch(const std::runtime_error&) {
- // most likely a stack overflow, ignore...
- return false;
- }
- }
-
-private:
- const string& str;
-};
-
-bool UserMatch::Rule::match(const string& str) const {
- return boost::apply_visitor(Match(str), search);
+ return field == rhs.field && StringMatch::operator==(rhs);
}
void UserMatch::addRule(Rule&& rule) {
=== modified file 'dcpp/UserMatch.h'
--- dcpp/UserMatch.h 2011-12-11 18:36:51 +0000
+++ dcpp/UserMatch.h 2011-12-15 17:08:18 +0000
@@ -21,8 +21,7 @@
#include "forward.h"
#include "Flags.h"
-#include "Pointer.h"
-#include "StringSearch.h"
+#include "StringMatch.h"
#include <string>
#include <vector>
@@ -69,7 +68,7 @@
string name;
- struct Rule {
+ struct Rule : StringMatch {
enum {
NICK,
CID,
@@ -79,26 +78,7 @@
FIELD_LAST
} field;
- string pattern;
-
- enum Method {
- PARTIAL,
- EXACT,
- REGEX,
-
- METHOD_LAST
- };
-
- Method getMethod() const;
- void setMethod(Method method);
-
bool operator==(const Rule& rhs) const;
-
- private:
- friend struct UserMatch;
- bool prepare();
- bool match(const string& str) const;
- boost::variant<StringSearch, string, boost::regex> search;
};
vector<Rule> rules;