diff options
author | HampusM <hampus@hampusmat.com> | 2022-01-09 21:47:23 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-01-09 21:47:23 +0100 |
commit | 8ceb79db1d0687bba005cef4a77bb889bf7ec3c3 (patch) | |
tree | b7c13359f652506d60c8556ea386ae8d50bfc5bc /src/utils.cpp | |
parent | 097aa95c1f0cb159e7d9d0a3edf9284c421ee298 (diff) |
refactor: rewrite to c++
Diffstat (limited to 'src/utils.cpp')
-rw-r--r-- | src/utils.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..30ff4ec --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,34 @@ +#include "utils.hpp" +#include <climits> +#include <stdexcept> + +unsigned int str_to_uint(std::string str) +{ + if (str.at(0) == '-') + throw "Less than 0"; + + std::size_t waste_pos; + + unsigned long num; + + try + { + num = std::stoul(str, &waste_pos, 10); + } + catch (const std::invalid_argument &exc) + { + throw "Not a number"; + } + catch (const std::out_of_range &exc) + { + throw "Out of range"; + } + + if (waste_pos != str.length()) + throw "Not a number"; + + if (num > (unsigned long)UINT_MAX) + throw "Out of range"; + + return (unsigned int)num; +} |