-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjconfig.cpp
102 lines (79 loc) · 1.61 KB
/
jconfig.cpp
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
#include "jconfig.h"
#include "debug.h"
JConfig::JConfig() {}
JConfig::JConfig(const QString &source, enum SourceType type)
{
switch (type)
{
case SourceString:
parse(source);
break;
case SourceFile:
load(source);
break;
}
}
JConfig::JConfig(QIODevice *device)
{
load(device);
}
JConfig::JConfig(const Json &json)
{
m_type = Null;
setValue(json);
}
bool JConfig::load(const QString &filename)
{
if (!filename.isNull())
m_filename = filename;
if (m_filename.isNull())
return false;
bool result = false;
QFile file(m_filename);
if (file.exists() && file.open(QIODevice::ReadOnly))
{
result = load(&file);
file.close();
}
return result;
}
bool JConfig::load(QIODevice *device)
{
parse(device->readAll());
return (error() == ErrorNone) && !isNull();
}
bool JConfig::save(const QString &filename)
{
if (!filename.isNull())
m_filename = filename;
if (m_filename.isNull())
return false;
bool result = false;
QFile file(m_filename);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
result = save(&file);
file.close();
}
return result;
}
bool JConfig::save(QIODevice *device) const
{
const QByteArray &data = dump().toUtf8();
return device->write(data) == data.size();
}
QString JConfig::toPath(const Json &def) const
{
QString path = toString(def);
if (path.isNull() || path[0] == '/')
return path;
return JCONF_BASEDIR + "/" + path;
}
const JConfig &JConfig::operator[](const JsonIndex &idx) const
{
return ((const JConfig &)Json::operator[](idx));
}
JConfig &JConfig::operator[](const JsonIndex &idx)
{
return ((JConfig &)Json::operator[](idx));
}