Unified method for testing if c strings contain a value

This commit is contained in:
Linus Dierheimer
2022-01-07 18:00:38 +01:00
parent 3f30fa8d4a
commit 0879746e0f
8 changed files with 44 additions and 23 deletions
+19
View File
@@ -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;
}
+12 -12
View File
@@ -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
+2 -1
View File
@@ -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);
+5 -5
View File
@@ -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);
+1 -2
View File
@@ -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 ||
+2
View File
@@ -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);
+1 -1
View File
@@ -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;
+2 -2
View File
@@ -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;