1 | /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #ifndef SBUILD_KEYFILE_H |
---|
20 | #define SBUILD_KEYFILE_H |
---|
21 | |
---|
22 | #include <sbuild/sbuild-basic-keyfile.h> |
---|
23 | |
---|
24 | namespace sbuild |
---|
25 | { |
---|
26 | |
---|
27 | /** |
---|
28 | * Traits class for an INI-style configuration file. The format is |
---|
29 | * documented in schroot.conf(5). |
---|
30 | */ |
---|
31 | struct keyfile_traits |
---|
32 | { |
---|
33 | /// Group name. |
---|
34 | typedef std::string group_name_type; |
---|
35 | |
---|
36 | /// Key name. |
---|
37 | typedef std::string key_type; |
---|
38 | |
---|
39 | /// Value. |
---|
40 | typedef std::string value_type; |
---|
41 | |
---|
42 | /// Comment. |
---|
43 | typedef std::string comment_type; |
---|
44 | |
---|
45 | /// Line number. |
---|
46 | typedef unsigned int size_type; |
---|
47 | }; |
---|
48 | |
---|
49 | template <typename K> |
---|
50 | class keyfile_parser : public basic_keyfile_parser<K> |
---|
51 | { |
---|
52 | public: |
---|
53 | // Workaround for GCC bug. |
---|
54 | typedef keyfile_base::error error; |
---|
55 | // This is the correct form, but is not currently supported by |
---|
56 | // GCC. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14258 |
---|
57 | // using typename basic_keyfile_parser<K>::error; |
---|
58 | |
---|
59 | using basic_keyfile_parser<K>::group; |
---|
60 | using basic_keyfile_parser<K>::group_set; |
---|
61 | using basic_keyfile_parser<K>::key; |
---|
62 | using basic_keyfile_parser<K>::key_set; |
---|
63 | using basic_keyfile_parser<K>::value; |
---|
64 | using basic_keyfile_parser<K>::value_set; |
---|
65 | using basic_keyfile_parser<K>::comment; |
---|
66 | using basic_keyfile_parser<K>::comment_set; |
---|
67 | using basic_keyfile_parser<K>::line_number; |
---|
68 | |
---|
69 | keyfile_parser(): |
---|
70 | basic_keyfile_parser<K>() |
---|
71 | {} |
---|
72 | |
---|
73 | virtual ~keyfile_parser() |
---|
74 | {} |
---|
75 | |
---|
76 | virtual void |
---|
77 | parse_line (std::string const& line) |
---|
78 | { |
---|
79 | if (comment_set == true) |
---|
80 | { |
---|
81 | comment.clear(); |
---|
82 | comment_set = false; |
---|
83 | } |
---|
84 | if (group_set == true) |
---|
85 | { |
---|
86 | // The group isn't cleared |
---|
87 | group_set = false; |
---|
88 | } |
---|
89 | if (key_set == true) |
---|
90 | { |
---|
91 | key.clear(); |
---|
92 | key_set = false; |
---|
93 | } |
---|
94 | if (value_set == true) |
---|
95 | { |
---|
96 | value.clear(); |
---|
97 | value_set = false; |
---|
98 | } |
---|
99 | |
---|
100 | if (line.length() == 0) |
---|
101 | { |
---|
102 | // Empty line; do nothing. |
---|
103 | } |
---|
104 | else if (line[0] == '#') // Comment line |
---|
105 | { |
---|
106 | if (!comment.empty()) |
---|
107 | comment += '\n'; |
---|
108 | comment += line.substr(1); |
---|
109 | } |
---|
110 | else if (line[0] == '[') // Group |
---|
111 | { |
---|
112 | std::string::size_type fpos = line.find_first_of(']'); |
---|
113 | std::string::size_type lpos = line.find_last_of(']'); |
---|
114 | if (fpos == std::string::npos || lpos == std::string::npos || |
---|
115 | fpos != lpos) |
---|
116 | throw error(line_number, keyfile_base::INVALID_GROUP, line); |
---|
117 | group = line.substr(1, fpos - 1); |
---|
118 | |
---|
119 | if (group.length() == 0) |
---|
120 | throw error(line_number, keyfile_base::INVALID_GROUP, line); |
---|
121 | |
---|
122 | comment_set = true; |
---|
123 | group_set = true; |
---|
124 | } |
---|
125 | else // Item |
---|
126 | { |
---|
127 | std::string::size_type pos = line.find_first_of('='); |
---|
128 | if (pos == std::string::npos) |
---|
129 | throw error(line_number, keyfile_base::INVALID_LINE, line); |
---|
130 | if (pos == 0) |
---|
131 | throw error(line_number, keyfile_base::NO_KEY, line); |
---|
132 | key = line.substr(0, pos); |
---|
133 | if (pos == line.length() - 1) |
---|
134 | value = ""; |
---|
135 | else |
---|
136 | value = line.substr(pos + 1); |
---|
137 | |
---|
138 | // No group specified |
---|
139 | if (group.empty()) |
---|
140 | throw error(line_number, keyfile_base::NO_GROUP, line); |
---|
141 | |
---|
142 | comment_set = true; |
---|
143 | key_set = true; |
---|
144 | value_set = true; |
---|
145 | } |
---|
146 | |
---|
147 | basic_keyfile_parser<K>::parse_line(line); |
---|
148 | } |
---|
149 | }; |
---|
150 | |
---|
151 | /** |
---|
152 | * Configuration file parser. This class loads an INI-style |
---|
153 | * configuration file from a file or stream. The format is |
---|
154 | * documented in schroot.conf(5). |
---|
155 | */ |
---|
156 | typedef basic_keyfile<keyfile_traits, keyfile_parser<keyfile_traits> > keyfile; |
---|
157 | |
---|
158 | } |
---|
159 | |
---|
160 | #endif /* SBUILD_KEYFILE_H */ |
---|
161 | |
---|
162 | /* |
---|
163 | * Local Variables: |
---|
164 | * mode:C++ |
---|
165 | * End: |
---|
166 | */ |
---|