Config.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  TalkingHead - A talking head for robots
00003  *  Copyright (C) 2012 AG Aktives Sehen <agas@uni-koblenz.de>
00004  *                     Universitaet Koblenz-Landau
00005  *
00006  *  This program is free software: you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation, either version 3 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00019  *  MA 02110-1301  USA or see <http://www.gnu.org/licenses/>.
00020  *******************************************************************************/
00021 
00022 #ifndef TALKING_HEAD_INCLUDE_CONFIG_H_
00023 #define TALKING_HEAD_INCLUDE_CONFIG_H_
00024 
00025 #include <fstream>
00026 #include <iostream>
00027 #include <map>
00028 #include <sstream>
00029 #include <stdexcept>
00030 #include <string>
00031 
00032 #include "Data.h"
00033 
00040 class Config
00041 {
00042 public:
00043 
00045     Config( const char* filename, const char fieldSeparator = ':' )
00046     {
00047         std::fstream file( filename, std::ios::in );
00048 
00049         if( file )
00050         {
00051             std::string line;
00052 
00053             while( getline( file, line ) )
00054             {
00055                 size_t sep = line.find_first_of( fieldSeparator );
00056 
00057                 if (sep != std::string::npos)
00058                 {
00059                     std::string key   = trim( line.substr( 0, sep ) );
00060                     std::string value = trim( line.substr( sep + 1 ) );
00061 
00062                     if( !key.empty() && !value.empty() )
00063                     {
00064                         myConfigItems[key] = static_cast<Data>( value );
00065                     }
00066                     else
00067                     {
00068                         throw std::runtime_error( "Error within configuration file." );
00069                     }
00070                 }
00071                 else
00072                 {
00073                     throw std::runtime_error( "Error within configuration file." );
00074                 }
00075             }
00076         }
00077         else
00078         {
00079             throw std::runtime_error( "Cannot open config file." );
00080         }
00081     }
00082 
00084     ~Config(){}
00085 
00087     Data get( const std::string& key ) const
00088     {
00089         std::map<std::string, Data>::const_iterator it = myConfigItems.find( key );
00090 
00091         if ( it != myConfigItems.end() )
00092         {
00093             return it->second;
00094         }
00095         else
00096         {
00097             throw std::runtime_error( "Cannot find config item." );
00098         }
00099     }
00100 
00101     friend std::ostream& operator<<( std::ostream& os, const Config& config )
00102     {
00103       for( std::map<std::string, Data>::const_iterator it = config.myConfigItems.begin();
00104            it != config.myConfigItems.end();
00105            ++it )
00106       {
00107          const std::string& key = it->first;
00108          const std::string  val = it->second;
00109 
00110          std::cout << key << "\t" << val << std::endl;
00111       }
00112 
00113       return os;
00114     }
00115 
00116 private:
00117 
00119    std::string trim( std::string str )
00120    {
00121       size_t pos = str.find_first_not_of( " \t\n" );
00122 
00123       if ( pos != std::string::npos )
00124       {
00125          str.erase( 0, pos );
00126       }
00127 
00128       pos = str.find_last_not_of( " \t\n" );
00129 
00130       if ( pos != std::string::npos )
00131       {
00132          str.erase( pos + 1 );
00133       }
00134 
00135       return str;
00136    }
00137 
00138    std::map<std::string, Data> myConfigItems;
00139 };
00140 
00141 #endif // TALKING_HEAD_INCLUDE_CONFIG_H_


robot_face
Author(s): AGAS, Julian Giesen, David Gossow
autogenerated on Mon Oct 6 2014 04:10:26