openModeller  Version 1.4.0
Settings.cpp
Go to the documentation of this file.
00001 
00027 #include <openmodeller/Settings.hh>
00028 #include <openmodeller/Log.hh>
00029 #include <openmodeller/Exceptions.hh>
00030 
00031 #include <iostream>
00032 #include <string.h>
00033 
00034 using namespace std;
00035 
00036 /**************************************************************************
00037  *
00038  * Implementation of Settings class
00039  *
00040  *************************************************************************/
00041 
00042 /*******************/
00043 /*** constructor ***/
00044 
00045 Settings::Settings() :
00046   _fp(0)
00047 {
00048 }
00049 
00050 /******************/
00051 /*** destructor ***/
00052 
00053 Settings::~Settings()
00054 {
00055   Settings& conf = _getInstance();
00056 
00057   if ( conf._fp != 0 ) {
00058 
00059     delete conf._fp;
00060   }
00061 }
00062 
00063 /**************************/
00064 /*** Singleton accessor ***/
00065 Settings&
00066 Settings::_getInstance()
00067 {
00068   static Settings theInstance;
00069 
00070   if ( theInstance._fp == 0 ) {
00071 
00072     // Load settings from file in current directory, if name matches CONFIG_FILE compilation constant
00073     std::ifstream conf_file( CONFIG_FILE, std::ios::in );
00074 
00075     if ( conf_file ) {
00076 
00077       _loadConfig( &theInstance, CONFIG_FILE );
00078     }
00079   }
00080 
00081   return theInstance;
00082 }
00083 
00084 /*******************/
00085 /*** _loadConfig ***/
00086 void 
00087 Settings::_loadConfig( Settings * conf, const std::string configFile )
00088 {
00089   if ( conf->_fp != 0 ) {
00090 
00091     delete conf->_fp;
00092   }
00093 
00094   conf->_fp = new FileParser( configFile );
00095 }
00096 
00097 /******************/
00098 /*** loadConfig ***/
00099 void
00100 Settings::loadConfig( const std::string configFile )
00101 {
00102   Log::instance()->debug( "Loading configuration from: %s\n", configFile.c_str() );
00103 
00104   Settings& conf = _getInstance();
00105 
00106   _loadConfig( &conf, configFile );
00107 }
00108 
00109 /***********/
00110 /*** get ***/
00111 std::string 
00112 Settings::get( const std::string & key )
00113 {
00114   Settings& conf = _getInstance();
00115 
00116   if ( conf._fp == 0 ) {
00117 
00118     throw OmException( "openModeller configuration not loaded" );
00119   }
00120 
00121   return conf._fp->get( key );
00122 }
00123 
00124 /*************/
00125 /*** count ***/
00126 int
00127 Settings::count( const std::string & key )
00128 {
00129   Settings& conf = _getInstance();
00130 
00131   if ( conf._fp != 0 ) {
00132 
00133     return conf._fp->count( key );
00134   }
00135 
00136   return 0;
00137 }
00138 
00139 /**************/
00140 /*** getAll ***/
00141 std::vector<std::string>
00142 Settings::getAll( const std::string & key )
00143 {
00144   Settings& conf = _getInstance();
00145 
00146   if ( conf._fp == 0 ) {
00147 
00148     throw OmException( "openModeller configuration not loaded" );
00149   }
00150 
00151   return conf._fp->getAll( key );
00152 }