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
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 
179 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
180  QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
181 #else
182  QStringList list = eIter->second.split(';',Qt::SkipEmptyParts);
183 #endif
184 
185 
186  // get dimension of requested vector
187  VectorT tmp;
188  int dim = tmp.dim();
189 
190  if ( list.size() != dim ) {
191  std::cerr << "Differet size when reading Vector" << std::endl;
192  return false;
193  }
194 
195  bool ok = true;
196  for ( int i = 0 ; i < dim; ++i) {
197  bool tmpOk = false;
198  _val[i] = (typename VectorT::value_type) list[i].toFloat(&tmpOk);
199  ok &= tmpOk;
200  }
201 
202  return ok;
203 }
204 
205 // -----------------------------------------------------------------------------
206 
208 template < typename VectorT >
209 void
210 INIFile::
211 add_entryVec ( const QString & _section,
212  const QString & _key,
213  const VectorT & _value)
214 {
215 
216  // get dimension of stored vectors
217  VectorT tmp;
218  int dim = tmp.dim();
219 
220  QString list;
221  for (int j = 0; j < dim; ++j)
222  list += QString::number( _value[j] ) + ";";
223 
224  m_iniData[ _section ][ _key ] = list;
225 }
226 
227 // -----------------------------------------------------------------------------
228 
229 template < typename VectorT >
230 bool
231 INIFile::
232 get_entryVecd ( std::vector< VectorT > & _val ,
233  const QString & _section,
234  const QString & _key ) const
235 {
236  SectionMap::const_iterator sIter;
237  EntryMap::const_iterator eIter;
238 
239  _val.clear();
240 
241  // does the given section exist?
242  if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
243  return false;
244 
245  // does the given entry exist?
246  if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
247  return false;
248 
249  QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
250 
251  // get dimension of stored vectors
252  VectorT tmp;
253  int dim = tmp.dim();
254 
255  bool ok = true;
256  for ( int i = 0 ; i < list.size(); )
257  {
258  if ( list[i].isEmpty() )
259  continue;
260  bool tmpOk = false;
261 
262  std::vector<double> tmp;
263 
264  for (int j = 0; j < dim; ++j)
265  {
266  // check data type
267  tmp.push_back( list[i].toDouble(&tmpOk) );
268  ++i;
269  }
270 
271  VectorT vec;
272  for (int j = 0; j < dim; ++j)
273  vec[j] = (typename VectorT::value_type)(tmp[j]);
274 
275  _val.push_back(vec);
276  ok &= tmpOk;
277  }
278 
279  return ok;
280 }
281 
282 
283 // -----------------------------------------------------------------------------
284 
285 template < typename VectorT >
286 bool
287 INIFile::
288 get_entryVecf ( std::vector< VectorT > & _val ,
289  const QString & _section,
290  const QString & _key ) const
291 {
292  SectionMap::const_iterator sIter;
293  EntryMap::const_iterator eIter;
294 
295  _val.clear();
296 
297  // does the given section exist?
298  if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
299  return false;
300 
301  // does the given entry exist?
302  if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
303  return false;
304 
305  QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
306 
307  // get dimension of stored vectors
308  VectorT tmp;
309  int dim = tmp.dim();
310 
311  bool ok = true;
312  for ( int i = 0 ; i < list.size(); )
313  {
314  if ( list[i].isEmpty() )
315  continue;
316  bool tmpOk = false;
317 
318  std::vector<double> tmp;
319 
320  for (int j = 0; j < dim; ++j)
321  {
322  // check data type
323  tmp.push_back( list[i].toFloat(&tmpOk) );
324  ++i;
325  }
326 
327  VectorT vec;
328  for (int j = 0; j < dim; ++j)
329  vec[j] = (typename VectorT::value_type)(tmp[j]);
330 
331  _val.push_back(vec);
332  ok &= tmpOk;
333  }
334 
335  return ok;
336 }
337 
338 
339 // -----------------------------------------------------------------------------
340 
341 template < typename VectorT >
342 bool
343 INIFile::
344 get_entryVeci ( std::vector< VectorT > & _val ,
345  const QString & _section,
346  const QString & _key ) const
347 {
348  SectionMap::const_iterator sIter;
349  EntryMap::const_iterator eIter;
350 
351  _val.clear();
352 
353  // does the given section exist?
354  if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
355  return false;
356 
357  // does the given entry exist?
358  if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
359  return false;
360 
361  QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
362 
363  // get dimension of stored vectors
364  VectorT tmp;
365  int dim = tmp.dim();
366 
367  bool ok = true;
368  for ( int i = 0 ; i < list.size(); )
369  {
370  if ( list[i].isEmpty() )
371  continue;
372  bool tmpOk = false;
373 
374  std::vector<double> tmp;
375 
376  for (int j = 0; j < dim; ++j)
377  {
378  // check data type
379  tmp.push_back( list[i].toInt(&tmpOk) );
380  ++i;
381  }
382 
383  VectorT vec;
384  for (int j = 0; j < dim; ++j)
385  vec[j] = (typename VectorT::value_type)(tmp[j]);
386 
387  _val.push_back(vec);
388  ok &= tmpOk;
389  }
390 
391  return ok;
392 }
393 
394 
395 // -----------------------------------------------------------------------------
396 
397 
398 template < typename VectorT >
399 void
400 INIFile::
401 add_entryVec ( const QString & _section,
402  const QString & _key,
403  const std::vector< VectorT > & _value)
404 {
405  QString list;
406  typename std::vector< VectorT >::const_iterator viter;
407 
408  VectorT tmp;
409 
410  for(viter = _value.begin();viter!=_value.end();++viter)
411  {
412  for (int i = 0; i < tmp.dim(); ++i)
413  list += QString::number( (*viter)[i] ) + ";";
414  }
415 
416  m_iniData[ _section ][ _key ] = list;
417 }
bool get_entryVecf(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
SectionMap m_iniData
Stored data of an INI file.
Definition: INIFile.hh:377
bool get_entryVecd(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_d (double)
void add_entryVec(const QString &_section, const QString &_key, const VectorT &_value)
Addition of a Vec_n_something.
bool get_entryVeci(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)