aboutsummaryrefslogtreecommitdiff
path: root/src/ClientPattern.cc
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 /src/ClientPattern.cc
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.'
Diffstat (limited to 'src/ClientPattern.cc')
-rw-r--r--src/ClientPattern.cc82
1 files changed, 48 insertions, 34 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) {