From 5f71ef71da82adbb658688f04ca5034824fedf6d Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sun, 16 May 2021 15:10:15 +0200 Subject: [PATCH 1/3] small syntactic changes --- src/common/io.c | 10 +++++----- src/fastfetch.c | 2 +- src/modules/gpu.c | 5 +++-- src/modules/packages.c | 12 ++++++------ src/modules/resolution_x11.c | 5 +++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/common/io.c b/src/common/io.c index f8c7f2909..35f64fd23 100644 --- a/src/common/io.c +++ b/src/common/io.c @@ -292,23 +292,24 @@ void ffCacheClose(FFcache* cache) bool ffParsePropFile(const char* fileName, const char* start, FFstrbuf* buffer) { - char* line = NULL; - size_t len = 0; - FILE* file = fopen(fileName, "r"); if(file == NULL) return false; // handle errors in higher functions + char* line = NULL; + size_t len = 0; + while (getline(&line, &len, file) != -1) { if(ffGetPropValue(line, start, buffer)) break; } - fclose(file); if(line != NULL) free(line); + fclose(file); + return true; } @@ -329,7 +330,6 @@ bool ffParsePropFileHome(FFinstance* instance, const char* relativeFile, const c bool ffParsePropFileConfig(FFinstance* instance, const char* relativeFile, const char* start, FFstrbuf* buffer) { - uint32_t bufferLengthStart = buffer->length; bool foundAFile = false; diff --git a/src/fastfetch.c b/src/fastfetch.c index 27fe7d259..707968a73 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -64,7 +64,7 @@ static inline void printHelp() " -l , --logo : sets the shown logo. Also changes the main color accordingly\n" " --color-logo : if set to false, the logo will be black / white\n" "\n" - "Format options: Provide the format string for custom output (+)\n" + "Format options: Provide the format string for custom output. Use fastfetch --help *-format for specific help.\n" " --os-format \n" " --host-format \n" " --kernel-format \n" diff --git a/src/modules/gpu.c b/src/modules/gpu.c index 2af8e4ed3..524a5e09c 100644 --- a/src/modules/gpu.c +++ b/src/modules/gpu.c @@ -54,10 +54,11 @@ void ffPrintGPU(FFinstance* instance) if(ffPrintFromCache(instance, FF_GPU_MODULE_NAME, &instance->config.gpuKey, &instance->config.gpuFormat, FF_GPU_NUM_FORMAT_ARGS)) return; - void* pci = dlopen(instance->config.libPCI.length == 0 ? "libpci.so" : instance->config.libPCI.chars, RTLD_LAZY); + const char* pciLibName = instance->config.libPCI.length == 0 ? "libpci.so" : instance->config.libPCI.chars; + void* pci = dlopen(pciLibName, RTLD_LAZY); if(pci == NULL) { - ffPrintError(instance, FF_GPU_MODULE_NAME, 0, &instance->config.gpuKey, &instance->config.gpuFormat, FF_GPU_NUM_FORMAT_ARGS, "dlopen(\"libpci.so\", RTLD_LAZY) == NULL"); + ffPrintError(instance, FF_GPU_MODULE_NAME, 0, &instance->config.gpuKey, &instance->config.gpuFormat, FF_GPU_NUM_FORMAT_ARGS, "dlopen(\"%s\", RTLD_LAZY) == NULL", pciLibName); return; } diff --git a/src/modules/packages.c b/src/modules/packages.c index fc1a9d169..5ce2d8649 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -6,15 +6,15 @@ #define FF_PACKAGES_MODULE_NAME "Packages" #define FF_PACKAGES_NUM_FORMAT_ARGS 7 -static uint32_t getNumElements(const char* dirname, unsigned char type) { - uint32_t num_elements = 0; - DIR * dirp; - struct dirent *entry; - - dirp = opendir(dirname); +static uint32_t getNumElements(const char* dirname, unsigned char type) +{ + DIR* dirp = opendir(dirname); if(dirp == NULL) return 0; + uint32_t num_elements = 0; + + struct dirent *entry; while((entry = readdir(dirp)) != NULL) { if(entry->d_type == type) ++num_elements; diff --git a/src/modules/resolution_x11.c b/src/modules/resolution_x11.c index 43648e8ec..28ca0cd02 100644 --- a/src/modules/resolution_x11.c +++ b/src/modules/resolution_x11.c @@ -35,10 +35,11 @@ static void xCloseDisplay(void* library, Display* display) void ffPrintResolutionX11Backend(FFinstance* instance) { - void* x11 = dlopen(instance->config.libX11.length == 0 ? "libX11.so" : instance->config.libX11.chars, RTLD_LAZY); + const char* libX11Name = instance->config.libX11.length == 0 ? "libX11.so" : instance->config.libX11.chars; + void* x11 = dlopen(libX11Name, RTLD_LAZY); if(x11 == NULL) { - ffPrintError(instance, FF_RESOLUTION_MODULE_NAME, 0, &instance->config.resolutionKey, &instance->config.resolutionFormat, FF_RESOLUTION_NUM_FORMAT_ARGS, "dlopen(\"libX11.so\", RTLD_LAZY) == NULL"); + ffPrintError(instance, FF_RESOLUTION_MODULE_NAME, 0, &instance->config.resolutionKey, &instance->config.resolutionFormat, FF_RESOLUTION_NUM_FORMAT_ARGS, "dlopen(\"%s\", RTLD_LAZY) == NULL", libX11Name); return; } From c8a9f06b52d23561d4deec72a59ee241c71d030e Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sun, 16 May 2021 18:41:00 +0200 Subject: [PATCH 2/3] added multiline and xml support to ffGetPropValue --- src/common/detectWMDE.c | 3 -- src/common/parsing.c | 72 ++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/common/detectWMDE.c b/src/common/detectWMDE.c index 5d85a42a1..51ac91c96 100644 --- a/src/common/detectWMDE.c +++ b/src/common/detectWMDE.c @@ -236,13 +236,10 @@ static void getMate(FFinstance* instance, FFWMDEResult* result) //I parse the file 3 times by purpose, because the properties are not guaranteed to be in order ffParsePropFile("/usr/share/mate-about/mate-version.xml", "", &result->deVersion); - ffStrbufSubstrBeforeFirstC(&result->deVersion, '<'); ffStrbufAppendC(&result->deVersion, '.'); ffParsePropFile("/usr/share/mate-about/mate-version.xml", "", &result->deVersion); - ffStrbufSubstrBeforeFirstC(&result->deVersion, '<'); ffStrbufAppendC(&result->deVersion, '.'); ffParsePropFile("/usr/share/mate-about/mate-version.xml", "", &result->deVersion); - ffStrbufSubstrBeforeFirstC(&result->deVersion, '<'); if(result->deVersion.length == 0 && instance->config.allowSlowOperations) { diff --git a/src/common/parsing.c b/src/common/parsing.c index adf3d1d7a..2fbe94f93 100644 --- a/src/common/parsing.c +++ b/src/common/parsing.c @@ -107,48 +107,74 @@ void ffGetFontPretty(FFstrbuf* buffer, const FFstrbuf* name, double size) } } -bool ffGetPropValue(const char* line, const char* start, FFstrbuf* buffer) +bool ffGetPropValue(const char* lines, const char* start, FFstrbuf* buffer) { - uint32_t lineIndex = 0; - uint32_t startIndex = 0; + if(*lines == '\0') + return false; + + const char* startStart = start; //Skip any amount of whitespace at the begin of line - while(line[lineIndex] == ' ' || line[lineIndex] == '\t') - ++lineIndex; + while(*lines == ' ' || *lines == '\t') + ++lines; - while(start[startIndex] != '\0') + while(*start != '\0') { // Any amount of whitespace in the format string matches any amount of whitespace in the line, even none - if(start[startIndex] == ' ' || start[startIndex] == '\t') + if(*start == ' ' || *start == '\t') { - while(start[startIndex] == ' ' || start[startIndex] == '\t') - ++startIndex; + while(*start == ' ' || *start == '\t') + ++start; - while(line[lineIndex] == ' ' || line[lineIndex] == '\t') - ++lineIndex; + while(*lines == ' ' || *lines == '\t') + ++lines; continue; } - if(line[lineIndex] == '\0' || line[lineIndex] != start[startIndex]) + //Line doesn't match start, skip it + if(*lines != *start) + { + while(*lines != '\0' && *lines != '\n') + ++lines; + //Handle both cases in the following ifs + } + + //We reached the end of all lines without finding something + if(*lines == '\0') return false; - ++lineIndex; - ++startIndex; + ++lines; + + if(*lines == '\n') + start = startStart; //We reached we end of line, begin again from the next line. + else + ++start; //Line and start match, continue testing } - //Skip any amount of whitespace at the begin of the value - while(line[lineIndex] == ' ' || line[lineIndex] == '\t') - ++lineIndex; + char valueEnd = '\n'; - //Allow quotet values - const char* quotes = NULL; - if(line[lineIndex] == '"' || line[lineIndex] == '\'') - quotes = &line[lineIndex++]; + //Allow faster parsing of XML + if(*(lines - 1) == '>') + valueEnd = '<'; + + //Skip any amount of whitespace at the begin of the value + while(*lines == ' ' || *lines == '\t') + ++lines; + + //Allow faster parsing of quotet values + if(*lines == '"' || *lines == '\'') + { + valueEnd = *lines; + ++lines; + } //Copy the value to the buffer - while(line[lineIndex] != '\n' && line[lineIndex] != '\0' && (quotes == NULL || *quotes != line[lineIndex])) - ffStrbufAppendC(buffer, line[lineIndex++]); + while(*lines != '\0' && *lines != valueEnd) + { + ffStrbufAppendC(buffer, *lines); + ++lines; + } ffStrbufTrimRight(buffer, ' '); From 142d9c30e05a8fb64d60451d7333083682107904 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sun, 16 May 2021 21:02:06 +0200 Subject: [PATCH 3/3] faster ffGetPropValue --- src/common/parsing.c | 75 +++++++++++++++++++++++++------------------- src/fastfetch.h | 1 + 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/common/parsing.c b/src/common/parsing.c index 2fbe94f93..3263173b4 100644 --- a/src/common/parsing.c +++ b/src/common/parsing.c @@ -107,16 +107,16 @@ void ffGetFontPretty(FFstrbuf* buffer, const FFstrbuf* name, double size) } } -bool ffGetPropValue(const char* lines, const char* start, FFstrbuf* buffer) +static bool getPropValueLine(const char** linePtr, const char* start, FFstrbuf* buffer) { - if(*lines == '\0') + const char* line = *linePtr; + + if(*line == '\0') return false; - const char* startStart = start; - //Skip any amount of whitespace at the begin of line - while(*lines == ' ' || *lines == '\t') - ++lines; + while(*line == ' ' || *line == '\t') + ++line; while(*start != '\0') { @@ -126,57 +126,68 @@ bool ffGetPropValue(const char* lines, const char* start, FFstrbuf* buffer) while(*start == ' ' || *start == '\t') ++start; - while(*lines == ' ' || *lines == '\t') - ++lines; + while(*line == ' ' || *line == '\t') + ++line; continue; } //Line doesn't match start, skip it - if(*lines != *start) - { - while(*lines != '\0' && *lines != '\n') - ++lines; - //Handle both cases in the following ifs - } - - //We reached the end of all lines without finding something - if(*lines == '\0') + if(*line != *start || *line == '\0') return false; - ++lines; - - if(*lines == '\n') - start = startStart; //We reached we end of line, begin again from the next line. - else - ++start; //Line and start match, continue testing + //Line and start match, continue testing + ++line; + ++start; } char valueEnd = '\n'; //Allow faster parsing of XML - if(*(lines - 1) == '>') + if(*(line - 1) == '>') valueEnd = '<'; //Skip any amount of whitespace at the begin of the value - while(*lines == ' ' || *lines == '\t') - ++lines; + while(*line == ' ' || *line == '\t') + ++line; //Allow faster parsing of quotet values - if(*lines == '"' || *lines == '\'') + if(*line == '"' || *line == '\'') { - valueEnd = *lines; - ++lines; + valueEnd = *line; + ++line; } //Copy the value to the buffer - while(*lines != '\0' && *lines != valueEnd) + while(*line != valueEnd && *line != '\n' && *line != '\0') { - ffStrbufAppendC(buffer, *lines); - ++lines; + ffStrbufAppendC(buffer, *line); + ++line; } ffStrbufTrimRight(buffer, ' '); return true; } + +bool ffGetPropValue(const char* line, const char* start, FFstrbuf* buffer) +{ + return getPropValueLine(&line, start, buffer); +} + +bool ffGetPropValueFromLines(const char* lines, const char* start, FFstrbuf* buffer) +{ + while(!getPropValueLine(&lines, start, buffer)) + { + while(*lines != '\0' && *lines != '\n') + ++lines; + + if(*lines == '\0') + return false; + + //Skip '\n' + ++lines; + } + + return true; +} diff --git a/src/fastfetch.h b/src/fastfetch.h index 432f3f7f2..df8fd3483 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -272,6 +272,7 @@ void ffGetGtkPretty(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3 void ffGetFont(const char* font, FFstrbuf* name, double* size); void ffGetFontPretty(FFstrbuf* buffer, const FFstrbuf* name, double size); bool ffGetPropValue(const char* line, const char* start, FFstrbuf* buffer); +bool ffGetPropValueFromLines(const char* lines, const char* start, FFstrbuf* buffer); //common/settings.c FFvariant ffSettingsGetDConf(FFinstance* instance, const char* key, FFvarianttype type);