diff --git a/Code/Common/otbConfigurationFile.cxx b/Code/Common/otbConfigurationFile.cxx new file mode 100644 index 0000000000000000000000000000000000000000..d8678888be93a934ef73b0e02c73d38e168fc346 --- /dev/null +++ b/Code/Common/otbConfigurationFile.cxx @@ -0,0 +1,68 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + Some parts of this code are derived from ITK. See ITKCopyright.txt + for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbConfigurationFile.h" + +namespace otb +{ +/** Initialize the singleton */ +ConfigurationFile::Pointer ConfigurationFile::Instance = NULL; + +ConfigurationFile +::ConfigurationFile() +{ + std::string OTBBinDir(OTB_CONFIG); + try + { + m_OTBConfig = new ConfigFile(OTBBinDir + "/otb.conf"); + } + catch (ConfigFile::file_not_found& e) + { + itkExceptionMacro(<< "Error - File '" << e.filename << "' not found."); + } +} + +ConfigurationFile +::~ConfigurationFile() +{ +} + +ConfigurationFile::Pointer +ConfigurationFile +::GetInstance() +{ + if (!Instance) + { + Instance = Self::New(); + } + return Instance; +}; + + +void +ConfigurationFile +::PrintSelf(std::ostream& os, itk::Indent indent) const +{ + Superclass::PrintSelf(os, indent); + os << indent; + os << (*m_OTBConfig); + +} +} // end namespace otb diff --git a/Code/Common/otbConfigurationFile.h b/Code/Common/otbConfigurationFile.h index 45ecdaed66a32889c86c3de37d9bd2bab3cc2d28..0f327be6d4e6cb089c6513d23a62cb922c61bded 100644 --- a/Code/Common/otbConfigurationFile.h +++ b/Code/Common/otbConfigurationFile.h @@ -45,46 +45,41 @@ namespace otb /** Standard macro */ itkTypeMacro(ConfigurationFile,Object); - /** This is protected for the singleton. Use GetInstance() instead. */ - itkNewMacro(Self); + /** Get the unique instanc1e of the model */ -// static Pointer GetInstance() -// { -// if (!Instance) -// { -// Instance = Self::New(); -// } -// return Instance; -// }; + static Pointer GetInstance(); ConfigFile * GetOTBConfig() { return m_OTBConfig; }; - - std::string GetLanguage() - { - return m_OTBConfig->read<std::string>( "OTB_LANG" ); + + /** Get parameter*/ + template<typename T> T GetParameter(const std::string & key) const { + try + { + return m_OTBConfig->read<T>( key ); + } + catch( ConfigFile::key_not_found& e ) { + itkExceptionMacro(<< "Error - Key '" << e.key << "' not found."); + } + }; -// std::string lib(OTB_CONFIG); protected: - + /** This is protected for the singleton. Use GetInstance() instead. */ + itkNewMacro(Self); /** Constructor */ - ConfigurationFile() - { - std::string OTBBinDir(OTB_CONFIG); - m_OTBConfig = new ConfigFile(OTBBinDir + "/otb.conf"); - } - ; + ConfigurationFile(); + /** Destructor */ - ~ConfigurationFile(){}; + ~ConfigurationFile(); /** PrintSelf method */ - void PrintSelf(std::ostream& os, itk::Indent indent) const {}; + void PrintSelf(std::ostream& os, itk::Indent indent) const ; private: /** The instance singleton */ -// static Pointer Instance = NULL; + static Pointer Instance; ConfigFile * m_OTBConfig; }; }// end namespace diff --git a/Testing/Code/Common/otbConfigurationTest.cxx b/Testing/Code/Common/otbConfigurationTest.cxx index 64818c88aafa3103380561376f8539e10338fae8..5ddcf80a50ec9b02a703a8449c8eb749e88c5dc1 100644 --- a/Testing/Code/Common/otbConfigurationTest.cxx +++ b/Testing/Code/Common/otbConfigurationTest.cxx @@ -27,14 +27,13 @@ int otbConfigurationTest(int argc, char * argv[]) //Instantiation // ConfigurationType::Pointer conf = ConfigurationType::GetInstance(); - ConfigurationType::Pointer conf = ConfigurationType::New(); + ConfigurationType::Pointer conf = ConfigurationType::GetInstance(); // conf->Load(); + std::string lang = conf->GetParameter<std::string>("OTB_LANG"); - std::string lang = conf->GetLanguage(); + std::cout << conf << std::endl; - std::cout << "config language " << lang << std::endl; - - if (lang != "fr") + if (lang != "fr_FR.UTF-8") return EXIT_FAILURE; return EXIT_SUCCESS;