From: Johan Malm Date: Fri, 19 Jan 2024 19:06:07 +0000 (+0000) Subject: string-helpers.c: add string_empty() X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=c0668210463ef0a58c1b397af4cf0b2571db8170;p=proto%2Flabwc.git string-helpers.c: add string_empty() --- diff --git a/include/common/string-helpers.h b/include/common/string-helpers.h index 20f74d5a..4e204302 100644 --- a/include/common/string-helpers.h +++ b/include/common/string-helpers.h @@ -1,6 +1,13 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #ifndef LABWC_STRING_HELPERS_H #define LABWC_STRING_HELPERS_H +#include + +/** + * string_null_or_empty() - Check if string is NULL or empty + * @s: string to check + */ +bool string_null_or_empty(const char *s); /** * trim_last_field() - Trim last field of string splitting on provided delim diff --git a/src/button/button-png.c b/src/button/button-png.c index e053ad47..2c700a06 100644 --- a/src/button/button-png.c +++ b/src/button/button-png.c @@ -12,6 +12,7 @@ #include "buffer.h" #include "button/button-png.h" #include "button/common.h" +#include "common/string-helpers.h" #include "labwc.h" /* @@ -49,7 +50,7 @@ button_png_load(const char *button_name, struct lab_data_buffer **buffer) wlr_buffer_drop(&(*buffer)->base); *buffer = NULL; } - if (!button_name || !*button_name) { + if (string_null_or_empty(button_name)) { return; } diff --git a/src/button/button-svg.c b/src/button/button-svg.c index 2846e5b1..2128524f 100644 --- a/src/button/button-svg.c +++ b/src/button/button-svg.c @@ -12,6 +12,7 @@ #include "buffer.h" #include "button/button-svg.h" #include "button/common.h" +#include "common/string-helpers.h" #include "labwc.h" void @@ -22,7 +23,7 @@ button_svg_load(const char *button_name, struct lab_data_buffer **buffer, wlr_buffer_drop(&(*buffer)->base); *buffer = NULL; } - if (!button_name || !*button_name) { + if (string_null_or_empty(button_name)) { return; } diff --git a/src/button/button-xbm.c b/src/button/button-xbm.c index fe7460fa..125873c0 100644 --- a/src/button/button-xbm.c +++ b/src/button/button-xbm.c @@ -17,6 +17,7 @@ #include "button/common.h" #include "common/grab-file.h" #include "common/mem.h" +#include "common/string-helpers.h" #include "buffer.h" enum token_type { @@ -279,7 +280,7 @@ button_xbm_load(const char *button_name, struct lab_data_buffer **buffer, wlr_buffer_drop(&(*buffer)->base); *buffer = NULL; } - if (!button_name || !*button_name) { + if (string_null_or_empty(button_name)) { return; } color = argb32(rgba); diff --git a/src/common/font.c b/src/common/font.c index b27b7dde..3b997333 100644 --- a/src/common/font.c +++ b/src/common/font.c @@ -7,6 +7,7 @@ #include #include "common/font.h" #include "common/graphic-helpers.h" +#include "common/string-helpers.h" #include "labwc.h" #include "buffer.h" @@ -84,7 +85,7 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width, max_width = 2; } - if (!text || !*text) { + if (string_null_or_empty(text)) { return; } diff --git a/src/common/string-helpers.c b/src/common/string-helpers.c index ec2c86bd..37fdf4d0 100644 --- a/src/common/string-helpers.c +++ b/src/common/string-helpers.c @@ -6,6 +6,12 @@ #include "common/mem.h" #include "common/string-helpers.h" +bool +string_null_or_empty(const char *s) +{ + return !s || !*s; +} + void trim_last_field(char *buf, char delim) { diff --git a/src/config/libinput.c b/src/config/libinput.c index 4ea7499c..7ce62b79 100644 --- a/src/config/libinput.c +++ b/src/config/libinput.c @@ -4,6 +4,7 @@ #include "common/mem.h" #include "common/list.h" +#include "common/string-helpers.h" #include "config/libinput.h" #include "config/rcxml.h" @@ -27,7 +28,7 @@ libinput_category_init(struct libinput_category *l) enum lab_libinput_device_type get_device_type(const char *s) { - if (!s || !*s) { + if (string_null_or_empty(s)) { return LAB_LIBINPUT_DEVICE_NONE; } if (!strcasecmp(s, "default")) { diff --git a/src/config/session.c b/src/config/session.c index 6489752c..c87b0865 100644 --- a/src/config/session.c +++ b/src/config/session.c @@ -14,16 +14,10 @@ #include "config/session.h" #include "labwc.h" -static bool -string_empty(const char *s) -{ - return !s || !*s; -} - static void process_line(char *line) { - if (string_empty(line) || line[0] == '#') { + if (string_null_or_empty(line) || line[0] == '#') { return; } char *key = NULL; @@ -39,7 +33,7 @@ process_line(char *line) buf_add(&value, string_strip(++p)); buf_expand_shell_variables(&value); buf_expand_tilde(&value); - if (string_empty(key) || !value.len) { + if (string_null_or_empty(key) || !value.len) { goto error; } setenv(key, value.buf, 1); diff --git a/src/ssd/ssd_titlebar.c b/src/ssd/ssd_titlebar.c index 6d3e936c..b0ffe8a0 100644 --- a/src/ssd/ssd_titlebar.c +++ b/src/ssd/ssd_titlebar.c @@ -7,6 +7,7 @@ #include "common/mem.h" #include "common/scaled_font_buffer.h" #include "common/scene-helpers.h" +#include "common/string-helpers.h" #include "labwc.h" #include "node.h" #include "ssd-internal.h" @@ -325,7 +326,7 @@ ssd_update_title(struct ssd *ssd) struct view *view = ssd->view; char *title = (char *)view_get_string_prop(view, "title"); - if (!title || !*title) { + if (string_null_or_empty(title)) { return; }