Add string-helpers.c

This commit is contained in:
Johan Malm 2020-10-09 19:46:59 +01:00
parent 51507df2e7
commit 7e55e2cd09
6 changed files with 47 additions and 54 deletions

View file

@ -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 */

View file

@ -5,12 +5,12 @@
* session_environment_init - set enrivonment variables * session_environment_init - set enrivonment variables
* Note: Same as `. ~/.config/labwc/environment` (or equivalent XDG config dir) * 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 * session_autostart_init - run autostart file as shell script
* Note: Same as `sh ~/.config/labwc/autostart` (or equivalent XDG config dir) * 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 */ #endif /* __LABWC_SESSION_H */

View file

@ -5,4 +5,5 @@ labwc_sources += files(
'grab-file.c', 'grab-file.c',
'log.c', 'log.c',
'spawn.c', 'spawn.c',
'string-helpers.c',
) )

View file

@ -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;
}

View file

@ -8,6 +8,7 @@
#include "common/dir.h" #include "common/dir.h"
#include "common/log.h" #include "common/log.h"
#include "common/spawn.h" #include "common/spawn.h"
#include "common/string-helpers.h"
static bool static bool
isfile(const char *path) isfile(const char *path)
@ -22,30 +23,6 @@ string_empty(const char *s)
return !s || !*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 static void
process_line(char *line) process_line(char *line)
{ {
@ -58,8 +35,8 @@ process_line(char *line)
return; return;
} }
*p = '\0'; *p = '\0';
key = strstrip(line); key = string_strip(line);
value = strstrip(++p); value = string_strip(++p);
if (string_empty(key) || string_empty(value)) { if (string_empty(key) || string_empty(value)) {
return; return;
} }

View file

@ -8,6 +8,7 @@
#include "common/dir.h" #include "common/dir.h"
#include "common/log.h" #include "common/log.h"
#include "common/string-helpers.h"
#include "theme/theme.h" #include "theme/theme.h"
static int static int
@ -67,30 +68,6 @@ static void entry(const char *key, const char *value)
} }
/* clang-format on */ /* 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 static void
parse_config_line(char *line, char **key, char **value) parse_config_line(char *line, char **key, char **value)
{ {
@ -99,8 +76,8 @@ parse_config_line(char *line, char **key, char **value)
return; return;
} }
*p = '\0'; *p = '\0';
*key = strstrip(line); *key = string_strip(line);
*value = strstrip(++p); *value = string_strip(++p);
} }
static void static void