diff options
author | fluxgen <fluxgen> | 2004-05-02 21:02:26 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2004-05-02 21:02:26 (GMT) |
commit | d4bfeb646063367a4537954203c92e71d7242a98 (patch) | |
tree | 5cd974e0e06c327d112a46371edd1959c47a7e5c | |
parent | 95f721e35b91328615b9cb1de45da885ed921797 (diff) | |
download | fluxbox-d4bfeb646063367a4537954203c92e71d7242a98.zip fluxbox-d4bfeb646063367a4537954203c92e71d7242a98.tar.bz2 |
parses fluxbox menu file
-rw-r--r-- | src/FbMenuParser.cc | 127 | ||||
-rw-r--r-- | src/FbMenuParser.hh | 58 |
2 files changed, 185 insertions, 0 deletions
diff --git a/src/FbMenuParser.cc b/src/FbMenuParser.cc new file mode 100644 index 0000000..fbb96b1 --- /dev/null +++ b/src/FbMenuParser.cc | |||
@@ -0,0 +1,127 @@ | |||
1 | // FbMenuParser.cc for Fluxbox | ||
2 | // Copyright (c) 2004 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
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 | // $Id: FbMenuParser.cc,v 1.1 2004/05/02 21:02:26 fluxgen Exp $ | ||
24 | |||
25 | #include "FbMenuParser.hh" | ||
26 | |||
27 | #include "FbTk/StringUtil.hh" | ||
28 | |||
29 | bool 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 | |||
37 | Parser &FbMenuParser::operator >> (Parser::Item &out) { | ||
38 | if (eof()) { | ||
39 | out = Parser::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 DONE: // get new line and call this again | ||
63 | if (!nextLine()) { | ||
64 | out = Parser::s_empty_item; | ||
65 | return *this; | ||
66 | } | ||
67 | return (*this)>>out; | ||
68 | break; | ||
69 | } | ||
70 | |||
71 | std::string key; | ||
72 | int err = FbTk::StringUtil:: | ||
73 | getStringBetween(key, m_curr_line.c_str() + m_curr_pos, | ||
74 | first, second); | ||
75 | if (err <= 0) { | ||
76 | if (m_curr_token == TYPE) | ||
77 | m_curr_token = NAME; | ||
78 | else if (m_curr_token == NAME) | ||
79 | m_curr_token = ARGUMENT; | ||
80 | else if (m_curr_token == ARGUMENT) | ||
81 | m_curr_token = DONE; | ||
82 | |||
83 | out = Parser::s_empty_item; | ||
84 | return *this; | ||
85 | } | ||
86 | |||
87 | m_curr_pos += err; // update current position in current line | ||
88 | |||
89 | // set value | ||
90 | out.second = key; | ||
91 | |||
92 | // set type and next token to be read | ||
93 | switch (m_curr_token) { | ||
94 | case TYPE: | ||
95 | out.first = "TYPE"; | ||
96 | m_curr_token = NAME; | ||
97 | break; | ||
98 | case NAME: | ||
99 | out.first = "NAME"; | ||
100 | m_curr_token = ARGUMENT; | ||
101 | break; | ||
102 | case ARGUMENT: | ||
103 | out.first = "ARGUMENT"; | ||
104 | m_curr_token = DONE; | ||
105 | break; | ||
106 | case DONE: | ||
107 | break; | ||
108 | } | ||
109 | return *this; | ||
110 | } | ||
111 | |||
112 | Parser::Item FbMenuParser::nextItem() { | ||
113 | Parser::Item item; | ||
114 | (*this)>>item; | ||
115 | return item; | ||
116 | } | ||
117 | |||
118 | bool FbMenuParser::nextLine() { | ||
119 | if (!getline(m_file, m_curr_line)) | ||
120 | return false; | ||
121 | |||
122 | m_row++; | ||
123 | m_curr_pos = 0; | ||
124 | m_curr_token = TYPE; | ||
125 | |||
126 | return true; | ||
127 | } | ||
diff --git a/src/FbMenuParser.hh b/src/FbMenuParser.hh new file mode 100644 index 0000000..e274ed8 --- /dev/null +++ b/src/FbMenuParser.hh | |||
@@ -0,0 +1,58 @@ | |||
1 | // FbMenuParser.hh for Fluxbox | ||
2 | // Copyright (c) 2004 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
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 | // $Id: FbMenuParser.hh,v 1.1 2004/05/02 21:02:26 fluxgen Exp $ | ||
24 | |||
25 | #ifndef FBMENUPARSER_HH | ||
26 | #define FBMENUPARSER_HH | ||
27 | |||
28 | #include "Parser.hh" | ||
29 | |||
30 | #include <fstream> | ||
31 | |||
32 | class FbMenuParser: public Parser { | ||
33 | public: | ||
34 | FbMenuParser():m_row(0), m_curr_pos(0), m_curr_token(TYPE) {} | ||
35 | FbMenuParser(const std::string &filename):m_row(0), m_curr_pos(0), | ||
36 | m_curr_token(TYPE) { open(filename); } | ||
37 | ~FbMenuParser() { close(); } | ||
38 | |||
39 | bool open(const std::string &filename); | ||
40 | void close() { m_file.close(); } | ||
41 | Parser &operator >> (Parser::Item &out); | ||
42 | Parser::Item nextItem(); | ||
43 | |||
44 | bool isLoaded() const { return m_file.is_open(); } | ||
45 | bool eof() const { return m_file.eof(); } | ||
46 | int row() const { return m_row; } | ||
47 | std::string line() const { return m_curr_line; } | ||
48 | private: | ||
49 | bool nextLine(); | ||
50 | |||
51 | mutable std::ifstream m_file; | ||
52 | int m_row; | ||
53 | int m_curr_pos; | ||
54 | std::string m_curr_line; | ||
55 | enum Object {TYPE, NAME, ARGUMENT, DONE} m_curr_token; | ||
56 | }; | ||
57 | |||
58 | #endif // FBMENUPARSER_HH | ||