F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
StringToNumber.cpp
Go to the documentation of this file.
2 #include <cerrno>
3 #include <cstdlib>
4 
7  // Check for null input string
8  if (input == nullptr) {
10  }
11  // Invalid base e.g. not 0, 2-36
12  else if (base == 1 || base > 36) {
14  } else {
15  // Check the string is bounded within the specified length
16  FwSizeType length = Fw::StringUtils::string_length(input, buffer_size);
17  if (length == buffer_size) {
19  }
20  }
21  errno = 0;
22  return status;
23 }
24 
26  const char* original_input,
27  char*& internal_next,
28  char** external_next) {
29  // Check range, if error then
30  if (errno == ERANGE) {
32  }
33  // Invalid number conversion
34  else if ((internal_next == original_input) || (internal_next == nullptr)) {
35  internal_next = nullptr;
37  }
38  // Set output pointer in all cases
39  if (external_next != nullptr) {
40  *external_next = internal_next;
41  }
42  errno = 0;
43  return status;
44 }
45 
46 // Template for internal implementation only
47 // \tparam T: input type (U8, I8, U64, I64)
48 // \tparam Tinternal: function api type
49 // \tparam F: conversion function to use
50 template <typename T, typename Tinternal, Tinternal (*F)(const char*, char**, int)>
52  FwSizeType buffer_size,
53  T& output,
54  char** next,
55  U8 base) {
56  static_assert(std::numeric_limits<T>::is_integer, "Type must be integer");
57  static_assert(std::numeric_limits<Tinternal>::is_integer, "Type must be integer");
58  static_assert(std::numeric_limits<T>::is_signed == std::numeric_limits<Tinternal>::is_signed,
59  "Signedness must match");
60  static_assert(std::numeric_limits<T>::max() <= std::numeric_limits<Tinternal>::max(),
61  "Invalid internal type chosen");
63  "Invalid internal type chosen");
64 
65  char* output_next = nullptr;
68  Tinternal output_api = F(input, &output_next, base);
69  if (output_api > std::numeric_limits<T>::max()) {
71  output_api = std::numeric_limits<T>::max();
72  }
73  if (output_api < std::numeric_limits<T>::min()) {
75  output_api = std::numeric_limits<T>::min();
76  }
77  output = static_cast<T>(output_api);
78  }
79  status = string_to_helper_output_check(status, input, output_next, next);
80  return status;
81 }
82 
83 #if FW_HAS_64_BIT
85  FwSizeType buffer_size,
86  U64& output,
87  char** next,
88  U8 base) {
89  return string_to_number_as_template<U64, unsigned long long, strtoull>(input, buffer_size, output, next, base);
90 }
91 
93  FwSizeType buffer_size,
94  I64& output,
95  char** next,
96  U8 base) {
97  return string_to_number_as_template<I64, long long, strtoll>(input, buffer_size, output, next, base);
98 }
99 #endif
100 #if FW_HAS_32_BIT
102  FwSizeType buffer_size,
103  U32& output,
104  char** next,
105  U8 base) {
106  return string_to_number_as_template<U32, unsigned long long, strtoull>(input, buffer_size, output, next, base);
107 }
108 
110  FwSizeType buffer_size,
111  I32& output,
112  char** next,
113  U8 base) {
114  return string_to_number_as_template<I32, long long, strtoll>(input, buffer_size, output, next, base);
115 }
116 #endif
117 #if FW_HAS_16_BIT
119  FwSizeType buffer_size,
120  U16& output,
121  char** next,
122  U8 base) {
123  return string_to_number_as_template<U16, unsigned long long, strtoull>(input, buffer_size, output, next, base);
124 }
126  FwSizeType buffer_size,
127  I16& output,
128  char** next,
129  U8 base) {
130  return string_to_number_as_template<I16, long long, strtoll>(input, buffer_size, output, next, base);
131 }
132 #endif
134  FwSizeType buffer_size,
135  U8& output,
136  char** next,
137  U8 base) {
138  return string_to_number_as_template<U8, unsigned long long, strtoull>(input, buffer_size, output, next, base);
139 }
141  FwSizeType buffer_size,
142  I8& output,
143  char** next,
144  U8 base) {
145  return string_to_number_as_template<I8, long long, strtoll>(input, buffer_size, output, next, base);
146 }
147 #if FW_HAS_F64
149  FwSizeType buffer_size,
150  F64& output,
151  char** next) {
152  char* output_next = nullptr;
154  if (status == SUCCESSFUL_CONVERSION) {
155  output = strtod(input, &output_next);
156  }
157  status = string_to_helper_output_check(status, input, output_next, next);
158  return status;
159 }
160 #endif
161 
163  FwSizeType buffer_size,
164  F32& output,
165  char** next) {
166  char* output_next = nullptr;
168  if (status == SUCCESSFUL_CONVERSION) {
169  output = strtof(input, &output_next);
170  }
171  status = string_to_helper_output_check(status, input, output_next, next);
172  return status;
173 }
Fw::StringUtils::StringToNumberStatus string_to_helper_input_check(const CHAR *input, FwSizeType buffer_size, U8 base)
PlatformSizeType FwSizeType
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:50
StringToNumberStatus string_to_number(const CHAR *input, FwSizeType buffer_size, U8 &output, char **next, U8 base=0)
converts a string to a U8
char CHAR
Definition: BasicTypes.h:59
Fw::StringUtils::StringToNumberStatus string_to_number_as_template(const CHAR *input, FwSizeType buffer_size, T &output, char **next, U8 base)
float F32
32-bit floating point
Definition: BasicTypes.h:83
String did not contain a valid number matching supplied base.
Definition: StringUtils.hpp:60
Base was not supplied as 0, or 2-36.
Definition: StringUtils.hpp:59
A null string was supplied.
Definition: StringUtils.hpp:57
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
double F64
64-bit floating point (double). Required for compiler-supplied double promotion.
Definition: BasicTypes.h:85
Fw::StringUtils::StringToNumberStatus string_to_helper_output_check(Fw::StringUtils::StringToNumberStatus status, const char *original_input, char *&internal_next, char **external_next)
No \0 detected within the supplied length.
Definition: StringUtils.hpp:58
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
Definition: StringUtils.cpp:24
#define U64(C)
Definition: sha.h:180