Back to index

salome-gui  6.5.0
Public Member Functions | Protected Member Functions | Private Member Functions
QtxResourceMgr::IniFormat Class Reference

Reader/writer for .ini resources files. More...

Inheritance diagram for QtxResourceMgr::IniFormat:
Inheritance graph
[legend]
Collaboration diagram for QtxResourceMgr::IniFormat:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 IniFormat ()
 Constructor.
 ~IniFormat ()
 Destructor.

Protected Member Functions

virtual bool load (const QString &, QMap< QString, Section > &)
 Load resources from ini-file.
virtual bool save (const QString &, const QMap< QString, Section > &)
 Save resources to the ini-file.

Private Member Functions

bool load (const QString &, QMap< QString, Section > &, QSet< QString > &)
 Load resources from xml-file.

Detailed Description

Reader/writer for .ini resources files.

Definition at line 461 of file QtxResourceMgr.cxx.


Constructor & Destructor Documentation

Constructor.

Definition at line 478 of file QtxResourceMgr.cxx.

: Format( "ini" )
{
}

Destructor.

Definition at line 486 of file QtxResourceMgr.cxx.

{
}

Member Function Documentation

bool QtxResourceMgr::IniFormat::load ( const QString &  fname,
QMap< QString, Section > &  secMap 
) [protected, virtual]

Load resources from ini-file.

Parameters:
fnameresources file name
secMapresources map to be filled in
Returns:
true on success and false on error

Definition at line 496 of file QtxResourceMgr.cxx.

{
  QSet<QString> importHistory;
  return load( fname, secMap, importHistory );
}
bool QtxResourceMgr::IniFormat::load ( const QString &  fname,
QMap< QString, Section > &  secMap,
QSet< QString > &  importHistory 
) [private]

Load resources from xml-file.

Parameters:
fnameresources file name
secMapresources map to be filled in
importHistorylist of already imported resources files (to prevent import loops)
Returns:
true on success or false on error

Definition at line 510 of file QtxResourceMgr.cxx.

{
  QString aFName = fname.trimmed();
  if ( !QFileInfo( aFName ).exists() )
  {
    if ( QFileInfo( aFName + ".ini" ).exists() )
      aFName += ".ini";
    else if ( QFileInfo( aFName + ".INI" ).exists() )
      aFName += ".INI";
    else
      return false; // file does not exist
  }
  QFileInfo aFinfo( aFName );
  aFName = aFinfo.canonicalFilePath();

  if ( !importHistory.contains( aFName ) )
    importHistory.insert( aFName );
  else
    return true;   // already imported (prevent import loops)

  QFile file( aFName );
  if ( !file.open( QFile::ReadOnly ) )
    return false;  // file is not accessible

  QTextStream ts( &file );

  QString data;
  int line = 0;
  bool res = true;
  QString section;

  QString separator = option( "separator" );
  if ( separator.isNull() )
    separator = QString( "=" );

  QString comment = option( "comment" );
  if ( comment.isNull() )
    comment = QString( "#" );

  while ( true )
  {
    data = ts.readLine();
    line++;

    if ( data.isNull() )
      break;

    data = data.trimmed();
    if ( data.isEmpty() )
      continue;

    if ( data.startsWith( comment ) )
      continue;

    QRegExp rx( "^\\[([\\w\\s\\._]*)\\]$" );
    if ( rx.indexIn( data ) != -1 )
    {
      section = rx.cap( 1 );
      if ( section.isEmpty() )
      {
        res = false;
        qWarning() << "QtxResourceMgr: Empty section in line:" << line;
      }
    }
    else if ( data.contains( separator ) && !section.isEmpty() )
    {
      int pos = data.indexOf( separator );
      QString key = data.left( pos ).trimmed();
      QString val = data.mid( pos + 1 ).trimmed();
      secMap[section].insert( key, val );
    }
    else if ( section == "import" )
    {
      QString impFile = QDir::convertSeparators( Qtx::makeEnvVarSubst( data, Qtx::Always ) );
      QFileInfo impFInfo( impFile );
      if ( impFInfo.isRelative() )
             impFInfo.setFile( aFinfo.absoluteDir(), impFile );
    
      QMap<QString, Section> impMap;
      if ( !load( impFInfo.absoluteFilePath(), impMap, importHistory ) )
      {
        qDebug() << "QtxResourceMgr: Error with importing file:" << data;
      }
      else 
      {
             QMap<QString, Section>::const_iterator it = impMap.constBegin();
             for ( ; it != impMap.constEnd() ; ++it )
             { 
                if ( !secMap.contains( it.key() ) )
                {
                   // insert full section
                   secMap.insert( it.key(), it.value() );
                }
                else
                {
                   // insert all parameters from the section
                   Section::ConstIterator paramIt = it.value().begin();
                   for ( ; paramIt != it.value().end() ; ++paramIt )
                   {
                      if ( !secMap[it.key()].contains( paramIt.key() ) )
                             secMap[it.key()].insert( paramIt.key(), paramIt.value() );
                   }
                }
             }
      }
    }
    else
    {
      res = false;
      if ( section.isEmpty() )
             qWarning() << "QtxResourceMgr: Current section is empty";
      else
             qWarning() << "QtxResourceMgr: Error in line:" << line;
    }
  }

  file.close();

  return res; 
}
bool QtxResourceMgr::IniFormat::save ( const QString &  fname,
const QMap< QString, Section > &  secMap 
) [protected, virtual]

Save resources to the ini-file.

Parameters:
fnameresources file name
secMapresources map
Returns:
true on success and false on error

Definition at line 637 of file QtxResourceMgr.cxx.

{
  if ( !Qtx::mkDir( QFileInfo( fname ).absolutePath() ) )
    return false;

  QFile file( fname );
  if ( !file.open( QFile::WriteOnly ) )
    return false;

  QTextStream ts( &file );

  bool res = true;
  for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
  {
    QStringList data( QString( "[%1]" ).arg( it.key() ) );
    for ( Section::ConstIterator iter = it.value().begin(); iter != it.value().end(); ++iter )
      data.append( iter.key() + " = " + iter.value() );
    data.append( "" );

    for ( QStringList::ConstIterator itr = data.begin(); itr != data.end(); ++itr )
      ts << *itr << endl;
  }

  file.close();

  return res;
}

The documentation for this class was generated from the following file: