From 1f4cd4bd1509b4fe3b3c62738fbc59b4f5162f9b Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Wed, 3 Oct 2018 15:28:08 +0200 Subject: [PATCH] Use STR_(EMPTY|FULL) for string emptyness checks --- src/icon.c | 4 ++-- src/notification.c | 4 ++-- src/option_parser.c | 4 ++-- src/queues.c | 2 +- src/settings.c | 11 +++++------ src/utils.c | 4 ++-- src/utils.h | 3 +++ 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/icon.c b/src/icon.c index 6527973..ab43e19 100644 --- a/src/icon.c +++ b/src/icon.c @@ -76,7 +76,7 @@ GdkPixbuf *get_pixbuf_from_file(const char *filename) GdkPixbuf *get_pixbuf_from_icon(const char *iconname) { - if (!iconname || iconname[0] == '\0') + if (STR_EMPTY(iconname)) return NULL; const char *suffixes[] = { ".svg", ".png", ".xpm", NULL }; @@ -117,7 +117,7 @@ GdkPixbuf *get_pixbuf_from_icon(const char *iconname) break; start = end + 1; - } while (*(end) != '\0'); + } while (STR_FULL(end)); if (!pixbuf) LOG_W("No icon found in path: '%s'", iconname); } diff --git a/src/notification.c b/src/notification.c index eb57507..26ea205 100644 --- a/src/notification.c +++ b/src/notification.c @@ -89,7 +89,7 @@ void notification_print(const struct notification *n) /* see notification.h */ void notification_run_script(struct notification *n) { - if (!n->script || strlen(n->script) < 1) + if (STR_EMPTY(n->script)) return; if (n->script_run && !settings.always_run_script) @@ -300,7 +300,7 @@ void notification_init(struct notification *n) n->timeout = settings.timeouts[n->urgency]; /* Icon handling */ - if (n->icon && strlen(n->icon) <= 0) + if (STR_EMPTY(n->icon)) g_clear_pointer(&n->icon, g_free); if (!n->raw_icon && !n->icon) n->icon = g_strdup(settings.icons[n->urgency]); diff --git a/src/option_parser.c b/src/option_parser.c index 0cb8354..aeb9463 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -231,7 +231,7 @@ int load_ini_file(FILE *fp) char *start = g_strstrip(line); - if (*start == ';' || *start == '#' || strlen(start) == 0) + if (*start == ';' || *start == '#' || STR_EMPTY(start)) continue; if (*start == '[') { @@ -521,7 +521,7 @@ int option_get_bool(const char *ini_section, void cmdline_usage_append(const char *key, const char *type, const char *description) { char *key_type; - if (type && strlen(type) > 0) + if (STR_FULL(type)) key_type = g_strdup_printf("%s (%s)", key, type); else key_type = g_strdup(key); diff --git a/src/queues.c b/src/queues.c index f5d2077..6620eb8 100644 --- a/src/queues.c +++ b/src/queues.c @@ -125,7 +125,7 @@ int queues_notification_insert(struct notification *n) { /* do not display the message, if the message is empty */ - if (strlen(n->msg) == 0) { + if (STR_EMPTY(n->msg)) { if (settings.always_run_script) { notification_run_script(n); } diff --git a/src/settings.c b/src/settings.c index 2bca7bd..90c108b 100644 --- a/src/settings.c +++ b/src/settings.c @@ -80,7 +80,7 @@ static enum urgency ini_get_urgency(const char *section, const char *key, const int ret = def; char *urg = ini_get_string(section, key, ""); - if (strlen(urg) > 0) { + if (STR_FULL(urg)) { if (strcmp(urg, "low") == 0) ret = URG_LOW; else if (strcmp(urg, "normal") == 0) @@ -247,7 +247,7 @@ void load_settings(char *cmdline_config_path) "Ellipsize truncated lines on the start/middle/end" ); - if (strlen(c) == 0) { + if (STR_EMPTY(c)) { settings.ellipsize = defaults.ellipsize; } else if (strcmp(c, "start") == 0) { settings.ellipsize = ELLIPSE_START; @@ -346,8 +346,7 @@ void load_settings(char *cmdline_config_path) "alignment", "-align/-alignment", "", "Text alignment left/center/right" ); - - if (strlen(c) > 0) { + if (STR_FULL(c)) { if (strcmp(c, "left") == 0) settings.align = ALIGN_LEFT; else if (strcmp(c, "center") == 0) @@ -427,7 +426,7 @@ void load_settings(char *cmdline_config_path) "Color of the separator line (or 'auto')" ); - if (strlen(c) > 0) { + if (STR_FULL(c)) { if (strcmp(c, "auto") == 0) settings.sep_color = SEP_AUTO; else if (strcmp(c, "foreground") == 0) @@ -484,7 +483,7 @@ void load_settings(char *cmdline_config_path) "Align icons left/right/off" ); - if (strlen(c) > 0) { + if (STR_FULL(c)) { if (strcmp(c, "left") == 0) settings.icon_position = ICON_LEFT; else if (strcmp(c, "right") == 0) diff --git a/src/utils.c b/src/utils.c index 8f06141..fa5ae40 100644 --- a/src/utils.c +++ b/src/utils.c @@ -79,11 +79,11 @@ char *string_replace_all(const char *needle, const char *replacement, char *hays char *string_append(char *a, const char *b, const char *sep) { - if (!a || *a == '\0') { + if (STR_EMPTY(a)) { g_free(a); return g_strdup(b); } - if (!b || *b == '\0') + if (STR_EMPTY(b)) return a; char *new; diff --git a/src/utils.h b/src/utils.h index 48ee618..5cef754 100644 --- a/src/utils.h +++ b/src/utils.h @@ -4,6 +4,9 @@ #include +#define STR_EMPTY(s) (!s || (*s == '\0')) +#define STR_FULL(s) !(STR_EMPTY(s)) + /* replace all occurrences of the character needle with the character replacement in haystack */ char *string_replace_char(char needle, char replacement, char *haystack);