aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/configuration.cpp
blob: 75f863cbb21a1f9ed24e35023bfb68cd4fe0c624 (plain)
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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "configuration.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>

#ifndef NO_QT_SPEC
#include <QStandardPaths>
#endif

static std::unique_ptr<Configuration> s_conf;

Configuration::Configuration()
    : use_global(true)
{
    if(!s_conf) {
        throw std::runtime_error("Trying to use default Configuration, but none has been set!");
    }
}

Configuration::Configuration(std::initializer_list<std::pair<std::string, conf_value_t>> l) noexcept
#ifndef NO_QT_SPEC
    : m_homePath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString())
#endif
{
    for(const auto &i : l) {
        insert_or_assign(i.first, i.second);
    }
}

void Configuration::read_file(const std::string &location)
{
    std::fstream fs(location, std::fstream::in);
    if(fs.is_open()) {
        read(fs);
        fs.close();
    }
}

inline auto strip(std::string &value)
{
    value.erase(value.begin(), std::find_if(value.begin(), value.end(), std::bind1st(std::not_equal_to<char>(), ' ')));
    value.erase(std::find_if(value.rbegin(), value.rend(), std::bind1st(std::not_equal_to<char>(), ' ')).base(), value.end());
    return value;
}

inline auto parse(const std::string &line, std::string &section)
{
    struct {
        bool pair = false;
        std::string key;
        std::string value;
    } ret;

    if(line[0] == '#' || line.length() == 0) {
        return ret;
    }

    if(line.front() == '[' && line.back() == ']') {
        section = line.substr(1, line.length() - 2) + '/';
        return ret;
    }

    const auto pos = line.find_first_of('=');
    if(pos == std::string::npos) {
        return ret;
    }

    ret.key = line.substr(0, pos);
    strip(ret.key);
    if(ret.key.empty()) {
        return ret;
    }
    ret.key = section + ret.key;

    ret.value = line.substr(pos + 1);
    strip(ret.value);

    ret.pair = true;
    return ret;
}

#ifdef FUZZER
extern "C" int LLVMFuzzerTestOneInput(const char *Data, long long Size)
{
    std::string section;
    parse(std::string(Data, Size), section);
    return 0;
}
#endif

void Configuration::read(std::basic_istream<char> &input)
{
    std::string line, section;

    while(std::getline(input, line)) {
        if(line.rfind("@@") == 0) {
            if(line.rfind("@@include ") == 0) {
                read_file(line.substr(10));
            }
            continue;
        }

        const auto pair = parse(line, section);
        if(pair.pair) {

            if(this->count(pair.key) == 0) {
                // no type has been specified for this key, assuming std::string
                insert(std::make_pair(pair.key, pair.value));
                continue;
            }

            auto v = at(pair.key);
            if(std::holds_alternative<std::string>(v)) {
                at(pair.key) = pair.value;
            } else if(std::holds_alternative<int>(v)) {
                at(pair.key) = std::stoi(pair.value);
            } else if(std::holds_alternative<bool>(v)) {
                at(pair.key) = (pair.value == "true");
            }
        }
    }
}

void Configuration::move_global(std::unique_ptr<Configuration> &&conf)
{
    s_conf = std::move(conf);
}

Configuration *Configuration::instance()
{
    return s_conf.get();
}

std::ostream &operator<<(std::ostream &out, const Configuration &obj)
{
    if(obj.use_global) {
        out << *s_conf;
        return out;
    }

    // unordered_map is, well, unordered, so grab the keys and sort them before printing them
    std::vector<std::string> keys;
    for(const auto &pair : obj) {
        keys.emplace_back(pair.first);
    }
    std::sort(keys.begin(), keys.end());

    for(const auto &key : keys) {
        out << key << "=" << obj.value<std::string>(key.c_str()).value() << "\n";
    }

    return out;
}