parametersfileloadersaver.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 "parametersfileloadersaver.h"
22 #include "configurationparameters.h"
23 #include <QtDebug>
24 #include <QFile>
25 
26 namespace farsa {
27 
28 bool ParametersFileLoaderSaver::load(QString filename, ConfigurationParameters& configParams, bool keepOld)
29 {
30  // If prepareLoading hasn't been called before this function, returning false
31  if (filename.isNull()) {
32  return false;
33  }
34 
35  // Opening the file
36  QFile file(filename);
37 
38  // Checking we were able to open the file
39  if (!file.open(QFile::ReadOnly | QFile::Text)) {
40  //qWarning() << "WARNING: Impossible to read data from " << filename;
41  return false;
42  }
43 
44  // First deleting all old properties if we have to
45  if (!keepOld) {
46  configParams.clearAll();
47  }
48 
49  // Creating the textstream to load data
50  QTextStream in(&file);
51 
52  // Loading file
53  return loadParameters( in, configParams );
54 }
55 
56 bool ParametersFileLoaderSaver::save(QString filename, const ConfigurationParameters& configParams, bool append)
57 {
58  // If prepareSaving hasn't been called before this function, returning false
59  if (filename.isNull()) {
60  return false;
61  }
62 
63  // Opening the file
64  QFile file(filename);
65 
66  QIODevice::OpenMode openMode = QFile::WriteOnly | QFile::Text;
67  if ( append ) {
68  openMode |= QFile::Append;
69  } else {
70  openMode |= QFile::Truncate;
71  }
72  // Checking we were able to open the file
73  if (!file.open(openMode)) {
74  //qWarning() << "WARNING: Impossible to write data to " << filename;
75  return false;
76  }
77 
78  // Creating the textstream to load data
79  QTextStream out(&file);
80 
81  // Saving file
82  return saveParameters( out, configParams );
83 }
84 
85 } // end namespace farsa
The class containing configuration parameters.
virtual bool loadParameters(QTextStream &stream, ConfigurationParameters &configParams)=0
The function actually load configuration from the given stream.
bool save(QString filename, const ConfigurationParameters &configParams, bool append=false)
Save configuration from the ConfigurationParameters to the given file.
bool load(QString filename, ConfigurationParameters &configParams, bool keepOld=false)
Load configuration from the given file and store them to ConfigurationParameters. ...
void clearAll()
Delete all parameters in all groups and subgroups.
virtual bool saveParameters(QTextStream &stream, const ConfigurationParameters &configParams)=0
The function actually save configuration to the given stream.