Data.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include "Data.h"
25 
26 #include <SimoxUtility/algorithm/string/string_tools.h>
27 
29 
31 {
32  bool
33  util::CheckStrIsInt(const std::string& s)
34  {
35  try
36  {
37  simox::alg::to_<int>(s);
38  }
39  catch (simox::error::SimoxError&)
40  {
41  return false;
42  }
43  return true;
44  }
45 
46  void
47  util::EnforceStrIsInt(const std::string& s)
48  {
49  if (!CheckStrIsInt(s))
50  {
52  __PRETTY_FUNCTION__, "Could not convert a string to an integer", s);
53  }
54  }
55 
56  bool
57  util::CheckStrIsFloat(const std::string& s)
58  {
59  try
60  {
61  simox::alg::to_<float>(s);
62  }
63  catch (simox::error::SimoxError&)
64  {
65  return false;
66  }
67  return true;
68  }
69 
70  void
71  util::EnforceStrIsFloat(const std::string& s)
72  {
73  if (!CheckStrIsFloat(s))
74  {
76  __PRETTY_FUNCTION__, "Could not convert a string to a floating point", s);
77  }
78  }
79 
80  bool
81  util::CheckStrIsNumber(const std::string& s)
82  {
83  return CheckStrIsInt(s) || CheckStrIsFloat(s);
84  }
85 
86  void
87  util::EnforceStrIsNumber(const std::string& s)
88  {
89  if (!CheckStrIsNumber(s))
90  {
92  __PRETTY_FUNCTION__, "Could not convert a string to a number", s);
93  }
94  }
95 
96  bool
97  util::CheckStrIsNumberSeq(const std::string& s)
98  {
99  std::vector<std::string> split = simox::alg::split(s, ",");
100  for (const auto& e : split)
101  {
102  if (!CheckStrIsNumber(e))
103  {
104  return false;
105  }
106  }
107  return true;
108  }
109 
110  void
111  util::EnforceStrIsNumberSeq(const std::string& s)
112  {
113  if (!CheckStrIsNumberSeq(s))
114  {
116  __PRETTY_FUNCTION__,
117  "Could not convert a string to a sequence of numbers. Note that number sequences "
118  "have to separated with ','",
119  s);
120  }
121  }
122 
123  bool
124  util::CheckStrIsBool(const std::string& s)
125  {
126  try
127  {
128  simox::alg::to_<bool>(s);
129  }
130  catch (simox::error::SimoxError&)
131  {
132  return false;
133  }
134  return true;
135  }
136 
137  void
138  util::EnforceStrIsBool(const std::string& s)
139  {
140  if (!CheckStrIsBool(s))
141  {
143  __PRETTY_FUNCTION__, "Could not convert a string to a bool", s);
144  }
145  }
146 
147  std::optional<RapidXmlReaderNode>
148  util::GetFirstNodeWithTag(const RapidXmlReaderNode& node, const std::string& name)
149  {
150  for (const auto& n : node.nodes())
151  {
152  if (HasTagName(n, name))
153  {
154  return n;
155  }
156  }
157 
158  return std::nullopt;
159  }
160 
161  void
162  util::EnforceAttribute(const RapidXmlReaderNode& node, const std::string& att)
163  {
164  if (!HasAttribute(node, att))
165  {
166  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
167  "A <" + node.name() + ">-tag does not have the '" +
168  att + "' attribute",
170  }
171  }
172 
173  bool
174  util::HasAttribute(const RapidXmlReaderNode& node, const std::string& att)
175  {
176  return node.has_attribute(att.c_str());
177  }
178 
179  std::string
180  util::GetAttribute(const RapidXmlReaderNode& node, const std::string& att)
181  {
182  EnforceAttribute(node, att);
183  return node.attribute_value(att.c_str());
184  }
185 
186  std::string
188  const std::string& att,
189  const std::string& def)
190  {
191  if (!(HasAttribute(node, att)))
192  {
193  return def;
194  }
195  return node.attribute_value(att.c_str());
196  }
197 
198  bool
199  util::AttributeIsTrue(const armarx::RapidXmlReaderNode& node, const std::string& att)
200  {
201  if (HasAttribute(node, att))
202  {
203  std::string v = simox::alg::to_lower(node.attribute_value(att.c_str()));
204  if (v == "1" or v == "true" or v == "yes" or v == "ja" or v == "")
205  {
206  return true;
207  }
208  }
209  return false;
210  }
211 
212  bool
213  util::HasTagName(const armarx::RapidXmlReaderNode& node, const std::string& name)
214  {
215  return (simox::alg::to_lower(name) == simox::alg::to_lower(node.name()));
216  }
217 
218  void
219  util::EnforceTagName(const armarx::RapidXmlReaderNode& node, const std::string& name)
220  {
221  if (!(HasTagName(node, name)))
222  {
224  __PRETTY_FUNCTION__, "The node has the wrong tag. Expected: " + name, node.name());
225  }
226  }
227 
228  void
229  util::EnforceTagNames(const RapidXmlReaderNode& node, const std::vector<std::string>& names)
230  {
231  for (const auto& name : names)
232  {
233  if (HasTagName(node, name))
234  {
235  return;
236  }
237  }
238  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
239  "The node has the wrong tag. Expected one of: " +
241  node.name());
242  }
243 
244  std::string
246  {
247  return simox::alg::to_lower(node.name());
248  }
249 
250  void
251  util::EnforceChildSizeSmaller(const RapidXmlReaderNode& node, const size_t size)
252  {
253  const size_t s = node.nodes().size();
254  if (s >= size)
255  {
256  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
257  "The node <" + node.name() +
258  "> has the wrong number of children",
259  std::to_string(s),
260  std::to_string(size));
261  }
262  }
263 
264  void
266  {
267  const size_t s = node.nodes().size();
268  if (s > size)
269  {
270  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
271  "The node <" + node.name() +
272  "> has the wrong number of children",
273  std::to_string(s),
274  std::to_string(size));
275  }
276  }
277 
278  void
279  util::EnforceChildSize(const armarx::RapidXmlReaderNode& node, const size_t size)
280  {
281  const size_t s = node.nodes().size();
282  if (s != size)
283  {
284  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
285  "The node <" + node.name() +
286  "> has the wrong number of children",
287  std::to_string(s),
288  std::to_string(size));
289  }
290  }
291 
292  void
294  {
295  const size_t s = node.nodes().size();
296  if (s < size)
297  {
298  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
299  "The node <" + node.name() +
300  "> has the wrong number of children",
301  std::to_string(s),
302  std::to_string(size));
303  }
304  }
305 
306  void
307  util::EnforceChildSizeGreater(const RapidXmlReaderNode& node, const size_t size)
308  {
309  const size_t s = node.nodes().size();
310  if (s <= size)
311  {
312  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
313  "The node <" + node.name() +
314  "> has the wrong number of children",
315  std::to_string(s),
316  std::to_string(size));
317  }
318  }
319 
320 } // namespace armarx::aron::typereader::xml
armarx::aron::typereader::xml::util::HasAttribute
bool HasAttribute(const RapidXmlReaderNode &node, const std::string &att)
Definition: Data.cpp:174
armarx::aron::typereader::xml::util::CheckStrIsNumber
bool CheckStrIsNumber(const std::string &)
Definition: Data.cpp:81
Data.h
armarx::aron::typereader::xml::util::CheckStrIsNumberSeq
bool CheckStrIsNumberSeq(const std::string &)
Definition: Data.cpp:97
armarx::aron::typereader::xml::util::EnforceTagNames
void EnforceTagNames(const RapidXmlReaderNode &node, const std::vector< std::string > &names)
Definition: Data.cpp:229
armarx::aron::typereader::xml::util::GetAttributeWithDefault
std::string GetAttributeWithDefault(const RapidXmlReaderNode &node, const std::string &att, const std::string &def)
Definition: Data.cpp:187
armarx::aron::typereader::xml::util::EnforceTagName
void EnforceTagName(const RapidXmlReaderNode &node, const std::string &name)
Definition: Data.cpp:219
armarx::aron::typereader::xml::util::CheckStrIsFloat
bool CheckStrIsFloat(const std::string &)
Definition: Data.cpp:57
armarx::aron::typereader::xml::util::EnforceStrIsInt
void EnforceStrIsInt(const std::string &)
Definition: Data.cpp:47
armarx::RapidXmlReaderNode::attribute_value
std::string attribute_value(const char *attrName) const
Definition: RapidXmlReader.h:198
armarx::aron::typereader::xml::util::CheckStrIsBool
bool CheckStrIsBool(const std::string &)
Definition: Data.cpp:124
armarx::aron::typereader::xml::util::EnforceChildSize
void EnforceChildSize(const RapidXmlReaderNode &node, const size_t size)
Definition: Data.cpp:279
armarx::aron::typereader::xml::util::HasTagName
bool HasTagName(const RapidXmlReaderNode &node, const std::string &name)
Definition: Data.cpp:213
armarx::aron::typereader::xml
All constantes for the aron XML parser, in addition to some utility functions wrapping around the arm...
Definition: Data.cpp:30
armarx::RapidXmlReaderNode::get_all_attributes
std::vector< std::pair< std::string, std::string > > get_all_attributes() const
Definition: RapidXmlReader.h:295
armarx::RapidXmlReaderNode::has_attribute
bool has_attribute(const char *attrName) const
Definition: RapidXmlReader.h:209
armarx::aron::typereader::xml::util::GetFirstNodeWithTag
std::optional< RapidXmlReaderNode > GetFirstNodeWithTag(const RapidXmlReaderNode &node, const std::string &name)
Definition: Data.cpp:148
armarx::RapidXmlReaderNode::name
std::string name() const
Definition: RapidXmlReader.h:349
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
armarx::aron::typereader::xml::util::EnforceStrIsNumberSeq
void EnforceStrIsNumberSeq(const std::string &)
Definition: Data.cpp:111
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:68
armarx::aron::typereader::xml::util::GetTagName
std::string GetTagName(const RapidXmlReaderNode &node)
Definition: Data.cpp:245
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::typereader::xml::util::EnforceStrIsFloat
void EnforceStrIsFloat(const std::string &)
Definition: Data.cpp:71
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
Exception.h
armarx::aron::typereader::xml::util::EnforceChildSizeGreaterEqual
void EnforceChildSizeGreaterEqual(const RapidXmlReaderNode &node, const size_t size)
Definition: Data.cpp:293
armarx::aron::typereader::xml::util::GetAttribute
std::string GetAttribute(const RapidXmlReaderNode &node, const std::string &att)
Definition: Data.cpp:180
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
armarx::aron::typereader::xml::util::EnforceAttribute
void EnforceAttribute(const RapidXmlReaderNode &node, const std::string &att)
Definition: Data.cpp:162
armarx::aron::typereader::xml::util::EnforceChildSizeSmallerEqual
void EnforceChildSizeSmallerEqual(const RapidXmlReaderNode &node, const size_t size)
Definition: Data.cpp:265
armarx::RapidXmlReaderNode::nodes
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
Definition: RapidXmlReader.h:158
armarx::aron::typereader::xml::util::EnforceStrIsBool
void EnforceStrIsBool(const std::string &)
Definition: Data.cpp:138
armarx::aron::typereader::xml::util::CheckStrIsInt
bool CheckStrIsInt(const std::string &)
Definition: Data.cpp:33
armarx::aron::typereader::xml::util::EnforceChildSizeSmaller
void EnforceChildSizeSmaller(const RapidXmlReaderNode &node, const size_t size)
Definition: Data.cpp:251
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::aron::typereader::xml::util::EnforceChildSizeGreater
void EnforceChildSizeGreater(const RapidXmlReaderNode &node, const size_t size)
Definition: Data.cpp:307
armarx::aron::typereader::xml::util::EnforceStrIsNumber
void EnforceStrIsNumber(const std::string &)
Definition: Data.cpp:87
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36
armarx::aron::typereader::xml::util::AttributeIsTrue
bool AttributeIsTrue(const RapidXmlReaderNode &node, const std::string &att)
Definition: Data.cpp:199