1
/* Copyright © 2005-2006 Roger Leigh <rleigh@debian.org>
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 2 of the License, or
6
* (at your option) any later version.
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.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18
*********************************************************************/
22
#include "sbuild-keyfile.h"
26
#include <boost/format.hpp>
29
using namespace sbuild;
37
keyfile::keyfile (std::string const& file):
41
std::ifstream fs(file.c_str());
44
fs.imbue(std::locale("C"));
49
throw error(parse_error::BAD_FILE, file);
53
keyfile::keyfile (std::istream& stream):
65
keyfile::get_groups () const
69
for (group_map_type::const_iterator pos = this->groups.begin();
70
pos != this->groups.end();
72
ret.push_back(pos->first);
78
keyfile::get_keys (std::string const& group) const
82
const group_type *found_group = find_group(group);
85
item_map_type const& items(std::tr1::get<1>(*found_group));
86
for (item_map_type::const_iterator pos = items.begin();
89
ret.push_back(pos->first);
96
keyfile::has_group (std::string const& group) const
98
return (find_group(group) != 0);
102
keyfile::has_key (std::string const& group,
103
std::string const& key) const
105
return (find_item(group, key) != 0);
109
keyfile::set_group (std::string const& group,
110
std::string const& comment)
112
set_group(group, comment, 0);
116
keyfile::set_group (std::string const& group,
117
std::string const& comment,
120
if (!has_group(group))
122
(group_map_type::value_type(group,
130
keyfile::get_comment (std::string const& group) const
132
const keyfile::group_type *found_group = find_group(group);
134
return std::tr1::get<2>(*found_group);
136
return std::string();
140
keyfile::get_comment (std::string const& group,
141
std::string const& key) const
143
const item_type *found_item = find_item(group, key);
145
return std::tr1::get<2>(*found_item);
147
return std::string();
151
keyfile::get_line (std::string const& group) const
153
const keyfile::group_type *found_group = find_group(group);
155
return std::tr1::get<3>(*found_group);
161
keyfile::get_line (std::string const& group,
162
std::string const& key) const
164
const item_type *found_item = find_item(group, key);
166
return std::tr1::get<3>(*found_item);
172
keyfile::get_locale_string (std::string const& group,
173
std::string const& key,
174
std::string& value) const
176
std::string localename = std::locale("").name();
177
std::string::size_type pos;
180
// Strip off any charset.
181
if ((pos = localename.find_first_of('.')) != std::string::npos)
182
localename = localename.substr(0, pos);
183
status = get_locale_string(group, key, localename, value);
185
// Strip off territory.
186
if (status == false &&
187
(pos = localename.find_first_of('_')) != std::string::npos)
189
localename = localename.substr(0, pos);
190
status = get_locale_string(group, key, localename, value);
193
// Fall back to non-localised version.
195
status = get_value(group, key, value);
201
keyfile::get_locale_string (std::string const& group,
202
std::string const& key,
204
std::string& value) const
206
bool status = get_locale_string(group, key, value);
207
check_priority(group, key, priority, status);
212
keyfile::get_locale_string (std::string const& group,
213
std::string const& key,
214
std::string const& locale,
215
std::string& value) const
217
std::string lkey = key + '[' + locale + ']';
218
return get_value(group, lkey, value);
222
keyfile::get_locale_string (std::string const& group,
223
std::string const& key,
224
std::string const& locale,
226
std::string& value) const
228
bool status = get_locale_string(group, key, locale, value);
229
check_priority(group, key, priority, status);
234
keyfile::remove_group (std::string const& group)
236
group_map_type::iterator pos = this->groups.find(group);
237
if (pos != this->groups.end())
238
this->groups.erase(pos);
242
keyfile::remove_key (std::string const& group,
243
std::string const& key)
245
group_type *found_group = find_group(group);
248
item_map_type& items = std::tr1::get<1>(*found_group);
249
item_map_type::iterator pos = items.find(key);
250
if (pos != items.end())
256
keyfile::operator += (keyfile const& rhs)
258
for (group_map_type::const_iterator gp = rhs.groups.begin();
259
gp != rhs.groups.end();
262
group_type const& group = gp->second;
263
std::string const& groupname = std::tr1::get<0>(group);
264
std::string const& comment = std::tr1::get<2>(group);
265
unsigned int const& line = std::tr1::get<3>(group);
266
set_group(groupname, comment, line);
268
item_map_type const& items(std::tr1::get<1>(group));
269
for (item_map_type::const_iterator it = items.begin();
273
item_type const& item = it->second;
274
std::string const& key(std::tr1::get<0>(item));
275
std::string const& value(std::tr1::get<1>(item));
276
std::string const& comment(std::tr1::get<2>(item));
277
unsigned int const& line(std::tr1::get<3>(item));
278
set_value(groupname, key, value, comment, line);
285
operator + (keyfile const& lhs,
293
const keyfile::group_type *
294
keyfile::find_group (std::string const& group) const
296
group_map_type::const_iterator pos = this->groups.find(group);
297
if (pos != this->groups.end())
303
keyfile::group_type *
304
keyfile::find_group (std::string const& group)
306
group_map_type::iterator pos = this->groups.find(group);
307
if (pos != this->groups.end())
313
const keyfile::item_type *
314
keyfile::find_item (std::string const& group,
315
std::string const& key) const
317
const group_type *found_group = find_group(group);
320
item_map_type const& items = std::tr1::get<1>(*found_group);
321
item_map_type::const_iterator pos = items.find(key);
322
if (pos != items.end())
330
keyfile::find_item (std::string const& group,
331
std::string const& key)
333
group_type *found_group = find_group(group);
336
item_map_type& items = std::tr1::get<1>(*found_group);
337
item_map_type::iterator pos = items.find(key);
338
if (pos != items.end())
346
keyfile::print_comment (std::string const& comment,
347
std::ostream& stream)
349
std::string::size_type last_pos = 0;
350
std::string::size_type pos = comment.find_first_of('\n', last_pos);
357
stream << '#' << comment.substr(last_pos, pos - last_pos) << '\n';
360
if (pos < comment.length() - 1)
363
pos = comment.find_first_of('\n', last_pos);
371
keyfile::check_priority (std::string const& group,
372
std::string const& key,
380
case PRIORITY_REQUIRED:
382
throw error(group, parse_error::MISSING_KEY, key);
393
case PRIORITY_DEPRECATED:
395
<< format(_("%1% chroot: A deprecated parameter \"%2%\" has been specified."))
399
<< _("This option will be removed in the future.") << std::endl;
401
case PRIORITY_OBSOLETE:
403
<< format(_("%1% chroot: An obsolete parameter \"%2%\" has been specified."))
407
<< _("This option has been removed, and no longer has any effect.") << std::endl;
409
case PRIORITY_DISALLOWED:
411
throw error(group, parse_error::DISALLOWED_KEY, key);