[Ocfs2-tools-devel] [PATCH 05/32] libtools: Add string functions to remove leading and trailing whitespaces

Sunil Mushran sunil.mushran at oracle.com
Tue Sep 14 15:54:35 PDT 2010


Add string functions (tools_strchomp(), tools_strchug() and tools_strstrip())
in libtools-internal, to remove leading and trailing whitespaces.

Signed-off-by: Sunil Mushran <sunil.mushran at oracle.com>
---
 include/tools-internal/Makefile |    2 +-
 include/tools-internal/utils.h  |   42 ++++++++++++++++
 libtools-internal/Makefile      |    2 +-
 libtools-internal/utils.c       |  104 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 148 insertions(+), 2 deletions(-)
 create mode 100644 include/tools-internal/utils.h
 create mode 100644 libtools-internal/utils.c

diff --git a/include/tools-internal/Makefile b/include/tools-internal/Makefile
index 3e5649e..ec12d90 100644
--- a/include/tools-internal/Makefile
+++ b/include/tools-internal/Makefile
@@ -2,7 +2,7 @@ TOPDIR = ../..
 
 include $(TOPDIR)/Preamble.make
 
-HFILES = verbose.h progress.h
+HFILES = verbose.h progress.h utils.h
 
 DIST_FILES = $(HFILES)
 
diff --git a/include/tools-internal/utils.h b/include/tools-internal/utils.h
new file mode 100644
index 0000000..899b3bd
--- /dev/null
+++ b/include/tools-internal/utils.h
@@ -0,0 +1,42 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * utils.h
+ *
+ * Utility functions
+ *
+ * Copyright (C) 2010 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#ifndef _INTERNAL_UTILS_H
+#define _INTERNAL_UTILS_H
+
+/*
+ * Removes trailing whitespace from a string. It does not allocate or reallocate
+ * any memory. It modifies the string in place.
+ */
+char *tools_strchomp(char *str);
+
+/*
+ * Removes leading whitespace from a string, by moving the rest of the
+ * characters forward. It does not allocate or reallocate any memory.
+ * It modifies the string in place.
+ */
+char *tools_strchug(char *str);
+
+/*
+ * Removes both the leading and trailing whitespaces
+ */
+#define tools_strstrip(str)	tools_strchug(tools_strchomp(str))
+
+#endif  /* _INTERNAL_UTILS_H */
diff --git a/libtools-internal/Makefile b/libtools-internal/Makefile
index 7b6bd42..05e3216 100644
--- a/libtools-internal/Makefile
+++ b/libtools-internal/Makefile
@@ -28,7 +28,7 @@ debug_%: debug_%.o
 
 endif
 
-CFILES = verbose.c progress.c
+CFILES = verbose.c progress.c utils.c
 HFILES = libtools-internal.h
 
 OBJS = $(subst .c,.o,$(CFILES))
diff --git a/libtools-internal/utils.c b/libtools-internal/utils.c
new file mode 100644
index 0000000..e6a374f
--- /dev/null
+++ b/libtools-internal/utils.c
@@ -0,0 +1,104 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * utils.c
+ *
+ * Utility functions
+ *
+ * Copyright (C) 2010 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#define _LARGEFILE64_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <assert.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <ctype.h>
+
+#include "tools-internal/utils.h"
+#include "libtools-internal.h"
+
+/*
+ * Public API
+ */
+
+char *tools_strchomp(char *str)
+{
+	int len = strlen(str);
+	char *p;
+
+	if (!len)
+		return str;
+
+	p = str + len - 1;;
+	while (isspace(*p) && len--)
+		*p-- = '\0';
+
+	return str;
+}
+
+char *tools_strchug(char *str)
+{
+	int len = strlen(str);
+	char *p = str;
+
+	if (!len)
+		return str;
+
+	while (isspace(*p) && len--)
+		p++;
+
+	if (len)
+		memmove(str, p, len);
+
+	str[len] = '\0';
+
+	return str;
+}
+
+#ifdef DEBUG_EXE
+
+typedef char * (*test_func)(char *str);
+static void do_test(char **s, test_func f)
+{
+	int i;
+	char tmp[100];
+
+	for (i = 0; s[i]; ++i) {
+		strncpy(tmp, s[i], sizeof(tmp));
+		printf("before:>%s< ", tmp);
+		*(f)(tmp);
+		printf("after:>%s<\n", tmp);
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	char *m[] = { "xxx", "xxx  \t", "xxx\n", "xx  x\n ", NULL };
+	char *u[] = { "xxx", "  \txxx", "\nxxx", " \nx xx", NULL };
+
+	setbuf(stdout, NULL);
+	setbuf(stderr, NULL);
+
+	printf("Testing tools_strchomp():\n");
+	do_test(m, &tools_strchomp);
+
+	printf("\nTesting tools_strchug():\n");
+	do_test(u, &tools_strchug);
+
+	return 0;
+}
+#endif
-- 
1.7.0.4




More information about the Ocfs2-tools-devel mailing list