openModeller  Version 1.5.0
Settings.cpp
Go to the documentation of this file.
1 
27 #include <openmodeller/Settings.hh>
28 #include <openmodeller/Log.hh>
30 
31 #include <iostream>
32 #include <string.h>
33 
34 using namespace std;
35 
36 /**************************************************************************
37  *
38  * Implementation of Settings class
39  *
40  *************************************************************************/
41 
42 /*******************/
43 /*** constructor ***/
44 
46  _fp(0)
47 {
48 }
49 
50 /******************/
51 /*** destructor ***/
52 
54 {
55  Settings& conf = _getInstance();
56 
57  if ( conf._fp != 0 ) {
58 
59  delete conf._fp;
60  }
61 }
62 
63 /**************************/
64 /*** Singleton accessor ***/
65 Settings&
67 {
68  static Settings theInstance;
69 
70  if ( theInstance._fp == 0 ) {
71 
72  // Load settings from file in current directory, if name matches CONFIG_FILE compilation constant
73  std::ifstream conf_file( CONFIG_FILE, std::ios::in );
74 
75  if ( conf_file ) {
76 
77  _loadConfig( &theInstance, CONFIG_FILE );
78  }
79  }
80 
81  return theInstance;
82 }
83 
84 /*******************/
85 /*** _loadConfig ***/
86 void
87 Settings::_loadConfig( Settings * conf, const std::string configFile )
88 {
89  if ( conf->_fp != 0 ) {
90 
91  delete conf->_fp;
92  }
93 
94  conf->_fp = new FileParser( configFile );
95 }
96 
97 /******************/
98 /*** loadConfig ***/
99 void
100 Settings::loadConfig( const std::string configFile )
101 {
102  Log::instance()->debug( "Loading configuration from: %s\n", configFile.c_str() );
103 
104  Settings& conf = _getInstance();
105 
106  _loadConfig( &conf, configFile );
107 }
108 
109 /***********/
110 /*** get ***/
111 std::string
112 Settings::get( const std::string & key )
113 {
114  Settings& conf = _getInstance();
115 
116  if ( conf._fp == 0 ) {
117 
118  throw OmException( "openModeller configuration not loaded" );
119  }
120 
121  return conf._fp->get( key );
122 }
123 
124 /*************/
125 /*** count ***/
126 int
127 Settings::count( const std::string & key )
128 {
129  Settings& conf = _getInstance();
130 
131  if ( conf._fp != 0 ) {
132 
133  return conf._fp->count( key );
134  }
135 
136  return 0;
137 }
138 
139 /**************/
140 /*** getAll ***/
141 std::vector<std::string>
142 Settings::getAll( const std::string & key )
143 {
144  Settings& conf = _getInstance();
145 
146  if ( conf._fp == 0 ) {
147 
148  throw OmException( "openModeller configuration not loaded" );
149  }
150 
151  return conf._fp->getAll( key );
152 }
static void loadConfig(const std::string configFile)
Definition: Settings.cpp:100
int count(const std::string &key) const
Definition: FileParser.cpp:165
static std::vector< std::string > getAll(const std::string &key)
Definition: Settings.cpp:142
FileParser * _fp
Definition: Settings.hh:79
~Settings()
Definition: Settings.cpp:53
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
static std::string get(const std::string &key)
Definition: Settings.cpp:112
static Settings & _getInstance()
Definition: Settings.cpp:66
Settings()
Definition: Settings.cpp:45
std::vector< std::string > getAll(const std::string &key) const
Definition: FileParser.cpp:185
static void _loadConfig(Settings *settings, const std::string configFile)
Definition: Settings.cpp:87
static int count(const std::string &key)
Definition: Settings.cpp:127
std::string get(const std::string &key) const
Definition: FileParser.cpp:145
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237