Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkStandardFileLocations.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include <itkMacro.h>
20 #include <itkObject.h>
21 #include <itksys/SystemTools.hxx>
22 #include <mitkConfig.h>
23 
24 #include <algorithm>
25 
27 {
28 }
29 
31 {
32 }
33 
35 {
36  static StandardFileLocations::Pointer m_Instance = nullptr;
37 
38  if (m_Instance.IsNull())
39  m_Instance = StandardFileLocations::New();
40  return m_Instance;
41 }
42 
43 void mitk::StandardFileLocations::AddDirectoryForSearch(const char *dir, bool insertInFrontOfSearchList)
44 {
45  // Do nothing if directory is already included into search list (TODO more clever: search only once!)
46  FileSearchVectorType::iterator iter;
47  if (m_SearchDirectories.size() > 0)
48  {
49  iter = std::find(m_SearchDirectories.begin(), m_SearchDirectories.end(), std::string(dir));
50  if (iter != m_SearchDirectories.end())
51  return;
52  }
53  // insert dir into queue
54  if (insertInFrontOfSearchList)
55  {
56  auto it = m_SearchDirectories.begin();
57  m_SearchDirectories.insert(it, std::string(dir));
58  }
59  else
60  m_SearchDirectories.push_back(std::string(dir));
61 }
62 
64 {
65  FileSearchVectorType::iterator it;
66  // background layers
67  if (m_SearchDirectories.size() > 0)
68  {
69  it = std::find(m_SearchDirectories.begin(), m_SearchDirectories.end(), std::string(dir));
70  if (it != m_SearchDirectories.end())
71  {
72  m_SearchDirectories.erase(it);
73  return;
74  }
75  }
76 }
77 
79 {
80  FileSearchVectorType::iterator it;
81 
82  for (it = m_SearchDirectories.begin(); it != m_SearchDirectories.end(); ++it)
83  {
84  std::string currDir = (*it);
85 
86  // Perhaps append "/" before appending filename
87  if (currDir.find_last_of("\\") + 1 != currDir.size() || currDir.find_last_of("/") + 1 != currDir.size())
88  currDir += "/";
89 
90  // Append filename
91  currDir += filename;
92 
93  // Perhaps remove "/" after filename
94  if (currDir.find_last_of("\\") + 1 == currDir.size() || currDir.find_last_of("/") + 1 == currDir.size())
95  currDir.erase(currDir.size() - 1, currDir.size());
96 
97  // convert to OS dependent path schema
98  currDir = itksys::SystemTools::ConvertToOutputPath(currDir.c_str());
99 
100  // On windows systems, the ConvertToOutputPath method quotes pathes that contain empty spaces.
101  // These quotes are not expected by the FileExists method and therefore removed, if existing.
102  if (currDir.find_last_of("\"") + 1 == currDir.size())
103  currDir.erase(currDir.size() - 1, currDir.size());
104  if (currDir.find_last_of("\"") == 0)
105  currDir.erase(0, 1);
106 
107  // Return first found path
108  if (itksys::SystemTools::FileExists(currDir.c_str()))
109  return currDir;
110  }
111  return std::string("");
112 }
113 
114 std::string mitk::StandardFileLocations::FindFile(const char *filename, const char *pathInSourceDir)
115 {
116  std::string directoryPath;
117 
118  // 1. look for MITKCONF environment variable
119  const char *mitkConf = itksys::SystemTools::GetEnv("MITKCONF");
120  if (mitkConf != nullptr)
121  AddDirectoryForSearch(mitkConf, false);
122 
123 // 2. use .mitk-subdirectory in home directory of the user
124 #if defined(_WIN32) && !defined(__CYGWIN__)
125  const char *homeDrive = itksys::SystemTools::GetEnv("HOMEDRIVE");
126  const char *homePath = itksys::SystemTools::GetEnv("HOMEPATH");
127 
128  if ((homeDrive != NULL) || (homePath != NULL))
129  {
130  directoryPath = homeDrive;
131  directoryPath += homePath;
132  directoryPath += "/.mitk/";
133  AddDirectoryForSearch(directoryPath.c_str(), false);
134  }
135 
136 #else
137  const char *homeDirectory = itksys::SystemTools::GetEnv("HOME");
138  if (homeDirectory != nullptr)
139  {
140  directoryPath = homeDirectory;
141  directoryPath += "/.mitk/";
142  AddDirectoryForSearch(directoryPath.c_str(), false);
143  }
144 
145 #endif // defined(_WIN32) && !defined(__CYGWIN__)
146 
147  // 3. look in the current working directory
148  directoryPath = "";
149  AddDirectoryForSearch(directoryPath.c_str());
150 
151  directoryPath = itksys::SystemTools::GetCurrentWorkingDirectory();
152  AddDirectoryForSearch(directoryPath.c_str(), false);
153 
154  std::string directoryBinPath = directoryPath + "/bin";
155  AddDirectoryForSearch(directoryBinPath.c_str(), false);
156  // 4. use a source tree location from compile time
157  directoryPath = MITK_ROOT;
158  if (pathInSourceDir)
159  {
160  directoryPath += pathInSourceDir;
161  }
162  directoryPath += '/';
163  AddDirectoryForSearch(directoryPath.c_str(), false);
164 
165  return SearchDirectoriesForFile(filename);
166 }
167 
169 {
170  const char *mitkoptions = itksys::SystemTools::GetEnv("MITKOPTIONS");
171  std::string optionsDirectory;
172 
173  if (mitkoptions != nullptr)
174  {
175  // 1. look for MITKOPTIONS environment variable
176  optionsDirectory = mitkoptions;
177  optionsDirectory += "/";
178  }
179  else
180  {
181  // 2. use .mitk-subdirectory in home directory of the user
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))
187  {
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();
192  }
193  else
194  {
195  homeDirectory = homeDrive;
196  homeDirectory += homePath;
197  }
198  if (itksys::SystemTools::FileExists(homeDirectory.c_str()) == false)
199  {
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();
204  }
205 #else
206  const char *home = itksys::SystemTools::GetEnv("HOME");
207  if (home == nullptr)
208  {
209  itkGenericOutputMacro(<< "Environment variable HOME not set"
210  << ". Using current working directory as home directory: "
211  << itksys::SystemTools::GetCurrentWorkingDirectory());
212  homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
213  }
214  else
215  homeDirectory = home;
216  if (itksys::SystemTools::FileExists(homeDirectory.c_str()) == false)
217  {
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();
222  }
223 #endif // defined(_WIN32) && !defined(__CYGWIN__)
224 
225  optionsDirectory = homeDirectory;
226  optionsDirectory += "/.mitk";
227  }
228 
229  optionsDirectory = itksys::SystemTools::ConvertToOutputPath(optionsDirectory.c_str());
230  if (itksys::SystemTools::CountChar(optionsDirectory.c_str(), '"') > 0)
231  {
232  char *unquoted = itksys::SystemTools::RemoveChars(optionsDirectory.c_str(), "\"");
233  optionsDirectory = unquoted;
234  delete[] unquoted;
235  }
236 
237  if (itksys::SystemTools::MakeDirectory(optionsDirectory.c_str()) == false)
238  {
239  itkGenericExceptionMacro(<< "Could not create .mitk directory at " << optionsDirectory);
240  }
241  return optionsDirectory;
242 }
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.
#define MITK_ROOT
Definition: mitkConfig.h:9
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.