From edbe6c2bdc6a3011e5aea208c44be5eee02f120b Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Mon, 14 Sep 2015 18:37:38 -0400 Subject: move strtrim to util-common Signed-off-by: Andrew Gregory Signed-off-by: Allan McRae --- src/common/util-common.c | 39 +++++++++++++++++++++++++++++++++++++++ src/common/util-common.h | 2 ++ src/pacman/ini.c | 2 +- src/pacman/util.c | 39 --------------------------------------- src/pacman/util.h | 1 - src/util/pactree.c | 36 ------------------------------------ 6 files changed, 42 insertions(+), 77 deletions(-) (limited to 'src') diff --git a/src/common/util-common.c b/src/common/util-common.c index e8341688..542dcfde 100644 --- a/src/common/util-common.c +++ b/src/common/util-common.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include #include #include #include @@ -127,6 +128,44 @@ char *safe_fgets(char *s, int size, FILE *stream) return ret; } +/* Trim whitespace and newlines from a string + */ +size_t strtrim(char *str) +{ + char *end, *pch = str; + + if(str == NULL || *str == '\0') { + /* string is empty, so we're done. */ + return 0; + } + + while(isspace((unsigned char)*pch)) { + pch++; + } + if(pch != str) { + size_t len = strlen(pch); + if(len) { + memmove(str, pch, len + 1); + pch = str; + } else { + *str = '\0'; + } + } + + /* check if there wasn't anything but whitespace in the string. */ + if(*str == '\0') { + return 0; + } + + end = (str + strlen(str) - 1); + while(isspace((unsigned char)*end)) { + end--; + } + *++end = '\0'; + + return end - pch; +} + #ifndef HAVE_STRNLEN /* A quick and dirty implementation derived from glibc */ /** Determines the length of a fixed-size string. diff --git a/src/common/util-common.h b/src/common/util-common.h index a2093bef..af2ebdaa 100644 --- a/src/common/util-common.h +++ b/src/common/util-common.h @@ -30,6 +30,8 @@ int llstat(char *path, struct stat *buf); char *safe_fgets(char *s, int size, FILE *stream); +size_t strtrim(char *str); + #ifndef HAVE_STRNDUP char *strndup(const char *s, size_t n); #endif diff --git a/src/pacman/ini.c b/src/pacman/ini.c index fed0b187..da30af1c 100644 --- a/src/pacman/ini.c +++ b/src/pacman/ini.c @@ -24,7 +24,7 @@ #include #include "ini.h" -#include "util.h" +#include "util-common.h" /** * @brief Parse a pacman-style INI config file. diff --git a/src/pacman/util.c b/src/pacman/util.c index 3d71d8b0..1542f8a2 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -29,7 +29,6 @@ #include /* intmax_t */ #include #include -#include #include #include #include @@ -353,44 +352,6 @@ void indentprint(const char *str, unsigned short indent, unsigned short cols) free(wcstr); } -/* Trim whitespace and newlines from a string - */ -size_t strtrim(char *str) -{ - char *end, *pch = str; - - if(str == NULL || *str == '\0') { - /* string is empty, so we're done. */ - return 0; - } - - while(isspace((unsigned char)*pch)) { - pch++; - } - if(pch != str) { - size_t len = strlen(pch); - if(len) { - memmove(str, pch, len + 1); - pch = str; - } else { - *str = '\0'; - } - } - - /* check if there wasn't anything but whitespace in the string. */ - if(*str == '\0') { - return 0; - } - - end = (str + strlen(str) - 1); - while(isspace((unsigned char)*end)) { - end--; - } - *++end = '\0'; - - return end - pch; -} - /* Replace all occurrences of 'needle' with 'replace' in 'str', returning * a new string (must be free'd) */ char *strreplace(const char *str, const char *needle, const char *replace) diff --git a/src/pacman/util.h b/src/pacman/util.h index c82d816e..744c13ca 100644 --- a/src/pacman/util.h +++ b/src/pacman/util.h @@ -54,7 +54,6 @@ unsigned short getcols(void); void columns_cache_reset(void); int rmrf(const char *path); void indentprint(const char *str, unsigned short indent, unsigned short cols); -size_t strtrim(char *str); char *strreplace(const char *str, const char *needle, const char *replace); void string_display(const char *title, const char *string, unsigned short cols); double humanize_size(off_t bytes, const char target_unit, int precision, diff --git a/src/util/pactree.c b/src/util/pactree.c index 11ad7cae..6969995c 100644 --- a/src/util/pactree.c +++ b/src/util/pactree.c @@ -123,42 +123,6 @@ int searchsyncs = 0; const char *dbpath = DBPATH; const char *configfile = CONFFILE; -static size_t strtrim(char *str) -{ - char *end, *pch = str; - - if(str == NULL || *str == '\0') { - /* string is empty, so we're done. */ - return 0; - } - - while(isspace((unsigned char)*pch)) { - pch++; - } - if(pch != str) { - size_t len = strlen(pch); - if(len) { - memmove(str, pch, len + 1); - pch = str; - } else { - *str = '\0'; - } - } - - /* check if there wasn't anything but whitespace in the string. */ - if(*str == '\0') { - return 0; - } - - end = (str + strlen(str) - 1); - while(isspace((unsigned char)*end)) { - end--; - } - *++end = '\0'; - - return end - pch; -} - static int register_syncs(void) { FILE *fp; -- cgit v1.2.3-24-g4f1b