string_utilities.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // © Copyright 2020, Septentrio NV/SA.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived
15 // from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // *****************************************************************************
30 
31 // ROSaic includes
33 // C++ library includes
34 #include <cerrno>
35 #include <cmath>
36 #include <cstdlib>
37 #include <iomanip>
38 #include <limits>
39 #include <sstream>
40 
47 namespace string_utilities {
53  bool toDouble(const std::string& string, double& value)
54  {
55  if (string.empty())
56  {
57  return false;
58  }
59 
60  char* end;
61  errno = 0;
62 
63  double value_new = std::strtod(string.c_str(), &end);
64 
65  if (errno != 0 || end != string.c_str() + string.length())
66  {
67  return false;
68  }
69 
70  value = value_new;
71  return true;
72  }
73 
79  bool toFloat(const std::string& string, float& value)
80  {
81  if (string.empty())
82  {
83  return false;
84  }
85 
86  char* end;
87  errno = 0;
88  float value_new = std::strtof(string.c_str(), &end);
89 
90  if (errno != 0 || end != string.c_str() + string.length())
91  {
92  return false;
93  }
94 
95  value = value_new;
96  return true;
97  }
98 
104  bool toInt32(const std::string& string, int32_t& value, int32_t base)
105  {
106  if (string.empty())
107  {
108  return false;
109  }
110 
111  char* end;
112  errno = 0;
113  int64_t value_new = std::strtol(string.c_str(), &end, base);
114 
115  if (errno != 0 || end != string.c_str() + string.length())
116  {
117  return false;
118  }
119 
120  if (value_new > std::numeric_limits<int32_t>::max() ||
121  value_new < std::numeric_limits<int32_t>::min())
122  {
123  return false;
124  }
125 
126  value = (int32_t)value_new;
127  return true;
128  }
129 
135  bool toUInt32(const std::string& string, uint32_t& value, int32_t base)
136  {
137  if (string.empty())
138  {
139  return false;
140  }
141 
142  char* end;
143  errno = 0;
144  int64_t value_new = std::strtol(string.c_str(), &end, base);
145 
146  if (errno != 0 || end != string.c_str() + string.length())
147  {
148  return false;
149  }
150 
151  if (value_new > std::numeric_limits<uint32_t>::max() || value_new < 0)
152  {
153  return false;
154  }
155 
156  value = (uint32_t)value_new;
157  return true;
158  }
159 
163  int8_t toInt8(const std::string& string, int8_t& value, int32_t base)
164  {
165  char* end;
166  errno = 0;
167  int64_t value_new = std::strtol(string.c_str(), &end, base);
168 
169  value = (int8_t)value_new;
170  return value;
171  }
172 
176  uint8_t toUInt8(const std::string& string, uint8_t& value, int32_t base)
177  {
178  char* end;
179  errno = 0;
180  int64_t value_new = std::strtol(string.c_str(), &end, base);
181 
182  value = (uint8_t)value_new;
183  return true;
184  }
185 
186  std::string trimDecimalPlaces(double num)
187  {
188  num = std::round(num * 1000);
189  num = num / 1000;
190  std::stringstream ss;
191  ss << std::fixed;
192  ss << std::setprecision(3);
193  ss << num;
194  return ss.str();
195  }
196 
197  bool containsSpace(const std::string str)
198  {
199  for (size_t i = 0; i < str.size(); ++i)
200  {
201  if (std::isspace(str[i]))
202  return true;
203  }
204  return false;
205  }
206 } // namespace string_utilities
bool toFloat(const std::string &string, float &value)
Interprets the contents of "string" as a floating point number of type float.
bool toDouble(const std::string &string, double &value)
Interprets the contents of "string" as a floating point number of type double It stores the "string"&#39;...
uint8_t toUInt8(const std::string &string, uint8_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever unsigned integer type your...
bool containsSpace(const std::string str)
Checks if a string contains spaces.
bool toUInt32(const std::string &string, uint32_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever unsigned integer type your...
TF2SIMD_FORCE_INLINE tf2Scalar length(const Quaternion &q)
Declares lower-level string utility functions used when parsing messages.
std::string trimDecimalPlaces(double num)
Trims decimal places to two.
int8_t toInt8(const std::string &string, int8_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever integer type your system h...
bool toInt32(const std::string &string, int32_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever integer type your system h...


septentrio_gnss_driver
Author(s): Tibor Dome
autogenerated on Sat Mar 11 2023 03:12:56