summaryrefslogtreecommitdiffstats
path: root/src/utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.cpp')
-rw-r--r--src/utils.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/utils.cpp b/src/utils.cpp
index 9976e88..f0df772 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -426,3 +426,29 @@ void printEnsemble(const shared_ptr<dabEnsemble> ensemble)
}
}
+long hexparse(const std::string& input)
+{
+ long value = 0;
+ errno = 0;
+
+ // Do not use strtol's base=0 because
+ // we do not want to accept octal.
+ if (input.find("0x") == 0) {
+ value = strtol(input.c_str() + 2, nullptr, 16);
+ }
+ else {
+ value = strtol(input.c_str(), nullptr, 10);
+ }
+
+ if ((value == LONG_MIN or value == LONG_MAX) and errno == ERANGE) {
+ throw out_of_range("hexparse: value out of range");
+ }
+ else if (value == 0 and errno != 0) {
+ stringstream ss;
+ ss << "hexparse: " << strerror(errno);
+ throw invalid_argument(ss.str());
+ }
+
+ return value;
+}
+