OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimEnvironmentUtility.cpp
Go to the documentation of this file.
2 #include <cstdlib>
3 #include <sstream>
4 
5 #if defined(_WIN32) && !defined(__CYGWIN__)
6 # define OSSIM_ENVIRONMENT_UTILITY_UNIX 0
7 # include <direct.h>
8 #else
9 # define OSSIM_ENVIRONMENT_UTILITY_UNIX 1
10 #endif
11 
12 using namespace std;
13 
15 
17 {
18  ossimFilename dir = getUserOssimPluginDir();
19 
20  if(!dir.empty())
21  {
22  thePluginSearchPath.push_back(dir);
23  }
24 
25  dir = getInstalledOssimPluginDir();
26  if(!dir.empty())
27  {
28  thePluginSearchPath.push_back(dir);
29  }
30 
31 
32 }
33 
35 {
36  if(!theInstance)
37  {
38  theInstance = new ossimEnvironmentUtility;
39  }
40 
41  return theInstance;
42 }
43 
45 {
46  ossimString result;
47  char* lookup = std::getenv(variable.c_str());
48  // getenv returns NULL if not found.
49  if (lookup)
50  {
51  result = (const char*)lookup;
52  }
53  return result;
54 }
55 
56 void ossimEnvironmentUtility::setEnvironmentVariable(const char* variable, const char* value) const
57 {
58 #if OSSIM_ENVIRONMENT_UTILITY_UNIX
59  setenv(variable, value, 1); // For backwards compatiblity.
60 #else
61  ostringstream arg;
62  arg<<variable<<"="<<value;
63  _putenv( arg.str().c_str() );
64 #endif
65 }
66 
68 {
69  ossimFilename result = getUserDir();
70 
71 #if OSSIM_ENVIRONMENT_UTILITY_UNIX
72 # ifdef __APPLE__
73  result = result.dirCat("Library/Application Support/ossim");
74 
75 # else
76  result = result.dirCat(".ossim");
77 # endif
78 #else
79  result = result.dirCat("Application Data\\ossim");
80 #endif
81 
82  return result;
83 }
84 
86 {
87 #if OSSIM_ENVIRONMENT_UTILITY_UNIX
88  return getEnvironmentVariable("USER");
89 #else
90  return getEnvironmentVariable("USERNAME");
91 #endif
92 }
93 
95 {
96  ossimFilename result;
97 
98 #if OSSIM_ENVIRONMENT_UTILITY_UNIX
99  result = ossimFilename(getEnvironmentVariable("HOME"));
100 #else
101  result =ossimFilename(getEnvironmentVariable("USERPROFILE"));
102 #endif
103 
104  return result;
105 }
106 
108 {
109  ossimFilename result = getUserOssimSupportDir();
110 
111  if(result == "")
112  {
113  return "";
114  }
115 
116  result = result.dirCat("preferences");
117 
118  return result;
119 }
120 
122 {
123  ossimFilename result = getUserOssimSupportDir();
124 
125  if(result != "")
126  {
127  result = result.dirCat("plugins");
128  }
129 
130  return result;
131 }
132 
134 {
135  ossimFilename result;
136 #if OSSIM_ENVIRONMENT_UTILITY_UNIX
137 # ifdef __APPLE__
138  result = "/Library/Application Support/ossim";
139 # else
140  result = "/usr/share/ossim";
141  if(!result.exists())
142  {
143  result = "/usr/local/share/ossim";
144  }
145 # endif
146 #else
147  // NEED the windows test here.
148 #endif
149  if(result != "")
150  {
151  if(!result.exists())
152  {
153  result = "";
154  }
155  }
156 
157  return result;
158 }
159 
161 {
162  ossimFilename result = getInstalledOssimSupportDir();
163 
164  //Need generic unix plugin location
165 #if OSSIM_ENVIRONMENT_UTILITY_UNIX
166 # ifndef __APPLE__
167  return "";
168 # endif
169 #endif
170  if((result!="")&&result.exists())
171  {
172  result = result.dirCat("plugins");
173  }
174  else
175  {
176  result = "";
177  }
178 
179  return result;
180 
181 }
182 
184 {
185  ossimFilename result = getInstalledOssimSupportDir();
186 
187 
188  if((result!="")&&result.exists())
189  {
190  result = result.dirCat("preferences");
191  }
192  else
193  {
194  result = "";
195  }
196 
197  if(!result.exists())
198  {
199  result = "";
200  }
201 
202  return result;
203 
204 }
205 
207 {
208  ossimFilename result;
209 
210 #if OSSIM_ENVIRONMENT_UTILITY_UNIX
211  result = getEnvironmentVariable("PWD");
212 #else
213  char buf[512];
214  _getcwd(buf, 512);
215  result = buf;
216 #endif
217 
218  return result;
219 }
220 
222 {
223  ossimFilename result;
224 
225  result = findPlugin(file);
226  if(!result.empty()) return result;
227 
228  result = findData(file);
229 
230 
231  return result;
232 }
233 
235 {
236  theDataSearchPath.push_back(path);
237 }
238 
240 {
241  theDataSearchPath.insert(theDataSearchPath.begin(), path);
242 }
243 
245 {
246  thePluginSearchPath.push_back(path);
247 }
248 
250 {
251  thePluginSearchPath.insert(thePluginSearchPath.begin(), path);
252 }
253 
255 {
256  for(ossimEnvironmentUtility::FilenameListType::const_iterator iter = thePluginSearchPath.begin();
257  iter != thePluginSearchPath.end();
258  ++iter)
259  {
260  ossimFilename temp = iter->dirCat(plugin);
261  if(temp.exists())
262  {
263  return temp;
264  }
265  }
266 
267  return "";
268 }
269 
271 {
272  for(ossimEnvironmentUtility::FilenameListType::const_iterator iter = theDataSearchPath.begin();
273  iter != theDataSearchPath.end();
274  ++iter)
275  {
276  ossimFilename temp = iter->dirCat(data);
277  if(temp.exists())
278  {
279  return temp;
280  }
281  }
282 
283  return "";
284 }
285 
286 
288 {
289  return thePluginSearchPath;
290 }
291 
293 {
294  return thePluginSearchPath;
295 }
296 
298 {
299  return theDataSearchPath;
300 }
301 
303 {
304  return theDataSearchPath;
305 }
306 
307 // Hidden copy constructor.
309  const ossimEnvironmentUtility& /* obj */)
310 {
311 }
312 
313 // Hidden operator=
315  const ossimEnvironmentUtility& /* rhs */)
316 {
317  return *this;
318 }
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
static ossimEnvironmentUtility * theInstance
ossimFilename getInstalledOssimSupportDir() const
ossimFilename getUserOssimPluginDir() const
ossimFilename getInstalledOssimPluginDir() const
const ossimEnvironmentUtility & operator=(const ossimEnvironmentUtility &rhs)
ossimEnvironmentUtility::FilenameListType & getPluginSearchPath()
ossimFilename getUserOssimPreferences() const
ossimEnvironmentUtility::FilenameListType & getDataSearchPath()
void addPluginSearchPath(const ossimFilename &path)
ossimFilename getCurrentWorkingDir() const
bool exists() const
ossimFilename getInstalledOssimPreferences() const
void addPluginSearchPathToFront(const ossimFilename &path)
static ossimEnvironmentUtility * instance()
ossimFilename searchAllPaths(const ossimFilename &file) const
ossimFilename dirCat(const ossimFilename &file) const
void addDataSearchPath(const ossimFilename &path)
bool empty() const
Definition: ossimString.h:411
void addDataSearchPathToFront(const ossimFilename &path)
ossimString getEnvironmentVariable(const ossimString &variable) const
ossimFilename findPlugin(const ossimFilename &plugin) const
std::vector< ossimFilename > FilenameListType
void setEnvironmentVariable(const char *variable, const char *value) const
ossimFilename getUserOssimSupportDir() const
ossimFilename findData(const ossimFilename &data) const
ossimFilename getUserDir() const