aboutsummaryrefslogtreecommitdiff
path: root/util/FbMenuParser.cc
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-08-19 15:01:07 (GMT)
committerPaul Tagliamonte <paultag@fluxbox.org>2012-04-07 02:13:14 (GMT)
commit23736ab3339e9e5d6d1d7a09218f8df9a39a5ff5 (patch)
treec4313b638fbb361f932af02d651c1ed9c9de3ab1 /util/FbMenuParser.cc
parentb7ab35e5f5624589f57c9de8afefab952dfffd32 (diff)
downloadfluxbox_paul-23736ab3339e9e5d6d1d7a09218f8df9a39a5ff5.zip
fluxbox_paul-23736ab3339e9e5d6d1d7a09218f8df9a39a5ff5.tar.bz2
fluxbox-update_configs: automatically update menu file
To achieve this, I had to partially resurrect (and heavily modify) the old parsing code in MenuCreator.cc.
Diffstat (limited to 'util/FbMenuParser.cc')
-rw-r--r--util/FbMenuParser.cc137
1 files changed, 137 insertions, 0 deletions
diff --git a/util/FbMenuParser.cc b/util/FbMenuParser.cc
new file mode 100644
index 0000000..4923fe8
--- /dev/null
+++ b/util/FbMenuParser.cc
@@ -0,0 +1,137 @@
1// FbMenuParser.cc for Fluxbox
2// Copyright (c) 2004 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
3// and Simon Bowden (rathnor at users.sourceforge.net)
4//
5// Permission is hereby granted, free of charge, to any person obtaining a
6// copy of this software and associated documentation files (the "Software"),
7// to deal in the Software without restriction, including without limitation
8// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9// and/or sell copies of the Software, and to permit persons to whom the
10// Software is furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21// DEALINGS IN THE SOFTWARE.
22
23#include "FbMenuParser.hh"
24
25#include "FbTk/StringUtil.hh"
26
27const FbMenuParser::Item FbMenuParser::s_empty_item("", "");
28
29bool FbMenuParser::open(const std::string &filename) {
30 m_file.open(filename.c_str());
31 m_curr_pos = 0;
32 m_row = 0;
33 m_curr_token = DONE;
34 return isLoaded();
35}
36
37FbMenuParser &FbMenuParser::operator >> (Item &out) {
38 if (eof()) {
39 out = s_empty_item;
40 return *this;
41 }
42
43 if (m_curr_line.empty())
44 m_curr_token = DONE; // try next line
45
46 char first = '[';
47 char second = ']';
48
49 switch (m_curr_token) {
50 case TYPE:
51 first = '[';
52 second = ']';
53 break;
54 case NAME:
55 first = '(';
56 second = ')';
57 break;
58 case ARGUMENT:
59 first = '{';
60 second = '}';
61 break;
62 case ICON:
63 first = '<';
64 second = '>';
65 break;
66 case DONE: // get new line and call this again
67 if (!nextLine()) {
68 out = s_empty_item;
69 return *this;
70 }
71 return (*this)>>out;
72 break;
73 }
74
75 std::string key;
76 int err = FbTk::StringUtil::
77 getStringBetween(key, m_curr_line.c_str() + m_curr_pos,
78 first, second);
79 if (err <= 0) {
80 if (m_curr_token == TYPE)
81 m_curr_token = NAME;
82 else if (m_curr_token == NAME)
83 m_curr_token = ARGUMENT;
84 else if (m_curr_token == ARGUMENT)
85 m_curr_token = ICON;
86 else if (m_curr_token == ICON)
87 m_curr_token = DONE;
88
89 out = s_empty_item;
90 return *this;
91 }
92
93 m_curr_pos += err; // update current position in current line
94
95 // set value
96 out.second = key;
97
98 // set type and next token to be read
99 switch (m_curr_token) {
100 case TYPE:
101 out.first = "TYPE";
102 m_curr_token = NAME;
103 break;
104 case NAME:
105 out.first = "NAME";
106 m_curr_token = ARGUMENT;
107 break;
108 case ARGUMENT:
109 out.first = "ARGUMENT";
110 m_curr_token = ICON;
111 break;
112 case ICON:
113 out.first = "ICON";
114 m_curr_token = DONE;
115 break;
116 case DONE:
117 break;
118 }
119 return *this;
120}
121
122FbMenuParser::Item FbMenuParser::nextItem() {
123 Item item;
124 (*this)>>item;
125 return item;
126}
127
128bool FbMenuParser::nextLine() {
129 if (!std::getline(m_file, m_curr_line))
130 return false;
131
132 m_row++;
133 m_curr_pos = 0;
134 m_curr_token = TYPE;
135
136 return true;
137}