From f21352094c0949b643721ee5387fefae0cdab507 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 30 Oct 2016 11:31:56 +0100 Subject: Move hexparse to utils and add default PRBS poly --- src/utils.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/utils.cpp') 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 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; +} + -- cgit v1.2.3