diff options
author | Roger Leigh <rleigh@debian.org> | 2006-06-29 09:52:25 +0000 |
---|---|---|
committer | Roger Leigh <rleigh@debian.org> | 2006-06-29 09:52:25 +0000 |
commit | e8039d25fe67af2f5d6dcdadaf981a78feea9074 (patch) | |
tree | 2968fdb9a35f5e8c4afd7538ac68af9ee407ff5e | |
parent | 40050e1152d6767f59a37272f077ce473b544319 (diff) | |
download | schroot-e8039d25fe67af2f5d6dcdadaf981a78feea9074.tar.gz |
* test/sbuild-keyfile.cc: Add test for getting line numbers.
* sbuild/sbuild-keyfile.h:
(item_type, group_type): Add line number to both tuples.
(set_value): Add overloaded private method which adds a line
number to its arguments.
(operator >>): Set line numbers when deserialising.
* sbuild/sbuild-keyfile.cc
(get_line): New methods for getting the line numbers of groups and
keys.
(set_group): Add overloaded private method which adds a line
number to its arguments.
-rw-r--r-- | sbuild/sbuild-keyfile.cc | 38 | ||||
-rw-r--r-- | sbuild/sbuild-keyfile.h | 81 | ||||
-rw-r--r-- | test/sbuild-keyfile.cc | 8 |
3 files changed, 112 insertions, 15 deletions
diff --git a/sbuild/sbuild-keyfile.cc b/sbuild/sbuild-keyfile.cc index a763e2f9..eb349e52 100644 --- a/sbuild/sbuild-keyfile.cc +++ b/sbuild/sbuild-keyfile.cc @@ -109,12 +109,21 @@ void keyfile::set_group (std::string const& group, std::string const& comment) { + set_group(group, comment, 0); +} + +void +keyfile::set_group (std::string const& group, + std::string const& comment, + unsigned int line) +{ if (!has_group(group)) this->groups.insert (group_map_type::value_type(group, group_type(group, item_map_type(), - comment))); + comment, + line))); } std::string @@ -138,6 +147,27 @@ keyfile::get_comment (std::string const& group, return std::string(); } +unsigned int +keyfile::get_line (std::string const& group) const +{ + const keyfile::group_type *found_group = find_group(group); + if (found_group) + return std::tr1::get<3>(*found_group); + else + return 0; +} + +unsigned int +keyfile::get_line (std::string const& group, + std::string const& key) const +{ + const item_type *found_item = find_item(group, key); + if (found_item) + return std::tr1::get<3>(*found_item); + else + return 0; +} + bool keyfile::get_locale_string (std::string const& group, std::string const& key, @@ -232,7 +262,8 @@ keyfile::operator += (keyfile const& rhs) group_type const& group = gp->second; std::string const& groupname = std::tr1::get<0>(group); std::string const& comment = std::tr1::get<2>(group); - set_group(groupname, comment); + unsigned int const& line = std::tr1::get<3>(group); + set_group(groupname, comment, line); item_map_type const& items(std::tr1::get<1>(group)); for (item_map_type::const_iterator it = items.begin(); @@ -243,7 +274,8 @@ keyfile::operator += (keyfile const& rhs) std::string const& key(std::tr1::get<0>(item)); std::string const& value(std::tr1::get<1>(item)); std::string const& comment(std::tr1::get<2>(item)); - set_value(groupname, key, value, comment); + unsigned int const& line(std::tr1::get<3>(item)); + set_value(groupname, key, value, comment, line); } } return *this; diff --git a/sbuild/sbuild-keyfile.h b/sbuild/sbuild-keyfile.h index cb700b3c..906b924f 100644 --- a/sbuild/sbuild-keyfile.h +++ b/sbuild/sbuild-keyfile.h @@ -47,14 +47,15 @@ namespace sbuild class keyfile { private: - /// Key-value-comment tuple. - typedef std::tr1::tuple<std::string,std::string,std::string> item_type; + /// Key-value-comment-line tuple. + typedef std::tr1::tuple<std::string,std::string,std::string,unsigned int> + item_type; /// Map between key name and key-value-comment tuple. typedef std::map<std::string,item_type> item_map_type; - /// Group-items-comment tuple. - typedef std::tr1::tuple<std::string,item_map_type,std::string> group_type; + /// Group-items-comment-line tuple. + typedef std::tr1::tuple<std::string,item_map_type,std::string,unsigned int> group_type; /// Map between group name and group-items-comment tuple. typedef std::map<std::string,group_type> group_map_type; @@ -133,8 +134,8 @@ namespace sbuild std::string const& key) const; /** - * Set a group. The group will be created (and the comment set) - * only if the group does not already exist. + * Set a group. The group will be created (and the comment set) + * only if the group does not already exist. * * @param group the group to set. * @param comment the comment to set. @@ -143,8 +144,23 @@ namespace sbuild set_group (std::string const& group, std::string const& comment); + private: + /** + * Set a group. The group will be created (and the comment set) + * only if the group does not already exist. + * + * @param group the group to set. + * @param comment the comment to set. + * @param line the line number in the input file, or 0 otherwise. + */ + void + set_group (std::string const& group, + std::string const& comment, + unsigned int line); + + public: /** - * Get a group comment. + * Get a group comment. * * @param group the group to find. * @returns the comment. @@ -153,7 +169,7 @@ namespace sbuild get_comment (std::string const& group) const; /** - * Get a key comment. + * Get a key comment. * * @param group the group to find. * @param key the key to find. @@ -164,6 +180,26 @@ namespace sbuild std::string const& key) const; /** + * Get a group line number. + * + * @param group the group to find. + * @returns the line number, or 0 if not available. + */ + unsigned int + get_line (std::string const& group) const; + + /** + * Get a key line number. + * + * @param group the group to find. + * @param key the key to find. + * @returns the line number, or 0 if not available. + */ + unsigned int + get_line (std::string const& group, + std::string const& key) const; + + /** * Get a key value. * * @param group the group the key is in. @@ -403,6 +439,27 @@ namespace sbuild os.imbue(std::locale("C")); os << std::boolalpha << value; + set_value(group, key, os.str(), comment, 0); + } + + private: + /** + * Set a key value. + * + * @param group the group the key is in. + * @param key the key to set. + * @param value the value to get the key's value from. This must + * @param comment the comment for this key. + * @param line the line number in the input file, or 0 otherwise. + * allow output to an ostream. + */ + void + set_value (std::string const& group, + std::string const& key, + std::string const& value, + std::string const& comment, + unsigned int line) + { set_group(group, ""); group_type *found_group = find_group(group); assert (found_group != 0); // should not fail @@ -412,12 +469,12 @@ namespace sbuild item_map_type::iterator pos = items.find(key); if (pos != items.end()) items.erase(pos); - items.insert (item_map_type::value_type(key, - item_type(key, os.str(), comment))); + item_type(key, value, comment, line))); } + public: /** * Set a key value from a list. * @@ -565,7 +622,7 @@ namespace sbuild log_warning() << e.what() << std::endl; } else - tmp.set_group(group, comment); + tmp.set_group(group, comment, linecount); comment.clear(); } else // Item @@ -598,7 +655,7 @@ namespace sbuild log_warning() << e.what() << std::endl; } else - tmp.set_value(group, key, value, comment); + tmp.set_value(group, key, value, comment, linecount); comment.clear(); } } diff --git a/test/sbuild-keyfile.cc b/test/sbuild-keyfile.cc index cf7df1e0..9e39eb2a 100644 --- a/test/sbuild-keyfile.cc +++ b/test/sbuild-keyfile.cc @@ -39,6 +39,7 @@ class test_keyfile : public TestFixture CPPUNIT_TEST(test_get_value_fail); CPPUNIT_TEST(test_get_list_value); CPPUNIT_TEST(test_get_list_value_fail); + CPPUNIT_TEST(test_get_line); CPPUNIT_TEST(test_set_value); CPPUNIT_TEST(test_set_list_value); CPPUNIT_TEST(test_remove_group); @@ -182,6 +183,13 @@ public: // TODO: Test priority. // TODO: Test comments, when available. + void test_get_line() + { + CPPUNIT_ASSERT(this->kf->get_line("group2") == 10); + CPPUNIT_ASSERT(this->kf->get_line("group1", "age") == 4); + CPPUNIT_ASSERT(this->kf->get_line("group2", "name") == 11); + } + void test_set_value() { this->kf->set_value("group1", "name", "Adam Smith"); |