configuration/src/inifilesupport.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Tomassino Ferrauto                              *
00003  *   t_ferrauto@yahoo.it                                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "private/inifilesupport.h"
00022 #include "configurationparameters.h"
00023 #include <QtDebug>
00024 #include <QFile>
00025 #include <QRegExp>
00026 
00027 namespace farsa {
00028 
00029 IniFileLoaderSaver::IniFileLoaderSaver() :
00030     ParametersFileLoaderSaver()
00031 {
00032 }
00033 
00034 IniFileLoaderSaver::~IniFileLoaderSaver()
00035 {
00036 }
00037 
00038 bool IniFileLoaderSaver::saveParameters(QTextStream &stream, const ConfigurationParameters& configParams)
00039 {
00040     //--- write recursevly all groups starting from the root
00041     return writeGroupToStream( "", stream, configParams );
00042 }
00043 
00044 bool IniFileLoaderSaver::writeGroupToStream(QString groupPath, QTextStream &outstream, const ConfigurationParameters& configParams) {
00045     //--- write parameters first
00046     QStringList paramList = configParams.getParametersList( groupPath );
00047     foreach( QString param, paramList ) {
00048         QString value = configParams.getValue( groupPath + ConfigurationParameters::GroupSeparator() + param );
00049         if ( ! value.isEmpty() ) {
00050             outstream << param << " = " << value << "\n";
00051         }
00052     }
00053     //--- add an extra empty row for dividing sub-groups
00054     outstream << "\n";
00055     //--- and then all subgroups recursively
00056     QStringList groupList = configParams.getGroupsList( groupPath );
00057     foreach( QString group, groupList ) {
00058         //--- is there the responsability to write the group header
00059         //--- because in this way we can avoid to write the [ROOT] for root node
00060         QString subgroup = groupPath + ConfigurationParameters::GroupSeparator() + group;
00061         outstream << "[" << subgroup.section('/',1) << "]" << "\n";
00062         writeGroupToStream( subgroup, outstream, configParams );
00063     }
00064     return true;
00065 }
00066 
00067 bool IniFileLoaderSaver::loadParameters(QTextStream &stream, ConfigurationParameters& configParams)
00068 {
00069     // Regular expressions for comment, group and parameters
00070     QRegExp comment("([#;]+.*)");
00071     QRegExp group("\\[(.*)\\]");
00072     QRegExp param("([^=]+)=([^=]+)");
00073 
00074     QString groupPath = "";
00075     while(!stream.atEnd()) {
00076         // Reading a line and removing comments
00077         QString line = stream.readLine().remove(comment).simplified();
00078 
00079         // Checking whether the line is a group
00080         group.indexIn(line);
00081         QString newgroup = group.cap(1).simplified();
00082         if (!newgroup.isNull()) {
00083             // A new group, adding to root
00084             groupPath = newgroup;
00085             configParams.createGroup( groupPath );
00086             continue;
00087         }
00088 
00089         // If we get here we should have a property of groupPath
00090         param.indexIn(line);
00091         QString name = param.cap(1).simplified();
00092         QString value = param.cap(2).simplified();
00093         if ((!name.isNull()) && (!value.isNull())) {
00094             configParams.createParameter( groupPath, name );
00095             configParams.setValue( groupPath + ConfigurationParameters::GroupSeparator() + name, value );
00096         }
00097     }
00098     return true;
00099 }
00100 
00101 } // end namespace farsa