summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-12-06 15:28:00 -0500
committerJonathan Bradley <jcb@pikum.xyz>2023-12-06 15:28:00 -0500
commit8d27ab131e0829fb847c93529628e4751b356b5f (patch)
tree500315467814e493b113dbb18e91e84ac509401e
parent8629b2497b7cb453d1e4a3ed6897cfaf8a771863 (diff)
add str2num for uint8_t
-rw-r--r--src/helpers.cpp36
-rw-r--r--src/helpers.hpp2
2 files changed, 38 insertions, 0 deletions
diff --git a/src/helpers.cpp b/src/helpers.cpp
index 22be4de..e08906b 100644
--- a/src/helpers.cpp
+++ b/src/helpers.cpp
@@ -114,6 +114,42 @@ STR2NUM_ERROR str2num(uint16_t &i, char const *s, int base) {
return SUCCESS;
}
+STR2NUM_ERROR str2num(int8_t &i, char const *s, int base) {
+ char *end;
+ long l;
+ errno = 0;
+ l = strtol(s, &end, base);
+ if (errno == ERANGE && l == LONG_MAX) {
+ return OVERFLOW;
+ }
+ if (errno == ERANGE && l == LONG_MIN) {
+ return UNDERFLOW;
+ }
+ if (*s == '\0' || *end != '\0') {
+ return INCONVERTIBLE;
+ }
+ i = l;
+ return SUCCESS;
+}
+
+STR2NUM_ERROR str2num(uint8_t &i, char const *s, int base) {
+ char *end;
+ long l;
+ errno = 0;
+ l = strtoul(s, &end, base);
+ if (errno == ERANGE && l == LONG_MAX) {
+ return OVERFLOW;
+ }
+ if (errno == ERANGE && l == LONG_MIN) {
+ return UNDERFLOW;
+ }
+ if (*s == '\0' || *end != '\0') {
+ return INCONVERTIBLE;
+ }
+ i = l;
+ return SUCCESS;
+}
+
STR2NUM_ERROR str2num(float &f, char const *s, char *&pEnd) {
float l;
errno = 0;
diff --git a/src/helpers.hpp b/src/helpers.hpp
index a5c8089..347232a 100644
--- a/src/helpers.hpp
+++ b/src/helpers.hpp
@@ -11,6 +11,8 @@ STR2NUM_ERROR str2num(int32_t &i, char const *s, int base = 0);
STR2NUM_ERROR str2num(uint32_t &i, char const *s, int base = 0);
STR2NUM_ERROR str2num(int16_t &i, char const *s, int base = 0);
STR2NUM_ERROR str2num(uint16_t &i, char const *s, int base = 0);
+STR2NUM_ERROR str2num(int8_t &i, char const *s, int base = 0);
+STR2NUM_ERROR str2num(uint8_t &i, char const *s, int base = 0);
STR2NUM_ERROR str2num(float &f, char const *s);
STR2NUM_ERROR str2num(double &d, char const *s);
STR2NUM_ERROR str2num(float &f, char const *s, char *&pEnd);