73 QFile inputFile( _filename );
79 if ( inputFile.exists() && inputFile.open(QIODevice::ReadOnly | QIODevice::Text) ) {
89 }
else if( _create ) {
93 QFile outputFile(_filename);
95 if ( outputFile.open( QIODevice::Text |
96 QIODevice::WriteOnly |
97 QIODevice::Truncate ) ) {
105 std::cerr <<
"Unable to create File : " << std::endl;
111 std::cerr <<
"Unable to open File : " << std::endl;
133 QString line, section;
134 bool inSection =
false;
137 while( !_inputStream.atEnd() ) {
140 QByteArray lineBuffer = _inputStream.readLine();
141 line = QString(lineBuffer);
142 line = line.trimmed();
144 if( line.isEmpty() || line[ 0 ] ==
'#' )
148 if( line[ 0 ] ==
'[' && line[ line.length()-1 ] ==
']' ) {
151 section = line.mid( 1, line.length() - 2 );
154 }
else if( inSection ) {
159 pos = line.indexOf(
'=' );
163 key = line.mid( 0, pos );
165 value = line.mid( pos + 1, line.length() - 1 );
166 value = value.trimmed();
168 if( key.isEmpty() || value.isEmpty() )
193 if ( ! outputFile.open( QIODevice::WriteOnly ) )
196 QTextStream out(&outputFile);
199 SectionMap::const_iterator sIter, sEnd;
200 EntryMap::const_iterator eIter, eEnd;
203 for( sIter =
m_iniData.begin(); sIter != sEnd; ++sIter ) {
205 out <<
"[" << sIter->first <<
"]\n";
207 eEnd = sIter->second.end();
208 for( eIter = sIter->second.begin(); eIter != eEnd; ++eIter ) {
209 out << eIter->first <<
"=";
210 out << eIter->second;
238 static SectionMap::const_iterator mapIter;
241 && mapIter->second.find( _key ) != mapIter->second.end() );
258 const QString & _key,
259 const QString & _value )
269 const QString & _key,
270 const double & _value)
272 m_iniData[ _section ][ _key ] = QString::number( _value );
280 const QString & _key,
281 const float & _value)
283 m_iniData[ _section ][ _key ] = QString::number( _value );
291 const QString & _key ,
294 m_iniData[ _section ][ _key ] = QString::number( _value );
302 const QString & _key ,
303 const unsigned int & _value)
305 m_iniData[ _section ][ _key ] = QString::number( _value );
313 const QString & _key ,
316 m_iniData[ _section ][ _key ] = (_value ?
"true" :
"false");
324 const QString & _key,
325 const std::vector<float> & _value)
328 std::vector<float>::const_iterator viter;
329 for(viter = _value.begin();viter!=_value.end();++viter)
330 list += QString::number( *viter ) +
";";
339 const QString & _key,
340 const std::vector<double> & _value)
343 std::vector<double>::const_iterator viter;
344 for(viter = _value.begin();viter!=_value.end();++viter)
345 list += QString::number( *viter ) +
";";
354 const QString & _key,
355 const std::vector<bool> & _value)
358 std::vector<bool>::const_iterator viter;
359 for(viter = _value.begin();viter!=_value.end();++viter){
372 const QString & _key,
373 const std::vector<int> & _value)
376 std::vector<int>::const_iterator viter;
377 for(viter = _value.begin();viter!=_value.end();++viter)
378 list += QString::number( *viter ) +
";";
385 const QString & _key,
386 const std::vector<QString> & _value)
389 std::vector<QString>::const_iterator viter;
390 for(viter = _value.begin();viter!=_value.end();++viter) {
391 list += *viter +
";";
399 const QString & _key,
400 const QStringList & _value)
402 QString list = _value.join(
";");
411 SectionMap::iterator sIter;
412 EntryMap::iterator eIter;
417 if( (eIter = sIter->second.find( _key )) != sIter->second.end() )
418 sIter->second.erase( eIter );
434 const QString & _section,
435 const QString & _key )
const 437 SectionMap::const_iterator sIter;
438 EntryMap::const_iterator eIter;
445 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
448 _val = eIter->second;
457 const QString & _section,
458 const QString & _key )
const 460 SectionMap::const_iterator sIter;
461 EntryMap::const_iterator eIter;
468 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
472 _val = eIter->second.toDouble(&ok);
481 const QString & _section,
482 const QString & _key )
const 484 SectionMap::const_iterator sIter;
485 EntryMap::const_iterator eIter;
492 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
496 _val = eIter->second.toFloat(&ok);
505 const QString & _section,
506 const QString & _key )
const 508 SectionMap::const_iterator sIter;
509 EntryMap::const_iterator eIter;
516 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
520 _val = eIter->second.toInt(&ok);
528 const QString & _section,
529 const QString & _key )
const 531 SectionMap::const_iterator sIter;
532 EntryMap::const_iterator eIter;
539 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
543 _val = eIter->second.toUInt(&ok);
552 const QString & _section,
553 const QString & _key)
const 555 SectionMap::const_iterator sIter;
556 EntryMap::const_iterator eIter;
563 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
566 if( eIter->second ==
"true" || eIter->second ==
"false" ) {
567 _val = (eIter->second ==
"true");
579 const QString & _section,
580 const QString & _key )
const 582 SectionMap::const_iterator sIter;
583 EntryMap::const_iterator eIter;
592 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
595 QStringList list = eIter->second.split(
';');
598 for (
int i = 0 ; i < list.size(); ++i) {
599 if ( list[i].isEmpty() )
602 _val.push_back(list[i].toFloat(&tmpOk));
614 const QString & _section,
615 const QString & _key )
const 617 SectionMap::const_iterator sIter;
618 EntryMap::const_iterator eIter;
627 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
630 QStringList list = eIter->second.split(
';');
633 for (
int i = 0 ; i < list.size(); ++i) {
634 if ( list[i].isEmpty() )
637 _val.push_back(list[i].toDouble(&tmpOk));
649 const QString & _section,
650 const QString & _key )
const 652 SectionMap::const_iterator sIter;
653 EntryMap::const_iterator eIter;
662 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
665 QStringList list = eIter->second.split(
';');
668 for (
int i = 0 ; i < list.size(); ++i) {
669 if ( list[i].isEmpty() )
671 if (list[i] ==
"true")
672 _val.push_back(
true);
674 _val.push_back(
false);
685 const QString & _section,
686 const QString & _key )
const 688 SectionMap::const_iterator sIter;
689 EntryMap::const_iterator eIter;
698 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
701 QStringList list = eIter->second.split(
';');
704 for (
int i = 0 ; i < list.size(); ++i) {
705 if ( list[i].isEmpty() )
708 _val.push_back(list[i].toInt(&tmpOk));
720 const QString & _section,
721 const QString & _key )
const 723 SectionMap::const_iterator sIter;
724 EntryMap::const_iterator eIter;
733 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
736 QStringList list = eIter->second.split(
';');
739 for (
int i = 0 ; i < list.size(); ++i) {
740 if ( list[i].isEmpty() )
742 _val.push_back(list[i]);
753 const QString & _section,
754 const QString & _key )
const 756 SectionMap::const_iterator sIter;
757 EntryMap::const_iterator eIter;
766 if( (eIter = sIter->second.find( _key )) == sIter->second.end() )
769 _val = eIter->second.split(
';');
772 if ( _val.isEmpty() )
INIFile()
Default constructor.
bool mf_isConnected
Flag: this object is connected to an INI file.
bool section_exists(const QString &_section) const
Check if given section exists in the current INI file.
bool is_connected() const
Check if object is connected to file.
bool parseFile(QFile &_inputStream)
Read content of an INI file.
bool connect(const QString &name, const bool create)
Connect INIFile object with given filename.
std::map< QString, QString > EntryMap
Type for map of contained entries.
void add_section(const QString &_sectionname)
Addition of a section.
QString m_filename
Name of current INI file.
SectionMap m_iniData
Stored data of an INI file.
void add_entry(const QString &_section, const QString &_key, const QString &_value)
Addition / modification of a string entry.
void delete_entry(const QString &_section, const QString &_key)
Deletion of an entry.
bool entry_exists(const QString &_section, const QString &_key) const
Check if given entry esists in the current INI file.
void disconnect()
Remove connection of this object to a file.
void delete_section(const QString &_sectionname)
Deletion of an entire section.
bool writeFile(void)
Write data to file we are currently connected to.
bool get_entry(QString &_val, const QString &_section, const QString &_key) const
Access to a string entry.