aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/string.c
diff options
context:
space:
mode:
authorHitoshi Mitake <mitake@dcl.info.waseda.ac.jp>2009-11-15 06:36:53 -0500
committerIngo Molnar <mingo@elte.hu>2009-11-15 08:54:23 -0500
commitd2fb8b4151a92223da6a84006f8f248ebeb6677d (patch)
treea25e75e2338f6b678de94b8a5b24a77104f01a02 /tools/perf/util/string.c
parent0ffa798d947f5f5e40690cc9d38e678080a34f87 (diff)
perf tools: Add new perf_atoll() function to parse string representing size in bytes
This patch modifies util/string.[ch] to add new function: perf_atoll() to parse string representing size in bytes. This function parses (\d+)(b|B|kb|KB|mb|MB|gb|GB) (e.g. "256MB") and returns its numeric value. (e.g. 268435456) Signed-off-by: Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Paul Mackerras <paulus@samba.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> LKML-Reference: <1258285013-4759-1-git-send-email-mitake@dcl.info.waseda.ac.jp> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'tools/perf/util/string.c')
-rw-r--r--tools/perf/util/string.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
index 04743d3e9039..227043577e06 100644
--- a/tools/perf/util/string.c
+++ b/tools/perf/util/string.c
@@ -1,5 +1,7 @@
1#include <string.h> 1#include <string.h>
2#include <stdlib.h>
2#include "string.h" 3#include "string.h"
4#include "util.h"
3 5
4static int hex(char ch) 6static int hex(char ch)
5{ 7{
@@ -43,3 +45,85 @@ char *strxfrchar(char *s, char from, char to)
43 45
44 return s; 46 return s;
45} 47}
48
49#define K 1024LL
50/*
51 * perf_atoll()
52 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
53 * and return its numeric value
54 */
55s64 perf_atoll(const char *str)
56{
57 unsigned int i;
58 s64 length = -1, unit = 1;
59
60 if (!isdigit(str[0]))
61 goto out_err;
62
63 for (i = 1; i < strlen(str); i++) {
64 switch (str[i]) {
65 case 'B':
66 case 'b':
67 break;
68 case 'K':
69 if (str[i + 1] != 'B')
70 goto out_err;
71 else
72 goto kilo;
73 case 'k':
74 if (str[i + 1] != 'b')
75 goto out_err;
76kilo:
77 unit = K;
78 break;
79 case 'M':
80 if (str[i + 1] != 'B')
81 goto out_err;
82 else
83 goto mega;
84 case 'm':
85 if (str[i + 1] != 'b')
86 goto out_err;
87mega:
88 unit = K * K;
89 break;
90 case 'G':
91 if (str[i + 1] != 'B')
92 goto out_err;
93 else
94 goto giga;
95 case 'g':
96 if (str[i + 1] != 'b')
97 goto out_err;
98giga:
99 unit = K * K * K;
100 break;
101 case 'T':
102 if (str[i + 1] != 'B')
103 goto out_err;
104 else
105 goto tera;
106 case 't':
107 if (str[i + 1] != 'b')
108 goto out_err;
109tera:
110 unit = K * K * K * K;
111 break;
112 case '\0': /* only specified figures */
113 unit = 1;
114 break;
115 default:
116 if (!isdigit(str[i]))
117 goto out_err;
118 break;
119 }
120 }
121
122 length = atoll(str) * unit;
123 goto out;
124
125out_err:
126 length = -1;
127out:
128 return length;
129}