From d3eabeb805fdbd162c0743ed86a67e014e37c097 Mon Sep 17 00:00:00 2001
From: Mathias Gumz <akira at fluxbox dot org>
Date: Fri, 17 Sep 2010 15:51:16 +0200
Subject: moved commandline parsing to its own function

---
 src/Screen.cc | 162 ++++++++++++++++++++++++++++++++++------------------------
 src/main.cc   |  56 ++++++++++++--------
 2 files changed, 132 insertions(+), 86 deletions(-)

diff --git a/src/Screen.cc b/src/Screen.cc
index daa87dc..0677a14 100644
--- a/src/Screen.cc
+++ b/src/Screen.cc
@@ -183,7 +183,7 @@ int calcSquareDistance(int x1, int y1, int x2, int y2) {
 
 class TabPlacementMenuItem: public FbTk::RadioMenuItem {
 public:
-    TabPlacementMenuItem(const FbTk::FbString & label, BScreen &screen,
+    TabPlacementMenuItem(FbTk::FbString & label, BScreen &screen,
                          FbWinFrame::TabPlacement place,
                          FbTk::RefCount<FbTk::Command<void> > &cmd):
         FbTk::RadioMenuItem(label, cmd),
@@ -208,28 +208,6 @@ void clampMenuDelay(int& delay) {
     delay = FbTk::Util::clamp(delay, 0, 5000);
 }
 
-
-struct TabPlacementString {
-    FbWinFrame::TabPlacement placement;
-    const char* str;
-};
-
-const TabPlacementString placement_strings[] = {
-    { FbWinFrame::TOPLEFT, "TopLeft" },
-    { FbWinFrame::TOP, "Top" },
-    { FbWinFrame::TOPRIGHT, "TopRight" },
-    { FbWinFrame::BOTTOMLEFT, "BottomLeft" },
-    { FbWinFrame::BOTTOM, "Bottom" },
-    { FbWinFrame::BOTTOMRIGHT, "BottomRight" },
-    { FbWinFrame::LEFTBOTTOM, "LeftBottom" },
-    { FbWinFrame::LEFT, "Left" },
-    { FbWinFrame::LEFTTOP, "LeftTop" },
-    { FbWinFrame::RIGHTBOTTOM, "RightBottom" },
-    { FbWinFrame::RIGHT, "Right" },
-    { FbWinFrame::RIGHTTOP, "RightTop" }
-};
-
-
 } // end anonymous namespace
 
 
@@ -239,25 +217,77 @@ namespace FbTk {
 template<>
 string FbTk::Resource<FbWinFrame::TabPlacement>::
 getString() const {
-
-    size_t i = (m_value == FbTk::Util::clamp(m_value, FbWinFrame::TOPLEFT, FbWinFrame::RIGHTTOP)
-                ? m_value 
-                : FbWinFrame::DEFAULT) - 1;
-    return placement_strings[i].str;
+    switch (m_value) {
+    case FbWinFrame::TOPLEFT:
+        return string("TopLeft");
+        break;
+    case FbWinFrame::BOTTOMLEFT:
+        return string("BottomLeft");
+        break;
+    case FbWinFrame::TOP:
+        return string("Top");
+        break;
+    case FbWinFrame::BOTTOM:
+        return string("Bottom");
+        break;
+    case FbWinFrame::TOPRIGHT:
+        return string("TopRight");
+        break;
+    case FbWinFrame::BOTTOMRIGHT:
+        return string("BottomRight");
+        break;
+    case FbWinFrame::LEFTTOP:
+        return string("LeftTop");
+        break;
+    case FbWinFrame::LEFT:
+        return string("Left");
+        break;
+    case FbWinFrame::LEFTBOTTOM:
+        return string("LeftBottom");
+        break;
+    case FbWinFrame::RIGHTTOP:
+        return string("RightTop");
+        break;
+    case FbWinFrame::RIGHT:
+        return string("Right");
+        break;
+    case FbWinFrame::RIGHTBOTTOM:
+        return string("RightBottom");
+        break;
+    }
+    //default string
+    return string("TopLeft");
 }
 
 template<>
 void FbTk::Resource<FbWinFrame::TabPlacement>::
 setFromString(const char *strval) {
-
-    size_t i;
-    for (i = 0; i < sizeof(placement_strings)/sizeof(TabPlacementString); ++i) {
-        if (strcasecmp(strval, placement_strings[i].str) == 0) {
-            m_value = placement_strings[i].placement;
-            return;
-        }
-    }
-    setDefaultValue();
+    if (strcasecmp(strval, "TopLeft") == 0)
+        m_value = FbWinFrame::TOPLEFT;
+    else if (strcasecmp(strval, "BottomLeft") == 0)
+        m_value = FbWinFrame::BOTTOMLEFT;
+    else if (strcasecmp(strval, "Top") == 0)
+        m_value = FbWinFrame::TOP;
+    else if (strcasecmp(strval, "Bottom") == 0)
+        m_value = FbWinFrame::BOTTOM;
+    else if (strcasecmp(strval, "TopRight") == 0)
+        m_value = FbWinFrame::TOPRIGHT;
+    else if (strcasecmp(strval, "BottomRight") == 0)
+        m_value = FbWinFrame::BOTTOMRIGHT;
+    else if (strcasecmp(strval, "LeftTop") == 0)
+        m_value = FbWinFrame::LEFTTOP;
+    else if (strcasecmp(strval, "Left") == 0)
+        m_value = FbWinFrame::LEFT;
+    else if (strcasecmp(strval, "LeftBottom") == 0)
+        m_value = FbWinFrame::LEFTBOTTOM;
+    else if (strcasecmp(strval, "RightTop") == 0)
+        m_value = FbWinFrame::RIGHTTOP;
+    else if (strcasecmp(strval, "Right") == 0)
+        m_value = FbWinFrame::RIGHT;
+    else if (strcasecmp(strval, "RightBottom") == 0)
+        m_value = FbWinFrame::RIGHTBOTTOM;
+    else
+        setDefaultValue();
 }
 
 } // end namespace FbTk
@@ -1619,41 +1649,41 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
     tab_width_item->setCommand(save_and_reconftabs);
     tab_menu->insert(tab_width_item);
 
+
+    typedef pair<FbTk::FbString, FbWinFrame::TabPlacement> PlacementP;
+    typedef list<PlacementP> Placements;
+    Placements place_menu;
+
     // menu is 3 wide, 5 down
-    struct PlacementP {
-         const FbTk::FbString label;
-         FbWinFrame::TabPlacement placement;
-    };
-    static const PlacementP place_menu[] = {
-
-        { _FB_XTEXT(Align, TopLeft, "Top Left", "Top Left"), FbWinFrame::TOPLEFT},
-        { _FB_XTEXT(Align, LeftTop, "Left Top", "Left Top"), FbWinFrame::LEFTTOP},
-        { _FB_XTEXT(Align, LeftCenter, "Left Center", "Left Center"), FbWinFrame::LEFT},
-        { _FB_XTEXT(Align, LeftBottom, "Left Bottom", "Left Bottom"), FbWinFrame::LEFTBOTTOM},
-        { _FB_XTEXT(Align, BottomLeft, "Bottom Left", "Bottom Left"), FbWinFrame::BOTTOMLEFT},
-        { _FB_XTEXT(Align, TopCenter, "Top Center", "Top Center"), FbWinFrame::TOP},
-        { "", FbWinFrame::TOPLEFT},
-        { "", FbWinFrame::TOPLEFT},
-        { "", FbWinFrame::TOPLEFT},
-        { _FB_XTEXT(Align, BottomCenter, "Bottom Center", "Bottom Center"), FbWinFrame::BOTTOM},
-        { _FB_XTEXT(Align, TopRight, "Top Right", "Top Right"), FbWinFrame::TOPRIGHT},
-        { _FB_XTEXT(Align, RightTop, "Right Top", "Right Top"), FbWinFrame::RIGHTTOP},
-        { _FB_XTEXT(Align, RightCenter, "Right Center", "Right Center"), FbWinFrame::RIGHT},
-        { _FB_XTEXT(Align, RightBottom, "Right Bottom", "Right Bottom"), FbWinFrame::RIGHTBOTTOM},
-        { _FB_XTEXT(Align, BottomRight, "Bottom Right", "Bottom Right"), FbWinFrame::BOTTOMRIGHT}
-    };
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, TopLeft, "Top Left", "Top Left"), FbWinFrame::TOPLEFT));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, LeftTop, "Left Top", "Left Top"), FbWinFrame::LEFTTOP));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, LeftCenter, "Left Center", "Left Center"), FbWinFrame::LEFT));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, LeftBottom, "Left Bottom", "Left Bottom"), FbWinFrame::LEFTBOTTOM));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, BottomLeft, "Bottom Left", "Bottom Left"), FbWinFrame::BOTTOMLEFT));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, TopCenter, "Top Center", "Top Center"), FbWinFrame::TOP));
+    place_menu.push_back(PlacementP("", FbWinFrame::TOPLEFT));
+    place_menu.push_back(PlacementP("", FbWinFrame::TOPLEFT));
+    place_menu.push_back(PlacementP("", FbWinFrame::TOPLEFT));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, BottomCenter, "Bottom Center", "Bottom Center"), FbWinFrame::BOTTOM));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, TopRight, "Top Right", "Top Right"), FbWinFrame::TOPRIGHT));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, RightTop, "Right Top", "Right Top"), FbWinFrame::RIGHTTOP));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, RightCenter, "Right Center", "Right Center"), FbWinFrame::RIGHT));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, RightBottom, "Right Bottom", "Right Bottom"), FbWinFrame::RIGHTBOTTOM));
+    place_menu.push_back(PlacementP(_FB_XTEXT(Align, BottomRight, "Bottom Right", "Bottom Right"), FbWinFrame::BOTTOMRIGHT));
 
     tabplacement_menu->setMinimumSublevels(3);
     // create items in sub menu
-    for (size_t i=0; i< sizeof(place_menu)/sizeof(PlacementP); ++i) {
-        const PlacementP& p = place_menu[i];
-        if (p.label == "") {
-            tabplacement_menu->insert(p.label);
+    for (size_t i=0; i<15; ++i) {
+        FbTk::FbString &str = place_menu.front().first;
+        FbWinFrame::TabPlacement placement = place_menu.front().second;
+        if (str == "") {
+            tabplacement_menu->insert("");
             tabplacement_menu->setItemEnabled(i, false);
-        } else
-            tabplacement_menu->insert(new TabPlacementMenuItem(p.label, *this, p.placement, save_and_reconftabs));
+        } else {
+            tabplacement_menu->insert(new TabPlacementMenuItem(str, *this, placement, save_and_reconftabs));
+        }
+        place_menu.pop_front();
     }
-
     tabplacement_menu->updateMenu();
 
     menu.insert(tabmenu_label, tab_menu);
diff --git a/src/main.cc b/src/main.cc
index d65212b..dc42b27 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -182,14 +182,15 @@ static void showInfo(ostream &ostr) {
         endl;
 }
 
-int main(int argc, char **argv) {
+struct Options {
+    std::string session_display;
+    std::string rc_file;
+    std::string log_filename;
+    bool xsync;
+};
 
-    string session_display("");
-    string rc_file;
-    string log_filename;
-    bool xsync = false;
+static void parseOptions(int argc, char** argv, Options& opts) {
 
-    FbTk::NLSInit("fluxbox.cat");
     _FB_USES_NLS;
 
     int i;
@@ -204,7 +205,7 @@ int main(int argc, char **argv) {
                 exit(EXIT_FAILURE);
             }
 
-            rc_file = argv[i];
+            opts.rc_file = argv[i];
         } else if (arg == "-display" || arg == "--display") {
             // check for -display option... to run on a display other than the one
             // set by the environment variable DISPLAY
@@ -216,8 +217,8 @@ int main(int argc, char **argv) {
                 exit(EXIT_FAILURE);
             }
 
-            session_display = argv[i];
-            string display_env = "DISPLAY=" + session_display;
+            opts.session_display = argv[i];
+            string display_env = "DISPLAY=" + opts.session_display;
             if (putenv(const_cast<char *>(display_env.c_str()))) {
                 cerr<<_FB_CONSOLETEXT(main, WarnDisplayEnv,
                                 "warning: couldn't set environment variable 'DISPLAY'",
@@ -226,16 +227,16 @@ int main(int argc, char **argv) {
             }
         } else if (arg == "-version" || arg == "-v" || arg == "--version") {
             // print current version string
-            cout << "Fluxbox " << __fluxbox_version << " : (c) 2001-2008 Fluxbox Team " << endl << endl;
+            cout << "Fluxbox " << __fluxbox_version << " : (c) 2001-2010 Fluxbox Team " << endl << endl;
             exit(EXIT_SUCCESS);
         } else if (arg == "-log" || arg == "--log") {
             if (++i >= argc) {
                 cerr<<_FB_CONSOLETEXT(main, LOGRequiresArg, "error: '-log' needs an argument", "")<<endl;
                 exit(EXIT_FAILURE);
             }
-            log_filename = argv[i];
+            opts.log_filename = argv[i];
         } else if (arg == "-sync" || arg == "--sync") {
-             xsync = true;
+            opts.xsync = true;
         } else if (arg == "-help" || arg == "-h" || arg == "--help") {
             // print program usage and command line options
             printf(_FB_CONSOLETEXT(main, Usage,
@@ -252,7 +253,7 @@ int main(int argc, char **argv) {
                            "-help\t\t\t\tdisplay this help text and exit.\n\n",
 
                            "Main usage string. Please lay it out nicely. There is one %s that is given the version").c_str(),
-                   __fluxbox_version, "2001-2008");
+                   __fluxbox_version, "2001-2010");
             exit(EXIT_SUCCESS);
         } else if (arg == "-info" || arg == "-i" || arg == "--info") {
             showInfo(cout);
@@ -268,23 +269,34 @@ int main(int argc, char **argv) {
             FbTk::ThemeManager::instance().setVerbose(true);
         }
     }
+}
+
+
+
+int main(int argc, char **argv) {
+
+    FbTk::NLSInit("fluxbox.cat");
+
+    Options opts;
+    parseOptions(argc, argv, opts);
 
 #ifdef __EMX__
     _chdir2(getenv("X11ROOT"));
 #endif // __EMX__
     auto_ptr<Fluxbox> fluxbox;
-    int exitcode=EXIT_FAILURE;
 
     streambuf *outbuf = 0;
     streambuf *errbuf = 0;
 
-    ofstream log_file(log_filename.c_str());
+    ofstream log_file(opts.log_filename.c_str());
+
+    _FB_USES_NLS;
 
     // setup log file
-    if (log_file) {
-        cerr<<_FB_CONSOLETEXT(main, LoggingTo, "Logging to", "Logging to a file")<<": "<<log_filename<<endl;
+    if (log_file.is_open()) {
+        cerr<<_FB_CONSOLETEXT(main, LoggingTo, "Logging to", "Logging to a file")<<": "<<opts.log_filename<<endl;
         log_file<<"------------------------------------------"<<endl;
-        log_file<<_FB_CONSOLETEXT(main, LogFile, "Log File", "")<<": "<<log_filename<<endl;
+        log_file<<_FB_CONSOLETEXT(main, LogFile, "Log File", "")<<": "<<opts.log_filename<<endl;
         showInfo(log_file);
         log_file<<"------------------------------------------"<<endl;
         // setup log to use cout and cerr stream
@@ -292,10 +304,14 @@ int main(int argc, char **argv) {
         errbuf = cerr.rdbuf(log_file.rdbuf());
     }
 
+    int exitcode = EXIT_FAILURE;
+
     try {
 
-        fluxbox.reset(new Fluxbox(argc, argv, session_display.c_str(),
-                                  rc_file.c_str(), xsync));
+        fluxbox.reset(new Fluxbox(argc, argv,
+                    opts.session_display.c_str(),
+                    opts.rc_file.c_str(),
+                    opts.xsync));
         fluxbox->eventLoop();
 
         exitcode = EXIT_SUCCESS;
-- 
cgit v0.11.2