-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesktopFile.cpp
40 lines (36 loc) · 1.2 KB
/
DesktopFile.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
#include "DesktopFile.h"
#include <QBuffer>
#include <iostream>
DesktopFile::DesktopFile(QByteArray contents) {
QBuffer b(&contents);
b.open(QBuffer::ReadOnly);
String currentSection;
QHash<String,String> sectionContent;
while(b.canReadLine()) {
String l=b.readLine().trimmed();
if(l.startsWith('[') && l.endsWith(']')) {
if(sectionContent.count())
insert(currentSection, sectionContent);
currentSection = l.sliced(1, l.length()-2);
sectionContent.clear();
} else if(l.contains('=')) {
sectionContent.insert(l.sliced(0, l.indexOf('=')), l.sliced(l.indexOf('=')+1));
}
}
if(sectionContent.count())
insert(currentSection, sectionContent);
}
QList<String> DesktopFile::sections() const {
QList<String> ret;
for(auto i=cbegin(), e=cend(); i!=e; ++i)
ret.append(i.key());
return ret;
}
String DesktopFile::value(String const &key, String const &dflt, String const §ion) const {
return QHash<String, QHash<String, String>>::value(section).value(key, dflt);
}
bool DesktopFile::hasKey(String const &key, String const §ion) const {
if(!QHash<String, QHash<String, String>>::contains(section))
return false;
return QHash<String, QHash<String, String>>::value(section).contains(key);
}