19 #ifndef SBUILD_BASIC_KEYFILE_H 20 #define SBUILD_BASIC_KEYFILE_H 23 #include <sbuild/sbuild-log.h> 24 #include <sbuild/sbuild-keyfile-base.h> 25 #include <sbuild/sbuild-parse-error.h> 26 #include <sbuild/sbuild-parse-value.h> 27 #include <sbuild/sbuild-types.h> 29 #include <sbuild/sbuild-util.h> 36 #include <boost/format.hpp> 70 typename K::group_name_type
group;
76 typename K::key_type
key;
137 template <
typename K,
typename P = basic_keyfile_parser<K> >
167 typedef std::tuple<key_type,value_type,comment_type,size_type>
174 typedef std::tuple<group_name_type,item_map_type,comment_type,size_type>
group_type;
220 get_keys (group_name_type
const&
group)
const;
231 check_keys (group_name_type
const&
group,
232 key_list
const& keys)
const;
241 has_group (group_name_type
const&
group)
const;
251 has_key (group_name_type
const&
group,
252 key_type
const&
key)
const;
262 set_group (group_name_type
const&
group,
274 set_group (group_name_type
const&
group,
285 get_comment (group_name_type
const&
group)
const;
295 get_comment (group_name_type
const&
group,
296 key_type
const&
key)
const;
305 get_line (group_name_type
const&
group)
const;
315 get_line (group_name_type
const&
group,
316 key_type
const&
key)
const;
328 template <
typename T>
335 <<
", key=" << key << std::endl;
336 const item_type *found_item = find_item(group, key);
339 value_type
const& strval(std::get<1>(*found_item));
347 size_type line = get_line(group, key);
350 error ep(line, group, key, PASSTHROUGH_LGK, e);
355 error ep(group, key, PASSTHROUGH_GK, e);
377 template <
typename T>
384 bool status = get_value(group, key, value);
385 check_priority(group, key, priority, status);
399 get_locale_string (group_name_type
const&
group,
401 value_type&
value)
const;
415 get_locale_string (group_name_type
const& group,
418 value_type& value)
const;
431 get_locale_string (group_name_type
const& group,
433 std::string
const& locale,
434 value_type& value)
const;
450 get_locale_string (group_name_type
const& group,
452 std::string
const& locale,
454 value_type& value)
const;
468 template <
typename C>
474 value_type item_value;
475 if (get_value(group, key, item_value))
479 for (
typename value_list::const_iterator pos = items.begin();
484 typename C::value_type tmp;
492 size_type line = get_line(group, key);
495 error ep(line, group, key, PASSTHROUGH_LGK, e);
500 error ep(group, key, PASSTHROUGH_GK, e);
506 container.push_back(tmp);
527 template <
typename C>
534 bool status = get_list_value(group, key, container);
535 check_priority(group, key, priority, status);
551 template <
typename C>
557 value_type item_value;
558 if (get_value(group, key, item_value))
562 for (
typename value_list::const_iterator pos = items.begin();
567 typename C::value_type tmp;
575 size_type line = get_line(group, key);
578 error ep(line, group, key, PASSTHROUGH_LGK, e);
583 error ep(group, key, PASSTHROUGH_GK, e);
589 container.insert(tmp);
610 template <
typename C>
617 bool status = get_set_value(group, key, container);
618 check_priority(group, key, priority, status);
630 template <
typename T>
636 set_value(group, key, value, comment_type());
648 template <
typename T>
655 set_value(group, key, value, comment, 0);
668 template <
typename T>
676 std::ostringstream os;
677 os.imbue(std::locale::classic());
678 os << std::boolalpha <<
value;
680 set_group(group,
"");
681 group_type *found_group = find_group(group);
682 assert (found_group != 0);
684 item_map_type& items = std::get<1>(*found_group);
686 typename item_map_type::iterator pos = items.find(key);
687 if (pos != items.end())
690 (
typename item_map_type::value_type(key,
704 template <
typename I>
711 set_list_value(group, key, begin, end, comment_type());
724 template <
typename I>
732 set_list_value (group, key, begin, end, comment, 0);
746 template <
typename I>
757 for (I pos = begin; pos !=
end;)
759 std::ostringstream os;
760 os.imbue(std::locale::classic());
761 os << std::boolalpha << *pos;
766 strval += this->separator;
770 set_value (group, key, strval, comment, line);
782 template <
typename I>
789 std::vector<typename std::iterator_traits<I>::value_type> l(begin, end);
790 std::sort(l.begin(), l.end());
791 set_list_value(group, key, l.begin(), l.end());
804 template <
typename I>
812 std::vector<typename std::iterator_traits<I>::value_type> l(begin, end);
813 std::sort(l.begin(), l.end());
814 set_list_value(group, key, l.begin(), l.end(),
comment);
828 template <
typename I>
837 std::vector<typename std::iterator_traits<I>::value_type> l(begin, end);
838 std::sort(l.begin(), l.end());
839 set_list_value(group, key, l.begin(), l.end(),
comment, line);
848 remove_group (group_name_type
const& group);
857 remove_key (group_name_type
const& group,
858 key_type
const& key);
876 template <
typename _K,
typename _P>
888 template <
class charT,
class traits>
890 std::basic_istream<charT,traits>&
900 while (std::getline(stream, line))
902 state.parse_line(line);
908 throw error(state.line_number, DUPLICATE_GROUP, state.group);
910 tmp.
set_group(state.group, state.comment, state.line_number);
914 if (state.key_set && state.value_set)
916 if (tmp.
has_key(state.group, state.key))
917 throw error(state.line_number, state.group, DUPLICATE_KEY, state.key);
919 tmp.
set_value(state.group, state.key, state.value, state.comment, state.line_number);
938 template <
class charT,
class traits>
940 std::basic_ostream<charT,traits>&
941 operator << (std::basic_ostream<charT,traits>& stream,
944 size_type group_count = 0;
946 for (
typename group_map_type::const_iterator gp = kf.groups.begin();
947 gp != kf.groups.end();
953 group_type
const& group = gp->second;
954 group_name_type
const& groupname = std::get<0>(
group);
957 if (comment.length() > 0)
958 print_comment(comment, stream);
960 stream <<
'[' << groupname <<
']' <<
'\n';
962 item_map_type
const& items(std::get<1>(group));
963 for (
typename item_map_type::const_iterator it = items.begin();
968 key_type
const&
key(std::get<0>(item));
969 value_type
const&
value(std::get<1>(item));
970 comment_type
const&
comment(std::get<2>(item));
972 if (comment.length() > 0)
973 print_comment(comment, stream);
975 stream << key <<
'=' << value <<
'\n';
990 find_group (group_name_type
const& group)
const;
999 find_group (group_name_type
const& group);
1009 find_item (group_name_type
const& group,
1010 key_type
const& key)
const;
1020 find_item (group_name_type
const& group,
1021 key_type
const& key);
1032 check_priority (group_name_type
const& group,
1033 key_type
const& key,
1049 print_comment (comment_type
const&
comment,
1050 std::ostream& stream);
1070 template<
class C,
typename T>
1073 T (C::* method)()
const,
1075 group_name_type
const& group,
1076 key_type
const& key)
1081 basic_keyfile.
set_value(group, key, (
object.*method)());
1083 catch (std::runtime_error
const& e)
1085 throw error(group, key, PASSTHROUGH_GK, e);
1101 template<
class C,
typename T>
1104 T
const& (C::* method)()
const,
1106 group_name_type
const& group,
1107 key_type
const& key)
1112 basic_keyfile.
set_value(group, key, (
object.*method)());
1114 catch (std::runtime_error
const& e)
1116 throw error(group, key, PASSTHROUGH_GK, e);
1133 template<
class C,
typename T>
1136 T (C::* method)()
const,
1138 group_name_type
const& group,
1139 key_type
const& key)
1145 (
object.*method)().
begin(),
1146 (
object.*method)().
end());
1148 catch (std::runtime_error
const& e)
1150 throw error(group, key, PASSTHROUGH_GK, e);
1168 template<
class C,
typename T>
1171 T
const& (C::* method)()
const,
1173 group_name_type
const& group,
1174 key_type
const& key)
1180 (
object.*method)().
begin(),
1181 (
object.*method)().
end());
1183 catch (std::runtime_error
const& e)
1185 throw error(group, key, PASSTHROUGH_GK, e);
1202 template<
class C,
typename T>
1205 T (C::* method)()
const,
1207 group_name_type
const& group,
1208 key_type
const& key)
1214 (
object.*method)().
begin(),
1215 (
object.*method)().
end());
1217 catch (std::runtime_error
const& e)
1219 throw error(group, key, PASSTHROUGH_GK, e);
1237 template<
class C,
typename T>
1240 T
const& (C::* method)()
const,
1242 group_name_type
const& group,
1243 key_type
const& key)
1249 (
object.*method)().
begin(),
1250 (
object.*method)().
end());
1252 catch (std::runtime_error
const& e)
1254 throw error(group, key, PASSTHROUGH_GK, e);
1272 template<
class C,
typename T>
1275 void (C::* method)(T param),
1277 group_name_type
const& group,
1278 key_type
const& key,
1284 if (basic_keyfile.
get_value(group, key, priority, value)
1286 (
object.*method)(
value);
1288 catch (std::runtime_error
const& e)
1290 size_type line = basic_keyfile.
get_line(group, key);
1292 throw error(line, group, key, PASSTHROUGH_LGK, e);
1294 throw error(group, key, PASSTHROUGH_GK, e);
1312 template<
class C,
typename T>
1315 void (C::* method)(T
const& param),
1317 group_name_type
const& group,
1318 key_type
const& key,
1324 if (basic_keyfile.
get_value(group, key, priority, value)
1326 (
object.*method)(
value);
1328 catch (std::runtime_error
const& e)
1330 size_type line = basic_keyfile.
get_line(group, key);
1332 throw error(line, group, key, PASSTHROUGH_LGK, e);
1334 throw error(group, key, PASSTHROUGH_GK, e);
1352 template<
class C,
typename T>
1355 void (C::* method)(T param),
1357 group_name_type
const& group,
1358 key_type
const& key,
1366 (
object.*method)(
value);
1368 catch (std::runtime_error
const& e)
1370 size_type line = basic_keyfile.
get_line(group, key);
1372 throw error(line, group, key, PASSTHROUGH_LGK, e);
1374 throw error(group, key, PASSTHROUGH_GK, e);
1395 template<
class C,
typename T>
1398 void (C::* method)(T
const& param),
1400 group_name_type
const& group,
1401 key_type
const& key,
1409 (
object.*method)(
value);
1411 catch (std::runtime_error
const& e)
1413 size_type line = basic_keyfile.
get_line(group, key);
1415 throw error(line, group, key, PASSTHROUGH_LGK, e);
1417 throw error(group, key, PASSTHROUGH_GK, e);
1437 template<
class C,
typename T>
1440 void (C::* method)(T param),
1442 group_name_type
const& group,
1443 key_type
const& key,
1449 if (basic_keyfile.
get_set_value(group, key, priority, value)
1451 (
object.*method)(
value);
1453 catch (std::runtime_error
const& e)
1455 size_type line = basic_keyfile.
get_line(group, key);
1457 throw error(line, group, key, PASSTHROUGH_LGK, e);
1459 throw error(group, key, PASSTHROUGH_GK, e);
1480 template<
class C,
typename T>
1483 void (C::* method)(T
const& param),
1485 group_name_type
const& group,
1486 key_type
const& key,
1492 if (basic_keyfile.
get_set_value(group, key, priority, value)
1494 (
object.*method)(
value);
1496 catch (std::runtime_error
const& e)
1498 size_type line = basic_keyfile.
get_line(group, key);
1500 throw error(line, group, key, PASSTHROUGH_LGK, e);
1502 throw error(group, key, PASSTHROUGH_GK, e);
1511 #include <sbuild/sbuild-basic-keyfile.tcc> static void set_object_list_value(C const &object, T const &(C::*method)() const, basic_keyfile &basic_keyfile, group_name_type const &group, key_type const &key)
Set a key list value from an object method return value.
Definition: sbuild-basic-keyfile.h:1170
static void get_object_value(C &object, void(C::*method)(T const ¶m), basic_keyfile const &basic_keyfile, group_name_type const &group, key_type const &key, basic_keyfile::priority priority)
Get a key value and set it by reference in an object using an object method.
Definition: sbuild-basic-keyfile.h:1314
K::size_type line_number
Line number.
Definition: sbuild-basic-keyfile.h:94
K::comment_type comment_type
Comment.
Definition: sbuild-basic-keyfile.h:151
std::vector< group_name_type > group_list
Vector of groups.
Definition: sbuild-basic-keyfile.h:157
virtual void end()
Stop processing input.
Definition: sbuild-basic-keyfile.h:127
Debian source builder components.
Definition: sbuild-auth-null.h:24
bool get_set_value(group_name_type const &group, key_type const &key, priority priority, C &container) const
Get a key value as a set.
Definition: sbuild-basic-keyfile.h:612
void set_value(group_name_type const &group, key_type const &key, T const &value, comment_type const &comment)
Set a key value.
Definition: sbuild-basic-keyfile.h:650
priority
Configuration parameter priority.
Definition: sbuild-keyfile-base.h:47
void set_set_value(group_name_type const &group, key_type const &key, I begin, I end, comment_type const &comment)
Set a key value from a set.
Definition: sbuild-basic-keyfile.h:806
virtual ~basic_keyfile_parser()
The destructor.
Definition: sbuild-basic-keyfile.h:65
K::value_type value_type
Value.
Definition: sbuild-basic-keyfile.h:148
Notification messages.
Definition: sbuild-log.h:31
bool key_set
Key name is set.
Definition: sbuild-basic-keyfile.h:79
static void set_object_value(C const &object, T(C::*method)() const, basic_keyfile &basic_keyfile, group_name_type const &group, key_type const &key)
Set a key value from an object method return value.
Definition: sbuild-basic-keyfile.h:1072
bool comment_set
Comment is set.
Definition: sbuild-basic-keyfile.h:91
void set_group(group_name_type const &group, comment_type const &comment)
Set a group.
bool get_set_value(group_name_type const &group, key_type const &key, C &container) const
Get a key value as a set.
Definition: sbuild-basic-keyfile.h:553
std::tuple< group_name_type, item_map_type, comment_type, size_type > group_type
Group-items-comment-line tuple.
Definition: sbuild-basic-keyfile.h:174
value_type separator
The separator used as a list item delimiter.
Definition: sbuild-basic-keyfile.h:1055
void set_set_value(group_name_type const &group, key_type const &key, I begin, I end, comment_type const &comment, size_type line)
Set a key value from a set.
Definition: sbuild-basic-keyfile.h:830
std::ostream & log_debug(debug_level level)
Log a debug message.
Definition: sbuild-log.cc:110
static void set_object_value(C const &object, T const &(C::*method)() const, basic_keyfile &basic_keyfile, group_name_type const &group, key_type const &key)
Set a key value from an object method return value reference.
Definition: sbuild-basic-keyfile.h:1103
void log_exception_warning(std::exception const &e)
Log an exception as a warning.
Definition: sbuild-log.cc:143
Basic keyfile parser template.
Definition: sbuild-basic-keyfile.h:44
bool get_list_value(group_name_type const &group, key_type const &key, priority priority, C &container) const
Get a key value as a list.
Definition: sbuild-basic-keyfile.h:529
static void get_object_value(C &object, void(C::*method)(T param), basic_keyfile const &basic_keyfile, group_name_type const &group, key_type const &key, basic_keyfile::priority priority)
Get a key value and set it in an object using an object method.
Definition: sbuild-basic-keyfile.h:1274
bool group_set
Group name is set.
Definition: sbuild-basic-keyfile.h:73
void set_list_value(group_name_type const &group, key_type const &key, I begin, I end)
Set a key value from a list.
Definition: sbuild-basic-keyfile.h:706
Configuration file parser.
Definition: sbuild-basic-keyfile.h:138
virtual void parse_line(std::string const &line)
Parse a line of input.
Definition: sbuild-basic-keyfile.h:117
group_map_type groups
The top-level groups.
Definition: sbuild-basic-keyfile.h:1053
std::vector< S > split_string(S const &value, S const &separator)
Split a string into a string_list.
Definition: sbuild-util.h:152
bool get_list_value(group_name_type const &group, key_type const &key, C &container) const
Get a key value as a list.
Definition: sbuild-basic-keyfile.h:470
std::vector< value_type > value_list
Vector of values.
Definition: sbuild-basic-keyfile.h:160
std::map< key_type, item_type > item_map_type
Map between key name and key-value-comment tuple.
Definition: sbuild-basic-keyfile.h:171
void set_list_value(group_name_type const &group, key_type const &key, I begin, I end, comment_type const &comment, size_type line)
Set a key value from a list.
Definition: sbuild-basic-keyfile.h:748
bool has_group(group_name_type const &group) const
Check if a group exists.
void set_list_value(group_name_type const &group, key_type const &key, I begin, I end, comment_type const &comment)
Set a key value from a list.
Definition: sbuild-basic-keyfile.h:726
std::tuple< key_type, value_type, comment_type, size_type > item_type
Key-value-comment-line tuple.
Definition: sbuild-basic-keyfile.h:168
virtual void begin()
Start processing input.
Definition: sbuild-basic-keyfile.h:101
K::value_type value
Value.
Definition: sbuild-basic-keyfile.h:82
K::key_type key_type
Key name.
Definition: sbuild-basic-keyfile.h:145
mntstream & operator>>(mntstream &stream, mntstream::mntentry &entry)
The overloaded extraction operator.
Definition: sbuild-mntstream.cc:168
void set_value(group_name_type const &group, key_type const &key, T const &value, comment_type const &comment, size_type line)
Set a key value.
Definition: sbuild-basic-keyfile.h:670
K::group_name_type group_name_type
Group name.
Definition: sbuild-basic-keyfile.h:142
bool get_value(group_name_type const &group, key_type const &key, T &value) const
Get a key value.
Definition: sbuild-basic-keyfile.h:330
static void get_object_list_value(C &object, void(C::*method)(T param), basic_keyfile const &basic_keyfile, group_name_type const &group, key_type const &key, basic_keyfile::priority priority)
Get a key list value and set it in an object using an object method.
Definition: sbuild-basic-keyfile.h:1354
std::map< group_name_type, group_type > group_map_type
Map between group name and group-items-comment tuple.
Definition: sbuild-basic-keyfile.h:177
K::group_name_type group
Group name.
Definition: sbuild-basic-keyfile.h:70
void set_value(group_name_type const &group, key_type const &key, T const &value)
Set a key value.
Definition: sbuild-basic-keyfile.h:632
Parse error.
Definition: sbuild-parse-error.h:35
static void set_object_list_value(C const &object, T(C::*method)() const, basic_keyfile &basic_keyfile, group_name_type const &group, key_type const &key)
Set a key list value from an object method return value.
Definition: sbuild-basic-keyfile.h:1135
std::vector< key_type > key_list
Vector of keys.
Definition: sbuild-basic-keyfile.h:180
Internationalisation functions.
System group database entry.
Definition: sbuild-util.h:795
static void get_object_list_value(C &object, void(C::*method)(T const ¶m), basic_keyfile const &basic_keyfile, group_name_type const &group, key_type const &key, basic_keyfile::priority priority)
Get a key list value and set it by reference in an object using an object method. ...
Definition: sbuild-basic-keyfile.h:1397
K::key_type key
Key name.
Definition: sbuild-basic-keyfile.h:76
K::size_type size_type
Line number.
Definition: sbuild-basic-keyfile.h:154
bool get_value(group_name_type const &group, key_type const &key, priority priority, T &value) const
Get a key value.
Definition: sbuild-basic-keyfile.h:379
void set_set_value(group_name_type const &group, key_type const &key, I begin, I end)
Set a key value from a set.
Definition: sbuild-basic-keyfile.h:784
K::comment_type comment
Comment.
Definition: sbuild-basic-keyfile.h:88
Informational messages.
Definition: sbuild-log.h:32
void parse_value(std::string const &value, bool &parsed_value)
Parse a boolean value.
Definition: sbuild-parse-value.cc:49
Base class for key-value configuration file formats.
Definition: sbuild-keyfile-base.h:43
static void get_object_set_value(C &object, void(C::*method)(T param), basic_keyfile const &basic_keyfile, group_name_type const &group, key_type const &key, basic_keyfile::priority priority)
Get a key set value and set it in an object using an object method.
Definition: sbuild-basic-keyfile.h:1439
static void get_object_set_value(C &object, void(C::*method)(T const ¶m), basic_keyfile const &basic_keyfile, group_name_type const &group, key_type const &key, basic_keyfile::priority priority)
Get a key set value and set it by reference in an object using an object method.
Definition: sbuild-basic-keyfile.h:1482
basic_keyfile_parser()
The constructor.
Definition: sbuild-basic-keyfile.h:51
bool value_set
Value is set.
Definition: sbuild-basic-keyfile.h:85
static void set_object_set_value(C const &object, T(C::*method)() const, basic_keyfile &basic_keyfile, group_name_type const &group, key_type const &key)
Set a key set value from an object method return value.
Definition: sbuild-basic-keyfile.h:1204
keyfile_base::error error
Exception type.
Definition: sbuild-basic-keyfile.h:48
static void set_object_set_value(C const &object, T const &(C::*method)() const, basic_keyfile &basic_keyfile, group_name_type const &group, key_type const &key)
Set a key set value from an object method return value.
Definition: sbuild-basic-keyfile.h:1239
size_type get_line(group_name_type const &group) const
Get a group line number.
P parse_type
Parse type.
Definition: sbuild-basic-keyfile.h:164
bool has_key(group_name_type const &group, key_type const &key) const
Check if a key exists.