All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf tools: New function to parse string representing size in bytes
@ 2009-11-15  3:50 Hitoshi Mitake
  2009-11-15  5:11 ` Frederic Weisbecker
  0 siblings, 1 reply; 14+ messages in thread
From: Hitoshi Mitake @ 2009-11-15  3:50 UTC (permalink / raw)
  To: Ingo Molnar
  Cc: linux-kernel, Hitoshi Mitake, Peter Zijlstra, Paul Mackerras,
	Frederic Weisbecker

This patch modifies util/string.[ch] to add new function: bytesexp2int()
to parse string representing size in bytes.

Below is the description of bytesexp2int().

Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB) (e.g. "256MB")
and return its numeric value. (e.g. 268435456)

The parameter str is not changed before and after calling,
but it changed temporally and internally for atoi().
So type of str is char *, not const char *.

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>
---
 tools/perf/util/string.c |   89 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/string.h |    1 +
 2 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
index 04743d3..bbedb06 100644
--- a/tools/perf/util/string.c
+++ b/tools/perf/util/string.c
@@ -1,4 +1,5 @@
 #include <string.h>
+#include <stdlib.h>
 #include "string.h"
 
 static int hex(char ch)
@@ -43,3 +44,91 @@ char *strxfrchar(char *s, char from, char to)
 
 	return s;
 }
+
+static int digit(char ch)
+{
+	if ('0' <= ch && ch <= '9')
+		return 1;
+	return 0;
+}
+
+#define K 1024
+/*
+ * bytesexp2int()
+ * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB) (e.g. "256MB")
+ * and return its numeric value
+ *
+ * The parameter str is not changed before and after calling,
+ * but it changed temporally and internally for atoi().
+ * So type of str is char *, not const char *.
+ */
+int bytesexp2int(char *str)
+{
+	int i, unit = 1;
+	char shelter = '\0';
+	size_t length = -1;
+
+	if (!digit(str[0]))
+		goto err;
+	
+	for (i = 1; i < (int)strlen(str); i++) {
+		switch (str[i]) {
+		case 'B':
+		case 'b':
+			break;
+		case 'K':
+			if (str[i + 1] != 'B')
+				goto err;
+			else
+				goto kilo;
+		case 'k':
+			if (str[i + 1] != 'b')
+				goto err;
+kilo:
+			unit = K;
+			break;
+		case 'M':
+			if (str[i + 1] != 'B')
+				goto err;
+			else
+				goto mega;
+		case 'm':
+			if (str[i + 1] != 'b')
+				goto err;
+mega:
+			unit = K * K;
+			break;
+		case 'G':
+			if (str[i + 1] != 'B')
+				goto err;
+			else
+				goto giga;
+		case 'g':
+			if (str[i + 1] != 'b')
+				goto err;
+giga:
+			unit = K * K * K;
+			break;
+		case '\0':	/* only specified figures */
+			unit = 1;
+			break;
+		default:
+			if (!digit(str[i]))
+				goto err;
+			break;
+		}
+	}
+
+	shelter = str[i];
+	str[i] = (char)'\0';
+	length = atoi(str) * unit;
+	if (shelter != '\0')
+		str[i] = shelter;
+
+	goto end;
+
+err:
+	length = -1;
+end:
+	return length;
+}
diff --git a/tools/perf/util/string.h b/tools/perf/util/string.h
index 2c84bf6..adbf077 100644
--- a/tools/perf/util/string.h
+++ b/tools/perf/util/string.h
@@ -5,6 +5,7 @@
 
 int hex2u64(const char *ptr, u64 *val);
 char *strxfrchar(char *s, char from, char to);
+int bytesexp2int(char *str);
 
 #define _STR(x) #x
 #define STR(x) _STR(x)
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2009-11-15 14:19 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-15  3:50 [PATCH] perf tools: New function to parse string representing size in bytes Hitoshi Mitake
2009-11-15  5:11 ` Frederic Weisbecker
2009-11-15  8:08   ` Ingo Molnar
2009-11-15  8:15     ` Hitoshi Mitake
2009-11-15  8:17       ` [PATCH v2] " Hitoshi Mitake
2009-11-15  8:57         ` Ingo Molnar
2009-11-15  9:52           ` Hitoshi Mitake
2009-11-15 10:19             ` [PATCH v3] " Hitoshi Mitake
2009-11-15 10:28               ` Ingo Molnar
2009-11-15 10:57                 ` Hitoshi Mitake
2009-11-15 11:04                   ` Ingo Molnar
2009-11-15 11:32                     ` Hitoshi Mitake
2009-11-15 11:36                       ` [PATCH v4] " Hitoshi Mitake
2009-11-15 14:18                         ` [tip:perf/core] perf tools: Add new perf_atoll() " tip-bot for Hitoshi Mitake

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.