diff options
author | Josh Blum <josh@joshknows.com> | 2011-07-22 01:19:03 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2011-07-22 13:59:47 -0700 |
commit | da40a1aebc16ca05219883c18b2cf96989c470fd (patch) | |
tree | 24d3c902b2d5795755be3171388fef81a0da3a7a /host/lib/property_tree.cpp | |
parent | 833b5369378d5788dd430451803bf36a96dc3901 (diff) | |
download | uhd-da40a1aebc16ca05219883c18b2cf96989c470fd.tar.gz uhd-da40a1aebc16ca05219883c18b2cf96989c470fd.tar.bz2 uhd-da40a1aebc16ca05219883c18b2cf96989c470fd.zip |
uhd: replaced boost filesystem path with fs_path in property tree
Diffstat (limited to 'host/lib/property_tree.cpp')
-rw-r--r-- | host/lib/property_tree.cpp | 102 |
1 files changed, 73 insertions, 29 deletions
diff --git a/host/lib/property_tree.cpp b/host/lib/property_tree.cpp index 58cd3f6ea..50e82a6ba 100644 --- a/host/lib/property_tree.cpp +++ b/host/lib/property_tree.cpp @@ -22,17 +22,63 @@ #include <boost/make_shared.hpp> #include <iostream> +using namespace uhd; + +/*********************************************************************** + * Helper function to iterate through paths + **********************************************************************/ +#include <boost/tokenizer.hpp> +#define path_tokenizer(path) \ + boost::tokenizer<boost::char_separator<char> > \ + (path, boost::char_separator<char>("/")) + +/*********************************************************************** + * Property path implementation wrapper + **********************************************************************/ +fs_path::fs_path(void): std::string(){} +fs_path::fs_path(const char *p): std::string(p){} +fs_path::fs_path(const std::string &p): std::string(p){} + +std::string fs_path::leaf(void) const{ + const size_t pos = this->rfind("/"); + if (pos == std::string::npos) return *this; + return this->substr(pos+1); +} + +fs_path fs_path::branch_path(void) const{ + const size_t pos = this->rfind("/"); + if (pos == std::string::npos) return *this; + return fs_path(this->substr(0, pos)); +} + +fs_path uhd::operator/(const fs_path &lhs, const fs_path &rhs){ + //strip trailing slash on left-hand-side + if (not lhs.empty() and *lhs.rbegin() == '/'){ + return fs_path(lhs.substr(0, lhs.size()-1)) / rhs; + } + + //strip leading slash on right-hand-side + if (not rhs.empty() and *rhs.begin() == '/'){ + return lhs / fs_path(rhs.substr(1)); + } + + return fs_path(lhs + "/" + rhs); +} + +/*********************************************************************** + * Property tree implementation + **********************************************************************/ class property_tree_impl : public uhd::property_tree{ public: - property_tree_impl(const path_type &root = path_type()): + property_tree_impl(const fs_path &root = fs_path()): _root(root) { _guts = boost::make_shared<tree_guts_type>(); } - sptr subtree(const path_type &path_) const{ - const path_type path = _root / path_; + sptr subtree(const fs_path &path_) const{ + const fs_path path = _root / path_; boost::mutex::scoped_lock lock(_guts->mutex); property_tree_impl *subtree = new property_tree_impl(path); @@ -40,42 +86,39 @@ public: return sptr(subtree); } - void remove(const path_type &path_){ - const path_type path = _root / path_; + void remove(const fs_path &path_){ + const fs_path path = _root / path_; boost::mutex::scoped_lock lock(_guts->mutex); node_type *parent = NULL; node_type *node = &_guts->root; - BOOST_FOREACH(const path_type &branch, path){ - const std::string name = branch.string(); + BOOST_FOREACH(const std::string &name, path_tokenizer(path)){ if (not node->has_key(name)) throw_path_not_found(path); parent = node; node = &(*node)[name]; } if (parent == NULL) throw uhd::runtime_error("Cannot uproot"); - parent->pop(path_type(path.leaf()).string()); + parent->pop(fs_path(path.leaf())); } - bool exists(const path_type &path_) const{ - const path_type path = _root / path_; + bool exists(const fs_path &path_) const{ + const fs_path path = _root / path_; boost::mutex::scoped_lock lock(_guts->mutex); node_type *node = &_guts->root; - BOOST_FOREACH(const path_type &branch, path){ - const std::string name = branch.string(); + BOOST_FOREACH(const std::string &name, path_tokenizer(path)){ if (not node->has_key(name)) return false; node = &(*node)[name]; } return true; } - std::vector<std::string> list(const path_type &path_) const{ - const path_type path = _root / path_; + std::vector<std::string> list(const fs_path &path_) const{ + const fs_path path = _root / path_; boost::mutex::scoped_lock lock(_guts->mutex); node_type *node = &_guts->root; - BOOST_FOREACH(const path_type &branch, path){ - const std::string name = branch.string(); + BOOST_FOREACH(const std::string &name, path_tokenizer(path)){ if (not node->has_key(name)) throw_path_not_found(path); node = &(*node)[name]; } @@ -83,37 +126,35 @@ public: return node->keys(); } - void _create(const path_type &path_, const boost::shared_ptr<void> &prop){ - const path_type path = _root / path_; + void _create(const fs_path &path_, const boost::shared_ptr<void> &prop){ + const fs_path path = _root / path_; boost::mutex::scoped_lock lock(_guts->mutex); node_type *node = &_guts->root; - BOOST_FOREACH(const path_type &branch, path){ - const std::string name = branch.string(); + BOOST_FOREACH(const std::string &name, path_tokenizer(path)){ if (not node->has_key(name)) (*node)[name] = node_type(); node = &(*node)[name]; } - if (node->prop.get() != NULL) throw uhd::runtime_error("Cannot create! Property already exists at: " + path.string()); + if (node->prop.get() != NULL) throw uhd::runtime_error("Cannot create! Property already exists at: " + path); node->prop = prop; } - boost::shared_ptr<void> &_access(const path_type &path_) const{ - const path_type path = _root / path_; + boost::shared_ptr<void> &_access(const fs_path &path_) const{ + const fs_path path = _root / path_; boost::mutex::scoped_lock lock(_guts->mutex); node_type *node = &_guts->root; - BOOST_FOREACH(const path_type &branch, path){ - const std::string name = branch.string(); + BOOST_FOREACH(const std::string &name, path_tokenizer(path)){ if (not node->has_key(name)) throw_path_not_found(path); node = &(*node)[name]; } - if (node->prop.get() == NULL) throw uhd::runtime_error("Cannot access! Property uninitialized at: " + path.string()); + if (node->prop.get() == NULL) throw uhd::runtime_error("Cannot access! Property uninitialized at: " + path); return node->prop; } private: - void throw_path_not_found(const path_type &path) const{ - throw uhd::lookup_error("Path not found in tree: " + path.string()); + void throw_path_not_found(const fs_path &path) const{ + throw uhd::lookup_error("Path not found in tree: " + path); } //basic structural node element @@ -129,9 +170,12 @@ private: //members, the tree and root prefix boost::shared_ptr<tree_guts_type> _guts; - const path_type _root; + const fs_path _root; }; +/*********************************************************************** + * Property tree factory + **********************************************************************/ uhd::property_tree::sptr uhd::property_tree::make(void){ return sptr(new property_tree_impl()); } |