Developer Documentation

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 //== IMPLEMENTATION ==========================================================
74
76 template < typename VectorT >
77 bool
78 INIFile::
79 get_entryVeci ( VectorT & _val ,
80 const QString & _section,
81 const QString & _key ) const
82 {
83 SectionMap::const_iterator sIter;
84 EntryMap::const_iterator eIter;
85
86 // does the given section exist?
87 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
88 return false;
89
90 // does the given entry exist?
91 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
92 return false;
93
94 QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
95
96 // get dimension of requested vector
97 VectorT tmp;
98 int dim = tmp.dim();
99
100 if ( list.size() != dim ) {
101 std::cerr << "Differet size when reading Vector" << std::endl;
102 return false;
103 }
104
105 bool ok = true;
106 for ( int i = 0 ; i < dim; ++i) {
107 bool tmpOk = false;
108 _val[i] = (typename VectorT::value_type) list[i].toInt(&tmpOk);
109 ok &= tmpOk;
110 }
111
112 return ok;
113 }
114
115 // -----------------------------------------------------------------------------
116
118 template < typename VectorT >
119 bool
120 INIFile::
121 get_entryVecd ( VectorT & _val ,
122 const QString & _section,
123 const QString & _key ) const
124 {
125 SectionMap::const_iterator sIter;
126 EntryMap::const_iterator eIter;
127
128 // does the given section exist?
129 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
130 return false;
131
132 // does the given entry exist?
133 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
134 return false;
135
136 QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
137
138 // get dimension of requested vector
139 VectorT tmp;
140 int dim = tmp.dim();
141
142 if ( list.size() != dim ) {
143 std::cerr << "Differet size when reading Vector" << std::endl;
144 return false;
145 }
146
147 bool ok = true;
148 for ( int i = 0 ; i < dim; ++i) {
149 bool tmpOk = false;
150 _val[i] = (typename VectorT::value_type) list[i].toDouble(&tmpOk);
151 ok &= tmpOk;
152 }
153
154 return ok;
155 }
156
157 // -----------------------------------------------------------------------------
158
160 template < typename VectorT >
161 bool
162 INIFile::
163 get_entryVecf ( VectorT & _val ,
164 const QString & _section,
165 const QString & _key ) const
166 {
167 SectionMap::const_iterator sIter;
168 EntryMap::const_iterator eIter;
169
170 // does the given section exist?
171 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
172 return false;
173
174 // does the given entry exist?
175 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
176 return false;
177
178 QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
179
180 // get dimension of requested vector
181 VectorT tmp;
182 int dim = tmp.dim();
183
184 if ( list.size() != dim ) {
185 std::cerr << "Differet size when reading Vector" << std::endl;
186 return false;
187 }
188
189 bool ok = true;
190 for ( int i = 0 ; i < dim; ++i) {
191 bool tmpOk = false;
192 _val[i] = (typename VectorT::value_type) list[i].toFloat(&tmpOk);
193 ok &= tmpOk;
194 }
195
196 return ok;
197 }
198
199 // -----------------------------------------------------------------------------
200
202 template < typename VectorT >
203 void
204 INIFile::
205 add_entryVec ( const QString & _section,
206 const QString & _key,
207 const VectorT & _value)
208 {
209
210 // get dimension of stored vectors
211 VectorT tmp;
212 int dim = tmp.dim();
213
214 QString list;
215 for (int j = 0; j < dim; ++j)
216 list += QString::number( _value[j] ) + ";";
217
218 m_iniData[ _section ][ _key ] = list;
219 }
220
221 // -----------------------------------------------------------------------------
222
223 template < typename VectorT >
224 bool
225 INIFile::
226 get_entryVecd ( std::vector< VectorT > & _val ,
227 const QString & _section,
228 const QString & _key ) const
229 {
230 SectionMap::const_iterator sIter;
231 EntryMap::const_iterator eIter;
232
233 _val.clear();
234
235 // does the given section exist?
236 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
237 return false;
238
239 // does the given entry exist?
240 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
241 return false;
242
243 QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
244
245 // get dimension of stored vectors
246 VectorT tmp;
247 int dim = tmp.dim();
248
249 bool ok = true;
250 for ( int i = 0 ; i < list.size(); )
251 {
252 if ( list[i].isEmpty() )
253 continue;
254 bool tmpOk = false;
255
256 std::vector<double> tmp;
257
258 for (int j = 0; j < dim; ++j)
259 {
260 // check data type
261 tmp.push_back( list[i].toDouble(&tmpOk) );
262 ++i;
263 }
264
265 VectorT vec;
266 for (int j = 0; j < dim; ++j)
267 vec[j] = (typename VectorT::value_type)(tmp[j]);
268
269 _val.push_back(vec);
270 ok &= tmpOk;
271 }
272
273 return ok;
274 }
275
276
277 // -----------------------------------------------------------------------------
278
279 template < typename VectorT >
280 bool
281 INIFile::
282 get_entryVecf ( std::vector< VectorT > & _val ,
283 const QString & _section,
284 const QString & _key ) const
285 {
286 SectionMap::const_iterator sIter;
287 EntryMap::const_iterator eIter;
288
289 _val.clear();
290
291 // does the given section exist?
292 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
293 return false;
294
295 // does the given entry exist?
296 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
297 return false;
298
299 QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
300
301 // get dimension of stored vectors
302 VectorT tmp;
303 int dim = tmp.dim();
304
305 bool ok = true;
306 for ( int i = 0 ; i < list.size(); )
307 {
308 if ( list[i].isEmpty() )
309 continue;
310 bool tmpOk = false;
311
312 std::vector<double> tmp;
313
314 for (int j = 0; j < dim; ++j)
315 {
316 // check data type
317 tmp.push_back( list[i].toFloat(&tmpOk) );
318 ++i;
319 }
320
321 VectorT vec;
322 for (int j = 0; j < dim; ++j)
323 vec[j] = (typename VectorT::value_type)(tmp[j]);
324
325 _val.push_back(vec);
326 ok &= tmpOk;
327 }
328
329 return ok;
330 }
331
332
333 // -----------------------------------------------------------------------------
334
335 template < typename VectorT >
336 bool
337 INIFile::
338 get_entryVeci ( std::vector< VectorT > & _val ,
339 const QString & _section,
340 const QString & _key ) const
341 {
342 SectionMap::const_iterator sIter;
343 EntryMap::const_iterator eIter;
344
345 _val.clear();
346
347 // does the given section exist?
348 if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
349 return false;
350
351 // does the given entry exist?
352 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
353 return false;
354
355 QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
356
357 // get dimension of stored vectors
358 VectorT tmp;
359 int dim = tmp.dim();
360
361 bool ok = true;
362 for ( int i = 0 ; i < list.size(); )
363 {
364 if ( list[i].isEmpty() )
365 continue;
366 bool tmpOk = false;
367
368 std::vector<double> tmp;
369
370 for (int j = 0; j < dim; ++j)
371 {
372 // check data type
373 tmp.push_back( list[i].toInt(&tmpOk) );
374 ++i;
375 }
376
377 VectorT vec;
378 for (int j = 0; j < dim; ++j)
379 vec[j] = (typename VectorT::value_type)(tmp[j]);
380
381 _val.push_back(vec);
382 ok &= tmpOk;
383 }
384
385 return ok;
386 }
387
388
389 // -----------------------------------------------------------------------------
390
391
392 template < typename VectorT >
393 void
394 INIFile::
395 add_entryVec ( const QString & _section,
396 const QString & _key,
397 const std::vector< VectorT > & _value)
398 {
399 QString list;
400 typename std::vector< VectorT >::const_iterator viter;
401
402 VectorT tmp;
403
404 for(viter = _value.begin();viter!=_value.end();++viter)
405 {
406 for (int i = 0; i < tmp.dim(); ++i)
407 list += QString::number( (*viter)[i] ) + ";";
408 }
409
410 m_iniData[ _section ][ _key ] = list;
411 }
INIFile::get_entryVecd
bool get_entryVecd(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_d (double)
Definition: INIFileT_impl.hh:121
VectorT
INIFile::get_entryVecf
bool get_entryVecf(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
Definition: INIFileT_impl.hh:163
INIFile::get_entryVeci
bool get_entryVeci(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
Definition: INIFileT_impl.hh:79
INIFile::m_iniData
SectionMap m_iniData
Stored data of an INI file.
Definition: INIFile.hh:377
INIFile::add_entryVec
void add_entryVec(const QString &_section, const QString &_key, const VectorT &_value)
Addition of a Vec_n_something.
Definition: INIFileT_impl.hh:205