]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Add string-helpers.c
authorJohan Malm <jgm323@gmail.com>
Fri, 9 Oct 2020 18:46:59 +0000 (19:46 +0100)
committerJohan Malm <jgm323@gmail.com>
Fri, 9 Oct 2020 18:46:59 +0000 (19:46 +0100)
include/common/string-helpers.h [new file with mode: 0644]
include/config/session.h
src/common/meson.build
src/common/string-helpers.c [new file with mode: 0644]
src/config/session.c
src/theme/theme.c

diff --git a/include/common/string-helpers.h b/include/common/string-helpers.h
new file mode 100644 (file)
index 0000000..de3f348
--- /dev/null
@@ -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 */
index 89365a506740d3d8fdb4f8fc216f131425369c6e..9f635bafa1343f8c5f6c6b2e710cab235596a3f6 100644 (file)
@@ -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 */
index d2f1af5f1eec556eaef8398ef5385323cae714ea..962b2b94c9dc3624444c81a9b782926e3fb75a11 100644 (file)
@@ -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 (file)
index 0000000..5476da8
--- /dev/null
@@ -0,0 +1,27 @@
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+
+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;
+}
index eb7cd003827e1faa762bc850c6e4d3ca42bd1ae6..7dcf7c7507070ba7707b25bd02a7d78fe22f55a0 100644 (file)
@@ -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;
        }
index a7348ea697a1423c00431717076ca0ce708ac8fb..ffc1e7e36520994f1897d5558d576e9d6628421c 100644 (file)
@@ -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