aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.cpp
blob: b281b70f1f4f17ef7a0cbc7b5ff7210eae6f3101 (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
164
165
166
167
168
169
/*
 * 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/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "configuration.h"
#include <QtCore/QStandardPaths>
#include <QFile>
#include <sstream>

using namespace libconfig;

Configuration::Configuration(const std::string &path, const std::string &home)
{
    m_userCfg = new Config();
    // prettier output
    m_userCfg->setOption(Config::OptionSemicolonSeparators, true);
    m_userCfg->setOption(Config::OptionColonAssignmentForGroups, false);
    m_userCfg->setOption(Config::OptionColonAssignmentForNonGroups, false);
    m_userCfg->setOption(Config::OptionOpenBraceOnSeparateLine, false);
    m_userCfg->setOption(Config::OptionFsync, true);

    m_userCfgPath = path;
    m_homePath = home;
}

Configuration::~Configuration()
{
    delete m_userCfg;
}

bool Configuration::read(const QString &path)
{
    QFile conf(path);

    if(!conf.open(QIODevice::ReadOnly)) {
        return false;
    }

    try {
        m_userCfg->readString(conf.readAll().toStdString());
        conf.close();
    } catch(const ParseException &e) {
        return false;
    }

    return true;
}

bool Configuration::writeIfNeeded(const std::string &path)
{
    if(!path.empty())
        m_userCfgPath = path;

    if(!changed)
        return true;

    try {
        m_userCfg->writeFile(m_userCfgPath.c_str());
    } catch(const FileIOException &e) {
        return false;
    }

    changed = false;
    return true;
}

std::vector<std::string> Configuration::childrenSettings(const char *name)
{
    std::vector<std::string> groupNames;
    const Setting &root = m_userCfg->lookup(name);

    for(const Setting &setting : root) {
        if(setting.getType() != Setting::TypeGroup) {
            groupNames.emplace_back(setting.getName());
            //groupNames.push_back(setting.getName());
        }
    }

    return groupNames;
}

std::vector<std::string> Configuration::childrenGroups(const char *name)
{
    std::vector<std::string> groupNames;
    const Setting &root = m_userCfg->lookup(name);

    for(const Setting &setting : root) {
        if(setting.getType() == Setting::TypeGroup) {
            groupNames.emplace_back(setting.getName());
            //groupNames.push_back(setting.getName());
        }
    }
    return groupNames;
}

std::string castToString(const libconfig::Setting &v)
{
    // cast depending on type
    // type checks are done during compile time
    switch(v.getType()) {
    case Setting::TypeNone:
        return std::string();

    case Setting::TypeInt:
        // int, unsigned int, long, unsigned long
        return std::to_string(static_cast<int32_t>(v));

    case Setting::TypeInt64:
        // int, unsigned int; long long, unsigned long long
        return std::to_string(static_cast<int64_t>(v));

    case Setting::TypeFloat:
        // float, double
        return std::to_string(static_cast<double>(v));

    case Setting::TypeString:
        // const char*, std::string
        return std::string(static_cast<const char *>(v));

    case Setting::TypeBoolean:
        // bool
        return std::string(static_cast<bool>(v) ? "true" : "false");

    case Setting::TypeGroup:
    case Setting::TypeArray:
    case Setting::TypeList:
        return std::string();
    }
}

void setFromString(libconfig::Setting &setting, const std::string &value)
{
    switch(setting.getType()) {
        case libconfig::Setting::TypeNone:
            break;

        case libconfig::Setting::TypeInt:
        case libconfig::Setting::TypeInt64:
            setting = std::stoi(value);
            break;

        case libconfig::Setting::TypeFloat:
            setting = std::stod(value);
            break;

        case libconfig::Setting::TypeString:
            setting = value.c_str();
            break;

        case libconfig::Setting::TypeBoolean:
            if(value == "true") {
                setting = true;
            } else if(value == "false") {
                setting = false;
            }
            break;

        case libconfig::Setting::TypeGroup:
            break;
        case libconfig::Setting::TypeArray:
            break;
        case libconfig::Setting::TypeList:
            break;
    }
}