code cleanup

* moved code from public API to internals
* avoid code duplication ( while(!m_terms.empty()) ...)
* cosmetic '(*it)->' vs 'term.'
This commit is contained in:
Mathias Gumz 2010-09-10 16:35:49 +02:00
parent 882a50fe1d
commit a6ed9498cc
2 changed files with 57 additions and 55 deletions

View file

@ -32,6 +32,7 @@
#include "FbTk/StringUtil.hh" #include "FbTk/StringUtil.hh"
#include "FbTk/App.hh" #include "FbTk/App.hh"
#include "FbTk/stringstream.hh" #include "FbTk/stringstream.hh"
#include "FbTk/STLUtil.hh"
// use GNU extensions // use GNU extensions
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
@ -120,6 +121,28 @@ Prop2String property_2_strings[] = { // sorted by 'prop'
} // end of anonymous namespace } // end of anonymous namespace
/**
* This is the type of the actual pattern we want to match against
* We have a "term" in the whole expression which is the full pattern
* we also need to keep track of the uncompiled regular expression
* for final output
*/
struct ClientPattern::Term {
Term(const FbTk::FbString& _regstr, WinProperty _prop, bool _negate) :
orig(_regstr),
regexp(_regstr, true),
prop(_prop),
negate(_negate) {
}
FbTk::FbString orig;
FbTk::RegExp regexp;
WinProperty prop;
bool negate;
};
ClientPattern::ClientPattern(): ClientPattern::ClientPattern():
m_matchlimit(0), m_matchlimit(0),
m_nummatches(0) {} m_nummatches(0) {}
@ -202,8 +225,7 @@ ClientPattern::ClientPattern(const char *str):
str+pos, str+pos,
'{', '}'); '{', '}');
if (err > 0) { if (err > 0) {
FbTk_istringstream iss(number.c_str()); FbTk::StringUtil::extractNumber(number, m_matchlimit);
iss >> m_matchlimit;
pos+=err; pos+=err;
} }
// we don't care if there isn't one // we don't care if there isn't one
@ -219,21 +241,12 @@ ClientPattern::ClientPattern(const char *str):
} }
if (had_error) { if (had_error) {
// delete all the terms FbTk::STLUtil::destroyAndClear(m_terms);
while (!m_terms.empty()) {
Term * term = m_terms.back();
delete term;
m_terms.pop_back();
}
} }
} }
ClientPattern::~ClientPattern() { ClientPattern::~ClientPattern() {
// delete all the terms FbTk::STLUtil::destroyAndClear(m_terms);
while (!m_terms.empty()) {
delete m_terms.back();
m_terms.pop_back();
}
} }
// return a string representation of this pattern // return a string representation of this pattern
@ -268,25 +281,23 @@ bool ClientPattern::match(const Focusable &win) const {
Terms::const_iterator it = m_terms.begin(); Terms::const_iterator it = m_terms.begin();
Terms::const_iterator it_end = m_terms.end(); Terms::const_iterator it_end = m_terms.end();
for (; it != it_end; ++it) { for (; it != it_end; ++it) {
if ((*it)->orig == "[current]") { const Term& term = *(*it);
if (term.orig == "[current]") {
WinClient *focused = FocusControl::focusedWindow(); WinClient *focused = FocusControl::focusedWindow();
if ((*it)->prop == WORKSPACE) { if (term.prop == WORKSPACE) {
if (!(*it)->negate ^ (getProperty((*it)->prop, win) == FbTk::StringUtil::number2String(win.screen().currentWorkspaceID()))) if (!term.negate ^ (getProperty(term.prop, win) == FbTk::StringUtil::number2String(win.screen().currentWorkspaceID())))
return false; return false;
} else if ((*it)->prop == WORKSPACENAME) { } else if (term.prop == WORKSPACENAME) {
const Workspace *w = win.screen().currentWorkspace(); const Workspace *w = win.screen().currentWorkspace();
if (!w || (!(*it)->negate ^ if (!w || (!term.negate ^ (getProperty(term.prop, win) == w->name())))
(getProperty((*it)->prop, win) == w->name())))
return false; return false;
} else if (!focused || (!(*it)->negate ^ } else if (!focused || (!term.negate ^ (getProperty(term.prop, win) == getProperty(term.prop, *focused))))
(getProperty((*it)->prop, win) ==
getProperty((*it)->prop, *focused))))
return false; return false;
} else if ((*it)->prop == HEAD && (*it)->orig == "[mouse]") { } else if (term.prop == HEAD && term.orig == "[mouse]") {
if (!(*it)->negate ^ (getProperty((*it)->prop, win) == FbTk::StringUtil::number2String(win.screen().getCurrHead()))) if (!term.negate ^ (getProperty(term.prop, win) == FbTk::StringUtil::number2String(win.screen().getCurrHead())))
return false; return false;
} else if (!(*it)->negate ^ (*it)->regexp.match(getProperty((*it)->prop, win))) } else if (!term.negate ^ term.regexp.match(getProperty(term.prop, win)))
return false; return false;
} }
return true; return true;
@ -315,19 +326,22 @@ bool ClientPattern::dependsOnCurrentWorkspace() const {
// add an expression to match against // add an expression to match against
// The first argument is a regular expression, the second is the member // The first argument is a regular expression, the second is the member
// function that we wish to match against. // function that we wish to match against.
bool ClientPattern::addTerm(const string &str, WinProperty prop, bool negate) { bool ClientPattern::addTerm(const FbTk::FbString &str, WinProperty prop, bool negate) {
Term *term = new Term(str, true); bool rc = false;
term->orig = str; Term* term = new Term(str, prop, negate);
term->prop = prop;
term->negate = negate;
if (term->regexp.error()) { if (!term)
return rc;
if (!term->regexp.error()) {
m_terms.push_back(term);
rc = true;
} else {
delete term; delete term;
return false;
} }
m_terms.push_back(term);
return true; return rc;
} }
FbTk::FbString ClientPattern::getProperty(WinProperty prop, const Focusable &client) { FbTk::FbString ClientPattern::getProperty(WinProperty prop, const Focusable &client) {

View file

@ -26,6 +26,7 @@
#include "FbTk/RegExp.hh" #include "FbTk/RegExp.hh"
#include "FbTk/NotCopyable.hh" #include "FbTk/NotCopyable.hh"
#include "FbTk/FbString.hh"
#include <list> #include <list>
@ -48,7 +49,7 @@ public:
~ClientPattern(); ~ClientPattern();
/// @return a string representation of this pattern /// @return a string representation of this pattern
std::string toString() const; FbTk::FbString toString() const;
enum WinProperty { enum WinProperty {
TITLE = 0, CLASS, NAME, ROLE, TRANSIENT, TITLE = 0, CLASS, NAME, ROLE, TRANSIENT,
@ -71,7 +72,7 @@ public:
* @param prop is the member function that we wish to match against * @param prop is the member function that we wish to match against
* @return false if the regexp wasn't valid * @return false if the regexp wasn't valid
*/ */
bool addTerm(const std::string &str, WinProperty prop, bool negate = false); bool addTerm(const FbTk::FbString &str, WinProperty prop, bool negate = false);
void addMatch() { ++m_nummatches; } void addMatch() { ++m_nummatches; }
void removeMatch() { --m_nummatches; } void removeMatch() { --m_nummatches; }
@ -86,29 +87,16 @@ public:
*/ */
int error() const { return m_terms.empty() ? 1 : 0; } int error() const { return m_terms.empty() ? 1 : 0; }
static std::string getProperty(WinProperty prop, const Focusable &client); static FbTk::FbString getProperty(WinProperty prop, const Focusable &client);
private: private:
/** struct Term;
* This is the type of the actual pattern we want to match against friend struct Term;
* We have a "term" in the whole expression which is the full pattern
* we also need to keep track of the uncompiled regular expression
* for final output
*/
struct Term {
Term(const std::string &regstr, bool full_match) :regexp(regstr, full_match){};
std::string orig;
FbTk::RegExp regexp;
WinProperty prop;
bool negate;
};
typedef std::list<Term *> Terms; typedef std::list<Term *> Terms;
Terms m_terms; ///< our pattern is made up of a sequence of terms currently we "and" them all Terms m_terms; ///< our pattern is made up of a sequence of terms, currently we "and" them all
int m_matchlimit;
int m_matchlimit, m_nummatches; int m_nummatches;
}; };
#endif // CLIENTPATTERN_HH #endif // CLIENTPATTERN_HH