Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
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_