diff options
author | Josh Blum <josh@joshknows.com> | 2010-08-13 12:20:11 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2010-08-13 12:20:11 -0700 |
commit | 6d2899d18661e4cec5ae90692e08404ea48bc112 (patch) | |
tree | 5029781c6408cb2d0bdd25ca92a5becd44183138 | |
parent | 8bdc6cda2b5cc33b3eaf7f51d21d51887b77f45b (diff) | |
parent | 805d4a0cff00fb4e0071bb300436bb7eefb8fb16 (diff) | |
download | uhd-6d2899d18661e4cec5ae90692e08404ea48bc112.tar.gz uhd-6d2899d18661e4cec5ae90692e08404ea48bc112.tar.bz2 uhd-6d2899d18661e4cec5ae90692e08404ea48bc112.zip |
Merge branch 'split_string' into next
-rw-r--r-- | host/include/uhd/utils/algorithm.hpp | 21 | ||||
-rw-r--r-- | host/lib/types.cpp | 19 | ||||
-rw-r--r-- | host/lib/usrp/subdev_spec.cpp | 9 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/usrp2_impl.cpp | 12 | ||||
-rw-r--r-- | host/lib/utils/paths.cpp | 10 | ||||
-rw-r--r-- | host/lib/utils/warning.cpp | 8 | ||||
-rw-r--r-- | host/utils/uhd_usrp_probe.cpp | 4 |
7 files changed, 39 insertions, 44 deletions
diff --git a/host/include/uhd/utils/algorithm.hpp b/host/include/uhd/utils/algorithm.hpp index 1b5eacfa9..53c571e4e 100644 --- a/host/include/uhd/utils/algorithm.hpp +++ b/host/include/uhd/utils/algorithm.hpp @@ -22,6 +22,9 @@ #include <boost/range/begin.hpp> #include <boost/range/end.hpp> #include <boost/range/size.hpp> +#include <boost/algorithm/string.hpp> +#include <vector> +#include <string> /*! \file algorithm.hpp * Useful templated functions and classes that I like to pretend are part of stl. @@ -30,6 +33,24 @@ namespace std{ /*! + * Split a string at the separation characters. + * \param string the string to split + * \param sep the separator characters + * \return a range of strings + */ + inline std::vector<std::string> split_string( + const std::string &string, const std::string &sep = "\t " + ){ + std::vector<std::string> strings; + if (not string.empty()) boost::split( + // do not split an empty string: + // let me tell you about the time when boost::split segfaulted... + strings, string, boost::is_any_of(sep) + ); + return strings; + } + + /*! * A wrapper around std::copy that takes ranges instead of iterators. * * Copy the elements of the source range into the destination range. diff --git a/host/lib/types.cpp b/host/lib/types.cpp index 5c0fb1f42..f957cd83f 100644 --- a/host/lib/types.cpp +++ b/host/lib/types.cpp @@ -27,7 +27,6 @@ #include <uhd/types/otw_type.hpp> #include <uhd/types/io_type.hpp> #include <uhd/types/serial.hpp> -#include <boost/algorithm/string.hpp> #include <boost/math/special_functions/round.hpp> #include <boost/foreach.hpp> #include <boost/format.hpp> @@ -181,15 +180,10 @@ static std::string trim(const std::string &in){ } device_addr_t::device_addr_t(const std::string &args){ - //split the args at the semi-colons - std::vector<std::string> pairs; - boost::split(pairs, args, boost::is_any_of(arg_delim)); - BOOST_FOREACH(const std::string &pair, pairs){ + BOOST_FOREACH(const std::string &pair, std::split_string(args, arg_delim)){ if (trim(pair) == "") continue; - //split the key value pairs at the equals - std::vector<std::string> key_val; - boost::split(key_val, pair, boost::is_any_of(pair_delim)); + std::vector<std::string> key_val = std::split_string(pair, pair_delim); if (key_val.size() != 2) throw std::runtime_error("invalid args string: "+args); (*this)[trim(key_val.front())] = trim(key_val.back()); } @@ -238,13 +232,12 @@ mac_addr_t mac_addr_t::from_string(const std::string &mac_addr_str){ throw std::runtime_error("expected exactly 5 or 17 characters"); //split the mac addr hex string at the colons - std::vector<std::string> hex_strs; - boost::split(hex_strs, mac_addr_str, boost::is_any_of(":")); - for (size_t i = 0; i < hex_strs.size(); i++){ + size_t i = 0; + BOOST_FOREACH(const std::string &hex_str, std::split_string(mac_addr_str, ":")){ int hex_num; - std::istringstream iss(hex_strs[i]); + std::istringstream iss(hex_str); iss >> std::hex >> hex_num; - bytes[i] = boost::uint8_t(hex_num); + bytes[i++] = boost::uint8_t(hex_num); } } diff --git a/host/lib/usrp/subdev_spec.cpp b/host/lib/usrp/subdev_spec.cpp index 0f00e2f74..765eecb3d 100644 --- a/host/lib/usrp/subdev_spec.cpp +++ b/host/lib/usrp/subdev_spec.cpp @@ -16,7 +16,7 @@ // #include <uhd/usrp/subdev_spec.hpp> -#include <boost/algorithm/string.hpp> +#include <uhd/utils/algorithm.hpp> #include <boost/format.hpp> #include <boost/foreach.hpp> #include <stdexcept> @@ -35,12 +35,9 @@ subdev_spec_pair_t::subdev_spec_pair_t( } subdev_spec_t::subdev_spec_t(const std::string &markup){ - std::vector<std::string> pairs; - boost::split(pairs, markup, boost::is_any_of("\t ")); - BOOST_FOREACH(const std::string &pair, pairs){ + BOOST_FOREACH(const std::string &pair, std::split_string(markup)){ if (pair == "") continue; - std::vector<std::string> db_sd; - boost::split(db_sd, pair, boost::is_any_of(":")); + std::vector<std::string> db_sd = std::split_string(pair, ":"); switch(db_sd.size()){ case 1: this->push_back(subdev_spec_pair_t("", db_sd.front())); break; case 2: this->push_back(subdev_spec_pair_t(db_sd.front(), db_sd.back())); break; diff --git a/host/lib/usrp/usrp2/usrp2_impl.cpp b/host/lib/usrp/usrp2/usrp2_impl.cpp index 21f411afe..45362c20f 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.cpp +++ b/host/lib/usrp/usrp2/usrp2_impl.cpp @@ -21,7 +21,7 @@ #include <uhd/usrp/device_props.hpp> #include <uhd/utils/assert.hpp> #include <uhd/utils/static.hpp> -#include <boost/algorithm/string.hpp> +#include <uhd/utils/algorithm.hpp> #include <boost/assign/list_of.hpp> #include <boost/format.hpp> #include <boost/foreach.hpp> @@ -41,12 +41,6 @@ static const size_t DISCOVERY_TIMEOUT_MS = 100; /*********************************************************************** * Helper Functions **********************************************************************/ -std::vector<std::string> split_addrs(const std::string &addrs_str){ - std::vector<std::string> addrs; - boost::split(addrs, addrs_str, boost::is_any_of("\t ")); - return addrs; -} - template <class T> std::string num2str(T num){ return boost::lexical_cast<std::string>(num); } @@ -80,7 +74,7 @@ static uhd::device_addrs_t usrp2_find(const device_addr_t &hint){ } //if there are multiple addresses, just return good, dont test - std::vector<std::string> addrs = split_addrs(hint["addr"]); + std::vector<std::string> addrs = std::split_string(hint["addr"]); if (addrs.size() > 1){ device_addr_t new_addr; new_addr["type"] = "usrp2"; @@ -155,7 +149,7 @@ static device::sptr usrp2_make(const device_addr_t &device_addr){ std::vector<udp_simple::sptr> ctrl_transports; std::vector<udp_zero_copy::sptr> data_transports; - BOOST_FOREACH(const std::string &addr, split_addrs(device_addr["addr"])){ + BOOST_FOREACH(const std::string &addr, std::split_string(device_addr["addr"])){ ctrl_transports.push_back(udp_simple::make_connected( addr, num2str(USRP2_UDP_CTRL_PORT) )); diff --git a/host/lib/utils/paths.cpp b/host/lib/utils/paths.cpp index 4029bd989..9ac20d855 100644 --- a/host/lib/utils/paths.cpp +++ b/host/lib/utils/paths.cpp @@ -17,7 +17,7 @@ #include "constants.hpp" #include <uhd/config.hpp> -#include <boost/algorithm/string.hpp> +#include <uhd/utils/algorithm.hpp> #include <boost/program_options.hpp> #include <boost/filesystem.hpp> #include <boost/foreach.hpp> @@ -58,15 +58,9 @@ static std::vector<fs::path> get_env_paths(const std::string &var_name){ po::store(po::parse_environment(desc, boost::bind(&name_mapper, var_name, _1)), vm); po::notify(vm); - //split the path at the path separators - std::vector<std::string> path_strings; - if (not var_value.empty()) boost::split(//dont split empty strings - path_strings, var_value, boost::is_any_of(env_path_sep) - ); - //convert to filesystem path, filter blank paths std::vector<fs::path> paths; - BOOST_FOREACH(std::string &path_string, path_strings){ + BOOST_FOREACH(const std::string &path_string, std::split_string(var_value, env_path_sep)){ if (path_string.empty()) continue; paths.push_back(fs::system_complete(path_string)); } diff --git a/host/lib/utils/warning.cpp b/host/lib/utils/warning.cpp index ae4d4c7aa..8a7d35a23 100644 --- a/host/lib/utils/warning.cpp +++ b/host/lib/utils/warning.cpp @@ -16,7 +16,7 @@ // #include <uhd/utils/warning.hpp> -#include <boost/algorithm/string.hpp> +#include <uhd/utils/algorithm.hpp> #include <boost/foreach.hpp> #include <iostream> #include <vector> @@ -24,13 +24,9 @@ using namespace uhd; void uhd::print_warning(const std::string &msg){ - //extract the message lines - std::vector<std::string> lines; - boost::split(lines, msg, boost::is_any_of("\n")); - //print the warning message std::cerr << std::endl << "Warning:" << std::endl; - BOOST_FOREACH(const std::string &line, lines){ + BOOST_FOREACH(const std::string &line, std::split_string(msg, "\n")){ std::cerr << " " << line << std::endl; } } diff --git a/host/utils/uhd_usrp_probe.cpp b/host/utils/uhd_usrp_probe.cpp index 097317516..8947034d7 100644 --- a/host/utils/uhd_usrp_probe.cpp +++ b/host/utils/uhd_usrp_probe.cpp @@ -18,6 +18,7 @@ #include <uhd/utils/safe_main.hpp> #include <uhd/device.hpp> #include <uhd/types/ranges.hpp> +#include <uhd/utils/algorithm.hpp> #include <uhd/usrp/device_props.hpp> #include <uhd/usrp/mboard_props.hpp> #include <uhd/usrp/dboard_props.hpp> @@ -26,7 +27,6 @@ #include <uhd/usrp/subdev_props.hpp> #include <uhd/usrp/dboard_id.hpp> #include <boost/program_options.hpp> -#include <boost/algorithm/string.hpp> #include <boost/format.hpp> #include <boost/foreach.hpp> #include <iostream> @@ -43,7 +43,7 @@ static std::string make_border(const std::string &text){ std::stringstream ss; ss << boost::format(" _____________________________________________________") << std::endl; ss << boost::format(" /") << std::endl; - std::vector<std::string> lines; boost::split(lines, text, boost::is_any_of("\n")); + std::vector<std::string> lines = std::split_string(text, "\n"); while (lines.back() == "") lines.pop_back(); //strip trailing newlines if (lines.size()) lines[0] = " " + lines[0]; //indent the title line BOOST_FOREACH(const std::string &line, lines){ |