Skip to content

Commit 11809e3

Browse files
Parse config files faster
1 parent 35253e0 commit 11809e3

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

src/fastfetch.c

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "common/parsing.h"
55

66
#include <stdlib.h>
7+
#include <ctype.h>
78
#include <string.h>
89
#include <dirent.h>
910
#include <sys/stat.h>
@@ -453,58 +454,57 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
453454

454455
static void parseConfigFile(FFinstance* instance, FFdata* data, FILE* file)
455456
{
456-
char* lineStart = NULL;
457+
char* line = NULL;
457458
size_t len = 0;
458459
ssize_t read;
459460

460-
FFstrbuf line;
461-
ffStrbufInitA(&line, 256); //The default structure line needs this size
462-
463-
while ((read = getline(&lineStart, &len, file)) != -1)
461+
while ((read = getline(&line, &len, file)) != -1)
464462
{
465-
ffStrbufSetS(&line, lineStart);
466-
ffStrbufTrimRight(&line, '\n');
467-
ffStrbufTrim(&line, ' ');
463+
char* lineStart = line;
464+
char* lineEnd = line + read - 1;
465+
466+
//Trim line left
467+
while(isspace(*lineStart))
468+
++lineStart;
468469

469-
if(line.length == 0 || line.chars[0] == '#')
470+
//Continue if line is empty or a comment
471+
if(*lineStart == '\0' || *lineStart == '#')
470472
continue;
471473

472-
uint32_t firstSpace = ffStrbufFirstIndexC(&line, ' ');
474+
//Trim line right
475+
while(lineEnd > lineStart && isspace(*lineEnd))
476+
--lineEnd;
477+
*(lineEnd + 1) = '\0';
478+
479+
char* valueStart = strchr(lineStart, ' ');
473480

474-
if(firstSpace >= line.length)
481+
//If the line has no white space, it is only a key
482+
if(valueStart == NULL)
475483
{
476-
parseOption(instance, data, line.chars, NULL);
484+
parseOption(instance, data, lineStart, NULL);
477485
continue;
478486
}
479487

480-
//Separate key and value by simply replacing the first space with a \0
481-
char* valueStart = &line.chars[firstSpace];
488+
//separate the key from the value
482489
*valueStart = '\0';
483490
++valueStart;
484491

485-
//Trim whitespace at beginning of value
486-
while(*valueStart == ' ')
492+
//Trim space of value left
493+
while(isspace(*valueStart))
487494
++valueStart;
488495

489496
//If we want whitespace in values, we need to quote it. This is done to keep consistency with shell.
490-
if(*valueStart == '"')
497+
if((*valueStart == '"' || *valueStart == '\'') && *valueStart == *lineEnd && lineEnd > valueStart)
491498
{
492-
char* last = line.chars + line.length - 1;
493-
if(*last == '"')
494-
{
495-
++valueStart;
496-
*last = '\0';
497-
--line.length;
498-
}
499+
++valueStart;
500+
--lineEnd;
499501
}
500502

501-
parseOption(instance, data, line.chars, valueStart);
503+
parseOption(instance, data, lineStart, valueStart);
502504
}
503505

504-
ffStrbufDestroy(&line);
505-
506-
if(lineStart != NULL)
507-
free(lineStart);
506+
if(line != NULL)
507+
free(line);
508508
}
509509

510510
static void optionParseConfigFile(FFinstance* instance, FFdata* data, const char* key, const char* value)
@@ -1281,7 +1281,6 @@ static void parseArguments(FFinstance* instance, FFdata* data, int argc, const c
12811281
{
12821282
if(i == argc - 1 || (
12831283
*argv[i + 1] == '-' &&
1284-
strcasecmp(argv[i], "--offsetx") != 0 && // --offsetx allows negative values
12851284
strcasecmp(argv[i], "--separator-string") != 0 // Separator string can start with a -
12861285
)) {
12871286
parseOption(instance, data, argv[i], NULL);

0 commit comments

Comments
 (0)