Skip to content

Commit 35253e0

Browse files
Unified storage of --set and --set-keyless values
1 parent 112e83b commit 35253e0

File tree

3 files changed

+50
-70
lines changed

3 files changed

+50
-70
lines changed

src/fastfetch.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
#include <dirent.h>
99
#include <sys/stat.h>
1010

11+
typedef struct CustomValue
12+
{
13+
bool printKey;
14+
FFstrbuf value;
15+
} CustomValue;
16+
1117
// Things only needed by fastfetch
1218
typedef struct FFdata
1319
{
14-
FFvaluestore customWithKey; //Prints with key
15-
FFvaluestore customKeyless; //Prints without
20+
FFvaluestore customValues;
1621
FFstrbuf structure;
1722
bool loadUserConfig;
1823
} FFdata;
@@ -670,7 +675,7 @@ static uint32_t optionParseUInt32(const char* key, const char* value)
670675
return num;
671676
}
672677

673-
static void optionParseCustom(const char* key, const char* value, FFvaluestore* valuestore)
678+
static void optionParseCustomValue(FFdata* data, const char* key, const char* value, bool printKey)
674679
{
675680
if(value == NULL)
676681
{
@@ -688,7 +693,12 @@ static void optionParseCustom(const char* key, const char* value, FFvaluestore*
688693

689694
*separator = '\0';
690695

691-
ffValuestoreSet(valuestore, value, separator + 1);
696+
bool created;
697+
CustomValue* customValue = ffValuestoreSet(&data->customValues, value, &created);
698+
if(created)
699+
ffStrbufInit(&customValue->value);
700+
ffStrbufSetS(&customValue->value, separator + 1);
701+
customValue->printKey = printKey;
692702
}
693703

694704
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
902912
else if(strcasecmp(key, "-c") == 0 || strcasecmp(key, "--color") == 0)
903913
optionParseColor(key, value, &instance->config.mainColor);
904914
else if(strcasecmp(key, "--set") == 0)
905-
optionParseCustom(key, value, &data->customWithKey);
915+
optionParseCustomValue(data, key, value, true);
906916
else if(strcasecmp(key, "--set-keyless") == 0)
907-
optionParseCustom(key, value, &data->customKeyless);
917+
optionParseCustomValue(data, key, value, false);
908918

909919
////////////////////////////////
910920
//Format + Key + Error options//
@@ -1286,17 +1296,10 @@ static void parseArguments(FFinstance* instance, FFdata* data, int argc, const c
12861296

12871297
static void parseStructureCommand(FFinstance* instance, FFdata* data, const char* line)
12881298
{
1289-
const char* setValue = ffValuestoreGet(&data->customWithKey, line);
1290-
if(setValue != NULL)
1291-
{
1292-
ffPrintCustom(instance, line, setValue);
1293-
return;
1294-
}
1295-
1296-
setValue = ffValuestoreGet(&data->customKeyless, line);
1297-
if(setValue != NULL)
1299+
CustomValue* customValue = ffValuestoreGet(&data->customValues, line);
1300+
if(customValue != NULL)
12981301
{
1299-
ffPrintCustom(instance, NULL, setValue);
1302+
ffPrintCustom(instance, customValue->printKey ? line : NULL, customValue->value.chars);
13001303
return;
13011304
}
13021305

@@ -1387,8 +1390,7 @@ int main(int argc, const char** argv)
13871390

13881391
//Data stores things only needed for the configuration of fastfetch
13891392
FFdata data;
1390-
ffValuestoreInit(&data.customWithKey);
1391-
ffValuestoreInit(&data.customKeyless);
1393+
ffValuestoreInit(&data.customValues, sizeof(CustomValue));
13921394
ffStrbufInitA(&data.structure, 256);
13931395
data.loadUserConfig = true;
13941396

src/util/FFvaluestore.c

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,46 @@
11
#include "FFvaluestore.h"
22

33
#include <string.h>
4-
#include <stdlib.h>
54

6-
void ffValuestoreInit(FFvaluestore* vs)
5+
typedef char KeyType[33]; //32 byte key + \0
6+
7+
void ffValuestoreInit(FFvaluestore* vs, uint32_t valueSize)
78
{
8-
vs->capacity = 32;
9-
vs->pairs = calloc(vs->capacity, sizeof(FFvaluestorePair));
10-
vs->size = 0;
9+
ffListInit(vs, sizeof(KeyType) + valueSize);
1110
}
1211

13-
void ffValuestoreSet(FFvaluestore* vs, const char* name, const char* value)
12+
void* ffValuestoreGet(FFvaluestore* vs, const char* key)
1413
{
15-
for(uint32_t i = 0; i < vs->size; i++)
14+
for(uint32_t i = 0; i < vs->length; i++)
1615
{
17-
if(strcasecmp(vs->pairs[i].name, name) == 0)
18-
{
19-
strcpy(vs->pairs[i].value, value);
20-
return;
21-
}
16+
char* element = ffListGet(vs, i);
17+
if(strcmp(element, key) == 0)
18+
return element + sizeof(KeyType);
2219
}
2320

24-
if(vs->size == vs->capacity)
25-
{
26-
vs->pairs = realloc(vs->pairs, vs->capacity * sizeof(FFvaluestorePair) * 2);
27-
vs->capacity *= 2;
28-
}
29-
30-
strcpy(vs->pairs[vs->size].name, name);
31-
strcpy(vs->pairs[vs->size].value, value);
32-
33-
++vs->size;
21+
return NULL;
3422
}
3523

36-
const char* ffValuestoreGet(FFvaluestore* vs, const char* name)
24+
void* ffValuestoreSet(FFvaluestore* vs, const char* key, bool* created)
3725
{
38-
for(uint32_t i = 0; i < vs->size; i++)
26+
char* element = ffValuestoreGet(vs, key);
27+
if(element != NULL)
3928
{
40-
if(strcasecmp(vs->pairs[i].name, name) == 0)
41-
return vs->pairs[i].value;
29+
if(created != NULL)
30+
*created = false;
31+
return element;
4232
}
4333

44-
return NULL;
45-
}
34+
element = ffListAdd(vs);
35+
strncpy(element, key, sizeof(KeyType) - 1);
36+
element[sizeof(KeyType) - 1] = '\0';
4637

47-
bool ffValuestoreContains(FFvaluestore* vs, const char* name)
48-
{
49-
return ffValuestoreGet(vs, name) != NULL;
38+
if(created != NULL)
39+
*created = true;
40+
return element + sizeof(KeyType);
5041
}
5142

52-
void ffValuestoreDelete(FFvaluestore* vs)
43+
void ffValuestoreDestroy(FFvaluestore* vs)
5344
{
54-
free(vs->pairs);
45+
ffListDestroy(vs);
5546
}

src/util/FFvaluestore.h

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,13 @@
33
#ifndef FASTFETCH_INCLUDED_FFVALUESTORE
44
#define FASTFETCH_INCLUDED_FFVALUESTORE
55

6-
#include <stdbool.h>
7-
#include <stdint.h>
6+
#include "util/FFlist.h"
87

9-
typedef struct FFvaluestorePair
10-
{
11-
char name[32];
12-
char value[1024];
13-
} FFvaluestorePair;
8+
typedef FFlist FFvaluestore;
149

15-
typedef struct FFvaluestore
16-
{
17-
FFvaluestorePair* pairs;
18-
uint32_t size;
19-
uint32_t capacity;
20-
} FFvaluestore;
21-
22-
void ffValuestoreInit(FFvaluestore* vs);
23-
void ffValuestoreSet(FFvaluestore* vs, const char* name, const char* value);
24-
const char* ffValuestoreGet(FFvaluestore* vs, const char* name);
25-
bool ffValuestoreContains(FFvaluestore* vs, const char* name);
26-
void ffValuestoreDelete(FFvaluestore* vs);
10+
void ffValuestoreInit(FFvaluestore* vs, uint32_t valueSize);
11+
void* ffValuestoreGet(FFvaluestore* vs, const char* key);
12+
void* ffValuestoreSet(FFvaluestore* vs, const char* key, bool* created); //created may be NULL
13+
void ffValuestoreDestroy(FFvaluestore* vs);
2714

2815
#endif

0 commit comments

Comments
 (0)