mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
refactor(WMTheme): clean up detectOpenbox
Simplify the code and implement stricter checking of xml contents.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "wmtheme.h"
|
||||
#include "common/io/io.h"
|
||||
#include "common/properties.h"
|
||||
#include "common/parsing.h"
|
||||
#include "common/settings.h"
|
||||
@@ -142,50 +143,45 @@ static bool detectOpenbox(const FFstrbuf* dePrettyName, FFstrbuf* themeOrError)
|
||||
else
|
||||
ffStrbufAppendS(&absolutePath, ".config/openbox/rc.xml");
|
||||
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
|
||||
FILE* file = fopen(absolutePath.chars, "r");
|
||||
if(file == NULL)
|
||||
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
|
||||
if (!ffReadFileBuffer(absolutePath.chars, &content))
|
||||
{
|
||||
ffStrbufAppendF(themeOrError, "Couldn't open \"%s\"", absolutePath.chars);
|
||||
|
||||
ffStrbufAppendF(themeOrError, "Couldn't read \"%s\"", absolutePath.chars);
|
||||
return false;
|
||||
}
|
||||
|
||||
while(getline(&line, &len, file) != -1)
|
||||
{
|
||||
if(strstr(line, "<theme>") != 0)
|
||||
break;
|
||||
}
|
||||
const char *themeStart = strstr(content.chars, "<theme>");
|
||||
if (themeStart == NULL)
|
||||
goto theme_not_found;
|
||||
|
||||
while(getline(&line, &len, file) != -1)
|
||||
{
|
||||
if(strstr(line, "<name>") != 0)
|
||||
{
|
||||
ffStrbufAppendS(themeOrError, line);
|
||||
ffStrbufRemoveStrings(themeOrError, 2, (const char*[]) { "<name>", "</name>" });
|
||||
ffStrbufTrimRight(themeOrError, '\n');
|
||||
ffStrbufTrim(themeOrError, ' ');
|
||||
break;
|
||||
}
|
||||
const char *themeEnd = strstr(themeStart, "</theme>");
|
||||
if (__builtin_expect(themeEnd == NULL, false)) // very rare case
|
||||
goto theme_not_found;
|
||||
|
||||
if(strstr(line, "</theme>") != 0) // sanity check
|
||||
break;
|
||||
}
|
||||
const char *nameStart = strstr(themeStart, "<name>");
|
||||
if (nameStart == NULL)
|
||||
goto name_not_found;
|
||||
|
||||
if(line != NULL)
|
||||
free(line);
|
||||
const char *nameEnd = strstr(nameStart, "</name>");
|
||||
if (nameEnd == NULL || nameEnd > themeEnd) // (nameEnd > themeEnd) means name is not a theme's child
|
||||
goto name_not_found;
|
||||
|
||||
fclose(file);
|
||||
nameStart += strlen("<name>");
|
||||
ffStrbufAppendNS(themeOrError, (uint32_t)(nameEnd - nameStart), nameStart);
|
||||
ffStrbufTrim(themeOrError, ' ');
|
||||
|
||||
if(themeOrError->length == 0)
|
||||
{
|
||||
ffStrbufAppendF(themeOrError, "Couldn't find theme name in \"%s\"", absolutePath.chars);
|
||||
return false;
|
||||
}
|
||||
goto name_not_found;
|
||||
|
||||
return true;
|
||||
|
||||
theme_not_found:
|
||||
ffStrbufAppendF(themeOrError, "Couldn't find theme node in \"%s\"", absolutePath.chars);
|
||||
return false;
|
||||
|
||||
name_not_found:
|
||||
ffStrbufAppendF(themeOrError, "Couldn't find theme name in \"%s\"", absolutePath.chars);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ffDetectWmTheme(FFstrbuf* themeOrError)
|
||||
|
||||
Reference in New Issue
Block a user