Developer Documentation
Loading...
Searching...
No Matches
INIFileT_impl.hh
1/*===========================================================================*\
2* *
3* OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39* *
40\*===========================================================================*/
41
42
43
44
45
46
47//=============================================================================
48//
49// CLASS INIFile Templates - IMPLEMENTATION
50//
51//=============================================================================
52
53#define INIFILE_C
54
55//== INCLUDES =================================================================
56
57#include "INIFile.hh"
58
59//std include
60#include <fstream>
61#include <iostream>
62#include <functional>
63#include <sstream>
64#include <cstring>
65#include <cctype>
66//#include <ios>
67
68#include <QString>
69#include <QFile>
70#include <QTextStream>
71#include <QStringList>
72
73#if QT_VERSION_MAJOR < 6
74#define SkipEmptyPartsIndependent QString::SkipEmptyParts
75#else
76#define SkipEmptyPartsIndependent Qt::SkipEmptyParts
77#endif
78
79//== IMPLEMENTATION ==========================================================
80
82template < typename VectorT >
83bool
85get_entryVeci ( VectorT & _val ,
86 const QString & _section,
87 const QString & _key ) const
88{
89 SectionMap::const_iterator sIter;
90 EntryMap::const_iterator eIter;
91
92 // does the given section exist?
93 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
94 return false;
95
96 // does the given entry exist?
97 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
98 return false;
99
100 QStringList list = eIter->second.split(';',SkipEmptyPartsIndependent);
101
102 // get dimension of requested vector
103 VectorT tmp;
104 int dim = tmp.dim();
105
106 if ( list.size() != dim ) {
107 std::cerr << "Differet size when reading Vector" << std::endl;
108 return false;
109 }
110
111 bool ok = true;
112 for ( int i = 0 ; i < dim; ++i) {
113 bool tmpOk = false;
114 _val[i] = (typename VectorT::value_type) list[i].toInt(&tmpOk);
115 ok &= tmpOk;
116 }
117
118 return ok;
119}
120
121// -----------------------------------------------------------------------------
122
124template < typename VectorT >
125bool
127get_entryVecd ( VectorT & _val ,
128 const QString & _section,
129 const QString & _key ) const
130{
131 SectionMap::const_iterator sIter;
132 EntryMap::const_iterator eIter;
133
134 // does the given section exist?
135 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
136 return false;
137
138 // does the given entry exist?
139 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
140 return false;
141
142 QStringList list = eIter->second.split(';',SkipEmptyPartsIndependent);
143
144 // get dimension of requested vector
145 VectorT tmp;
146 int dim = tmp.dim();
147
148 if ( list.size() != dim ) {
149 std::cerr << "Differet size when reading Vector" << std::endl;
150 return false;
151 }
152
153 bool ok = true;
154 for ( int i = 0 ; i < dim; ++i) {
155 bool tmpOk = false;
156 _val[i] = (typename VectorT::value_type) list[i].toDouble(&tmpOk);
157 ok &= tmpOk;
158 }
159
160 return ok;
161}
162
163// -----------------------------------------------------------------------------
164
166template < typename VectorT >
167bool
169get_entryVecf ( VectorT & _val ,
170 const QString & _section,
171 const QString & _key ) const
172{
173 SectionMap::const_iterator sIter;
174 EntryMap::const_iterator eIter;
175
176 // does the given section exist?
177 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
178 return false;
179
180 // does the given entry exist?
181 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
182 return false;
183
184
185 QStringList list = eIter->second.split(';',SkipEmptyPartsIndependent);
186
187
188 // get dimension of requested vector
189 VectorT tmp;
190 int dim = tmp.dim();
191
192 if ( list.size() != dim ) {
193 std::cerr << "Differet size when reading Vector" << std::endl;
194 return false;
195 }
196
197 bool ok = true;
198 for ( int i = 0 ; i < dim; ++i) {
199 bool tmpOk = false;
200 _val[i] = (typename VectorT::value_type) list[i].toFloat(&tmpOk);
201 ok &= tmpOk;
202 }
203
204 return ok;
205}
206
207// -----------------------------------------------------------------------------
208
210template < typename VectorT >
211void
213add_entryVec ( const QString & _section,
214 const QString & _key,
215 const VectorT & _value)
216{
217
218 // get dimension of stored vectors
219 VectorT tmp;
220 int dim = tmp.dim();
221
222 QString list;
223 for (int j = 0; j < dim; ++j)
224 list += QString::number( _value[j] ) + ";";
225
226 m_iniData[ _section ][ _key ] = list;
227}
228
229// -----------------------------------------------------------------------------
230
231template < typename VectorT >
232bool
234get_entryVecd ( std::vector< VectorT > & _val ,
235 const QString & _section,
236 const QString & _key ) const
237{
238 SectionMap::const_iterator sIter;
239 EntryMap::const_iterator eIter;
240
241 _val.clear();
242
243 // does the given section exist?
244 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
245 return false;
246
247 // does the given entry exist?
248 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
249 return false;
250
251 QStringList list = eIter->second.split(';',SkipEmptyPartsIndependent);
252
253 // get dimension of stored vectors
254 VectorT tmp;
255 int dim = tmp.dim();
256
257 bool ok = true;
258 for ( int i = 0 ; i < list.size(); )
259 {
260 if ( list[i].isEmpty() )
261 continue;
262 bool tmpOk = false;
263
264 std::vector<double> tmp;
265
266 for (int j = 0; j < dim; ++j)
267 {
268 // check data type
269 tmp.push_back( list[i].toDouble(&tmpOk) );
270 ++i;
271 }
272
273 VectorT vec;
274 for (int j = 0; j < dim; ++j)
275 vec[j] = (typename VectorT::value_type)(tmp[j]);
276
277 _val.push_back(vec);
278 ok &= tmpOk;
279 }
280
281 return ok;
282}
283
284
285// -----------------------------------------------------------------------------
286
287template < typename VectorT >
288bool
290get_entryVecf ( std::vector< VectorT > & _val ,
291 const QString & _section,
292 const QString & _key ) const
293{
294 SectionMap::const_iterator sIter;
295 EntryMap::const_iterator eIter;
296
297 _val.clear();
298
299 // does the given section exist?
300 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
301 return false;
302
303 // does the given entry exist?
304 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
305 return false;
306
307 QStringList list = eIter->second.split(';',SkipEmptyPartsIndependent);
308
309 // get dimension of stored vectors
310 VectorT tmp;
311 int dim = tmp.dim();
312
313 bool ok = true;
314 for ( int i = 0 ; i < list.size(); )
315 {
316 if ( list[i].isEmpty() )
317 continue;
318 bool tmpOk = false;
319
320 std::vector<double> tmp;
321
322 for (int j = 0; j < dim; ++j)
323 {
324 // check data type
325 tmp.push_back( list[i].toFloat(&tmpOk) );
326 ++i;
327 }
328
329 VectorT vec;
330 for (int j = 0; j < dim; ++j)
331 vec[j] = (typename VectorT::value_type)(tmp[j]);
332
333 _val.push_back(vec);
334 ok &= tmpOk;
335 }
336
337 return ok;
338}
339
340
341// -----------------------------------------------------------------------------
342
343template < typename VectorT >
344bool
346get_entryVeci ( std::vector< VectorT > & _val ,
347 const QString & _section,
348 const QString & _key ) const
349{
350 SectionMap::const_iterator sIter;
351 EntryMap::const_iterator eIter;
352
353 _val.clear();
354
355 // does the given section exist?
356 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
357 return false;
358
359 // does the given entry exist?
360 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
361 return false;
362
363 QStringList list = eIter->second.split(';',SkipEmptyPartsIndependent);
364
365 // get dimension of stored vectors
366 VectorT tmp;
367 int dim = tmp.dim();
368
369 bool ok = true;
370 for ( int i = 0 ; i < list.size(); )
371 {
372 if ( list[i].isEmpty() )
373 continue;
374 bool tmpOk = false;
375
376 std::vector<double> tmp;
377
378 for (int j = 0; j < dim; ++j)
379 {
380 // check data type
381 tmp.push_back( list[i].toInt(&tmpOk) );
382 ++i;
383 }
384
385 VectorT vec;
386 for (int j = 0; j < dim; ++j)
387 vec[j] = (typename VectorT::value_type)(tmp[j]);
388
389 _val.push_back(vec);
390 ok &= tmpOk;
391 }
392
393 return ok;
394}
395
396
397// -----------------------------------------------------------------------------
398
399
400template < typename VectorT >
401void
403add_entryVec ( const QString & _section,
404 const QString & _key,
405 const std::vector< VectorT > & _value)
406{
407 QString list;
408 typename std::vector< VectorT >::const_iterator viter;
409
410 VectorT tmp;
411
412 for(viter = _value.begin();viter!=_value.end();++viter)
413 {
414 for (int i = 0; i < tmp.dim(); ++i)
415 list += QString::number( (*viter)[i] ) + ";";
416 }
417
418 m_iniData[ _section ][ _key ] = list;
419}
bool get_entryVeci(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
bool get_entryVecd(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_d (double)
bool get_entryVecf(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
void add_entryVec(const QString &_section, const QString &_key, const VectorT &_value)
Addition of a Vec_n_something.
SectionMap m_iniData
Stored data of an INI file.
Definition INIFile.hh:377