INIFileT.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 8520 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2010-02-10 15:56:59 +0100 (Mi, 10. Feb 2010) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS INIFile Templates - IMPLEMENTATION
00049 //
00050 //=============================================================================
00051 
00052 #define INIFILE_C
00053 
00054 //== INCLUDES =================================================================
00055 
00056 #include "INIFile.hh"
00057 
00058 //std include
00059 #include <fstream>
00060 #include <iostream>
00061 #include <functional>
00062 #include <sstream>
00063 #include <cstring>
00064 #include <cctype>
00065 //#include <ios>
00066 
00067 #include <QString>
00068 #include <QFile>
00069 #include <QTextStream>
00070 #include <QStringList>
00071 
00072 //== IMPLEMENTATION ==========================================================
00073 
00075 template < typename VectorT >
00076 bool
00077 INIFile::
00078 get_entryVeci ( VectorT & _val ,
00079                 const QString & _section,
00080                 const QString & _key ) const
00081 {
00082   SectionMap::const_iterator sIter;
00083   EntryMap::const_iterator eIter;
00084 
00085   // does the given section exist?
00086   if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
00087     return false;
00088 
00089   // does the given entry exist?
00090   if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
00091     return false;
00092 
00093   QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
00094 
00095   // get dimension of requested vector
00096   VectorT tmp;
00097   int dim = tmp.dim();
00098 
00099   if ( list.size() != dim ) {
00100     std::cerr << "Differet size when reading Vector" << std::endl;
00101     return false;
00102   }
00103 
00104   bool ok = true;
00105   for ( int i = 0 ; i < dim; ++i) {
00106     bool tmpOk = false;
00107     _val[i] = (typename VectorT::value_type) list[i].toInt(&tmpOk);
00108     ok &= tmpOk;
00109   }
00110 
00111   return ok;
00112 }
00113 
00114 // -----------------------------------------------------------------------------
00115 
00117 template < typename VectorT >
00118 bool
00119 INIFile::
00120 get_entryVecd ( VectorT & _val ,
00121                 const QString & _section,
00122                 const QString & _key ) const
00123 {
00124   SectionMap::const_iterator sIter;
00125   EntryMap::const_iterator eIter;
00126 
00127   // does the given section exist?
00128   if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
00129     return false;
00130 
00131   // does the given entry exist?
00132   if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
00133     return false;
00134 
00135   QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
00136 
00137   // get dimension of requested vector
00138   VectorT tmp;
00139   int dim = tmp.dim();
00140 
00141   if ( list.size() != dim ) {
00142     std::cerr << "Differet size when reading Vector" << std::endl;
00143     return false;
00144   }
00145 
00146   bool ok = true;
00147   for ( int i = 0 ; i < dim; ++i) {
00148     bool tmpOk = false;
00149     _val[i] = (typename VectorT::value_type) list[i].toDouble(&tmpOk);
00150     ok &= tmpOk;
00151   }
00152 
00153   return ok;
00154 }
00155 
00156 // -----------------------------------------------------------------------------
00157 
00159 template < typename VectorT >
00160 bool
00161 INIFile::
00162 get_entryVecf ( VectorT & _val ,
00163                 const QString & _section,
00164                 const QString & _key ) const
00165 {
00166   SectionMap::const_iterator sIter;
00167   EntryMap::const_iterator eIter;
00168 
00169   // does the given section exist?
00170   if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
00171     return false;
00172 
00173   // does the given entry exist?
00174   if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
00175     return false;
00176 
00177   QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
00178 
00179   // get dimension of requested vector
00180   VectorT tmp;
00181   int dim = tmp.dim();
00182 
00183   if ( list.size() != dim ) {
00184     std::cerr << "Differet size when reading Vector" << std::endl;
00185     return false;
00186   }
00187 
00188   bool ok = true;
00189   for ( int i = 0 ; i < dim; ++i) {
00190     bool tmpOk = false;
00191     _val[i] = (typename VectorT::value_type) list[i].toFloat(&tmpOk);
00192     ok &= tmpOk;
00193   }
00194 
00195   return ok;
00196 }
00197 
00198 // -----------------------------------------------------------------------------
00199 
00201 template < typename VectorT >
00202 void
00203 INIFile::
00204 add_entryVec ( const QString & _section,
00205                const QString & _key,
00206                const VectorT  & _value)
00207 {
00208 
00209   // get dimension of stored vectors
00210   VectorT tmp;
00211   int dim = tmp.dim();
00212 
00213   QString list;
00214   for (int j = 0; j < dim; ++j)
00215     list +=  QString::number( _value[j] ) + ";";
00216 
00217   m_iniData[ _section ][ _key ] = list;
00218 }
00219 
00220 // -----------------------------------------------------------------------------
00221 
00222 template < typename VectorT >
00223 bool
00224 INIFile::
00225 get_entryVecd ( std::vector< VectorT > & _val ,
00226                 const QString & _section,
00227                 const QString & _key ) const
00228 {
00229   SectionMap::const_iterator sIter;
00230   EntryMap::const_iterator eIter;
00231 
00232   _val.clear();
00233 
00234   // does the given section exist?
00235   if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
00236     return false;
00237 
00238   // does the given entry exist?
00239   if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
00240     return false;
00241 
00242   QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
00243 
00244   // get dimension of stored vectors
00245   VectorT tmp;
00246   int dim = tmp.dim();
00247 
00248   bool ok = true;
00249   for ( int i = 0 ; i < list.size(); )
00250   {
00251     if ( list[i].isEmpty() )
00252       continue;
00253     bool tmpOk = false;
00254 
00255     std::vector<double> tmp;
00256 
00257     for (int j = 0; j < dim; ++j)
00258     {
00259       // check data type
00260       tmp.push_back( list[i].toDouble(&tmpOk) );
00261       ++i;
00262     }
00263 
00264     VectorT vec;
00265     for (int j = 0; j < dim; ++j)
00266       vec[j] = (typename VectorT::value_type)(tmp[j]);
00267 
00268     _val.push_back(vec);
00269     ok &= tmpOk;
00270   }
00271 
00272   return ok;
00273 }
00274 
00275 
00276 // -----------------------------------------------------------------------------
00277 
00278 template < typename VectorT >
00279 bool
00280 INIFile::
00281 get_entryVecf ( std::vector< VectorT > & _val ,
00282                 const QString & _section,
00283                 const QString & _key ) const
00284 {
00285   SectionMap::const_iterator sIter;
00286   EntryMap::const_iterator eIter;
00287 
00288   _val.clear();
00289 
00290   // does the given section exist?
00291   if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
00292     return false;
00293 
00294   // does the given entry exist?
00295   if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
00296     return false;
00297 
00298   QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
00299 
00300   // get dimension of stored vectors
00301   VectorT tmp;
00302   int dim = tmp.dim();
00303 
00304   bool ok = true;
00305   for ( int i = 0 ; i < list.size(); )
00306   {
00307     if ( list[i].isEmpty() )
00308       continue;
00309     bool tmpOk = false;
00310 
00311     std::vector<double> tmp;
00312 
00313     for (int j = 0; j < dim; ++j)
00314     {
00315       // check data type
00316       tmp.push_back( list[i].toFloat(&tmpOk) );
00317       ++i;
00318     }
00319 
00320     VectorT vec;
00321     for (int j = 0; j < dim; ++j)
00322       vec[j] = (typename VectorT::value_type)(tmp[j]);
00323 
00324     _val.push_back(vec);
00325     ok &= tmpOk;
00326   }
00327 
00328   return ok;
00329 }
00330 
00331 
00332 // -----------------------------------------------------------------------------
00333 
00334 template < typename VectorT >
00335 bool
00336 INIFile::
00337 get_entryVeci ( std::vector< VectorT > & _val ,
00338                 const QString & _section,
00339                 const QString & _key ) const
00340 {
00341   SectionMap::const_iterator sIter;
00342   EntryMap::const_iterator eIter;
00343 
00344   _val.clear();
00345 
00346   // does the given section exist?
00347   if( (sIter = m_iniData.find( _section )) == m_iniData.end() )
00348     return false;
00349 
00350   // does the given entry exist?
00351   if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
00352     return false;
00353 
00354   QStringList list = eIter->second.split(';',QString::SkipEmptyParts);
00355 
00356   // get dimension of stored vectors
00357   VectorT tmp;
00358   int dim = tmp.dim();
00359 
00360   bool ok = true;
00361   for ( int i = 0 ; i < list.size(); )
00362   {
00363     if ( list[i].isEmpty() )
00364       continue;
00365     bool tmpOk = false;
00366 
00367     std::vector<double> tmp;
00368 
00369     for (int j = 0; j < dim; ++j)
00370     {
00371       // check data type
00372       tmp.push_back( list[i].toInt(&tmpOk) );
00373       ++i;
00374     }
00375 
00376     VectorT vec;
00377     for (int j = 0; j < dim; ++j)
00378       vec[j] = (typename VectorT::value_type)(tmp[j]);
00379 
00380     _val.push_back(vec);
00381     ok &= tmpOk;
00382   }
00383 
00384   return ok;
00385 }
00386 
00387 
00388 // -----------------------------------------------------------------------------
00389 
00390 
00391 template < typename VectorT >
00392 void
00393 INIFile::
00394 add_entryVec ( const QString & _section,
00395                const QString & _key,
00396                const std::vector< VectorT > & _value)
00397 {
00398   QString list;
00399   typename std::vector< VectorT >::const_iterator viter;
00400 
00401   VectorT tmp;
00402 
00403   for(viter = _value.begin();viter!=_value.end();++viter)
00404   {
00405     for (int i = 0; i < tmp.dim(); ++i)
00406       list += QString::number( (*viter)[i] ) + ";";
00407   }
00408 
00409   m_iniData[ _section ][ _key ] = list;
00410 }

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .