From 3b06b8f4893bd18440ecb9705414831a827cef90 Mon Sep 17 00:00:00 2001 From: Joao Eduardo Luis Date: Fri, 23 May 2014 16:51:37 +0100 Subject: [PATCH] common/strtol.cc: strict_strtosi() converts str with SI units to uint64_t Accepts values with a suffix (B, K, M, G, T, P, E) and returns the appropriate byte value. E.g., 10B = 10, while 10K = 10240. Signed-off-by: Joao Eduardo Luis (cherry picked from commit 67dc5751ba9a4e527ff12ea65000d1ba45d956f6) --- src/common/strtol.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ src/common/strtol.h | 5 +++++ 2 files changed, 48 insertions(+) diff --git a/src/common/strtol.cc b/src/common/strtol.cc index 8f12f08f19bee..840b3d9c6a046 100644 --- a/src/common/strtol.cc +++ b/src/common/strtol.cc @@ -17,6 +17,9 @@ #include #include #include +extern "C" { +#include +} using std::ostringstream; @@ -124,3 +127,43 @@ float strict_strtof(const char *str, std::string *err) *err = ""; return ret; } + +uint64_t strict_sistrtoll(const char *str, std::string *err) +{ + std::string s(str); + if (s.size() == 0) { + ostringstream oss; + oss << "strict_sistrtoll: value not specified"; + *err = oss.str(); + return 0; + } + const char &u = s.at(s.size()-1); //str[std::strlen(str)-1]; + int m = 0; + if (u == 'B') + m = 0; + else if (u == 'K') + m = 10; + else if (u == 'M') + m = 20; + else if (u == 'G') + m = 30; + else if (u == 'T') + m = 40; + else if (u == 'P') + m = 50; + else if (u == 'E') + m = 60; + else + m = -1; + + const char *v = NULL; + if (m >= 0) + s = std::string(str, s.size()-1); + v = s.c_str(); + + uint64_t r = strict_strtoll(v, 10, err); + if (err->empty() && m > 0) { + r = (r << m); + } + return r; +} diff --git a/src/common/strtol.h b/src/common/strtol.h index 80b5a3f9f9224..ea0a469a777e6 100644 --- a/src/common/strtol.h +++ b/src/common/strtol.h @@ -16,6 +16,9 @@ #define CEPH_COMMON_STRTOL_H #include +extern "C" { +#include +} long long strict_strtoll(const char *str, int base, std::string *err); @@ -25,4 +28,6 @@ double strict_strtod(const char *str, std::string *err); float strict_strtof(const char *str, std::string *err); +uint64_t strict_sistrtoll(const char *str, std::string *err); + #endif -- 2.39.5