OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimPreferences.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // DESCRIPTION:
6 // Contains implementation of class ossimPreferences. This class provides
7 // a static keywordlist for global preferences. Objects needing access to
8 // application-wide global parameters shall do so through this class.
9 //
10 // SOFTWARE HISTORY:
11 //>
12 // 23Apr2001 Oscar Kramer (okramer@imagelinks.com)
13 // Initial coding.
14 //<
15 //*****************************************************************************
16 
17 #include <cstdlib> /* for getenv() */
18 
20 #include <ossim/base/ossimNotify.h>
21 
22 //RTTI_DEF1(ossimPreferences, "ossimPreferences" , ossimObject)
23 
24 //***
25 // Define Trace flags for use within this file:
26 //***
27 #include <ossim/base/ossimTrace.h>
28 static ossimTrace traceExec ("ossimPreferences:exec");
29 static ossimTrace traceDebug ("ossimPreferences:debug");
30 
31 static const char* PREF_FILE_ENV_VAR_NAME = "OSSIM_PREFS_FILE";
32 
34 
36 {
43 }
44 
46 {
47  theInstance = NULL;
48 }
49 
50 /*!****************************************************************************
51  * METHOD: ossimPreferences::instance()
52  *
53  * This is the method by which run-time objects access this singleton instance
54  *
55  *****************************************************************************/
57 {
61  if (theInstance)
62  return theInstance;
63 
68 
69  return theInstance;
70 }
71 
72 /*!****************************************************************************
73  * METHOD: loadPreferences()
74  *
75  * Loads the preferences file specified in the runtime environment.
76  *
77  *****************************************************************************/
79 {
80  static const char MODULE[] = "ossimPreferences::loadPreferences()";
81  if (traceExec())
82  {
84  << "DEBUG: " << MODULE << " entering...\n";
85  }
86 
87  bool parsed_ok = false;
88 
92  char* pref_filename = getenv(PREF_FILE_ENV_VAR_NAME);
93 
94  //std::cout << "ossimPreferences::loadPreferences() ======== " << ossimString(pref_filename) << "\n";
95  if (pref_filename)
96  {
100  thePrefFilename = pref_filename;
101  parsed_ok = theKWL.addFile(pref_filename);
102 
106  if (!parsed_ok)
107  {
108  if(traceDebug())
109  {
111  << "WARNING: " << MODULE
112  << ", an error was encountered loading the prefererences "
113  << "file at \"" << thePrefFilename << "\" as specified by the "
114  << "environment variable \"" << PREF_FILE_ENV_VAR_NAME << "\"."
115  << "Preferences were not loaded.\n";
116 
117  }
118  }
119  }
120  if (traceExec())
121  {
123  << "DEBUG: " << MODULE << "returning...\n";
124  }
125  return parsed_ok;
126 }
127 
128 /*!****************************************************************************
129  * METHOD: loadPreferences(filename)
130  *
131  * Loads the preferences file specified in the arg.
132  *
133  *****************************************************************************/
135 {
136  static const char MODULE[] = "ossimPreferences::loadPreferences(filename)";
137  if (traceExec())
138  {
140  << "DEBUG: " << MODULE << ", entering...\n";
141  }
142 
143  bool parsed_ok;
144 
148  theKWL.clear();
149  theInstanceIsModified = true;
150 
154  thePrefFilename = pathname;
155  parsed_ok = theKWL.addFile(pathname);
156 
160  if (!parsed_ok)
161  {
163  << "WARNING: " << MODULE
164  << ", an error was encountered loading the prefererences "
165  << "file at \"" << pathname << "\". Preferences were not "
166  << "loaded.\n";
167  }
168 
169  if (traceExec())
170  {
172  << "DEBUG: " << MODULE<< ", returning...\n";
173  }
174 
175  return parsed_ok;
176 }
177 
178 /*!****************************************************************************
179  * METHOD: ossimPreferences::savePreferences()
180  *
181  * Saves KWL to the current filename.
182  *
183  *****************************************************************************/
185 {
186  static const char MODULE[] = "ossimPreferences::savePreferences()";
187  if (traceExec())
188  {
190  << "DEBUG: " << MODULE << ", entering...\n";
191  }
192 
193  bool success = true;
194 
199  {
201  theInstanceIsModified = false;
202  }
203 
204  if (traceExec())
205  {
207  << "DEBUG:" << MODULE << ", returning...\n";
208  }
209 
210  return success;
211 }
212 
213 /*!****************************************************************************
214  * METHOD: ossimPreferences::savePreferences(filename)
215  *
216  * Saves KWL to the specified filename.
217  *
218  *****************************************************************************/
220 {
221  static const char MODULE[] = "ossimPreferences::savePreferences()";
222  if (traceExec())
223  {
225  << "DEBUG: "<< MODULE << ", entering...\n";
226  }
227 
228  bool success = true;
229 
233  theKWL.write(pathname);
234 
235  thePrefFilename = pathname;
236  theInstanceIsModified = false;
237 
238  if (traceExec())
239  {
241  << "DEBUG: " << MODULE << ", returning...\n";
242  }
243 
244  return success;
245 }
246 
247 void ossimPreferences::addPreference(const char* key, const char* value)
248 {
249  theKWL.add(key, value, true);
250  theInstanceIsModified = true;
251 }
252 
254  const char* prefix,
255  bool stripPrefix)
256 {
257  theKWL.add(kwl, prefix, stripPrefix);
258  theInstanceIsModified = true;
259 }
260 
262 {
263  return thePrefFilename;
264 }
Represents serializable keyword/value map.
bool addFile(const char *file)
ossimKeywordlist theKWL
virtual bool write(const char *file, const char *comment=0) const
Methods to dump the ossimKeywordlist to a file on disk.
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
bool savePreferences() const
static ossimPreferences * theInstance
static ossimPreferences * instance()
void addPreference(const char *key, const char *value)
void addPreferences(const ossimKeywordlist &kwl, const char *prefix=0, bool stripPrefix=true)
ossimFilename thePrefFilename
void setExpandEnvVarsFlag(bool flag)
ossimFilename getPreferencesFilename() const
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)