-
Notifications
You must be signed in to change notification settings - Fork 2
/
posixconfigparser.cpp
53 lines (50 loc) · 1.28 KB
/
posixconfigparser.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
#include "posixconfigparser.h"
PosixConfigParser::PosixConfigParser(QString input)
: m_input(input)
{
parse();
}
void PosixConfigParser::set(QString key, QString value)
{
QStringList lines = m_input.split('\n');
bool complete = false;
for(int i = 0; i < lines.size(); i++)
{
QString line = lines[i];
if(line.indexOf('#') == 0) continue;
int e = line.indexOf('=');
if(e < 0) continue;
QString k = line.mid(0, e);
if(key == k)
{
lines[i] = QString("%1=\"%2\"").arg(key).arg(value);
complete = true;
break;
}
}
if(!complete)
{
lines.append(QString("%1=\"%2\"").arg(key).arg(value));
}
m_input = lines.join('\n');
parse();
}
void PosixConfigParser::parse()
{
config.clear();
QStringList lines = m_input.split('\n');
for(auto line : lines)
{
if(line.indexOf('#') == 0) continue;
int e = line.indexOf('=');
if(e < 0) continue;
QString key = line.mid(0, e);
QString value = line.mid(e + 1);
if(value.indexOf('"') == 0 || value.indexOf('\'') == 0)
{
value = value.mid(1);
value = value.mid(0, value.length() - 1);
}
config[key] = value;
}
}