]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/strtol.cc: strict_strtosi() converts str with SI units to uint64_t
authorJoao Eduardo Luis <joao.luis@inktank.com>
Fri, 23 May 2014 15:51:37 +0000 (16:51 +0100)
committerSage Weil <sage@redhat.com>
Sat, 16 Aug 2014 04:00:49 +0000 (21:00 -0700)
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 <joao.luis@inktank.com>
(cherry picked from commit 67dc5751ba9a4e527ff12ea65000d1ba45d956f6)

src/common/strtol.cc
src/common/strtol.h

index 8f12f08f19bee8c53bdaa7c50691e1a14e683984..840b3d9c6a046df45c858818c8dde360e6953a75 100644 (file)
@@ -17,6 +17,9 @@
 #include <sstream>
 #include <stdlib.h>
 #include <string>
+extern "C" {
+#include <stdint.h>
+}
 
 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;
+}
index 80b5a3f9f922469aade68fcba41fd0898b4ada4b..ea0a469a777e61e59aedb044861b66be10c87af2 100644 (file)
@@ -16,6 +16,9 @@
 #define CEPH_COMMON_STRTOL_H
 
 #include <string>
+extern "C" {
+#include <stdint.h>
+}
 
 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