ROSaic
Functions
string_utilities Namespace Reference

Functions

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"'s value in "value" and returns whether or not all went well. More...
 
bool toFloat (const std::string &string, float &value)
 Interprets the contents of "string" as a floating point number of type float. More...
 
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 has that is exactly 32 bits. More...
 
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 system has that is exactly 32 bits. More...
 
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 has that is exactly 8 bits. More...
 
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 system has that is exactly 8 bits. More...
 
std::string trimString (std::string str)
 Removes trailing zeros from a string representing a float or double except for the first zero after the decimal point. More...
 

Detailed Description

This namespace is for the functions that encapsulate basic string manipulation and conversion techniques.

Function Documentation

◆ toDouble()

bool string_utilities::toDouble ( const std::string &  string,
double &  value 
)

Interprets the contents of "string" as a floating point number of type double It stores the "string"'s value in "value" and returns whether or not all went well.

It checks whether an error occurred (via errno) and whether junk characters exist within "string", and returns true if the latter two tests are negative and the string is non-empty, false otherwise.

Parameters
[in]stringThe string whose content should be interpreted as a floating point number
[out]valueThe double variable that should be overwritten by the floating point number found in "string"
Returns
True if all went fine, false if not

Definition at line 51 of file string_utilities.cpp.

Referenced by GpggaParser::parseASCII(), GprmcParser::parseASCII(), parsing_utilities::parseDouble(), and io_comm_rx::timestampSBF().

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  }
Here is the caller graph for this function:

◆ toFloat()

bool string_utilities::toFloat ( const std::string &  string,
float &  value 
)

Interprets the contents of "string" as a floating point number of type float.

It checks whether an error occurred (via errno) and whether junk characters exist within "string", and returns true if the latter two tests are negative and the string is non-empty, false otherwise.

It stores the "string"'s value in "value" and returns whether or not all went well.

Parameters
[in]stringThe string whose content should be interpreted as a floating point number
[out]valueThe float variable that should be overwritten by the floating point number found in "string"
Returns
True if all went fine, false if not

Definition at line 77 of file string_utilities.cpp.

Referenced by parsing_utilities::parseFloat().

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  }
Here is the caller graph for this function:

◆ toInt32()

bool string_utilities::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 has that is exactly 32 bits.

It checks whether an error occurred (via errno) and whether junk characters exist within "string", and returns true if the latter two tests are negative and the string is non-empty, false otherwise.

It stores the "string"'s value in "value" and returns whether or not all went well.

Parameters
[in]stringThe string whose content should be interpreted as a floating point number
[out]valueThe int32_t variable that should be overwritten by the floating point number found in "string"
Returns
True if all went fine, false if not

Definition at line 102 of file string_utilities.cpp.

Referenced by parsing_utilities::parseInt16(), and parsing_utilities::parseInt32().

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  }
Here is the caller graph for this function:

◆ toInt8()

int8_t string_utilities::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 has that is exactly 8 bits.

Not used as of now..

It stores the "string"'s value in "value".

Parameters
[in]stringThe string whose content should be interpreted as a floating point number
[out]valueThe int8_t variable that should be overwritten by the floating point number found in "string"
Returns
The value found in "string"

Definition at line 161 of file string_utilities.cpp.

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  }

◆ toUInt32()

bool string_utilities::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 system has that is exactly 32 bits.

It checks whether an error occurred (via errno) and whether junk characters exist within "string", and returns true if the latter two tests are negative and the string is non-empty, false otherwise.

It stores the "string"'s value in "value" and returns whether or not all went well.

Parameters
[in]stringThe string whose content should be interpreted as a floating point number
[out]valueThe uint32_t variable that should be overwritten by the floating point number found in "string"
Returns
True if all went fine, false if not

Definition at line 133 of file string_utilities.cpp.

Referenced by parsing_utilities::parseUInt16(), parsing_utilities::parseUInt32(), and parsing_utilities::parseUInt8().

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  }
Here is the caller graph for this function:

◆ toUInt8()

uint8_t string_utilities::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 system has that is exactly 8 bits.

Not used as of now..

It stores the "string"'s value in "value".

Parameters
[in]stringThe string whose content should be interpreted as a floating point number
[out]valueThe uint8_t variable that should be overwritten by the floating point number found in "string"
Returns
The value found in "string"

Definition at line 174 of file string_utilities.cpp.

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  }

◆ trimString()

std::string string_utilities::trimString ( std::string  str)

Removes trailing zeros from a string representing a float or double except for the first zero after the decimal point.

Parameters
[in]strThe string whose trailing zeros shall be removed
Returns
The trimmed string

Definition at line 184 of file string_utilities.cpp.

Referenced by rosaic_node::ROSaicNode::configureRx().

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  }
Here is the caller graph for this function: