SpikeStream Library
0.2
|
00001 //SpikeStream includes 00002 #include "ConfigLoader.h" 00003 #include "SpikeStreamIOException.h" 00004 #include "Util.h" 00005 using namespace spikestream; 00006 00007 //Qt includes 00008 #include <QDir> 00009 #include <QFile> 00010 #include <QTextStream> 00011 #include <QCoreApplication> 00012 00013 //Other includes 00014 #include <iostream> 00015 using namespace std; 00016 00017 00019 ConfigLoader::ConfigLoader(){ 00020 QString rootDirectory = Util::getRootDirectory(); 00021 00022 /* Make sure that there is a config file already by attempting to copy from the template 00023 This function will not overwrite an existing config file */ 00024 QFile::copy (rootDirectory + "/spikestream.config.template", rootDirectory + "/spikestream.config"); 00025 00026 //Create config file 00027 QFile configFile(rootDirectory + "/spikestream.config"); 00028 if(!configFile.exists()){ 00029 QDir tmpDir(rootDirectory); 00030 throw SpikeStreamIOException("Cannot find config file in directory: " + tmpDir.absolutePath()); 00031 } 00032 00033 //Load the config file into the map 00034 if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text)) 00035 throw SpikeStreamIOException("Cannot open file for reading: " + configFile.fileName()); 00036 00037 QTextStream in(&configFile); 00038 QString line; 00039 while (!in.atEnd()) { 00040 line = in.readLine(); 00041 setParameter(line); 00042 } 00043 configFile.close(); 00044 } 00045 00046 00048 ConfigLoader::~ConfigLoader(){ 00049 } 00050 00051 00052 /*----------------------------------------------------------*/ 00053 /*----- PUBLIC METHODS -----*/ 00054 /*----------------------------------------------------------*/ 00055 00057 QString ConfigLoader::getParameter(const QString& paramName){ 00058 if(!configMap.contains(paramName)) 00059 throw SpikeStreamException("Configuration file does not contain a parameter with name: " + paramName); 00060 return configMap[paramName]; 00061 } 00062 00063 00064 /*----------------------------------------------------------*/ 00065 /*----- PRIVATE METHODS -----*/ 00066 /*----------------------------------------------------------*/ 00067 00069 void ConfigLoader::printConfig(){ 00070 cout<<"----------- SpikeStream Configuration -----------"<<endl; 00071 for (QHash<QString, QString>::iterator iter = configMap.begin(); iter != configMap.end(); ++iter) { 00072 cout << "Key: " << iter.key().toStdString() << "; "<< "Value: " << iter.value().toStdString()<< endl; 00073 } 00074 } 00075 00076 00079 void ConfigLoader::setParameter(const QString& configFileLine){ 00080 //Do nothing if line is a comment or is empty 00081 if(configFileLine.isEmpty() || configFileLine.at(0) == '#') 00082 return; 00083 00084 //Extract the parameter for this line 00085 QString paramName = configFileLine.section("=", 0, 0, QString::SectionSkipEmpty).trimmed(); 00086 QString paramValue = configFileLine.section("=", 1, 1, QString::SectionSkipEmpty).trimmed(); 00087 00088 if(configMap.contains(paramName)) 00089 throw SpikeStreamException("Duplicate parameter in config file: " + paramName); 00090 00091 configMap[paramName] = paramValue; 00092 } 00093 00094