aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/string.c
diff options
context:
space:
mode:
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}