From 3f76e117bf7735058f2b135beb72e015658f97eb Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Fri, 21 Oct 2011 08:42:48 +0200 Subject: refactored MenuCreator make public only what needs to be public --- src/MenuCreator.cc | 148 +++++++++++++++++++++++++++-------------------------- src/MenuCreator.hh | 39 ++++---------- 2 files changed, 85 insertions(+), 102 deletions(-) diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc index 74968e3..41dcf73 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -64,12 +64,79 @@ using std::list; using std::less; using FbTk::AutoReloadHelper; -list MenuCreator::encoding_stack; -list MenuCreator::stacksize_stack; +namespace { -FbTk::StringConvertor MenuCreator::m_stringconvertor(FbTk::StringConvertor::ToFbString); +FbTk::StringConvertor s_stringconvertor(FbTk::StringConvertor::ToFbString); + +list s_encoding_stack; +list s_stacksize_stack; + +/** + * Push the encoding onto the stack, and make it active. + */ +void startEncoding(const string &encoding) { + // we push it regardless of whether it's valid, since we + // need to stay balanced with the endEncodings. + s_encoding_stack.push_back(encoding); + + // this won't change if it doesn't succeed + s_stringconvertor.setSource(encoding); +} + +/** + * Pop the encoding from the stack, unless we are at our stacksize limit. + * Restore the previous (valid) encoding. + */ +void endEncoding() { + size_t min_size = s_stacksize_stack.back(); + if (s_encoding_stack.size() <= min_size) { + _FB_USES_NLS; + cerr<<_FB_CONSOLETEXT(Menu, ErrorEndEncoding, "Warning: unbalanced [encoding] tags", "User menu file had unbalanced [encoding] tags")<::reverse_iterator it = s_encoding_stack.rbegin(); + list::reverse_iterator it_end = s_encoding_stack.rend(); + while (it != it_end && !s_stringconvertor.setSource(*it)) + ++it; + + if (it == it_end) + s_stringconvertor.setSource(""); +} + + +/* push our encoding-stacksize onto the stack */ +void startFile() { + if (s_encoding_stack.empty()) + s_stringconvertor.setSource(""); + s_stacksize_stack.push_back(s_encoding_stack.size()); +} + +/** + * Pop necessary encodings from the stack + * (and endEncoding the final one) to our matching encoding-stacksize. + */ +void endFile() { + size_t target_size = s_stacksize_stack.back(); + size_t curr_size = s_encoding_stack.size(); + + if (target_size != curr_size) { + _FB_USES_NLS; + cerr<<_FB_CONSOLETEXT(Menu, ErrorEndEncoding, "Warning: unbalanced [encoding] tags", "User menu file had unbalanced [encoding] tags")< (target_size+1); --curr_size) + s_encoding_stack.pop_back(); + + if (curr_size == (target_size+1)) + endEncoding(); + + s_stacksize_stack.pop_back(); +} -namespace { void createStyleMenu(FbTk::Menu &parent, const string &label, AutoReloadHelper *reloader, const string &directory) { @@ -112,6 +179,7 @@ void createStyleMenu(FbTk::Menu &parent, const string &label, void createRootCmdMenu(FbTk::Menu &parent, const string &label, const string &directory, AutoReloadHelper *reloader, const string &cmd) { + // perform shell style ~ home directory expansion string rootcmddir(FbTk::StringUtil::expandFilename(directory)); @@ -320,9 +388,9 @@ void translateMenuItem(FbTk::Parser &parse, ParseItem &pitem, } else if (str_key == "separator") { menu.insert(new FbTk::MenuSeparator()); } else if (str_key == "encoding") { - MenuCreator::startEncoding(str_cmd); + startEncoding(str_cmd); } else if (str_key == "endencoding") { - MenuCreator::endEncoding(); + endEncoding(); } else if (!MenuCreator::createWindowMenuItem(str_key, str_label, menu)) { // if we didn't find any special menu item we try with command parser // we need to attach command to arguments so command parser can parse it @@ -393,7 +461,7 @@ bool MenuCreator::createFromFile(const string &filename, startFile(); if (begin) { string label; - if (!getStart(parser, label, m_stringconvertor)) { + if (!getStart(parser, label, s_stringconvertor)) { endFile(); return false; } @@ -404,7 +472,7 @@ bool MenuCreator::createFromFile(const string &filename, if (reloader) reloader->addFile(real_filename); - parseMenu(parser, inject_into, m_stringconvertor, reloader); + parseMenu(parser, inject_into, s_stringconvertor, reloader); endFile(); return true; @@ -552,68 +620,4 @@ bool MenuCreator::createWindowMenuItem(const string &type, return true; } -/* push our encoding-stacksize onto the stack */ -void MenuCreator::startFile() { - if (encoding_stack.empty()) - m_stringconvertor.setSource(""); - stacksize_stack.push_back(encoding_stack.size()); -} - -/** - * Pop necessary encodings from the stack - * (and endEncoding the final one) to our matching encoding-stacksize. - */ -void MenuCreator::endFile() { - size_t target_size = stacksize_stack.back(); - size_t curr_size = encoding_stack.size(); - - if (target_size != curr_size) { - _FB_USES_NLS; - cerr<<_FB_CONSOLETEXT(Menu, ErrorEndEncoding, "Warning: unbalanced [encoding] tags", "User menu file had unbalanced [encoding] tags")< (target_size+1); --curr_size) - encoding_stack.pop_back(); - - if (curr_size == (target_size+1)) - endEncoding(); - - stacksize_stack.pop_back(); -} - -/** - * Push the encoding onto the stack, and make it active. - */ -void MenuCreator::startEncoding(const string &encoding) { - // we push it regardless of whether it's valid, since we - // need to stay balanced with the endEncodings. - encoding_stack.push_back(encoding); - - // this won't change if it doesn't succeed - m_stringconvertor.setSource(encoding); -} - -/** - * Pop the encoding from the stack, unless we are at our stacksize limit. - * Restore the previous (valid) encoding. - */ -void MenuCreator::endEncoding() { - size_t min_size = stacksize_stack.back(); - if (encoding_stack.size() <= min_size) { - _FB_USES_NLS; - cerr<<_FB_CONSOLETEXT(Menu, ErrorEndEncoding, "Warning: unbalanced [encoding] tags", "User menu file had unbalanced [encoding] tags")<::reverse_iterator it = encoding_stack.rbegin(); - list::reverse_iterator it_end = encoding_stack.rend(); - while (it != it_end && !m_stringconvertor.setSource(*it)) - ++it; - - if (it == it_end) - m_stringconvertor.setSource(""); -} diff --git a/src/MenuCreator.hh b/src/MenuCreator.hh index a9fb649..91add47 100644 --- a/src/MenuCreator.hh +++ b/src/MenuCreator.hh @@ -35,37 +35,16 @@ class Menu; class FbMenu; class FluxboxWindow; -class MenuCreator { -public: - static FbMenu *createMenu(const std::string &label, int screen_num); - static FbMenu *createMenuType(const std::string &label, int screen_num); - static bool createFromFile(const std::string &filename, - FbTk::Menu &inject_into, - FbTk::AutoReloadHelper *reloader = NULL, - bool begin = true); - static bool createWindowMenuItem(const std::string &type, const std::string &label, +namespace MenuCreator { + + FbMenu *createMenu(const std::string &label, int screen_num); + FbMenu *createMenuType(const std::string &label, int screen_num); + bool createFromFile(const std::string &filename, + FbTk::Menu &inject_into, + FbTk::AutoReloadHelper *reloader = NULL, + bool begin = true); + bool createWindowMenuItem(const std::string &type, const std::string &label, FbTk::Menu &inject_into); - - /** - * Encoding-related helpers (encoding, aka codeset) - */ - - // Files are guaranteed to be "balanced", unlike user-created [encoding] tags. - static void startFile(); - static void endFile(); - - static void startEncoding(const std::string &encoding); - static void endEncoding(); - -private: - // stack of encodings - static std::list encoding_stack; - // stack of ints, representing stack size as each file is entered - // (a file should never end more encodings than it starts) - static std::list stacksize_stack; - - static FbTk::StringConvertor m_stringconvertor; - }; #endif // MENUCREATOR_HH -- cgit v0.11.2