forked from cculianu/SpikeGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParams.cpp
More file actions
68 lines (61 loc) · 2.1 KB
/
Params.cpp
File metadata and controls
68 lines (61 loc) · 2.1 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
#include "Params.h"
#include "Util.h"
#include <QTextStream>
#include <QRegExp>
#include <QFile>
#include "SpikeGL.h"
void Params::fromString(const QString & instr)
{
QString instrcpy (instr);
QTextStream ts(&instrcpy, QIODevice::ReadOnly|QIODevice::Text);
QString line;
clear();
// now parse remaining lines which should be name/value pairs
while ( !(line = ts.readLine()).isNull() ) {
line = line.trimmed();
QRegExp re_comments("((;)|(#)|(//)).*");
if (line.contains(re_comments)) {
if (excessiveDebug) Debug() << "Comment found and skipped: `" << re_comments.cap(0) << "'";
line.replace(re_comments, "");
line = line.trimmed();
}
if (!line.length()) continue;
QRegExp re("([^=]+)=(.*)");
if (re.exactMatch(line)) {
QString name = re.cap(1).trimmed(), value = re.cap(2).trimmed();
Debug() << "parsed name=value: '" << name << "'='" << value << "'";
(*this)[name] = value;
} else {
Debug() << "did not parse, setting '" << line << "'=''";
(*this)[line] = "";
}
}
}
QString Params::toString() const
{
QString ret("");
QTextStream ts(&ret, QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text);
for (const_iterator it = begin(); it != end(); ++it)
ts << it.key() << " = " << it.value().toString() << "\n";
ts.flush();
return ret;
}
bool Params::fromFile(const QString & fn)
{
QFile f(fn);
if (!f.open(QIODevice::ReadOnly|QIODevice::Text)) return false;
QString line, str("");
QTextStream ts(&f), strts(&str, QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text);
while ( !(line = ts.readLine()).isNull() ) {
strts << line << "\n";
}
strts.flush();
fromString(str);
return true;
}
bool Params::toFile(const QString & fn, bool append) const
{
QFile f(fn);
if (!f.open(QIODevice::WriteOnly|QIODevice::Text|(append ? QIODevice::Append : QIODevice::Truncate))) return false;
return (QTextStream(&f) << toString()).status() == QTextStream::Ok;
}