Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes

mitk::StandardFileLocations Class Reference

Provides a method to look for configuration and option files etc. More...

#include <mitkStandardFileLocations.h>

List of all members.

Public Types

typedef StandardFileLocations Self
typedef itk::Command Superclass
typedef itk::SmartPointer< SelfPointer

Public Member Functions

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.
void RemoveDirectoryForSearch (const char *dir)
 Remove a directory from the search queue: \ Use this function in combination with FindFile(). \.
std::string FindFile (const char *filename, const char *pathInSourceDir=NULL)
 looks for a file in several standard locations
std::string GetOptionDirectory ()
 Return directory of/for option files.

Static Public Member Functions

static StandardFileLocationsGetInstance ()

Protected Types

typedef std::vector< std::string > FileSearchVectorType

Protected Member Functions

 StandardFileLocations ()
virtual ~StandardFileLocations ()
std::string SearchDirectoriesForFile (const char *filename)

Static Protected Member Functions

static Pointer New ()

Protected Attributes

FileSearchVectorType m_SearchDirectories

Detailed Description

Provides a method to look for configuration and option files etc.

Call mitk::StandardFileLocations::FindFile(filename) to look for configuration files. Call mitk::StandardFileLocations::GetOptionDirectory() to look for/save option files.

Definition at line 34 of file mitkStandardFileLocations.h.


Member Typedef Documentation

typedef std::vector<std::string> mitk::StandardFileLocations::FileSearchVectorType [protected]

Definition at line 97 of file mitkStandardFileLocations.h.

typedef itk::SmartPointer<Self> mitk::StandardFileLocations::Pointer

Definition at line 39 of file mitkStandardFileLocations.h.

Definition at line 37 of file mitkStandardFileLocations.h.

Definition at line 38 of file mitkStandardFileLocations.h.


Constructor & Destructor Documentation

mitk::StandardFileLocations::StandardFileLocations (  ) [protected]

Definition at line 27 of file mitkStandardFileLocations.cpp.

{
}
mitk::StandardFileLocations::~StandardFileLocations (  ) [protected, virtual]

Definition at line 31 of file mitkStandardFileLocations.cpp.

{
}

Member Function Documentation

void mitk::StandardFileLocations::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.

Parameters:
dirdirectory you want to be searched in
insertInFrontOfSearchListwheather this search request shall be processed first

Definition at line 45 of file mitkStandardFileLocations.cpp.

Referenced by mitk::RigidRegistrationTestPreset::LoadPreset(), mitk::RigidRegistrationPreset::LoadPreset(), and mitk::TrackingVolume::TrackingVolume().

{
  std::string directory = dir;
    
  // Do nothing if directory is already included into search list (TODO more clever: search only once!)
  FileSearchVectorType::iterator iter;
  if(m_SearchDirectories.size() > 0)
  {
    iter = std::find(m_SearchDirectories.begin(), m_SearchDirectories.end(),std::string(dir));
    if ( iter != m_SearchDirectories.end() )
      return;
  }
  // insert dir into queue
  if(insertInFrontOfSearchList)
  {
    FileSearchVectorType::iterator it = m_SearchDirectories.begin();
    m_SearchDirectories.insert(it,std::string(dir));
  }
  else
    m_SearchDirectories.push_back(std::string(dir));
}
std::string mitk::StandardFileLocations::FindFile ( const char *  filename,
const char *  pathInSourceDir = NULL 
)

looks for a file in several standard locations

Parameters:
filenameThe file you want to fine, without any path
pathInSourceDirWhere in the source tree hierarchy would that file be?
Returns:
The absolute path to the file including the filename

This method appends several standard locations to the end of the searchqueue (if they not already exist) and then searches for the file within all directories contained in the search queue:

1. Add the directory specified in the environment variable MITKCONF 2. Add the .mitk directory in the home folder of the user 3. Add the current working directory 4. Add the (current working directory)/bin directory 5. Add the directory specified in pathInSourceDir, that is relative to the source code directory root (which is determined at compile time)

Already added directories in the searchqueue by using AddDirectoryForSearch before calling FindFile are still searched first, because above mentioned standard locations are always appended at the end of the list.

Definition at line 118 of file mitkStandardFileLocations.cpp.

References MITK_ROOT.

Referenced by mitk::RigidRegistrationTestPreset::LoadPreset(), mitk::RigidRegistrationPreset::LoadPreset(), mitk::ShaderRepository::LoadShaders(), mitk::EventMapper::LoadStandardBehavior(), and mitk::TrackingVolume::SetTrackingDeviceType().

{
  std::string directoryPath;

  // 1. look for MITKCONF environment variable
  const char* mitkConf = itksys::SystemTools::GetEnv("MITKCONF");  
  if (mitkConf!=NULL)
    AddDirectoryForSearch(mitkConf, false);

  // 2. use .mitk-subdirectory in home directory of the user
#if defined(_WIN32) && !defined(__CYGWIN__)
  const char* homeDrive = itksys::SystemTools::GetEnv("HOMEDRIVE");
  const char* homePath = itksys::SystemTools::GetEnv("HOMEPATH");
  
  if((homeDrive!=NULL) || (homePath!=NULL))
  {
    directoryPath = homeDrive;
    directoryPath += homePath;
    directoryPath += "/.mitk/";
    AddDirectoryForSearch(directoryPath.c_str(), false);
  }
 
#else
  const char* homeDirectory = itksys::SystemTools::GetEnv("HOME");
  if(homeDirectory != NULL)
  {
    directoryPath = homeDirectory;
    directoryPath += "/.mitk/";
    AddDirectoryForSearch(directoryPath.c_str(), false);
  }

#endif // defined(_WIN32) && !defined(__CYGWIN__)
 
  // 3. look in the current working directory
  directoryPath = "";
  AddDirectoryForSearch(directoryPath.c_str());
  
  directoryPath = itksys::SystemTools::GetCurrentWorkingDirectory();
  AddDirectoryForSearch(directoryPath.c_str(), false);

  std::string directoryBinPath = directoryPath + "/bin"; 
  AddDirectoryForSearch(directoryBinPath.c_str(), false);
  // 4. use a source tree location from compile time
  directoryPath = MITK_ROOT;
  if (pathInSourceDir)
  {
    directoryPath += pathInSourceDir;
  }
  directoryPath += '/';
  AddDirectoryForSearch(directoryPath.c_str(), false);
  
  return SearchDirectoriesForFile(filename);
}
mitk::StandardFileLocations * mitk::StandardFileLocations::GetInstance (  ) [static]
std::string mitk::StandardFileLocations::GetOptionDirectory (  )

Return directory of/for option files.

Returns:
The absolute path to the directory for option files.

This method looks for the directory of/for option files in two ways. The logic is as follows

1. If there is an environment variable MITKOPTIONS, then use that directory. 2. Use .mitk-subdirectory in home directory of the user

The directory will be created if it does not exist.

Definition at line 172 of file mitkStandardFileLocations.cpp.

Referenced by mitk::NavigationToolStorageDeserializer::Deserialize(), mitk::NavigationToolReader::DoRead(), mitk::NavigationToolWriter::DoWrite(), and mitk::NavigationToolStorageSerializer::Serialize().

{
  const char* mitkoptions = itksys::SystemTools::GetEnv("MITKOPTIONS");
  std::string optionsDirectory;

  if (mitkoptions!=NULL)
  {
    // 1. look for MITKOPTIONS environment variable
    optionsDirectory = mitkoptions;
    optionsDirectory += "/";
  }
  else
  {
    // 2. use .mitk-subdirectory in home directory of the user
    std::string homeDirectory;
#if defined(_WIN32) && !defined(__CYGWIN__)
    const char* homeDrive = itksys::SystemTools::GetEnv("HOMEDRIVE");
    const char* homePath = itksys::SystemTools::GetEnv("HOMEPATH");
    if((homeDrive==NULL) || (homePath==NULL))
    {
      itkGenericOutputMacro( << "Environment variables HOMEDRIVE and/or HOMEPATH not set" <<
        ". Using current working directory as home directory: " << itksys::SystemTools::GetCurrentWorkingDirectory());
      homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
    }
    else
    {
      homeDirectory = homeDrive;
      homeDirectory +=homePath;
    }
    if(itksys::SystemTools::FileExists(homeDirectory.c_str())==false)
    {
      itkGenericOutputMacro( << "Could not find home directory at " << homeDirectory << 
        ". Using current working directory as home directory: " << itksys::SystemTools::GetCurrentWorkingDirectory());
      homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
    }
#else
    const char* home = itksys::SystemTools::GetEnv("HOME");
    if(home==NULL)
    {
      itkGenericOutputMacro( << "Environment variable HOME not set" <<
        ". Using current working directory as home directory: " << itksys::SystemTools::GetCurrentWorkingDirectory());
      homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
    }
    else
      homeDirectory = home;
    if(itksys::SystemTools::FileExists(homeDirectory.c_str())==false)
    {
      itkGenericOutputMacro( << "Could not find home directory at " << homeDirectory << 
        ". Using current working directory as home directory: " << itksys::SystemTools::GetCurrentWorkingDirectory());
      homeDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
    }
#endif // defined(_WIN32) && !defined(__CYGWIN__)
   
    optionsDirectory = homeDirectory;
    optionsDirectory += "/.mitk";
  }

  optionsDirectory = itksys::SystemTools::ConvertToOutputPath(optionsDirectory.c_str());
  if(itksys::SystemTools::CountChar(optionsDirectory.c_str(),'"') > 0)
  {
    char * unquoted = itksys::SystemTools::RemoveChars(optionsDirectory.c_str(),"\"");
    optionsDirectory = unquoted;
    delete [] unquoted;
  }

  if(itksys::SystemTools::MakeDirectory(optionsDirectory.c_str())==false)
  {
    itkGenericExceptionMacro( << "Could not create .mitk directory at " << optionsDirectory );
  }
  return optionsDirectory;
}
static Pointer mitk::StandardFileLocations::New (  ) [static, protected]

Referenced by GetInstance().

void mitk::StandardFileLocations::RemoveDirectoryForSearch ( const char *  dir )

Remove a directory from the search queue: \ Use this function in combination with FindFile(). \.

Parameters:
dirdirectory you want to be searched in

Definition at line 67 of file mitkStandardFileLocations.cpp.

{
  FileSearchVectorType::iterator it;
  // background layers
  if(m_SearchDirectories.size() > 0)
  {
    it = std::find(m_SearchDirectories.begin(), m_SearchDirectories.end(),std::string(dir));
    if(it != m_SearchDirectories.end())
    {
      m_SearchDirectories.erase(it);
      return;
    }
  }
}
std::string mitk::StandardFileLocations::SearchDirectoriesForFile ( const char *  filename ) [protected]

Definition at line 82 of file mitkStandardFileLocations.cpp.

{
  FileSearchVectorType::iterator it;
 
  for(it = m_SearchDirectories.begin(); it != m_SearchDirectories.end(); it++)
  {
    std::string currDir = (*it);
    
    // Perhaps append "/" before appending filename
    if(currDir.find_last_of("\\")+1 != currDir.size() || currDir.find_last_of("/")+1 != currDir.size())
     currDir += "/";
    
    // Append filename
    currDir += filename;
    
    // Perhaps remove "/" after filename
    if(currDir.find_last_of("\\")+1 == currDir.size() || currDir.find_last_of("/")+1 == currDir.size())
     currDir.erase(currDir.size()-1,currDir.size());

    // convert to OS dependent path schema
    currDir = itksys::SystemTools::ConvertToOutputPath(currDir.c_str());

    // On windows systems, the ConvertToOutputPath method quotes pathes that contain empty spaces.
    // These quotes are not expected by the FileExists method and therefore removed, if existing.
    if(currDir.find_last_of("\"")+1 == currDir.size())
      currDir.erase(currDir.size()-1,currDir.size());
    if(currDir.find_last_of("\"") == 0)
      currDir.erase(0,1);
    
    // Return first found path   
    if(itksys::SystemTools::FileExists(currDir.c_str())) 
      return currDir;
  }
  return std::string(""); 
}

Member Data Documentation

Definition at line 100 of file mitkStandardFileLocations.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines