aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2010-08-24 15:30:24 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2010-08-24 15:30:24 (GMT)
commitb8987fc6238a747285b3aeac6f3b206a74e0d36a (patch)
treef8826582a15deec69942310e4405aac3b412b4c0
parent874eb67297cd63999730d1e779ff00581dcbb89d (diff)
downloadfluxbox_paul-b8987fc6238a747285b3aeac6f3b206a74e0d36a.zip
fluxbox_paul-b8987fc6238a747285b3aeac6f3b206a74e0d36a.tar.bz2
code simplification / deduplication
* parse the string only once for a bunch of chars to check instead of starting over again and again from the beginning, created a helper function to do this (FbTk::StringUtil::findCharFromAlphabetAfterTrigger) * put same code into a function (setClockModeLabel()) * use much simpler code to switch between 12h and 24h mode and replace the fmt-switches
-rw-r--r--ChangeLog3
-rw-r--r--src/ClockTool.cc88
-rw-r--r--src/FbTk/StringUtil.cc18
-rw-r--r--src/FbTk/StringUtil.hh12
4 files changed, 67 insertions, 54 deletions
diff --git a/ChangeLog b/ChangeLog
index e9ab118..4aacb60 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,9 @@
1 (Format: Year/Month/Day) 1 (Format: Year/Month/Day)
2Changes for 1.1.2 2Changes for 1.1.2
3 3
4*10/08/24:
5 * Code deduplication and simplification (Mathias)
6 Clocktool.cc FbTk/StringUtil.hh
4*10/08/20: 7*10/08/20:
5 * Added new action 'ActivateTab' (Mathias) 8 * Added new action 'ActivateTab' (Mathias)
6 Window.{cc,hh} Keys.cc fluxbox.cc CurrentWindowCmd.cc 9 Window.{cc,hh} Keys.cc fluxbox.cc CurrentWindowCmd.cc
diff --git a/src/ClockTool.cc b/src/ClockTool.cc
index 91fea93..c2d029f 100644
--- a/src/ClockTool.cc
+++ b/src/ClockTool.cc
@@ -50,6 +50,11 @@
50 50
51namespace { 51namespace {
52 52
53static const char SWITCHES_SECONDS[] = "crsSTX+";
54static const char SWITCHES_12_24H[] = "lIrkHT";
55static const char SWITCHES_24_12H[] = "kHTlIr";
56static const char SWITCH_AM_PM[] = "pP";
57
53/** 58/**
54 * return true if clock shows seconds. If clock doesn't show seconds then 59 * return true if clock shows seconds. If clock doesn't show seconds then
55 * there is no need to wake up every second to redraw the clock. 60 * there is no need to wake up every second to redraw the clock.
@@ -57,15 +62,11 @@ namespace {
57 62
58int showSeconds(const std::string& fmt_string) { 63int showSeconds(const std::string& fmt_string) {
59 64
60 return fmt_string.find("%c") != std::string::npos 65 return FbTk::StringUtil::findCharFromAlphabetAfterTrigger(
61 || fmt_string.find("%r") != std::string::npos 66 fmt_string, '%', SWITCHES_SECONDS, sizeof(SWITCHES_SECONDS), 0) != std::string::npos;
62 || fmt_string.find("%s") != std::string::npos
63 || fmt_string.find("%S") != std::string::npos
64 || fmt_string.find("%T") != std::string::npos
65 || fmt_string.find("%X") != std::string::npos
66 || fmt_string.find("%+") != std::string::npos;
67} 67}
68 68
69
69timeval calcNextTimeout(const std::string& fmt_string) { 70timeval calcNextTimeout(const std::string& fmt_string) {
70 timeval now; 71 timeval now;
71 timeval next; 72 timeval next;
@@ -93,69 +94,48 @@ class ClockMenuItem: public FbTk::MenuItem {
93public: 94public:
94 explicit ClockMenuItem(ClockTool &tool): 95 explicit ClockMenuItem(ClockTool &tool):
95 FbTk::MenuItem(""), m_tool(tool) { 96 FbTk::MenuItem(""), m_tool(tool) {
96 // determine 12/24 hour format 97
97 _FB_USES_NLS; 98 setClockModeLabel();
98 if (m_tool.timeFormat().find("%k") != std::string::npos ||
99 m_tool.timeFormat().find("%H") != std::string::npos ||
100 m_tool.timeFormat().find("%T") != std::string::npos)
101 setLabel( _FB_XTEXT(Toolbar, Clock24, "Clock: 24h", "set Clockmode to 24h") );
102 else
103 setLabel( _FB_XTEXT(Toolbar, Clock12, "Clock: 12h", "set Clockmode to 12h") );
104 setCloseOnClick(false); 99 setCloseOnClick(false);
105 } 100 }
106 101
107 void click(int button, int time, unsigned int mods) { 102 void click(int button, int time, unsigned int mods) {
108 std::string newformat = m_tool.timeFormat();
109 size_t pos = newformat.find("%k");
110 std::string newstr;
111 bool clock24hour = true;
112
113 _FB_USES_NLS;
114 103
115 if (pos != std::string::npos) 104 // does the current format string contain something with 24/12h?
116 newstr = "%l"; 105 size_t found;
117 else if ((pos = newformat.find("%H")) != std::string::npos) 106 size_t pos = FbTk::StringUtil::findCharFromAlphabetAfterTrigger(
118 newstr = "%I"; 107 m_tool.timeFormat(), '%', SWITCHES_24_12H, sizeof(SWITCHES_24_12H), &found);
119 else if ((pos = newformat.find("%T")) != std::string::npos)
120 newstr = "%r";
121
122 // 12 hour
123 if (newstr.empty()) {
124 clock24hour = false;
125 if ((pos = newformat.find("%l")) != std::string::npos)
126 newstr = "%k";
127 else if ((pos = newformat.find("%I")) != std::string::npos)
128 newstr = "%H";
129 else if ((pos = newformat.find("%r")) != std::string::npos)
130 newstr = "%T";
131
132 }
133 108
134 if (!newstr.empty()) { 109 if (pos != std::string::npos) { // if so, exchange it with 12/24h
110 std::string newformat = m_tool.timeFormat();
111 newformat[pos+1] = SWITCHES_12_24H[found];
135 112
136 newformat.replace(pos, 2, newstr); 113 if (found < 3) { // 24h? erase %P/%p (AM|PM / am|pm)
137 if (!clock24hour) { // erase %P/%p (AM|PM / am|pm) 114 pos = FbTk::StringUtil::findCharFromAlphabetAfterTrigger(
138 pos = newformat.find("%p"); 115 newformat, '%', SWITCH_AM_PM, sizeof(SWITCH_AM_PM), 0);
139 if (pos != std::string::npos) 116 if (pos != std::string::npos) {
140 newformat.erase(pos, 2);
141 else if ((pos = newformat.find("%P")) != std::string::npos)
142 newformat.erase(pos, 2); 117 newformat.erase(pos, 2);
118 }
143 } 119 }
144 120
145
146 m_tool.setTimeFormat(newformat); 121 m_tool.setTimeFormat(newformat);
147 122 setClockModeLabel();
148 if (m_tool.timeFormat().find("%k") != std::string::npos ||
149 m_tool.timeFormat().find("%H") != std::string::npos ||
150 m_tool.timeFormat().find("%T") != std::string::npos)
151 setLabel( _FB_XTEXT(Toolbar, Clock24, "Clock: 24h", "set Clockmode to 24h") );
152 else
153 setLabel( _FB_XTEXT(Toolbar, Clock12, "Clock: 12h", "set Clockmode to 12h") );
154 123
155 } // else some other strange format...so we don't do anything 124 } // else some other strange format...so we don't do anything
156 FbTk::MenuItem::click(button, time, mods); 125 FbTk::MenuItem::click(button, time, mods);
157 } 126 }
158private: 127private:
128
129 void setClockModeLabel() {
130 _FB_USES_NLS;
131 if (FbTk::StringUtil::findCharFromAlphabetAfterTrigger(
132 m_tool.timeFormat(), '%', SWITCHES_24_12H, 3, 0) != std::string::npos) {
133 setLabel( _FB_XTEXT(Toolbar, Clock24, "Clock: 24h", "set Clockmode to 24h") );
134 } else {
135 setLabel( _FB_XTEXT(Toolbar, Clock12, "Clock: 12h", "set Clockmode to 12h") );
136 }
137 }
138
159 ClockTool &m_tool; 139 ClockTool &m_tool;
160}; 140};
161 141
diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc
index 602211e..4678f03 100644
--- a/src/FbTk/StringUtil.cc
+++ b/src/FbTk/StringUtil.cc
@@ -164,6 +164,24 @@ string findExtension(const string &filename) {
164 return filename.substr(start_pos + 1); 164 return filename.substr(start_pos + 1);
165} 165}
166 166
167string::size_type findCharFromAlphabetAfterTrigger(const std::string& in, char trigger, const char alphabet[], size_t len_alphabet, size_t* found) {
168 for (const char* s = in.c_str(); *s != '\0'; ) {
169 if (*s++ == trigger && *s != '\0') {
170 for (const char* a = alphabet; (a - alphabet) < len_alphabet; ++a) {
171 if (*s == *a) {
172 if (found) {
173 *found = a - alphabet;
174 }
175 return s - in.c_str() - 1;
176 }
177 }
178 s++;
179 }
180 }
181 return string::npos;
182}
183
184
167string replaceString(const string &original, 185string replaceString(const string &original,
168 const char *findthis, 186 const char *findthis,
169 const char *replace) { 187 const char *replace) {
diff --git a/src/FbTk/StringUtil.hh b/src/FbTk/StringUtil.hh
index 7bc8f80..a16e709 100644
--- a/src/FbTk/StringUtil.hh
+++ b/src/FbTk/StringUtil.hh
@@ -50,6 +50,18 @@ std::string expandFilename(const std::string &filename);
50/// @return extension of filename (ex: filename.txt will return txt) 50/// @return extension of filename (ex: filename.txt will return txt)
51std::string findExtension(const std::string &filename); 51std::string findExtension(const std::string &filename);
52 52
53/// is the char after a 'trigger' part of an alphabet?
54/// @param in - string to analyze
55/// @param trigger - check for char after trigger
56/// @param alphabet - contains chars to search for
57/// @param len_alphabet - length of alphabet
58/// @param found - position of found char in alphabet (optional)
59/// @return position of trigger if found
60/// @return std::string::npos if nothing found
61std::string::size_type findCharFromAlphabetAfterTrigger(const std::string& in,
62 char trigger,
63 const char alphabet[], size_t len_alphabet, size_t* found);
64
53/// @return copy of original with find_string replaced with "replace" 65/// @return copy of original with find_string replaced with "replace"
54std::string replaceString(const std::string &original, 66std::string replaceString(const std::string &original,
55 const char *find_string, 67 const char *find_string,