Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
INIFileT.cc
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS INIFile Templates - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 #define INIFILE_C
60 
61 //== INCLUDES =================================================================
62 
63 #include "INIFile.hh"
64 
65 //std include
66 #include <fstream>
67 #include <iostream>
68 #include <functional>
69 #include <sstream>
70 #include <cstring>
71 #include <cctype>
72 //#include <ios>
73 
74 #include <QString>
75 #include <QFile>
76 #include <QTextStream>
77 #include <QStringList>
78 
79 //== IMPLEMENTATION ==========================================================
80 
82 template < typename VectorT >
83 bool
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(';',QString::SkipEmptyParts);
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 
124 template < typename VectorT >
125 bool
126 INIFile::
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(';',QString::SkipEmptyParts);
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 
166 template < typename VectorT >
167 bool
168 INIFile::
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  QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
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 }
void add_entryVec(const QString &_section, const QString &_key, const VectorT &_value)
Addition of a Vec_n_something.
Definition: INIFileT.cc:211
bool get_entryVeci(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
Definition: INIFileT.cc:85
bool get_entryVecd(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_d (double)
Definition: INIFileT.cc:127
SectionMap m_iniData
Stored data of an INI file.
Definition: INIFile.hh:383
bool get_entryVecf(VectorT &_val, const QString &_section, const QString &_key) const
Get a Vec_n_i (int)
Definition: INIFileT.cc:169