-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetparameter.cpp
184 lines (155 loc) · 6.07 KB
/
getparameter.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "getparameter.h"
#include <QTextStream>
#include <QDebug>
#include <QString>
#include <QRegularExpression>
#include <sstream>
#include <iostream>
template <typename T> T readParameter(QString parameterName, QString fileName, T defaultVal = -1.0)
{
T parameterVal = defaultVal;
bool success = false; // not successful, yet
// Open file
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
std::cerr << "could not open " << fileName.toStdString() <<
", using default value " <<
parameterName.toStdString()<< ": " << defaultVal;
return parameterVal;
}
// Read line-by-line
QTextStream stream(&file);
QString line;
while (stream.readLineInto(&line)) {
QStringList lineSegments = line.split(QRegularExpression(getparameter::delimiters), QString::SkipEmptyParts);
if (lineSegments.size() > 1)
if (lineSegments[0] == parameterName) {
QString parameterValStr = lineSegments[1];
std::string str = parameterValStr.toStdString();
std::istringstream stringStream(str);
stringStream >> parameterVal;
success = true;
if (stringStream.fail())
{
std::clog << "Could not read " << parameterName.toStdString()
<< " in " << fileName.toStdString()
<< ", using default value "
<< parameterName.toStdString() << defaultVal;
parameterVal = defaultVal;
success = false;
}
break;
}
}
// If EOF && first line segment != parameterName, parameterValue = defaultVal
if (!success) {
std::clog << "Could not read " << parameterName.toStdString()
<< " in " << fileName.toStdString()
<< ", using default value "
<< parameterName.toStdString() << defaultVal;
parameterVal = defaultVal;
}
file.close();
return parameterVal;
}
template <typename T> T getParameter(QString parameterName, QFile &file, T defaultVal = -1.0)
{
T parameterVal = defaultVal;
bool success = false; // not successful, yet
// Read line-by-line
QTextStream stream(&file);
QString line;
while (stream.readLineInto(&line)) {
QStringList lineSegments = line.split(QRegularExpression(getparameter::delimiters), QString::SkipEmptyParts);
if (lineSegments.size() > 1)
if (lineSegments[0] == parameterName) {
QString parameterValStr = lineSegments[1];
std::string str = parameterValStr.toStdString();
std::istringstream stringStream(str);
stringStream >> parameterVal;
success = true;
if (stringStream.fail())
{
std::clog << "Could not read " << parameterName.toStdString()
<< " in " << file.fileName().toStdString()
<< ", using default value " << parameterName.toStdString()
<< defaultVal;
parameterVal = defaultVal;
success = false;
}
break;
}
}
// If EOF && first line segment != parameterName, parameterValue = defaultVal
if (!success) {
std::clog << "Could not read " << parameterName.toStdString()
<< " in " << file.fileName().toStdString()
<< ", using default value " << parameterName.toStdString()
<< defaultVal;
parameterVal = defaultVal;
}
return parameterVal;
}
int getparameter::readInt(QString parameterName, QString fileName, int defaultVal)
{
return readParameter(parameterName, fileName, defaultVal);
}
double getparameter::readDouble(QString parameterName, QString fileName, double defaultVal)
{
return readParameter(parameterName, fileName, defaultVal);
}
unsigned int getparameter::readUInt(QString parameterName, QString fileName, unsigned int defaultVal)
{
return readParameter(parameterName, fileName, defaultVal);
}
int getparameter::getInt(QString parameterName, QFile &file, int defaultVal)
{
return getParameter(parameterName, file, defaultVal);
}
unsigned int getparameter::getUInt(QString parameterName, QFile &file, unsigned int defaultVal)
{
return getParameter(parameterName, file, defaultVal);
}
double getparameter::getDouble(QString parameterName, QFile &file, double defaultVal)
{
return getParameter(parameterName, file, defaultVal);
}
QString getparameter::readQString(QString parameterName, QString fileName, QString defaultVal)
{
QString parameterVal = defaultVal;
// Open file
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
std::clog << "could not open " << fileName.toStdString() << ", using default value "
<< parameterName.toStdString() << defaultVal.toStdString();
return parameterVal;
}
// Read line-by-line
QTextStream stream(&file);
QString line;
while (stream.readLineInto(&line)) {
QStringList lineSegments = line.split(QRegularExpression(getparameter::delimiters), QString::SkipEmptyParts);
if (lineSegments.size() > 1)
if (lineSegments[0] == parameterName) {
parameterVal = lineSegments[1];
break;
}
}
return parameterVal;
}
QString getparameter::getQString(QString parameterName, QFile &file, QString defaultVal)
{
QString parameterVal = defaultVal;
// Read line-by-line
QTextStream stream(&file);
QString line;
while (stream.readLineInto(&line)) {
QStringList lineSegments = line.split(QRegularExpression(getparameter::delimiters), QString::SkipEmptyParts);
if (lineSegments.size() > 1)
if (lineSegments[0] == parameterName) {
parameterVal = lineSegments[1];
break;
}
}
return parameterVal;
}