Main Page | Namespace List | Alphabetical List | Class List | Directories | File List | Class Members | File Members

ConfigLoader.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   SpikeStream Library                                                   *
00003  *   Copyright (C) 2007 by David Gamez                                     *
00004  *   david@davidgamez.eu                                                   *
00005  *   Version 0.1                                                           *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 
00023 //SpikeStream includes
00024 #include "ConfigLoader.h"
00025 
00026 
00027 /*! Constructor loads up the configuration data from the given file path. */
00028 ConfigLoader::ConfigLoader(string configPath){
00029 
00030         //Open stream to read file
00031         ifstream configReader;
00032         configReader.open(configPath.data());
00033 
00034         //If it is not there, throw exception and exit.
00035         if(!configReader){
00036                 cout<<"Configuration file not found!"<<endl;
00037                 throw 0;
00038         }
00039 
00040         //Next step is to read file, extract configuration parameters and put them in a map
00041         char row[200];
00042         while(!configReader.eof()){
00043                 configReader.getline(row, 200);
00044                 
00045                 //Extracts key and value pairs
00046                 if (!(row[0] == '#' | row[0] == ' ' | row[0] == 13 | row[0] == 0)){
00047                         string key = "";
00048                         int j=0;
00049                         while((row[j] != ' ') && (row[j]!= '=') && j<200){
00050                                 key += row[j];
00051                                 j++;
00052                         }
00053                         while(row[j] == ' ' | row [j] == '=')
00054                                 j++;
00055                         string value = "";
00056                         while(row[j] != 0 && row [j] != ' ' && row[j] != 13 && row[j] != 0){
00057                                 value += row[j];
00058                                 j++;
00059                         }
00060 
00061                         //Add to map
00062                         configMap[key] = value;
00063                 }
00064         }
00065         configReader.close();
00066 }
00067 
00068 
00069 /*! Destructor. */
00070 ConfigLoader::~ConfigLoader(){
00071         #ifdef MEMORY_DEBUG
00072                 cout<<"DELETING CONFIG LOADER"<<endl;
00073         #endif//MEMORY_DEBUG
00074 }
00075 
00076 
00077 //-----------------------------------------------------------------------
00078 //------------------------ PUBLIC METHODS -------------------------------
00079 //-----------------------------------------------------------------------
00080 
00081 /*! Extracts the configuration parameter as a cstring. */
00082 const char* ConfigLoader::getCharData(string key){
00083         string s = configMap[key];
00084         if(s.empty()){
00085                 cerr<<"Key \""<<key<<"\" not present in config file"<<endl;
00086                 throw 0;
00087         }
00088         return s.data();
00089 }
00090 
00091 
00092 /*! Extracts the configuration parameter as a string. */
00093 string ConfigLoader::getStringData(string key){
00094         string s = configMap[key];
00095         if(s.empty()){
00096                 cerr<<"Key \""<<key<<"\" not present in config file"<<endl;
00097                 throw 0;
00098         }
00099         return s;
00100 }
00101 
00102 
00103 //-----------------------------------------------------------------------
00104 //--------------------------- PRIVATE METHODS ---------------------------
00105 //-----------------------------------------------------------------------
00106 
00107 /*! Debug method for printing out the loaded configuration parameters. */
00108 void ConfigLoader::printConfig(){
00109         cout<<"Configuration for Neuron Application"<<endl;
00110         cout<<"---------------------------------------"<<endl;
00111         map<string, string>::iterator iter;
00112     for (iter = configMap.begin(); iter != configMap.end(); ++iter) {
00113         cout << "Key: " << iter->first << "; "
00114              << "Value: " << iter->second << endl;
00115     }
00116 }
00117 
00118 

Generated on Mon Sep 3 22:18:50 2007 for SpikeStream Library by  doxygen 1.4.4