ROSaic
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 <cstdlib>
36 #include <limits>
37 
45 {
51  bool toDouble(const std::string& string, double& value)
52  {
53  if (string.empty())
54  {
55  return false;
56  }
57 
58  char* end;
59  errno = 0;
60 
61  double value_new = std::strtod(string.c_str(), &end);
62 
63  if (errno != 0 || end != string.c_str() + string.length())
64  {
65  return false;
66  }
67 
68  value = value_new;
69  return true;
70  }
71 
77  bool toFloat(const std::string& string, float& value)
78  {
79  if (string.empty())
80  {
81  return false;
82  }
83 
84  char* end;
85  errno = 0;
86  float value_new = std::strtof(string.c_str(), &end);
87 
88  if (errno != 0 || end != string.c_str() + string.length())
89  {
90  return false;
91  }
92 
93  value = value_new;
94  return true;
95  }
96 
102  bool toInt32(const std::string& string, int32_t& value, int32_t base)
103  {
104  if (string.empty())
105  {
106  return false;
107  }
108 
109  char* end;
110  errno = 0;
111  int64_t value_new = std::strtol(string.c_str(), &end, base);
112 
113  if (errno != 0 || end != string.c_str() + string.length())
114  {
115  return false;
116  }
117 
118  if (value_new > std::numeric_limits<int32_t>::max() ||
119  value_new < std::numeric_limits<int32_t>::min())
120  {
121  return false;
122  }
123 
124  value = (int32_t) value_new;
125  return true;
126  }
127 
133  bool toUInt32(const std::string& string, uint32_t& value, int32_t base)
134  {
135  if (string.empty())
136  {
137  return false;
138  }
139 
140  char* end;
141  errno = 0;
142  int64_t value_new = std::strtol(string.c_str(), &end, base);
143 
144  if (errno != 0 || end != string.c_str() + string.length())
145  {
146  return false;
147  }
148 
149  if (value_new > std::numeric_limits<uint32_t>::max() || value_new < 0)
150  {
151  return false;
152  }
153 
154  value = (uint32_t) value_new;
155  return true;
156  }
157 
161  int8_t toInt8(const std::string& string, int8_t& value, int32_t base)
162  {
163  char* end;
164  errno = 0;
165  int64_t value_new = std::strtol(string.c_str(), &end, base);
166 
167  value = (int8_t) value_new;
168  return value;
169  }
170 
174  uint8_t toUInt8(const std::string& string, uint8_t& value, int32_t base)
175  {
176  char* end;
177  errno = 0;
178  int64_t value_new = std::strtol(string.c_str(), &end, base);
179 
180  value = (uint8_t) value_new;
181  return true;
182  }
183 
184  std::string trimString(std::string str)
185  {
186  for(std::string::size_type s=str.length()-1; s>0; --s)
187  {
188  if(str[s] == '0' && !isdigit(str[s-1])) break;
189  else if (str[s] == '0') str.erase(s,1);
190  else break;
191  }
192  return str;
193  }
194 }
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)
Interprets the contents of "string" as a floating point number of whatever unsigned integer type your...
bool toUInt32(const std::string &string, uint32_t &value, int32_t base)
Interprets the contents of "string" as a floating point number of whatever unsigned integer type your...
Declares lower-level string utility functions used when parsing messages.
int8_t toInt8(const std::string &string, int8_t &value, int32_t base)
Interprets the contents of "string" as a floating point number of whatever integer type your system h...
std::string trimString(std::string str)
Removes trailing zeros from a string representing a float or double except for the first zero after t...
bool toInt32(const std::string &string, int32_t &value, int32_t base)
Interprets the contents of "string" as a floating point number of whatever integer type your system h...