From 7e55e2cd0990664c2056de4d597b6ac844686107 Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Fri, 9 Oct 2020 19:46:59 +0100 Subject: [PATCH] Add string-helpers.c --- include/common/string-helpers.h | 11 +++++++++++ include/config/session.h | 4 ++-- src/common/meson.build | 1 + src/common/string-helpers.c | 27 +++++++++++++++++++++++++++ src/config/session.c | 29 +++-------------------------- src/theme/theme.c | 29 +++-------------------------- 6 files changed, 47 insertions(+), 54 deletions(-) create mode 100644 include/common/string-helpers.h create mode 100644 src/common/string-helpers.c diff --git a/include/common/string-helpers.h b/include/common/string-helpers.h new file mode 100644 index 00000000..de3f348c --- /dev/null +++ b/include/common/string-helpers.h @@ -0,0 +1,11 @@ +#ifndef __LABWC_STRING_HELPERs_H +#define __LABWC_STRING_HELPERS_H + +/** + * string_strip - strip white space left and right + * Note: this function does a left skip, so the returning pointer cannot be + * used to free any allocated memory + */ +char *string_strip(char *s); + +#endif /* __LABWC_STRING_HELPERs_H */ diff --git a/include/config/session.h b/include/config/session.h index 89365a50..9f635baf 100644 --- a/include/config/session.h +++ b/include/config/session.h @@ -5,12 +5,12 @@ * session_environment_init - set enrivonment variables * Note: Same as `. ~/.config/labwc/environment` (or equivalent XDG config dir) */ -session_environment_init(void); +void session_environment_init(void); /** * session_autostart_init - run autostart file as shell script * Note: Same as `sh ~/.config/labwc/autostart` (or equivalent XDG config dir) */ -session_autostart_init(void); +void session_autostart_init(void); #endif /* __LABWC_SESSION_H */ diff --git a/src/common/meson.build b/src/common/meson.build index d2f1af5f..962b2b94 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -5,4 +5,5 @@ labwc_sources += files( 'grab-file.c', 'log.c', 'spawn.c', + 'string-helpers.c', ) diff --git a/src/common/string-helpers.c b/src/common/string-helpers.c new file mode 100644 index 00000000..5476da80 --- /dev/null +++ b/src/common/string-helpers.c @@ -0,0 +1,27 @@ +#include +#include +#include + +static void +rtrim(char **s) +{ + size_t len = strlen(*s); + if (!len) { + return; + } + char *end = *s + len - 1; + while (end >= *s && isspace(*end)) { + end--; + } + *(end + 1) = '\0'; +} + +char * +string_strip(char *s) +{ + rtrim(&s); + while (isspace(*s)) { + s++; + } + return s; +} diff --git a/src/config/session.c b/src/config/session.c index eb7cd003..7dcf7c75 100644 --- a/src/config/session.c +++ b/src/config/session.c @@ -8,6 +8,7 @@ #include "common/dir.h" #include "common/log.h" #include "common/spawn.h" +#include "common/string-helpers.h" static bool isfile(const char *path) @@ -22,30 +23,6 @@ string_empty(const char *s) return !s || !*s; } -static void -rtrim(char **s) -{ - size_t len = strlen(*s); - if (!len) { - return; - } - char *end = *s + len - 1; - while (end >= *s && isspace(*end)) { - end--; - } - *(end + 1) = '\0'; -} - -static char * -strstrip(char *s) -{ - rtrim(&s); - while (isspace(*s)) { - s++; - } - return s; -} - static void process_line(char *line) { @@ -58,8 +35,8 @@ process_line(char *line) return; } *p = '\0'; - key = strstrip(line); - value = strstrip(++p); + key = string_strip(line); + value = string_strip(++p); if (string_empty(key) || string_empty(value)) { return; } diff --git a/src/theme/theme.c b/src/theme/theme.c index a7348ea6..ffc1e7e3 100644 --- a/src/theme/theme.c +++ b/src/theme/theme.c @@ -8,6 +8,7 @@ #include "common/dir.h" #include "common/log.h" +#include "common/string-helpers.h" #include "theme/theme.h" static int @@ -67,30 +68,6 @@ static void entry(const char *key, const char *value) } /* clang-format on */ -static void -rtrim(char **s) -{ - size_t len = strlen(*s); - if (!len) { - return; - } - char *end = *s + len - 1; - while (end >= *s && isspace(*end)) { - end--; - } - *(end + 1) = '\0'; -} - -static char * -strstrip(char *s) -{ - rtrim(&s); - while (isspace(*s)) { - s++; - } - return s; -} - static void parse_config_line(char *line, char **key, char **value) { @@ -99,8 +76,8 @@ parse_config_line(char *line, char **key, char **value) return; } *p = '\0'; - *key = strstrip(line); - *value = strstrip(++p); + *key = string_strip(line); + *value = string_strip(++p); } static void