SpikeStream Library  0.2
ConfigEditor.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "ConfigEditor.h"
00003 #include "SpikeStreamException.h"
00004 #include "SpikeStreamIOException.h"
00005 #include "Util.h"
00006 using namespace spikestream;
00007 
00008 //Qt includes
00009 #include <QDebug>
00010 #include <QTextStream>
00011 #include <QCoreApplication>
00012 
00013 
00015 ConfigEditor::ConfigEditor(){
00016         //Get root directory.
00017         rootDirectory = Util::getRootDirectory();
00018 }
00019 
00020 
00022 ConfigEditor::~ConfigEditor(){
00023 }
00024 
00025 
00026 /*----------------------------------------------------------*/
00027 /*-----                 PUBLIC METHODS                 -----*/
00028 /*----------------------------------------------------------*/
00029 
00031 void ConfigEditor::setConfigParameters(QHash<QString, QString> newParamMap){
00032         /* Make sure that there is a config file already by attempting to copy from the template
00033            This function will not overwrite an existing config file */
00034         QFile::copy (rootDirectory + "/spikestream.config.template", rootDirectory + "/spikestream.config");
00035 
00036         //Create config file
00037         QFile configFile(rootDirectory + "/spikestream.config");
00038         if(!configFile.exists())
00039                 throw SpikeStreamIOException("Cannot find config file at location: " + rootDirectory + "/spikestream.config");
00040 
00041         //Load the config file into a string
00042         QString configFileStr;
00043         if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text))
00044                 throw SpikeStreamIOException("Cannot open file for reading: " + configFile.fileName());
00045 
00046         QTextStream in(&configFile);
00047         QString line;
00048         while (!in.atEnd()) {
00049                 line = in.readLine() + "\n";
00050                 setParameter(newParamMap, line);
00051                 configFileStr += line;
00052         }
00053         configFile.close();
00054 
00055         //Write the string back to the file with the modified parameters
00056         if( !configFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
00057                 throw SpikeStreamIOException("Cannot open file for writing: " + configFile.fileName());
00058 
00059         QTextStream out( &configFile );
00060         out<<configFileStr;
00061         configFile.close();
00062 
00063 }
00064 
00065 
00066 /*----------------------------------------------------------*/
00067 /*-----                PRIVATE METHODS                 -----*/
00068 /*----------------------------------------------------------*/
00069 
00072 void ConfigEditor::setParameter(QHash<QString, QString>& paramMap, QString& configFileLine){
00073         //Do nothing if line is a comment or is empty
00074         if(configFileLine.isEmpty() || configFileLine.at(0) == '#')
00075                 return;
00076 
00077         //Get the parameter for this line
00078         QString paramName = configFileLine.section("=", 0, 0, QString::SectionSkipEmpty).trimmed();
00079         for(QHash<QString, QString>::iterator iter = paramMap.begin(); iter != paramMap.end(); ++iter){
00080                 if(iter.key() == paramName){
00081                         configFileLine = paramName + " = " + iter.value() + "\n";
00082                         return;
00083                 }
00084         }
00085 }
00086 
00087 
 All Classes Files Functions Variables Typedefs Defines