23 #ifndef MOOSSTRING20110527H 24 #define MOOSSTRING20110527H 26 #include "goby/moos/moos_header.h" 28 #include "goby/common/time.h" 29 #include "goby/util/as.h" 35 inline std::ostream& operator<<(std::ostream& os,
const CMOOSMsg& msg)
38 <<
" Key: " << msg.GetKey() <<
" Type: " << (msg.IsDouble() ?
"double" :
"string")
40 << (msg.IsDouble() ? goby::util::as<std::string>(msg.GetDouble()) : msg.GetString())
41 <<
" Time: " << goby::util::as<boost::posix_time::ptime>(msg.GetTime())
42 <<
" Community: " << msg.GetCommunity()
43 <<
" Source: " << msg.m_sSrc
44 <<
" Source Aux: " << msg.GetSourceAux();
57 inline bool val_from_string(std::string& out,
const std::string& str,
const std::string& key)
71 std::string::size_type start_pos = 0;
73 !(start_pos == 0 || start_pos == std::string::npos || str[start_pos - 1] ==
','))
77 boost::iterator_range<std::string::const_iterator> result =
78 boost::ifind_nth(str, std::string(key +
"="), match);
79 start_pos = (result) ? result.begin() - str.begin() : std::string::npos;
83 if (start_pos != std::string::npos)
87 std::string chopped = str.substr(start_pos);
93 std::string::size_type equal_pos = chopped.find(
"=");
96 bool is_array = (equal_pos + 1 < chopped.length() && chopped[equal_pos + 1] ==
'{');
98 if (equal_pos != std::string::npos)
102 std::string chopped_twice = chopped.substr(equal_pos);
108 std::string::size_type end_pos =
109 (is_array) ? chopped_twice.find(
"}") : chopped_twice.find(
",");
113 out = (is_array) ? chopped_twice.substr(2, end_pos - 2)
114 : chopped_twice.substr(1, end_pos - 1);
125 template <
typename T>
126 inline bool val_from_string(T& out,
const std::string& str,
const std::string& key)
129 if (!val_from_string(s, str, key))
134 out = boost::lexical_cast<T>(s);
136 catch (boost::bad_lexical_cast&)
145 inline bool val_from_string(
bool& out,
const std::string& str,
const std::string& key)
148 if (!val_from_string(s, str, key))
151 out = goby::util::as<bool>(s);
157 inline void stripblanks(std::string& s)
159 for (std::string::iterator it = s.end() - 1; it != s.begin() - 1; --it)
The global namespace for the Goby project.