20 #include <itkObject.h>
21 #include <itksys/SystemTools.hxx>
38 if (m_Instance.IsNull())
46 FileSearchVectorType::iterator iter;
47 if (m_SearchDirectories.size() > 0)
49 iter = std::find(m_SearchDirectories.begin(), m_SearchDirectories.end(), std::string(dir));
50 if (iter != m_SearchDirectories.end())
54 if (insertInFrontOfSearchList)
56 auto it = m_SearchDirectories.begin();
57 m_SearchDirectories.insert(it, std::string(dir));
60 m_SearchDirectories.push_back(std::string(dir));
65 FileSearchVectorType::iterator it;
67 if (m_SearchDirectories.size() > 0)
69 it = std::find(m_SearchDirectories.begin(), m_SearchDirectories.end(), std::string(dir));
70 if (it != m_SearchDirectories.end())
72 m_SearchDirectories.erase(it);
80 FileSearchVectorType::iterator it;
82 for (it = m_SearchDirectories.begin(); it != m_SearchDirectories.end(); ++it)
84 std::string currDir = (*it);
87 if (currDir.find_last_of(
"\\") + 1 != currDir.size() || currDir.find_last_of(
"/") + 1 != currDir.size())
94 if (currDir.find_last_of(
"\\") + 1 == currDir.size() || currDir.find_last_of(
"/") + 1 == currDir.size())
95 currDir.erase(currDir.size() - 1, currDir.size());
98 currDir = itksys::SystemTools::ConvertToOutputPath(currDir.c_str());
102 if (currDir.find_last_of(
"\"") + 1 == currDir.size())
103 currDir.erase(currDir.size() - 1, currDir.size());
104 if (currDir.find_last_of(
"\"") == 0)
108 if (itksys::SystemTools::FileExists(currDir.c_str()))
111 return std::string(
"");
116 std::string directoryPath;
119 const char *mitkConf = itksys::SystemTools::GetEnv(
"MITKCONF");
120 if (mitkConf !=
nullptr)
121 AddDirectoryForSearch(mitkConf,
false);
124 #if defined(_WIN32) && !defined(__CYGWIN__)
125 const char *homeDrive = itksys::SystemTools::GetEnv(
"HOMEDRIVE");
126 const char *homePath = itksys::SystemTools::GetEnv(
"HOMEPATH");
128 if ((homeDrive != NULL) || (homePath != NULL))
130 directoryPath = homeDrive;
131 directoryPath += homePath;
132 directoryPath +=
"/.mitk/";
133 AddDirectoryForSearch(directoryPath.c_str(),
false);
137 const char *homeDirectory = itksys::SystemTools::GetEnv(
"HOME");
138 if (homeDirectory !=
nullptr)
140 directoryPath = homeDirectory;
141 directoryPath +=
"/.mitk/";
142 AddDirectoryForSearch(directoryPath.c_str(),
false);
145 #endif // defined(_WIN32) && !defined(__CYGWIN__)
149 AddDirectoryForSearch(directoryPath.c_str());
151 directoryPath = itksys::SystemTools::GetCurrentWorkingDirectory();
152 AddDirectoryForSearch(directoryPath.c_str(),
false);
154 std::string directoryBinPath = directoryPath +
"/bin";
155 AddDirectoryForSearch(directoryBinPath.c_str(),
false);
160 directoryPath += pathInSourceDir;
162 directoryPath +=
'/';
163 AddDirectoryForSearch(directoryPath.c_str(),
false);
165 return SearchDirectoriesForFile(filename);
170 const char *mitkoptions = itksys::SystemTools::GetEnv(
"MITKOPTIONS");
171 std::string optionsDirectory;
173 if (mitkoptions !=
nullptr)
176 optionsDirectory = mitkoptions;
177 optionsDirectory +=
"/";
182 std::string homeDirectory;
183 #if defined(_WIN32) && !defined(__CYGWIN__)
184 const char *homeDrive = itksys::SystemTools::GetEnv(
"HOMEDRIVE");
185 const char *homePath = itksys::SystemTools::GetEnv(
"HOMEPATH");
186 if ((homeDrive == NULL) || (homePath == NULL))
188 itkGenericOutputMacro(<<
"Environment variables HOMEDRIVE and/or HOMEPATH not set"
189 <<
". Using current working directory as home directory: "
190 << itksys::SystemTools::GetCurrentWorkingDirectory());
191 homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
195 homeDirectory = homeDrive;
196 homeDirectory += homePath;
198 if (itksys::SystemTools::FileExists(homeDirectory.c_str()) ==
false)
200 itkGenericOutputMacro(<<
"Could not find home directory at " << homeDirectory
201 <<
". Using current working directory as home directory: "
202 << itksys::SystemTools::GetCurrentWorkingDirectory());
203 homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
206 const char *home = itksys::SystemTools::GetEnv(
"HOME");
209 itkGenericOutputMacro(<<
"Environment variable HOME not set"
210 <<
". Using current working directory as home directory: "
211 << itksys::SystemTools::GetCurrentWorkingDirectory());
212 homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
215 homeDirectory = home;
216 if (itksys::SystemTools::FileExists(homeDirectory.c_str()) ==
false)
218 itkGenericOutputMacro(<<
"Could not find home directory at " << homeDirectory
219 <<
". Using current working directory as home directory: "
220 << itksys::SystemTools::GetCurrentWorkingDirectory());
221 homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
223 #endif // defined(_WIN32) && !defined(__CYGWIN__)
225 optionsDirectory = homeDirectory;
226 optionsDirectory +=
"/.mitk";
229 optionsDirectory = itksys::SystemTools::ConvertToOutputPath(optionsDirectory.c_str());
230 if (itksys::SystemTools::CountChar(optionsDirectory.c_str(),
'"') > 0)
232 char *unquoted = itksys::SystemTools::RemoveChars(optionsDirectory.c_str(),
"\"");
233 optionsDirectory = unquoted;
237 if (itksys::SystemTools::MakeDirectory(optionsDirectory.c_str()) ==
false)
239 itkGenericExceptionMacro(<<
"Could not create .mitk directory at " << optionsDirectory);
241 return optionsDirectory;
virtual ~StandardFileLocations()
void RemoveDirectoryForSearch(const char *dir)
Remove a directory from the search queue: \ Use this function in combination with FindFile()...
std::string GetOptionDirectory()
Return directory of/for option files.
static const std::string filename
void AddDirectoryForSearch(const char *dir, bool insertInFrontOfSearchList=true)
Adds a directory into the search queue: \ Use this function in combination with FindFile(), after adding some \ directories, they will also be searched for the requested file.
std::string FindFile(const char *filename, const char *pathInSourceDir=nullptr)
looks for a file in several standard locations
static StandardFileLocations * GetInstance()
std::string SearchDirectoriesForFile(const char *filename)
Provides a method to look for configuration and option files etc.