From 427dff3450d203d7cc1b6c94750e5c159c4f2605 Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Wed, 21 Feb 2018 12:25:09 +0000 Subject: [PATCH] s390-tools: Move function util_strstrip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch - moves function util_strstrip to libutil/util_libc.c and deletes the extra files util_strstip.[ch] - adds doxygen comments for util_strstrip() - provides an example on how to use util_strstrip(). Signed-off-by: Thomas Richter Signed-off-by: Jan Höppner --- include/lib/util_libc.h | 10 ++++++++++ include/lib/util_str.h | 25 ------------------------ libutil/Makefile | 3 +-- libutil/util_libc.c | 30 ++++++++++++++++++++++++++++ libutil/util_libc_example.c | 9 +++++++++ libutil/util_str.c | 39 ------------------------------------- vmur/vmur.cpp | 2 +- 7 files changed, 51 insertions(+), 67 deletions(-) delete mode 100644 include/lib/util_str.h delete mode 100644 libutil/util_str.c diff --git a/include/lib/util_libc.h b/include/lib/util_libc.h index f24a1ebd..dc6b3d09 100644 --- a/include/lib/util_libc.h +++ b/include/lib/util_libc.h @@ -14,6 +14,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + /** * Allocate memory or panic in case of failure * @@ -125,4 +129,10 @@ int __util_vsprintf(const char *func, const char *file, int line, char *util_strcat_realloc(char *str1, const char *str2); void util_str_toupper(char *str); +char *util_strstrip(char *s); + +#ifdef __cplusplus +} +#endif + #endif /** LIB_UTIL_LIBC_H @} */ diff --git a/include/lib/util_str.h b/include/lib/util_str.h deleted file mode 100644 index abe14a8c..00000000 --- a/include/lib/util_str.h +++ /dev/null @@ -1,25 +0,0 @@ -/** - * @defgroup util_str - * @{ - * @brief Strip leading and trailing blanks - * - * Copyright IBM Corp. 2018 - * - * s390-tools is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef LIB_UTIL_STR_H -#define LIB_UTIL_STR_H - -#ifdef __cplusplus -extern "C" { -#endif - -char *util_strstrip(char *s); - -#ifdef __cplusplus -} -#endif - -#endif /** LIB_UTIL_STRSTRIP_H @} */ diff --git a/libutil/Makefile b/libutil/Makefile index 29b5ee76..fd5e2138 100644 --- a/libutil/Makefile +++ b/libutil/Makefile @@ -26,8 +26,7 @@ objects = util_base.o \ util_part.o \ util_prg.o \ util_proc.o \ - util_rec.o \ - util_str.o + util_rec.o util_base_example: util_base_example.o $(lib) util_panic_example: util_panic_example.o $(lib) diff --git a/libutil/util_libc.c b/libutil/util_libc.c index bf45bc5b..127b9211 100644 --- a/libutil/util_libc.c +++ b/libutil/util_libc.c @@ -198,3 +198,33 @@ int __util_vsprintf(const char *func, const char *file, int line, rc != -1, "Could not format string\n"); return rc; } + +/** + * Strip leading and trailing spaces from string + * + * Remove string \a s leading and trailing spaces + * + * @param[in,out] s String to manipulate + * + * @returns Pointer to first non-space character in string \a s + */ +char *util_strstrip(char *s) +{ + size_t size; + char *end; + + size = strlen(s); + + if (!size) + return s; + + end = s + size - 1; + while (end >= s && isspace(*end)) + end--; + *(end + 1) = '\0'; + + while (*s && isspace(*s)) + s++; + + return s; +} diff --git a/libutil/util_libc_example.c b/libutil/util_libc_example.c index c77777b1..3789c3a6 100644 --- a/libutil/util_libc_example.c +++ b/libutil/util_libc_example.c @@ -10,10 +10,13 @@ //! [code] #include #include +#include #include "lib/util_libc.h" #include "lib/util_panic.h" +#define EXAMPLE_WORD " /sys/devices/system/cpu " + /* * Demonstrate that out of memory is automatically handled via panic() */ @@ -22,6 +25,12 @@ int main(void) unsigned long ulong_max = (unsigned long)-1; void *ptr; char *zeroes, *str; + char buffer[sizeof(EXAMPLE_WORD)]; + + strcat(buffer, EXAMPLE_WORD); + fprintf(stderr, "Try to remove leading and trailing spaces from " + "\"%s\"\nresult = \"%s\"\n", EXAMPLE_WORD, + util_strstrip(buffer)); /* Use util_strcat_realloc() for string concatenation */ fprintf(stderr, "Try to concatenate \"Hello\", \", \" and \"world!\": "); diff --git a/libutil/util_str.c b/libutil/util_str.c deleted file mode 100644 index 4bc44ec3..00000000 --- a/libutil/util_str.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - * util - Utility function library - * - * Strip leading and trailing blanks. - * - * Copyright IBM Corp. 2018 - * - * s390-tools is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#include -#include - -#include "lib/util_str.h" - -/* - * strip leading and trailing blanks - */ -char *util_strstrip(char *s) -{ - size_t size; - char *end; - - size = strlen(s); - - if (!size) - return s; - - end = s + size - 1; - while (end >= s && isspace(*end)) - end--; - *(end + 1) = '\0'; - - while (*s && isspace(*s)) - s++; - - return s; -} diff --git a/vmur/vmur.cpp b/vmur/vmur.cpp index b2bc5231..eb77e6c7 100644 --- a/vmur/vmur.cpp +++ b/vmur/vmur.cpp @@ -31,7 +31,7 @@ #include "lib/vmdump.h" #include "lib/zt_common.h" -#include "lib/util_str.h" +#include "lib/util_libc.h" #include "vmur.h"