inifilesupport.cpp
1 /***************************************************************************
2  * Copyright (C) 2008 by Tomassino Ferrauto *
3  * t_ferrauto@yahoo.it *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include "private/inifilesupport.h"
22 #include "configurationparameters.h"
23 #include <QtDebug>
24 #include <QFile>
25 #include <QRegExp>
26 
27 namespace farsa {
28 
29 IniFileLoaderSaver::IniFileLoaderSaver() :
30  ParametersFileLoaderSaver()
31 {
32 }
33 
34 IniFileLoaderSaver::~IniFileLoaderSaver()
35 {
36 }
37 
38 bool IniFileLoaderSaver::saveParameters(QTextStream &stream, const ConfigurationParameters& configParams)
39 {
40  //--- write recursevly all groups starting from the root
41  return writeGroupToStream( "", stream, configParams );
42 }
43 
44 bool IniFileLoaderSaver::writeGroupToStream(QString groupPath, QTextStream &outstream, const ConfigurationParameters& configParams) {
45  //--- write parameters first
46  QStringList paramList = configParams.getParametersList( groupPath );
47  foreach( QString param, paramList ) {
48  QString value = configParams.getValue( groupPath + ConfigurationParameters::GroupSeparator() + param );
49  if ( ! value.isEmpty() ) {
50  outstream << param << " = " << value << "\n";
51  }
52  }
53  //--- add an extra empty row for dividing sub-groups
54  outstream << "\n";
55  //--- and then all subgroups recursively
56  QStringList groupList = configParams.getGroupsList( groupPath );
57  foreach( QString group, groupList ) {
58  //--- is there the responsability to write the group header
59  //--- because in this way we can avoid to write the [ROOT] for root node
60  QString subgroup = groupPath + ConfigurationParameters::GroupSeparator() + group;
61  outstream << "[" << subgroup.section('/',1) << "]" << "\n";
62  writeGroupToStream( subgroup, outstream, configParams );
63  }
64  return true;
65 }
66 
67 bool IniFileLoaderSaver::loadParameters(QTextStream &stream, ConfigurationParameters& configParams)
68 {
69  // Regular expressions for comment, group and parameters
70  QRegExp comment("([#;]+.*)");
71  QRegExp group("\\[(.*)\\]");
72  QRegExp param("([^=]+)=([^=]+)");
73 
74  QString groupPath = "";
75  while(!stream.atEnd()) {
76  // Reading a line and removing comments
77  QString line = stream.readLine().remove(comment).simplified();
78 
79  // Checking whether the line is a group
80  group.indexIn(line);
81  QString newgroup = group.cap(1).simplified();
82  if (!newgroup.isNull()) {
83  // A new group, adding to root
84  groupPath = newgroup;
85  configParams.createGroup( groupPath );
86  continue;
87  }
88 
89  // If we get here we should have a property of groupPath
90  param.indexIn(line);
91  QString name = param.cap(1).simplified();
92  QString value = param.cap(2).simplified();
93  if ((!name.isNull()) && (!value.isNull())) {
94  configParams.createParameter( groupPath, name );
95  configParams.setValue( groupPath + ConfigurationParameters::GroupSeparator() + name, value );
96  }
97  }
98  return true;
99 }
100 
101 } // end namespace farsa