From c4c96ec830f7e22c5f5183886de1f399333f60cd Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Thu, 1 Oct 2009 08:57:31 +0200 Subject: simpler code --- src/ClientPattern.cc | 64 +++++++++++++++++++++----------------------------- src/FbCommands.cc | 5 +--- src/FbTk/StringUtil.cc | 7 ++++++ src/FbTk/StringUtil.hh | 2 ++ src/Layer.hh | 50 ++++++++++++++++----------------------- src/Screen.cc | 4 +--- src/SystemTray.cc | 8 ++----- 7 files changed, 60 insertions(+), 80 deletions(-) diff --git a/src/ClientPattern.cc b/src/ClientPattern.cc index 84f421a..41080fa 100644 --- a/src/ClientPattern.cc +++ b/src/ClientPattern.cc @@ -102,39 +102,42 @@ ClientPattern::ClientPattern(const char *str): negate = true; memstr.assign(memstr, 0, memstr.length()-1); } - if (strcasecmp(memstr.c_str(), "name") == 0) { + + memstr = FbTk::StringUtil::toLower(memstr); + + if (memstr == "name") { prop = NAME; - } else if (strcasecmp(memstr.c_str(), "class") == 0) { + } else if (memstr == "class") { prop = CLASS; - } else if (strcasecmp(memstr.c_str(), "title") == 0) { + } else if (memstr == "title") { prop = TITLE; - } else if (strcasecmp(memstr.c_str(), "role") == 0) { + } else if (memstr == "role") { prop = ROLE; - } else if (strcasecmp(memstr.c_str(), "transient") == 0) { + } else if (memstr == "transient") { prop = TRANSIENT; - } else if (strcasecmp(memstr.c_str(), "maximized") == 0) { + } else if (memstr == "maximized") { prop = MAXIMIZED; - } else if (strcasecmp(memstr.c_str(), "minimized") == 0) { + } else if (memstr == "minimized") { prop = MINIMIZED; - } else if (strcasecmp(memstr.c_str(), "shaded") == 0) { + } else if (memstr == "shaded") { prop = SHADED; - } else if (strcasecmp(memstr.c_str(), "stuck") == 0) { + } else if (memstr == "stuck") { prop = STUCK; - } else if (strcasecmp(memstr.c_str(), "focushidden") == 0) { + } else if (memstr == "focushidden") { prop = FOCUSHIDDEN; - } else if (strcasecmp(memstr.c_str(), "iconhidden") == 0) { + } else if (memstr == "iconhidden") { prop = ICONHIDDEN; - } else if (strcasecmp(memstr.c_str(), "workspace") == 0) { + } else if (memstr == "workspace") { prop = WORKSPACE; - } else if (strcasecmp(memstr.c_str(), "workspacename") == 0) { + } else if (memstr == "workspacename") { prop = WORKSPACENAME; - } else if (strcasecmp(memstr.c_str(), "head") == 0) { + } else if (memstr == "head") { prop = HEAD; - } else if (strcasecmp(memstr.c_str(), "layer") == 0) { + } else if (memstr == "layer") { prop = LAYER; - } else if (strcasecmp(memstr.c_str(), "urgent") == 0) { + } else if (memstr == "urgent") { prop = URGENT; - } else if (strcasecmp(memstr.c_str(), "screen") == 0) { + } else if (memstr == "screen") { prop = SCREEN; } else { prop = NAME; @@ -279,9 +282,7 @@ bool ClientPattern::match(const Focusable &win) const { if ((*it)->orig == "[current]") { WinClient *focused = FocusControl::focusedWindow(); if ((*it)->prop == WORKSPACE) { - char tmpstr[128]; - sprintf(tmpstr, "%d", win.screen().currentWorkspaceID()); - if (!(*it)->negate ^ (getProperty((*it)->prop, win) == tmpstr)) + if (!(*it)->negate ^ (getProperty((*it)->prop, win) == FbTk::StringUtil::number2String(win.screen().currentWorkspaceID()))) return false; } else if ((*it)->prop == WORKSPACENAME) { const Workspace *w = win.screen().currentWorkspace(); @@ -292,16 +293,11 @@ bool ClientPattern::match(const Focusable &win) const { (getProperty((*it)->prop, win) == getProperty((*it)->prop, *focused)))) return false; - } else if ((*it)->prop == HEAD && - (*it)->orig == "[mouse]") { - int mouse_head = win.screen().getCurrHead(); - char num[32]; - sprintf(num, "%d", mouse_head); - if (!(*it)->negate ^ (getProperty((*it)->prop, win) == num)) + } else if ((*it)->prop == HEAD && (*it)->orig == "[mouse]") { + if (!(*it)->negate ^ (getProperty((*it)->prop, win) == FbTk::StringUtil::number2String(win.screen().getCurrHead()))) return false; - } else if (!(*it)->negate ^ - (*it)->regexp.match(getProperty((*it)->prop, win))) + } else if (!(*it)->negate ^ (*it)->regexp.match(getProperty((*it)->prop, win))) return false; } return true; @@ -385,9 +381,7 @@ string ClientPattern::getProperty(WinProperty prop, const Focusable &client) { break; case WORKSPACE: { unsigned int wsnum = (fbwin ? fbwin->workspaceNumber() : client.screen().currentWorkspaceID()); - char tmpstr[128]; - sprintf(tmpstr, "%d", wsnum); - return std::string(tmpstr); + return FbTk::StringUtil::number2String(wsnum); break; } case WORKSPACENAME: { @@ -401,9 +395,7 @@ string ClientPattern::getProperty(WinProperty prop, const Focusable &client) { if (!fbwin) return ""; int head = client.screen().getHead(fbwin->fbWindow()); - char tmpstr[128]; - sprintf(tmpstr, "%d", head); - return std::string(tmpstr); + return FbTk::StringUtil::number2String(head); break; } case LAYER: @@ -415,9 +407,7 @@ string ClientPattern::getProperty(WinProperty prop, const Focusable &client) { break; case SCREEN: { int screenId = client.screen().screenNumber(); - char tmpstr[32]; - sprintf(tmpstr, "%d", screenId); - return std::string(tmpstr); + return FbTk::StringUtil::number2String(screenId); break; } } diff --git a/src/FbCommands.cc b/src/FbCommands.cc index 26a431a..f050669 100644 --- a/src/FbCommands.cc +++ b/src/FbCommands.cc @@ -144,7 +144,6 @@ int ExecuteCmd::run() { string displaystring("DISPLAY="); displaystring += DisplayString(FbTk::App::instance()->display()); - char intbuff[64]; int screen_num = m_screen_num; if (screen_num < 0) { if (Fluxbox::instance()->mouseScreen() == 0) @@ -153,8 +152,6 @@ int ExecuteCmd::run() { screen_num = Fluxbox::instance()->mouseScreen()->screenNumber(); } - sprintf(intbuff, "%d", screen_num); - // get shell path from the environment // this process exits immediately, so we don't have to worry about memleaks const char *shell = getenv("SHELL"); @@ -163,7 +160,7 @@ int ExecuteCmd::run() { // remove last number of display and add screen num displaystring.erase(displaystring.size()-1); - displaystring += intbuff; + displaystring += FbTk::StringUtil::number2String(screen_num); setsid(); putenv(const_cast(displaystring.c_str())); diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index 18e000e..a0e402d 100644 --- a/src/FbTk/StringUtil.cc +++ b/src/FbTk/StringUtil.cc @@ -60,6 +60,13 @@ namespace FbTk { namespace StringUtil { + +std::string number2String(int num) { + char s[128]; + sprintf(s, "%d", num); + return std::string(s); +} + /** Tries to find a string in another and ignoring the case of the characters diff --git a/src/FbTk/StringUtil.hh b/src/FbTk/StringUtil.hh index 35f23a2..6493f74 100644 --- a/src/FbTk/StringUtil.hh +++ b/src/FbTk/StringUtil.hh @@ -30,6 +30,8 @@ namespace FbTk { namespace StringUtil { +/// creates a number to a string +std::string number2String(int num); /// Similar to `strstr' but this function ignores the case of both strings const char *strcasestr(const char *str, const char *ptn); diff --git a/src/Layer.hh b/src/Layer.hh index da8f04c..14054b0 100644 --- a/src/Layer.hh +++ b/src/Layer.hh @@ -22,16 +22,7 @@ #ifndef LAYER_HH #define LAYER_HH -#include -#include - -#ifdef HAVE_CSTRING - #include -#else - #include -#endif - -using std::string; +#include "FbTk/StringUtil.hh" /** * (This is not the layer->raise/lower handling stuff, @see FbTk::Layer) @@ -54,52 +45,51 @@ public: explicit Layer(int i) : m_num(i) {}; - static int getNumFromString(const string &str) { + static int getNumFromString(const std::string &str) { int tempnum = 0; + std::string v = FbTk::StringUtil::toLower(str); if (sscanf(str.c_str(), "%d", &tempnum) == 1) return tempnum; - if (strcasecmp(str.c_str(), "Menu") == 0) + if (v == "menu") return ::Layer::MENU; - if (strcasecmp(str.c_str(), "AboveDock") == 0) + if (v == "abovedock") return ::Layer::ABOVE_DOCK; - if (strcasecmp(str.c_str(), "Dock") == 0) + if (v == "dock") return ::Layer::DOCK; - if (strcasecmp(str.c_str(), "Top") == 0) + if (v == "top") return ::Layer::TOP; - if (strcasecmp(str.c_str(), "Normal") == 0) + if (v == "normal") return ::Layer::NORMAL; - if (strcasecmp(str.c_str(), "Bottom") == 0) + if (v == "bottom") return ::Layer::BOTTOM; - if (strcasecmp(str.c_str(), "Desktop") == 0) + if (v == "desktop") return ::Layer::DESKTOP; return -1; } - static string getString(int num) { + static std::string getString(int num) { switch (num) { case ::Layer::MENU: - return string("Menu"); + return std::string("Menu"); case ::Layer::ABOVE_DOCK: - return string("AboveDock"); + return std::string("AboveDock"); case ::Layer::DOCK: - return string("Dock"); + return std::string("Dock"); case ::Layer::TOP: - return string("Top"); + return std::string("Top"); case ::Layer::NORMAL: - return string("Normal"); + return std::string("Normal"); case ::Layer::BOTTOM: - return string("Bottom"); + return std::string("Bottom"); case ::Layer::DESKTOP: - return string("Desktop"); + return std::string("Desktop"); default: - char tmpstr[128]; - sprintf(tmpstr, "%d", num); - return string(tmpstr); + return FbTk::StringUtil::number2String(num); } } int getNum() const { return m_num; } - string getString() const { return getString(m_num); } + std::string getString() const { return getString(m_num); } Layer &operator=(int num) { m_num = num; return *this; } diff --git a/src/Screen.cc b/src/Screen.cc index 1581571..9e340f8 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -1231,10 +1231,8 @@ bool BScreen::isKdeDockapp(Window client) const { bool BScreen::addKdeDockapp(Window client) { XSelectInput(FbTk::App::instance()->display(), client, StructureNotifyMask); - char intbuff[16]; - sprintf(intbuff, "%d", screenNumber()); string atom_name("_NET_SYSTEM_TRAY_S"); - atom_name += intbuff; // append number + atom_name += FbTk::StringUtil::number2String(screenNumber()); // find the right atomhandler that has the name: _NET_SYSTEM_TRAY_S AtomHandler *handler = Fluxbox::instance()->getAtomHandler(atom_name); FbTk::EventHandler *evh = 0; diff --git a/src/SystemTray.cc b/src/SystemTray.cc index 05d8d09..3a38bb1 100644 --- a/src/SystemTray.cc +++ b/src/SystemTray.cc @@ -180,10 +180,8 @@ SystemTray::SystemTray(const FbTk::FbWindow& parent, Display *disp = fluxbox->display(); // setup atom name to _NET_SYSTEM_TRAY_S - char intbuff[16]; - sprintf(intbuff, "%d", m_window.screenNumber()); string atom_name("_NET_SYSTEM_TRAY_S"); - atom_name += intbuff; // append number + atom_name += FbTk::StringUtil::number2String(m_window.screenNumber()); // get selection owner and see if it's free Atom tray_atom = XInternAtom(disp, atom_name.c_str(), False); @@ -232,10 +230,8 @@ SystemTray::~SystemTray() { fluxbox->removeAtomHandler(m_handler.get()); Display *disp = fluxbox->display(); // setup atom name to _NET_SYSTEM_TRAY_S - char intbuff[16]; - sprintf(intbuff, "%d", m_window.screenNumber()); string atom_name("_NET_SYSTEM_TRAY_S"); - atom_name += intbuff; // append number + atom_name += FbTk::StringUtil::number2String(m_window.screenNumber()); // get selection owner and see if it's free Atom tray_atom = XInternAtom(disp, atom_name.c_str(), False); -- cgit v0.11.2