Unified storage of --set and --set-keyless values

This commit is contained in:
Linus Dierheimer
2022-08-20 18:17:00 +02:00
parent 112e83b7c3
commit 35253e085c
3 changed files with 53 additions and 73 deletions
+20 -18
View File
@@ -8,11 +8,16 @@
#include <dirent.h>
#include <sys/stat.h>
typedef struct CustomValue
{
bool printKey;
FFstrbuf value;
} CustomValue;
// Things only needed by fastfetch
typedef struct FFdata
{
FFvaluestore customWithKey; //Prints with key
FFvaluestore customKeyless; //Prints without
FFvaluestore customValues;
FFstrbuf structure;
bool loadUserConfig;
} FFdata;
@@ -670,7 +675,7 @@ static uint32_t optionParseUInt32(const char* key, const char* value)
return num;
}
static void optionParseCustom(const char* key, const char* value, FFvaluestore* valuestore)
static void optionParseCustomValue(FFdata* data, const char* key, const char* value, bool printKey)
{
if(value == NULL)
{
@@ -688,7 +693,12 @@ static void optionParseCustom(const char* key, const char* value, FFvaluestore*
*separator = '\0';
ffValuestoreSet(valuestore, value, separator + 1);
bool created;
CustomValue* customValue = ffValuestoreSet(&data->customValues, value, &created);
if(created)
ffStrbufInit(&customValue->value);
ffStrbufSetS(&customValue->value, separator + 1);
customValue->printKey = printKey;
}
static void parseOption(FFinstance* instance, FFdata* data, const char* key, const char* value)
@@ -902,9 +912,9 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
else if(strcasecmp(key, "-c") == 0 || strcasecmp(key, "--color") == 0)
optionParseColor(key, value, &instance->config.mainColor);
else if(strcasecmp(key, "--set") == 0)
optionParseCustom(key, value, &data->customWithKey);
optionParseCustomValue(data, key, value, true);
else if(strcasecmp(key, "--set-keyless") == 0)
optionParseCustom(key, value, &data->customKeyless);
optionParseCustomValue(data, key, value, false);
////////////////////////////////
//Format + Key + Error options//
@@ -1286,17 +1296,10 @@ static void parseArguments(FFinstance* instance, FFdata* data, int argc, const c
static void parseStructureCommand(FFinstance* instance, FFdata* data, const char* line)
{
const char* setValue = ffValuestoreGet(&data->customWithKey, line);
if(setValue != NULL)
CustomValue* customValue = ffValuestoreGet(&data->customValues, line);
if(customValue != NULL)
{
ffPrintCustom(instance, line, setValue);
return;
}
setValue = ffValuestoreGet(&data->customKeyless, line);
if(setValue != NULL)
{
ffPrintCustom(instance, NULL, setValue);
ffPrintCustom(instance, customValue->printKey ? line : NULL, customValue->value.chars);
return;
}
@@ -1387,8 +1390,7 @@ int main(int argc, const char** argv)
//Data stores things only needed for the configuration of fastfetch
FFdata data;
ffValuestoreInit(&data.customWithKey);
ffValuestoreInit(&data.customKeyless);
ffValuestoreInit(&data.customValues, sizeof(CustomValue));
ffStrbufInitA(&data.structure, 256);
data.loadUserConfig = true;
+27 -36
View File
@@ -1,55 +1,46 @@
#include "FFvaluestore.h"
#include <string.h>
#include <stdlib.h>
void ffValuestoreInit(FFvaluestore* vs)
typedef char KeyType[33]; //32 byte key + \0
void ffValuestoreInit(FFvaluestore* vs, uint32_t valueSize)
{
vs->capacity = 32;
vs->pairs = calloc(vs->capacity, sizeof(FFvaluestorePair));
vs->size = 0;
ffListInit(vs, sizeof(KeyType) + valueSize);
}
void ffValuestoreSet(FFvaluestore* vs, const char* name, const char* value)
void* ffValuestoreGet(FFvaluestore* vs, const char* key)
{
for(uint32_t i = 0; i < vs->size; i++)
for(uint32_t i = 0; i < vs->length; i++)
{
if(strcasecmp(vs->pairs[i].name, name) == 0)
{
strcpy(vs->pairs[i].value, value);
return;
}
}
if(vs->size == vs->capacity)
{
vs->pairs = realloc(vs->pairs, vs->capacity * sizeof(FFvaluestorePair) * 2);
vs->capacity *= 2;
}
strcpy(vs->pairs[vs->size].name, name);
strcpy(vs->pairs[vs->size].value, value);
++vs->size;
}
const char* ffValuestoreGet(FFvaluestore* vs, const char* name)
{
for(uint32_t i = 0; i < vs->size; i++)
{
if(strcasecmp(vs->pairs[i].name, name) == 0)
return vs->pairs[i].value;
char* element = ffListGet(vs, i);
if(strcmp(element, key) == 0)
return element + sizeof(KeyType);
}
return NULL;
}
bool ffValuestoreContains(FFvaluestore* vs, const char* name)
void* ffValuestoreSet(FFvaluestore* vs, const char* key, bool* created)
{
return ffValuestoreGet(vs, name) != NULL;
char* element = ffValuestoreGet(vs, key);
if(element != NULL)
{
if(created != NULL)
*created = false;
return element;
}
element = ffListAdd(vs);
strncpy(element, key, sizeof(KeyType) - 1);
element[sizeof(KeyType) - 1] = '\0';
if(created != NULL)
*created = true;
return element + sizeof(KeyType);
}
void ffValuestoreDelete(FFvaluestore* vs)
void ffValuestoreDestroy(FFvaluestore* vs)
{
free(vs->pairs);
ffListDestroy(vs);
}
+6 -19
View File
@@ -3,26 +3,13 @@
#ifndef FASTFETCH_INCLUDED_FFVALUESTORE
#define FASTFETCH_INCLUDED_FFVALUESTORE
#include <stdbool.h>
#include <stdint.h>
#include "util/FFlist.h"
typedef struct FFvaluestorePair
{
char name[32];
char value[1024];
} FFvaluestorePair;
typedef FFlist FFvaluestore;
typedef struct FFvaluestore
{
FFvaluestorePair* pairs;
uint32_t size;
uint32_t capacity;
} FFvaluestore;
void ffValuestoreInit(FFvaluestore* vs);
void ffValuestoreSet(FFvaluestore* vs, const char* name, const char* value);
const char* ffValuestoreGet(FFvaluestore* vs, const char* name);
bool ffValuestoreContains(FFvaluestore* vs, const char* name);
void ffValuestoreDelete(FFvaluestore* vs);
void ffValuestoreInit(FFvaluestore* vs, uint32_t valueSize);
void* ffValuestoreGet(FFvaluestore* vs, const char* key);
void* ffValuestoreSet(FFvaluestore* vs, const char* key, bool* created); //created may be NULL
void ffValuestoreDestroy(FFvaluestore* vs);
#endif