Goby3  3.1.4
2024.02.22
moos_string.h
Go to the documentation of this file.
1 // Copyright 2010-2021:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // The Goby Libraries are distributed in the hope that they will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef GOBY_MOOS_MOOS_STRING_H
26 #define GOBY_MOOS_MOOS_STRING_H
27 
28 #include "goby/moos/moos_header.h"
29 
30 #include "goby/time.h"
31 #include "goby/util/as.h"
32 
33 namespace goby
34 {
35 namespace moos
36 {
37 inline std::ostream& operator<<(std::ostream& os, const CMOOSMsg& msg)
38 {
39  os << "[[CMOOSMsg]]"
40  << " Key: " << msg.GetKey() << " Type: " << (msg.IsDouble() ? "double" : "string")
41  << " Value: "
42  << (msg.IsDouble() ? goby::util::as<std::string>(msg.GetDouble()) : msg.GetString())
43  << " Time: "
44  << time::convert<boost::posix_time::ptime>(msg.GetTime() * boost::units::si::seconds)
45  << " Community: " << msg.GetCommunity()
46  << " Source: " << msg.m_sSrc // no getter in CMOOSMsg!!!
47  << " Source Aux: " << msg.GetSourceAux();
48  return os;
49 }
50 
60 inline bool val_from_string(std::string& out, const std::string& str, const std::string& key)
61 {
62  // str: foo=1,bar={2,3,4,5},pig=3,cow=yes
63  // two cases:
64  // key: bar
65  // key: pig
66 
67  out.erase();
68 
69  // deal with foo=bar,o=bar problem when looking for "o=" since
70  // o is contained in foo
71 
72  // ok: beginning of string, end of string, comma right before start_pos
73  int match = 0;
74  std::string::size_type start_pos = 0;
75  while (match == 0 ||
76  !(start_pos == 0 || start_pos == std::string::npos || str[start_pos - 1] == ','))
77  {
78  // str: foo=1,bar={2,3,4,5},pig=3,cow=yes
79  // start_pos ^ ^
80  boost::iterator_range<std::string::const_iterator> result =
81  boost::ifind_nth(str, std::string(key + "="), match);
82  start_pos = (result) ? result.begin() - str.begin() : std::string::npos;
83  ++match;
84  }
85 
86  if (start_pos != std::string::npos)
87  {
88  // chopped: bar={2,3,4,5},pig=3,cow=yes
89  // chopped: pig=3,cow=yes
90  std::string chopped = str.substr(start_pos);
91 
92  // chopped: bar={2,3,4,5},pig=3,cow=yes
93  // equal_pos ^
94  // chopped: pig=3,cow=yes
95  // equal_pos ^
96  std::string::size_type equal_pos = chopped.find("=");
97 
98  // check for array
99  bool is_array = (equal_pos + 1 < chopped.length() && chopped[equal_pos + 1] == '{');
100 
101  if (equal_pos != std::string::npos)
102  {
103  // chopped_twice: ={2,3,4,5},pig=3,cow=yes
104  // chopped_twice: =pig=3,cow=yes
105  std::string chopped_twice = chopped.substr(equal_pos);
106 
107  // chopped_twice: ={2,3,4,5},pig=3,cow=yes
108  // end_pos ^
109  // chopped_twice: =pig=3,cow=yes
110  // end_pos ^
111  std::string::size_type end_pos =
112  (is_array) ? chopped_twice.find("}") : chopped_twice.find(",");
113 
114  // out: 2,3,4,5
115  // out: 3
116  out = (is_array) ? chopped_twice.substr(2, end_pos - 2)
117  : chopped_twice.substr(1, end_pos - 1);
118 
119  return true;
120  }
121  }
122 
123  return false;
124 }
125 
128 template <typename T>
129 inline bool val_from_string(T& out, const std::string& str, const std::string& key)
130 {
131  std::string s;
132  if (!val_from_string(s, str, key))
133  return false;
134 
135  try
136  {
137  out = boost::lexical_cast<T>(s);
138  }
139  catch (boost::bad_lexical_cast&)
140  {
141  return false;
142  }
143 
144  return true;
145 }
146 
148 inline bool val_from_string(bool& out, const std::string& str, const std::string& key)
149 {
150  std::string s;
151  if (!val_from_string(s, str, key))
152  return false;
153 
154  out = goby::util::as<bool>(s);
155  return true;
156 }
158 
160 inline void stripblanks(std::string& s)
161 {
162  for (std::string::iterator it = s.end() - 1; it != s.begin() - 1; --it)
163  {
164  if (isspace(*it))
165  s.erase(it);
166  }
167 }
168 } // namespace moos
169 } // namespace goby
170 
171 #endif
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
as.h
moos_header.h
goby::time::str
std::string str(TimeType value=SystemClock::now< TimeType >())
Returns the provided time (or current time if omitted) as a human-readable string.
Definition: convert.h:191
CMOOSMsg
goby::moos::val_from_string
bool val_from_string(std::string &out, const std::string &str, const std::string &key)
Definition: moos_string.h:60
goby::moos::operator<<
std::ostream & operator<<(std::ostream &os, const CMOOSMsg &msg)
Definition: moos_string.h:37
time.h
goby::msg
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::google::protobuf::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
Definition: option_extensions.pb.h:1327
goby::moos::stripblanks
void stripblanks(std::string &s)
remove all blanks from string s
Definition: moos_string.h:160