configuration/src/parametersfileloadersaver.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 "parametersfileloadersaver.h"
00022 #include "configurationparameters.h"
00023 #include <QtDebug>
00024 #include <QFile>
00025 
00026 namespace farsa {
00027 
00028 bool ParametersFileLoaderSaver::load(QString filename, ConfigurationParameters& configParams, bool keepOld)
00029 {
00030     // If prepareLoading hasn't been called before this function, returning false
00031     if (filename.isNull()) {
00032         return false;
00033     }
00034 
00035     // Opening the file
00036     QFile file(filename);
00037 
00038     // Checking we were able to open the file
00039     if (!file.open(QFile::ReadOnly | QFile::Text)) {
00040         qWarning() << "WARNING: Impossible to read data from " << filename;
00041         return false;
00042     }
00043 
00044     // First deleting all old properties if we have to
00045     if (!keepOld) {
00046         configParams.clearAll();
00047     }
00048 
00049     // Creating the textstream to load data
00050     QTextStream in(&file);
00051 
00052     // Loading file
00053     return loadParameters( in, configParams );
00054 }
00055 
00056 bool ParametersFileLoaderSaver::save(QString filename, const ConfigurationParameters& configParams, bool append)
00057 {
00058     // If prepareSaving hasn't been called before this function, returning false
00059     if (filename.isNull()) {
00060         return false;
00061     }
00062 
00063     // Opening the file
00064     QFile file(filename);
00065     
00066     QIODevice::OpenMode openMode = QFile::WriteOnly | QFile::Text;
00067     if ( append ) {
00068         openMode |= QFile::Append;
00069     } else {
00070         openMode |= QFile::Truncate;
00071     }
00072     // Checking we were able to open the file
00073     if (!file.open(openMode)) {
00074         qWarning() << "WARNING: Impossible to write data to " << filename;
00075         return false;
00076     }
00077 
00078     // Creating the textstream to load data
00079     QTextStream out(&file);
00080 
00081     // Saving file
00082     return saveParameters( out, configParams );
00083 }
00084 
00085 } // end namespace farsa