From 0879746e0f0c6f2658c26ab03b708d192592067c Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Fri, 7 Jan 2022 18:00:38 +0100 Subject: [PATCH] Unified method for testing if c strings contain a value --- src/common/parsing.c | 19 +++++++++++++++++++ src/detection/displayserver/wmde.c | 24 ++++++++++++------------ src/detection/displayserver/xlib.c | 3 ++- src/detection/terminalShell.c | 10 +++++----- src/fastfetch.c | 3 +-- src/fastfetch.h | 2 ++ src/modules/cursor.c | 2 +- src/modules/wmtheme.c | 4 ++-- 8 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/common/parsing.c b/src/common/parsing.c index 6fcf442e8..ac1197dd6 100644 --- a/src/common/parsing.c +++ b/src/common/parsing.c @@ -393,3 +393,22 @@ void ffParseSemver(FFstrbuf* buffer, const FFstrbuf* major, const FFstrbuf* mino ffStrbufAppend(buffer, patch); } + +static inline bool charIsNegliable(char c) +{ + return c == ' ' || c == '\t' || c == '\n' || c == '\r'; +} + +bool ffStrSet(const char* str) +{ + if(str == NULL) + return false; + + while(*str != '\0') + { + if(!charIsNegliable(*str)) + return true; + } + + return false; +} diff --git a/src/detection/displayserver/wmde.c b/src/detection/displayserver/wmde.c index 92564025e..5f13dc255 100644 --- a/src/detection/displayserver/wmde.c +++ b/src/detection/displayserver/wmde.c @@ -9,23 +9,23 @@ static const char* parseEnv() const char* env; env = getenv("XDG_CURRENT_DESKTOP"); - if(env != NULL && *env != '\0') + if(ffStrSet(env)) return env; env = getenv("XDG_SESSION_DESKTOP"); - if(env != NULL && *env != '\0') + if(ffStrSet(env)) return env; env = getenv("CURRENT_DESKTOP"); - if(env != NULL && *env != '\0') + if(ffStrSet(env)) return env; env = getenv("SESSION_DESKTOP"); - if(env != NULL && *env != '\0') + if(ffStrSet(env)) return env; env = getenv("DESKTOP_SESSION"); - if(env != NULL && *env != '\0') + if(ffStrSet(env)) return env; if(getenv("KDE_FULL_SESSION") != NULL || getenv("KDE_SESSION_UID") != NULL || getenv("KDE_SESSION_VERSION") != NULL) @@ -45,7 +45,7 @@ static const char* parseEnv() static void applyPrettyNameIfWM(FFDisplayServerResult* result, const char* processName) { - if(processName == NULL || *processName == '\0') + if(!ffStrSet(processName)) return; if(strcasecmp(processName, "kwin_wayland") == 0 || strcasecmp(processName, "kwin_x11") == 0 || strcasecmp(processName, "kwin") == 0) @@ -80,7 +80,7 @@ static void applyPrettyNameIfWM(FFDisplayServerResult* result, const char* proce static void applyBetterWM(FFDisplayServerResult* result, const char* processName) { - if(processName == NULL || *processName == '\0') + if(!ffStrSet(processName)) return; //If it is a known wm, this will set the pretty name @@ -211,7 +211,7 @@ static void getLXQt(const FFinstance* instance, FFDisplayServerResult* result) static void applyPrettyNameIfDE(const FFinstance* instance, FFDisplayServerResult* result, const char* name) { - if(name == NULL || *name == '\0') + if(!ffStrSet(name)) return; else if( @@ -260,7 +260,7 @@ static void getWMProtocolNameFromEnv(FFDisplayServerResult* result) //We don't need to check for wayland here, as the wayland code will always set the protocol name to wayland const char* env = getenv("XDG_SESSION_TYPE"); - if(env != NULL && *env != '\0') + if(ffStrSet(env)) { if(strcasecmp(env, "x11") == 0) ffStrbufSetS(&result->wmProtocolName, FF_DISPLAYSERVER_PROTOCOL_X11); @@ -273,14 +273,14 @@ static void getWMProtocolNameFromEnv(FFDisplayServerResult* result) } env = getenv("DISPLAY"); - if(env != NULL && *env != '\0') + if(ffStrSet(env)) { ffStrbufSetS(&result->wmProtocolName, FF_DISPLAYSERVER_PROTOCOL_X11); return; } env = getenv("TERM"); - if(env != NULL && *env != '\0') + if(ffStrSet(env)) { ffStrbufSetS(&result->wmProtocolName, FF_DISPLAYSERVER_PROTOCOL_TTY); return; @@ -398,7 +398,7 @@ void ffdsDetectWMDE(const FFinstance* instance, FFDisplayServerResult* result) //Return if both wm and de are set, or if env doesn't contain anything if( (result->wmPrettyName.length > 0 && result->dePrettyName.length > 0) || - (env == NULL || *env == '\0') + !ffStrSet(env) ) return; //If nothing is set, use env as WM diff --git a/src/detection/displayserver/xlib.c b/src/detection/displayserver/xlib.c index 87ffe0aab..355e3bd0f 100644 --- a/src/detection/displayserver/xlib.c +++ b/src/detection/displayserver/xlib.c @@ -44,7 +44,8 @@ static void x11DetectWMFromEWMH(X11PropertyData* data, Display* display, FFDispl const char* wmName = (const char*) x11GetProperty(data, display, *wmWindow, "_NET_WM_NAME"); if(wmName == NULL) wmName = (const char*) x11GetProperty(data, display, *wmWindow, "WM_NAME"); - if(wmName == NULL || *wmName == '\0') + + if(!ffStrSet(wmName)) return; ffStrbufSetS(&result->wmProcessName, wmName); diff --git a/src/detection/terminalShell.c b/src/detection/terminalShell.c index 411abad7f..5a7ad848b 100644 --- a/src/detection/terminalShell.c +++ b/src/detection/terminalShell.c @@ -55,8 +55,8 @@ static void getTerminalShell(FFTerminalShellResult* result, const char* pid) if( fscanf(stat, "%*s (%255[^)]) %*c %255s", name, ppid) != 2 || //stat (comm) state ppid - *name == '\0' || - *ppid == '\0' || + !ffStrSet(name) || + !ffStrSet(ppid) || *ppid == '-' || strcasecmp(ppid, "0") == 0 ) { @@ -119,15 +119,15 @@ static void getTerminalFromEnv(FFTerminalShellResult* result) term = getenv("SSH_TTY"); //Windows Terminal - if((term == NULL || *term == '\0') && getenv("WT_SESSION") != NULL) + if(!ffStrSet(term) && getenv("WT_SESSION") != NULL) term = "Windows Terminal"; //Normal Terminal - if(term == NULL || *term == '\0') + if(!ffStrSet(term)) term = getenv("TERM"); //TTY - if(term == NULL || *term == '\0' || strcasecmp(term, "linux") == 0) + if(!ffStrSet(term) || strcasecmp(term, "linux") == 0) term = ttyname(STDIN_FILENO); ffStrbufSetS(&result->terminalProcessName, term); diff --git a/src/fastfetch.c b/src/fastfetch.c index 7224c37bf..585b0bf10 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -492,8 +492,7 @@ static void optionParseConfigFile(FFinstance* instance, FFdata* data, const char static bool optionParseBoolean(const char* str) { return ( - str == NULL || - *str == '\0' || + !ffStrSet(str) || strcasecmp(str, "true") == 0 || strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 || diff --git a/src/fastfetch.h b/src/fastfetch.h index 3e0aad6ea..b1b76d838 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -391,6 +391,8 @@ bool ffGetPropValueFromLines(const char* lines, const char* start, FFstrbuf* buf void ffParseSemver(FFstrbuf* buffer, const FFstrbuf* major, const FFstrbuf* minor, const FFstrbuf* patch); +bool ffStrSet(const char* str); + //common/settings.c FFvariant ffSettingsGetDConf(FFinstance* instance, const char* key, FFvarianttype type); FFvariant ffSettingsGetGSettings(FFinstance* instance, const char* schemaName, const char* path, const char* key, FFvarianttype type); diff --git a/src/modules/cursor.c b/src/modules/cursor.c index bc3e7a2a2..08729ea68 100644 --- a/src/modules/cursor.c +++ b/src/modules/cursor.c @@ -155,7 +155,7 @@ static bool printCursorFromEnv(FFinstance* instance) { const char* xcursor_theme = getenv("XCURSOR_THEME"); - if(xcursor_theme == NULL || *xcursor_theme == '\0') + if(!ffStrSet(xcursor_theme)) return false; FFstrbuf theme; diff --git a/src/modules/wmtheme.c b/src/modules/wmtheme.c index ea58dbda4..a28f2fca9 100644 --- a/src/modules/wmtheme.c +++ b/src/modules/wmtheme.c @@ -64,7 +64,7 @@ static void printWMThemeFromSettings(FFinstance* instance, const char* dconfKey, { const char* theme = ffSettingsGet(instance, dconfKey, gsettingsSchemaName, gsettingsPath, gsettingsKey, FF_VARIANT_TYPE_STRING).strValue; - if(theme == NULL || *theme == '\0') + if(!ffStrSet(theme)) { ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Couldn't find WM theme in DConf or GSettings"); return; @@ -105,7 +105,7 @@ static void printGTKThemeAsWMTheme(FFinstance* instance) static void printMutter(FFinstance* instance) { const char* theme = ffSettingsGet(instance, "/org/gnome/shell/extensions/user-theme/name", "org.gnome.shell.extensions.user-theme", NULL, "name", FF_VARIANT_TYPE_STRING).strValue; - if(theme != NULL && *theme != '\0') + if(ffStrSet(theme)) { printWMTheme(instance, theme); return;