ffGetPropValue

This commit is contained in:
Linus Dierheimer
2021-05-13 12:27:44 +02:00
parent f8868ae3c4
commit 4d7a06ac97
16 changed files with 182 additions and 188 deletions
+3 -18
View File
@@ -77,32 +77,17 @@ static void detectGTKFromConfigFile(const char* filename, FFGTKResult* result)
while(getline(&line, &len, file) != -1)
{
if(result->theme.length == 0)
{
sscanf(line, "gtk-theme-name=%[^\n]", result->theme.chars);
sscanf(line, "gtk-theme-name=\"%[^\"]+", result->theme.chars);
}
ffGetPropValue(line, "gtk-theme-name=", &result->theme);
if(result->icons.length == 0)
{
sscanf(line, "gtk-icon-theme-name=%[^\n]", result->icons.chars);
sscanf(line, "gtk-icon-theme-name=\"%[^\"]+", result->icons.chars);
}
ffGetPropValue(line, "gtk-icon-theme-name=", &result->icons);
if(result->font.length == 0)
{
sscanf(line, "gtk-font-name=%[^\n]", result->font.chars);
sscanf(line, "gtk-font-name=\"%[^\"]+", result->font.chars);
}
ffGetPropValue(line, "gtk-font-name=", &result->font);
}
if(line != NULL)
free(line);
fclose(file);
ffStrbufRecalculateLength(&result->theme);
ffStrbufRecalculateLength(&result->icons);
ffStrbufRecalculateLength(&result->font);
}
static void detectGTKFromConfigDir(FFstrbuf* configDir, const char* version, FFGTKResult* result)
+6 -20
View File
@@ -27,7 +27,7 @@ static bool detectFromConfigFile(const FFstrbuf* filename, FFPlasmaResult* resul
if(line[0] == '[')
{
char categoryName[32];
sscanf(line, "[%[^]]", categoryName);
sscanf(line, "[%32[^]]", categoryName);
if(strcasecmp(categoryName, "General") == 0)
category = PLASMA_CATEGORY_GENERAL;
@@ -42,30 +42,16 @@ static bool detectFromConfigFile(const FFstrbuf* filename, FFPlasmaResult* resul
}
if(category == PLASMA_CATEGORY_KDE)
{
sscanf(line, "widgetStyle=%[^\n]", result->widgetStyle.chars);
sscanf(line, "widgetStyle=\"%[^\"]+", result->widgetStyle.chars);
}
ffGetPropValue(line, "widgetStyle=", &result->widgetStyle);
else if(category == PLASMA_CATEGORY_ICONS)
ffGetPropValue(line, "Theme=", &result->icons);
else if(category == PLASMA_CATEGORY_GENERAL)
{
sscanf(line, "ColorScheme=%[^\n]", result->colorScheme.chars);
sscanf(line, "ColorScheme=\"%[^\"]+", result->colorScheme.chars);
sscanf(line, "font=%[^\n]", result->font.chars);
sscanf(line, "font=\"%[^\"]+", result->font.chars);
}
else if(category == PLASMA_CATEGORY_ICONS)
{
sscanf(line, "Theme=%[^\n]", result->icons.chars);
sscanf(line, "Theme=\"%[^\"]+", result->icons.chars);
ffGetPropValue(line, "ColorScheme=", &result->colorScheme);
ffGetPropValue(line, "font=", &result->font);
}
}
ffStrbufRecalculateLength(&result->widgetStyle);
ffStrbufRecalculateLength(&result->colorScheme);
ffStrbufRecalculateLength(&result->icons);
ffStrbufRecalculateLength(&result->font);
if(line != NULL)
free(line);
+4 -12
View File
@@ -204,36 +204,28 @@ static void getKDE(FFWMDEResult* result)
{
ffStrbufSetS(&result->deProcessName, "plasmashell");
ffStrbufSetS(&result->dePrettyName, "KDE Plasma");
ffParsePropFile("/usr/share/xsessions/plasma.desktop", " X-KDE-PluginInfo-Version=%[^\n]", result->deVersion.chars);
ffStrbufRecalculateLength(&result->deVersion);
ffParsePropFile("/usr/share/xsessions/plasma.desktop", "X-KDE-PluginInfo-Version=", &result->deVersion);
}
static void getGnome(FFWMDEResult* result)
{
ffStrbufSetS(&result->deProcessName, "gnome-shell");
ffStrbufSetS(&result->dePrettyName, "GNOME");
ffParsePropFile("/usr/share/gnome-shell/org.gnome.Extensions", " version: '%[^']", result->deVersion.chars);
ffStrbufRecalculateLength(&result->deVersion);
ffParsePropFile("/usr/share/gnome-shell/org.gnome.Extensions", "version: ", &result->deVersion);
}
static void getCinnamon(FFWMDEResult* result)
{
ffStrbufSetS(&result->deProcessName, "cinnamon");
ffStrbufSetS(&result->dePrettyName, "Cinnamon");
ffParsePropFile("/usr/share/applications/cinnamon.desktop", " X-GNOME-Bugzilla-Version=%[^\n]", result->deVersion.chars);
ffStrbufRecalculateLength(&result->deVersion);
ffParsePropFile("/usr/share/applications/cinnamon.desktop", "X-GNOME-Bugzilla-Version=", &result->deVersion);
}
static void getXFCE4(FFinstance* instance, FFWMDEResult* result)
{
ffStrbufSetS(&result->deProcessName, "xfce4-session");
ffStrbufSetS(&result->dePrettyName, "Xfce4");
ffParsePropFile("/usr/share/gtk-doc/html/libxfce4ui/index.html", "<div><p class=\"releaseinfo\">Version %[^\n]", result->deVersion.chars);
ffStrbufRecalculateLength(&result->deVersion);
ffParsePropFile("/usr/share/gtk-doc/html/libxfce4ui/index.html", "<div><p class=\"releaseinfo\">Version ", &result->deVersion);
if(result->deVersion.length == 0 && instance->config.allowSlowOperations)
{
+1 -1
View File
@@ -113,7 +113,7 @@ void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, const FFst
FFstrbuf placeholderValue;
ffStrbufInit(&placeholderValue);
while(formatstr->chars[i] != '}' && i < formatstr->length)
while(i < formatstr->length && formatstr->chars[i] != '}')
ffStrbufAppendC(&placeholderValue, formatstr->chars[i++]);
// test for error, if so print it
+9 -9
View File
@@ -290,10 +290,8 @@ void ffCacheClose(FFcache* cache)
fclose(cache->split);
}
bool ffParsePropFile(const char* fileName, const char* regex, char* buffer)
bool ffParsePropFile(const char* fileName, const char* start, FFstrbuf* buffer)
{
buffer[0] = '\0'; //If an error occures, this is the indicator
char* line = NULL;
size_t len = 0;
@@ -303,7 +301,7 @@ bool ffParsePropFile(const char* fileName, const char* regex, char* buffer)
while (getline(&line, &len, file) != -1)
{
if (sscanf(line, regex, buffer) > 0)
if(ffGetPropValue(line, start, buffer))
break;
}
@@ -314,7 +312,7 @@ bool ffParsePropFile(const char* fileName, const char* regex, char* buffer)
return true;
}
bool ffParsePropFileHome(FFinstance* instance, const char* relativeFile, const char* regex, char* buffer)
bool ffParsePropFileHome(FFinstance* instance, const char* relativeFile, const char* start, FFstrbuf* buffer)
{
FFstrbuf absolutePath;
ffStrbufInitA(&absolutePath, 64);
@@ -322,15 +320,17 @@ bool ffParsePropFileHome(FFinstance* instance, const char* relativeFile, const c
ffStrbufAppendC(&absolutePath, '/');
ffStrbufAppendS(&absolutePath, relativeFile);
bool result = ffParsePropFile(absolutePath.chars, regex, buffer);
bool result = ffParsePropFile(absolutePath.chars, start, buffer);
ffStrbufDestroy(&absolutePath);
return result;
}
bool ffParsePropFileConfig(FFinstance* instance, const char* relativeFile, const char* regex, char* buffer)
bool ffParsePropFileConfig(FFinstance* instance, const char* relativeFile, const char* start, FFstrbuf* buffer)
{
uint32_t bufferLengthStart = buffer->length;
bool foundAFile = false;
for(uint32_t i = 0; i < instance->state.configDirs.length; i++)
@@ -343,12 +343,12 @@ bool ffParsePropFileConfig(FFinstance* instance, const char* relativeFile, const
ffStrbufAppendS(baseDir, relativeFile);
if(ffParsePropFile(baseDir->chars, regex, buffer))
if(ffParsePropFile(baseDir->chars, start, buffer))
foundAFile = true;
ffStrbufSubstrBefore(baseDir, baseDirLength);
if(*buffer != '\0')
if(bufferLengthStart < buffer->length)
return foundAFile;
}
+41 -1
View File
@@ -84,7 +84,7 @@ void ffGetFont(const char* font, FFstrbuf* name, double* size)
{
ffStrbufEnsureCapacity(name, 32);
int scanned = sscanf(font, "%[^,], %lf", name->chars, size);
int scanned = sscanf(font, "%31[^,], %lf", name->chars, size);
ffStrbufRecalculateLength(name);
@@ -106,3 +106,43 @@ void ffGetFontPretty(FFstrbuf* buffer, const FFstrbuf* name, double size)
ffStrbufAppendS(buffer, "pt)");
}
}
bool ffGetPropValue(const char* line, const char* start, FFstrbuf* buffer)
{
uint32_t lineIndex = 0;
uint32_t startIndex = 0;
//Skip any amount of whitespace at the begin of line
while(line[lineIndex] == ' ' || line[lineIndex] == '\t')
++lineIndex;
while(start[startIndex] != '\0')
{
// Any amount of whitespace in the format string matches any amount of whitespace in the line
if(start[startIndex] == ' ' || start[startIndex] == '\t')
{
while(start[startIndex] == ' ' || start[startIndex] == '\t')
++startIndex;
while(line[lineIndex] == ' ' || line[lineIndex] == '\t')
++lineIndex;
}
if(line[lineIndex] == '\0' || line[lineIndex] != start[startIndex])
return false;
++lineIndex;
++startIndex;
}
//Allow quotet values
const char* quotes = NULL;
if(line[lineIndex] == '"' || line[lineIndex] == '\'')
quotes = &line[lineIndex++];
//Copy the value to the buffer
while(line[lineIndex] != '\n' && line[lineIndex] != '\0' && (quotes == NULL || *quotes != line[lineIndex]))
ffStrbufAppendC(buffer, line[lineIndex++]);
return true;
}