-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKSegProperties.cpp
More file actions
121 lines (85 loc) · 2.83 KB
/
Copy pathKSegProperties.cpp
File metadata and controls
121 lines (85 loc) · 2.83 KB
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
/*
* KSeg
* Copyright (C) 1999-2006 Ilya Baran
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Send comments and/or bug reports to:
* ibaran@mit.edu
*/
#include <qdir.h>
#include <qfile.h>
#include <qtextstream.h>
#include "KSegProperties.H"
KSegProperties *KSegProperties::properties = NULL;
const QString KSegProperties::configFile = QDir::homeDirPath() + "/.kseg";
QString KSegProperties::getProperty(QString name)
{
if(properties == NULL) properties = new KSegProperties();
return properties->getProp(name);
}
void KSegProperties::setProperty(QString name, QString value)
{
if(properties == NULL) properties = new KSegProperties();
properties->setProp(name, value);
}
void KSegProperties::setPropertyDefault(QString name, QString value)
{
if(getProperty(name).isNull())
properties->setProp(name, value); //properties must already be created at this point
}
//private member functions
KSegProperties::KSegProperties()
{
QFile f(configFile);
if(!f.open(QIODevice::ReadOnly)) return;
QTextStream ts(&f);
QString curLine;
while(true) {
curLine = ts.readLine();
if(curLine.isNull()) break;
if(curLine.length() < 2) continue; //skip (almost) empty lines
if(curLine[0] == '#') continue; //comment line
int ind = curLine.find('=');
if(ind == -1) continue;
QString key = curLine.left(ind).simplifyWhiteSpace();
QString value = curLine.right(curLine.length() - ind - 1).simplifyWhiteSpace();
myMap[key] = value;
}
}
QString KSegProperties::getProp(QString name)
{
if(myMap.count(name) == 0) return QString();
return myMap[name];
}
void KSegProperties::setProp(QString name, QString value)
{
if(myMap.count(name) != 0 && myMap[name] == value) return;
if(value.isNull()) myMap.erase(name);
else myMap[name] = value;
save();
}
void KSegProperties::save()
{
QFile f(configFile);
if(!f.open(QIODevice::WriteOnly)) return;
QTextStream ts(&f);
hash_map<QString, QString>::iterator it;
ts << "# KSeg configuration file--automatically generated in random order :)\n";
for(it = myMap.begin(); it != myMap.end(); ++it) {
ts << it->first << " = " << it->second << "\n";
}
f.close();
}