summaryrefslogtreecommitdiffstats
path: root/src/common/impl/properties.c
blob: c13ed1cd6d0b8500e6eea3d993b2d39c30b9c557 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "fastfetch.h"
#include "common/properties.h"
#include "common/io.h"
#include "common/mallocHelper.h"

#include <stdlib.h>
#include <ctype.h>
#ifdef _WIN32
    #include "common/windows/getline.h"
#endif

bool ffParsePropLinePointer(const char** line, const char* start, FFstrbuf* buffer) {
    if (**line == '\0') {
        return false;
    }

    // Skip any amount of whitespace at the begin of line
    while (**line == ' ' || **line == '\t') {
        ++(*line);
    }

    while (*start != '\0') {
        // Any amount of whitespace in the format string matches any amount of whitespace in the line, even none
        if (*start == ' ' || *start == '\t') {
            while (*start == ' ' || *start == '\t') {
                ++start;
            }

            while (**line == ' ' || **line == '\t') {
                ++(*line);
            }

            continue;
        }

        // Line doesn't match start, skip it
        if (tolower(**line) != tolower(*start) || **line == '\0') {
            return false;
        }

        // Line and start match, continue testing
        ++(*line);
        ++start;
    }

    char valueEnd = '\n';

    // Allow faster parsing of XML
    if (*(*line - 1) == '>') {
        valueEnd = '<';
    }

    // Skip any amount of whitespace at the begin of the value
    while (**line == ' ' || **line == '\t') {
        ++(*line);
    }

    // Allow faster parsing of quoted values
    if (**line == '"' || **line == '\'') {
        valueEnd = **line;
        ++(*line);
    }

    // Copy the value to the buffer
    while (**line != valueEnd && **line != '\n' && **line != '\0') {
        ffStrbufAppendC(buffer, **line);
        ++(*line);
    }

    ffStrbufTrimRight(buffer, ' ');

    return true;
}

bool ffParsePropLines(const char* lines, const char* start, FFstrbuf* buffer) {
    while (!ffParsePropLinePointer(&lines, start, buffer)) {
        while (*lines != '\0' && *lines != '\n') {
            ++lines;
        }

        if (*lines == '\0') {
            return false;
        }

        // Skip '\n'
        ++lines;
    }

    return true;
}

// The following functions return true if the file was found, independently if start was found
// Buffers which already contain content are not overwritten
// The last occurrence of start in the first file will be the one used

bool ffParsePropFileValues(const char* filename, uint32_t numQueries, FFpropquery* queries) {
    FF_AUTO_CLOSE_FILE FILE* file = fopen(filename, "r");
    if (file == NULL) {
        return false;
    }

    bool valueStorage[32];
    bool* unsetValues = valueStorage;

    if (numQueries > ARRAY_SIZE(valueStorage)) {
        unsetValues = malloc(sizeof(bool) * numQueries);
    }

    bool allSet = true;
    for (uint32_t i = 0; i < numQueries; i++) {
        unsetValues[i] = queries[i].buffer->length == 0;
        if (unsetValues[i]) {
            allSet = false;
        }
    }

    if (!allSet) {
        FF_AUTO_FREE char* line = NULL;
        size_t len = 0;

        while (getline(&line, &len, file) != -1) {
            for (uint32_t i = 0; i < numQueries; i++) {
                if (!unsetValues[i]) {
                    continue;
                }

                uint32_t currentLength = queries[i].buffer->length;
                queries[i].buffer->length = 0;
                if (!ffParsePropLine(line, queries[i].start, queries[i].buffer)) {
                    queries[i].buffer->length = currentLength;
                }
            }
        }
    }

    if (unsetValues != valueStorage) {
        free(unsetValues);
    }
    return true;
}

bool ffParsePropFileHomeValues(const char* relativeFile, uint32_t numQueries, FFpropquery* queries) {
    FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateF("%s/%s", instance.state.platform.homeDir.chars, relativeFile);
    return ffParsePropFileValues(absolutePath.chars, numQueries, queries);
}

bool ffParsePropFileListValues(const FFlist* list, const char* relativeFile, uint32_t numQueries, FFpropquery* queries) {
    bool foundAFile = false;

    FF_LIST_FOR_EACH (FFstrbuf, dirPrefix, *list) {
        const uint32_t dirPrefixLength = dirPrefix->length;
        ffStrbufAppendS(dirPrefix, relativeFile);
        if (ffParsePropFileValues(dirPrefix->chars, numQueries, queries)) {
            foundAFile = true;
        }
        ffStrbufSubstrBefore(dirPrefix, dirPrefixLength);

        bool allSet = true;
        for (uint32_t k = 0; k < numQueries; k++) {
            if (queries[k].buffer->length == 0) {
                allSet = false;
                break;
            }
        }

        if (allSet) {
            break;
        }
    }

    return foundAFile;
}