From 4081ef3a953c6011732447797daea2aac4621086 Mon Sep 17 00:00:00 2001 From: Hitoshi Mitake Date: Sun, 15 Nov 2009 20:36:53 +0900 Subject: [PATCH] --- yaml --- r: 169623 b: refs/heads/master c: d2fb8b4151a92223da6a84006f8f248ebeb6677d h: refs/heads/master i: 169621: bb8028a57c943c581a4810100a98d1b106cc86be 169619: 20bda956aa5d752abc1944e8181c425ff47a967e 169615: 3ff9a25f95272f5f6a43ba9887c0ae4dccdd3fae v: v3 --- [refs] | 2 +- trunk/tools/perf/util/string.c | 84 ++++++++++++++++++++++++++++++++++ trunk/tools/perf/util/string.h | 1 + 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/[refs] b/[refs] index 484e02886f14..061ad162a4e5 100644 --- a/[refs] +++ b/[refs] @@ -1,2 +1,2 @@ --- -refs/heads/master: 0ffa798d947f5f5e40690cc9d38e678080a34f87 +refs/heads/master: d2fb8b4151a92223da6a84006f8f248ebeb6677d diff --git a/trunk/tools/perf/util/string.c b/trunk/tools/perf/util/string.c index 04743d3e9039..227043577e06 100644 --- a/trunk/tools/perf/util/string.c +++ b/trunk/tools/perf/util/string.c @@ -1,5 +1,7 @@ #include +#include #include "string.h" +#include "util.h" static int hex(char ch) { @@ -43,3 +45,85 @@ char *strxfrchar(char *s, char from, char to) return s; } + +#define K 1024LL +/* + * perf_atoll() + * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB") + * and return its numeric value + */ +s64 perf_atoll(const char *str) +{ + unsigned int i; + s64 length = -1, unit = 1; + + if (!isdigit(str[0])) + goto out_err; + + for (i = 1; i < strlen(str); i++) { + switch (str[i]) { + case 'B': + case 'b': + break; + case 'K': + if (str[i + 1] != 'B') + goto out_err; + else + goto kilo; + case 'k': + if (str[i + 1] != 'b') + goto out_err; +kilo: + unit = K; + break; + case 'M': + if (str[i + 1] != 'B') + goto out_err; + else + goto mega; + case 'm': + if (str[i + 1] != 'b') + goto out_err; +mega: + unit = K * K; + break; + case 'G': + if (str[i + 1] != 'B') + goto out_err; + else + goto giga; + case 'g': + if (str[i + 1] != 'b') + goto out_err; +giga: + unit = K * K * K; + break; + case 'T': + if (str[i + 1] != 'B') + goto out_err; + else + goto tera; + case 't': + if (str[i + 1] != 'b') + goto out_err; +tera: + unit = K * K * K * K; + break; + case '\0': /* only specified figures */ + unit = 1; + break; + default: + if (!isdigit(str[i])) + goto out_err; + break; + } + } + + length = atoll(str) * unit; + goto out; + +out_err: + length = -1; +out: + return length; +} diff --git a/trunk/tools/perf/util/string.h b/trunk/tools/perf/util/string.h index 2c84bf65ba0f..e50b07f80827 100644 --- a/trunk/tools/perf/util/string.h +++ b/trunk/tools/perf/util/string.h @@ -5,6 +5,7 @@ int hex2u64(const char *ptr, u64 *val); char *strxfrchar(char *s, char from, char to); +s64 perf_atoll(const char *str); #define _STR(x) #x #define STR(x) _STR(x)