aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2010-09-10 14:35:49 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2010-09-10 14:35:49 (GMT)
commita6ed9498cc0e31b0134ecd47de7d5383a062e535 (patch)
treee115f083f1b94f4933e27270dbc76e604258d48f
parent882a50fe1d4930b156965c54d9b66ecb27b4c9b2 (diff)
downloadfluxbox-a6ed9498cc0e31b0134ecd47de7d5383a062e535.zip
fluxbox-a6ed9498cc0e31b0134ecd47de7d5383a062e535.tar.bz2
code cleanup
* moved code from public API to internals * avoid code duplication ( while(!m_terms.empty()) ...) * cosmetic '(*it)->' vs 'term.'
-rw-r--r--src/ClientPattern.cc82
-rw-r--r--src/ClientPattern.hh30
2 files changed, 57 insertions, 55 deletions
diff --git a/src/ClientPattern.cc b/src/ClientPattern.cc
index 3305d97..7e7e715 100644
--- a/src/ClientPattern.cc
+++ b/src/ClientPattern.cc
@@ -32,6 +32,7 @@
32#include "FbTk/StringUtil.hh" 32#include "FbTk/StringUtil.hh"
33#include "FbTk/App.hh" 33#include "FbTk/App.hh"
34#include "FbTk/stringstream.hh" 34#include "FbTk/stringstream.hh"
35#include "FbTk/STLUtil.hh"
35 36
36// use GNU extensions 37// use GNU extensions
37#ifndef _GNU_SOURCE 38#ifndef _GNU_SOURCE
@@ -120,6 +121,28 @@ Prop2String property_2_strings[] = { // sorted by 'prop'
120} // end of anonymous namespace 121} // end of anonymous namespace
121 122
122 123
124/**
125 * This is the type of the actual pattern we want to match against
126 * We have a "term" in the whole expression which is the full pattern
127 * we also need to keep track of the uncompiled regular expression
128 * for final output
129 */
130struct ClientPattern::Term {
131
132 Term(const FbTk::FbString& _regstr, WinProperty _prop, bool _negate) :
133 orig(_regstr),
134 regexp(_regstr, true),
135 prop(_prop),
136 negate(_negate) {
137
138 }
139
140 FbTk::FbString orig;
141 FbTk::RegExp regexp;
142 WinProperty prop;
143 bool negate;
144};
145
123ClientPattern::ClientPattern(): 146ClientPattern::ClientPattern():
124 m_matchlimit(0), 147 m_matchlimit(0),
125 m_nummatches(0) {} 148 m_nummatches(0) {}
@@ -202,8 +225,7 @@ ClientPattern::ClientPattern(const char *str):
202 str+pos, 225 str+pos,
203 '{', '}'); 226 '{', '}');
204 if (err > 0) { 227 if (err > 0) {
205 FbTk_istringstream iss(number.c_str()); 228 FbTk::StringUtil::extractNumber(number, m_matchlimit);
206 iss >> m_matchlimit;
207 pos+=err; 229 pos+=err;
208 } 230 }
209 // we don't care if there isn't one 231 // we don't care if there isn't one
@@ -219,21 +241,12 @@ ClientPattern::ClientPattern(const char *str):
219 } 241 }
220 242
221 if (had_error) { 243 if (had_error) {
222 // delete all the terms 244 FbTk::STLUtil::destroyAndClear(m_terms);
223 while (!m_terms.empty()) {
224 Term * term = m_terms.back();
225 delete term;
226 m_terms.pop_back();
227 }
228 } 245 }
229} 246}
230 247
231ClientPattern::~ClientPattern() { 248ClientPattern::~ClientPattern() {
232 // delete all the terms 249 FbTk::STLUtil::destroyAndClear(m_terms);
233 while (!m_terms.empty()) {
234 delete m_terms.back();
235 m_terms.pop_back();
236 }
237} 250}
238 251
239// return a string representation of this pattern 252// return a string representation of this pattern
@@ -268,25 +281,23 @@ bool ClientPattern::match(const Focusable &win) const {
268 Terms::const_iterator it = m_terms.begin(); 281 Terms::const_iterator it = m_terms.begin();
269 Terms::const_iterator it_end = m_terms.end(); 282 Terms::const_iterator it_end = m_terms.end();
270 for (; it != it_end; ++it) { 283 for (; it != it_end; ++it) {
271 if ((*it)->orig == "[current]") { 284 const Term& term = *(*it);
285 if (term.orig == "[current]") {
272 WinClient *focused = FocusControl::focusedWindow(); 286 WinClient *focused = FocusControl::focusedWindow();
273 if ((*it)->prop == WORKSPACE) { 287 if (term.prop == WORKSPACE) {
274 if (!(*it)->negate ^ (getProperty((*it)->prop, win) == FbTk::StringUtil::number2String(win.screen().currentWorkspaceID()))) 288 if (!term.negate ^ (getProperty(term.prop, win) == FbTk::StringUtil::number2String(win.screen().currentWorkspaceID())))
275 return false; 289 return false;
276 } else if ((*it)->prop == WORKSPACENAME) { 290 } else if (term.prop == WORKSPACENAME) {
277 const Workspace *w = win.screen().currentWorkspace(); 291 const Workspace *w = win.screen().currentWorkspace();
278 if (!w || (!(*it)->negate ^ 292 if (!w || (!term.negate ^ (getProperty(term.prop, win) == w->name())))
279 (getProperty((*it)->prop, win) == w->name())))
280 return false; 293 return false;
281 } else if (!focused || (!(*it)->negate ^ 294 } else if (!focused || (!term.negate ^ (getProperty(term.prop, win) == getProperty(term.prop, *focused))))
282 (getProperty((*it)->prop, win) ==
283 getProperty((*it)->prop, *focused))))
284 return false; 295 return false;
285 } else if ((*it)->prop == HEAD && (*it)->orig == "[mouse]") { 296 } else if (term.prop == HEAD && term.orig == "[mouse]") {
286 if (!(*it)->negate ^ (getProperty((*it)->prop, win) == FbTk::StringUtil::number2String(win.screen().getCurrHead()))) 297 if (!term.negate ^ (getProperty(term.prop, win) == FbTk::StringUtil::number2String(win.screen().getCurrHead())))
287 return false; 298 return false;
288 299
289 } else if (!(*it)->negate ^ (*it)->regexp.match(getProperty((*it)->prop, win))) 300 } else if (!term.negate ^ term.regexp.match(getProperty(term.prop, win)))
290 return false; 301 return false;
291 } 302 }
292 return true; 303 return true;
@@ -315,19 +326,22 @@ bool ClientPattern::dependsOnCurrentWorkspace() const {
315// add an expression to match against 326// add an expression to match against
316// The first argument is a regular expression, the second is the member 327// The first argument is a regular expression, the second is the member
317// function that we wish to match against. 328// function that we wish to match against.
318bool ClientPattern::addTerm(const string &str, WinProperty prop, bool negate) { 329bool ClientPattern::addTerm(const FbTk::FbString &str, WinProperty prop, bool negate) {
330
331 bool rc = false;
332 Term* term = new Term(str, prop, negate);
319 333
320 Term *term = new Term(str, true); 334 if (!term)
321 term->orig = str; 335 return rc;
322 term->prop = prop;
323 term->negate = negate;
324 336
325 if (term->regexp.error()) { 337 if (!term->regexp.error()) {
338 m_terms.push_back(term);
339 rc = true;
340 } else {
326 delete term; 341 delete term;
327 return false;
328 } 342 }
329 m_terms.push_back(term); 343
330 return true; 344 return rc;
331} 345}
332 346
333FbTk::FbString ClientPattern::getProperty(WinProperty prop, const Focusable &client) { 347FbTk::FbString ClientPattern::getProperty(WinProperty prop, const Focusable &client) {
diff --git a/src/ClientPattern.hh b/src/ClientPattern.hh
index 9eb7653..6aa2e11 100644
--- a/src/ClientPattern.hh
+++ b/src/ClientPattern.hh
@@ -26,6 +26,7 @@
26 26
27#include "FbTk/RegExp.hh" 27#include "FbTk/RegExp.hh"
28#include "FbTk/NotCopyable.hh" 28#include "FbTk/NotCopyable.hh"
29#include "FbTk/FbString.hh"
29 30
30#include <list> 31#include <list>
31 32
@@ -48,7 +49,7 @@ public:
48 ~ClientPattern(); 49 ~ClientPattern();
49 50
50 /// @return a string representation of this pattern 51 /// @return a string representation of this pattern
51 std::string toString() const; 52 FbTk::FbString toString() const;
52 53
53 enum WinProperty { 54 enum WinProperty {
54 TITLE = 0, CLASS, NAME, ROLE, TRANSIENT, 55 TITLE = 0, CLASS, NAME, ROLE, TRANSIENT,
@@ -71,7 +72,7 @@ public:
71 * @param prop is the member function that we wish to match against 72 * @param prop is the member function that we wish to match against
72 * @return false if the regexp wasn't valid 73 * @return false if the regexp wasn't valid
73 */ 74 */
74 bool addTerm(const std::string &str, WinProperty prop, bool negate = false); 75 bool addTerm(const FbTk::FbString &str, WinProperty prop, bool negate = false);
75 76
76 void addMatch() { ++m_nummatches; } 77 void addMatch() { ++m_nummatches; }
77 void removeMatch() { --m_nummatches; } 78 void removeMatch() { --m_nummatches; }
@@ -86,29 +87,16 @@ public:
86 */ 87 */
87 int error() const { return m_terms.empty() ? 1 : 0; } 88 int error() const { return m_terms.empty() ? 1 : 0; }
88 89
89 static std::string getProperty(WinProperty prop, const Focusable &client); 90 static FbTk::FbString getProperty(WinProperty prop, const Focusable &client);
90 91
91private: 92private:
92 /** 93 struct Term;
93 * This is the type of the actual pattern we want to match against 94 friend struct Term;
94 * We have a "term" in the whole expression which is the full pattern
95 * we also need to keep track of the uncompiled regular expression
96 * for final output
97 */
98 struct Term {
99 Term(const std::string &regstr, bool full_match) :regexp(regstr, full_match){};
100 std::string orig;
101 FbTk::RegExp regexp;
102 WinProperty prop;
103 bool negate;
104 };
105
106
107 typedef std::list<Term *> Terms; 95 typedef std::list<Term *> Terms;
108 96
109 Terms m_terms; ///< our pattern is made up of a sequence of terms currently we "and" them all 97 Terms m_terms; ///< our pattern is made up of a sequence of terms, currently we "and" them all
110 98 int m_matchlimit;
111 int m_matchlimit, m_nummatches; 99 int m_nummatches;
112}; 100};
113 101
114#endif // CLIENTPATTERN_HH 102#endif // CLIENTPATTERN_HH