fixes #128 Configuration cleanup.
Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>actorid
parent
a931124a2f
commit
ea7eb7a62d
@ -0,0 +1,118 @@
|
||||
#include "configurationmanager.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace Cfg
|
||||
{
|
||||
|
||||
static const char* const openmwCfgFile = "openmw.cfg";
|
||||
static const char* const ogreCfgFile = "ogre.cfg";
|
||||
static const char* const pluginsCfgFile = "plugins.cfg";
|
||||
|
||||
|
||||
ConfigurationManager::ConfigurationManager()
|
||||
: mPath("openmw")
|
||||
{
|
||||
}
|
||||
|
||||
ConfigurationManager::~ConfigurationManager()
|
||||
{
|
||||
}
|
||||
|
||||
void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables,
|
||||
boost::program_options::options_description& description)
|
||||
{
|
||||
loadConfig(mPath.getGlobalConfigPath(), variables, description);
|
||||
loadConfig(mPath.getLocalConfigPath(), variables, description);
|
||||
loadConfig(mPath.getRuntimeConfigPath(), variables, description);
|
||||
boost::program_options::notify(variables);
|
||||
}
|
||||
|
||||
void ConfigurationManager::loadConfig(const boost::filesystem::path& path,
|
||||
boost::program_options::variables_map& variables,
|
||||
boost::program_options::options_description& description)
|
||||
{
|
||||
boost::filesystem::path cfgFile(path);
|
||||
cfgFile /= std::string(openmwCfgFile);
|
||||
if (boost::filesystem::is_regular_file(cfgFile))
|
||||
{
|
||||
std::cout << "Loading config file: " << cfgFile.string() << "... ";
|
||||
|
||||
std::ifstream configFileStream(cfgFile.string().c_str());
|
||||
if (configFileStream.is_open())
|
||||
{
|
||||
boost::program_options::store(boost::program_options::parse_config_file(
|
||||
configFileStream, description), variables);
|
||||
|
||||
std::cout << "done." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "failed." << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const boost::filesystem::path& ConfigurationManager::getGlobalConfigPath() const
|
||||
{
|
||||
return mPath.getGlobalConfigPath();
|
||||
}
|
||||
|
||||
void ConfigurationManager::setGlobalConfigPath(const boost::filesystem::path& newPath)
|
||||
{
|
||||
mPath.setGlobalConfigPath(newPath);
|
||||
}
|
||||
|
||||
const boost::filesystem::path& ConfigurationManager::getLocalConfigPath() const
|
||||
{
|
||||
return mPath.getLocalConfigPath();
|
||||
}
|
||||
|
||||
void ConfigurationManager::setLocalConfigPath(const boost::filesystem::path& newPath)
|
||||
{
|
||||
mPath.setLocalConfigPath(newPath);
|
||||
}
|
||||
|
||||
const boost::filesystem::path& ConfigurationManager::getRuntimeConfigPath() const
|
||||
{
|
||||
return mPath.getRuntimeConfigPath();
|
||||
}
|
||||
|
||||
void ConfigurationManager::setRuntimeConfigPath(const boost::filesystem::path& newPath)
|
||||
{
|
||||
mPath.setRuntimeConfigPath(newPath);
|
||||
}
|
||||
|
||||
const boost::filesystem::path& ConfigurationManager::getGlobalDataPath() const
|
||||
{
|
||||
return mPath.getGlobalDataPath();
|
||||
}
|
||||
|
||||
void ConfigurationManager::setGlobalDataPath(const boost::filesystem::path& newPath)
|
||||
{
|
||||
mPath.setGlobalDataPath(newPath);
|
||||
}
|
||||
|
||||
const boost::filesystem::path& ConfigurationManager::getLocalDataPath() const
|
||||
{
|
||||
return mPath.getLocalDataPath();
|
||||
}
|
||||
|
||||
void ConfigurationManager::setLocalDataPath(const boost::filesystem::path& newPath)
|
||||
{
|
||||
mPath.setLocalDataPath(newPath);
|
||||
}
|
||||
|
||||
const boost::filesystem::path& ConfigurationManager::getRuntimeDataPath() const
|
||||
{
|
||||
return mPath.getRuntimeDataPath();
|
||||
}
|
||||
|
||||
void ConfigurationManager::setRuntimeDataPath(const boost::filesystem::path& newPath)
|
||||
{
|
||||
mPath.setRuntimeDataPath(newPath);
|
||||
}
|
||||
|
||||
} /* namespace Cfg */
|
@ -0,0 +1,54 @@
|
||||
#ifndef COMPONENTS_CFG_CONFIGURATIONMANAGER_HPP
|
||||
#define COMPONENTS_CFG_CONFIGURATIONMANAGER_HPP
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <components/files/path.hpp>
|
||||
|
||||
/**
|
||||
* \namespace Cfg
|
||||
*/
|
||||
namespace Cfg
|
||||
{
|
||||
|
||||
/**
|
||||
* \struct ConfigurationManager
|
||||
*/
|
||||
struct ConfigurationManager
|
||||
{
|
||||
ConfigurationManager();
|
||||
virtual ~ConfigurationManager();
|
||||
|
||||
void readConfiguration(boost::program_options::variables_map& variables,
|
||||
boost::program_options::options_description& description);
|
||||
|
||||
const boost::filesystem::path& getGlobalConfigPath() const;
|
||||
void setGlobalConfigPath(const boost::filesystem::path& newPath);
|
||||
|
||||
const boost::filesystem::path& getLocalConfigPath() const;
|
||||
void setLocalConfigPath(const boost::filesystem::path& newPath);
|
||||
|
||||
const boost::filesystem::path& getRuntimeConfigPath() const;
|
||||
void setRuntimeConfigPath(const boost::filesystem::path& newPath);
|
||||
|
||||
const boost::filesystem::path& getGlobalDataPath() const;
|
||||
void setGlobalDataPath(const boost::filesystem::path& newPath);
|
||||
|
||||
const boost::filesystem::path& getLocalDataPath() const;
|
||||
void setLocalDataPath(const boost::filesystem::path& newPath);
|
||||
|
||||
const boost::filesystem::path& getRuntimeDataPath() const;
|
||||
void setRuntimeDataPath(const boost::filesystem::path& newPath);
|
||||
|
||||
private:
|
||||
void loadConfig(const boost::filesystem::path& path,
|
||||
boost::program_options::variables_map& variables,
|
||||
boost::program_options::options_description& description);
|
||||
|
||||
Files::Path<> mPath;
|
||||
};
|
||||
|
||||
} /* namespace Cfg */
|
||||
|
||||
#endif /* COMPONENTS_CFG_CONFIGURATIONMANAGER_HPP */
|
@ -0,0 +1,12 @@
|
||||
#include "fileops.hpp"
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace Files
|
||||
{
|
||||
|
||||
bool isFile(const char *name)
|
||||
{
|
||||
return boost::filesystem::exists(boost::filesystem::path(name));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
#ifndef COMPONENTS_FILES_FILEOPS_HPP
|
||||
#define COMPONENTS_FILES_FILEOPS_HPP
|
||||
|
||||
namespace Files
|
||||
{
|
||||
|
||||
///\brief Check if a given path is an existing file (not a directory)
|
||||
///\param [in] name - filename
|
||||
bool isFile(const char *name);
|
||||
|
||||
}
|
||||
|
||||
#endif /* COMPONENTS_FILES_FILEOPS_HPP */
|
@ -0,0 +1,159 @@
|
||||
/**
|
||||
* Open Morrowind - an opensource Elder Scrolls III: Morrowind
|
||||
* engine implementation.
|
||||
*
|
||||
* Copyright (C) 2011 Open Morrowind Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** \file components/files/linuxpath.cpp */
|
||||
|
||||
#include "linuxpath.hpp"
|
||||
|
||||
#if defined(__linux__)
|
||||
|
||||
#include <cstdlib>
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* \namespace Files
|
||||
*/
|
||||
namespace Files
|
||||
{
|
||||
|
||||
boost::filesystem::path LinuxPath::getLocalConfigPath() const
|
||||
{
|
||||
boost::filesystem::path localConfigPath(".");
|
||||
boost::filesystem::path suffix("/");
|
||||
|
||||
const char* theDir = getenv("OPENMW_CONFIG");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
theDir = getenv("XDG_CONFIG_HOME");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
theDir = getenv("HOME");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
struct passwd* pwd = getpwuid(getuid());
|
||||
if (pwd != NULL)
|
||||
{
|
||||
theDir = pwd->pw_dir;
|
||||
}
|
||||
}
|
||||
if (theDir != NULL)
|
||||
{
|
||||
suffix = boost::filesystem::path("/.config/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (theDir != NULL) {
|
||||
localConfigPath = boost::filesystem::path(theDir);
|
||||
}
|
||||
|
||||
localConfigPath /= suffix;
|
||||
|
||||
return localConfigPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path LinuxPath::getGlobalConfigPath() const
|
||||
{
|
||||
boost::filesystem::path globalConfigPath("/etc/xdg/");
|
||||
|
||||
char* theDir = getenv("XDG_CONFIG_DIRS");
|
||||
if (theDir != NULL)
|
||||
{
|
||||
// We take only first path from list
|
||||
char* ptr = strtok(theDir, ":");
|
||||
if (ptr != NULL)
|
||||
{
|
||||
globalConfigPath = boost::filesystem::path(ptr);
|
||||
globalConfigPath /= boost::filesystem::path("/");
|
||||
}
|
||||
}
|
||||
|
||||
return globalConfigPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path LinuxPath::getRuntimeConfigPath() const
|
||||
{
|
||||
return boost::filesystem::path("./");
|
||||
}
|
||||
|
||||
boost::filesystem::path LinuxPath::getLocalDataPath() const
|
||||
{
|
||||
boost::filesystem::path localDataPath(".");
|
||||
boost::filesystem::path suffix("/");
|
||||
|
||||
const char* theDir = getenv("OPENMW_DATA");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
theDir = getenv("XDG_DATA_HOME");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
theDir = getenv("HOME");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
struct passwd* pwd = getpwuid(getuid());
|
||||
if (pwd != NULL)
|
||||
{
|
||||
theDir = pwd->pw_dir;
|
||||
}
|
||||
}
|
||||
if (theDir != NULL)
|
||||
{
|
||||
suffix = boost::filesystem::path("/.local/share/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (theDir != NULL) {
|
||||
localDataPath = boost::filesystem::path(theDir);
|
||||
}
|
||||
|
||||
localDataPath /= suffix;
|
||||
return localDataPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path LinuxPath::getGlobalDataPath() const
|
||||
{
|
||||
boost::filesystem::path globalDataPath("/usr/local/share/");
|
||||
|
||||
char* theDir = getenv("XDG_DATA_DIRS");
|
||||
if (theDir != NULL)
|
||||
{
|
||||
// We take only first path from list
|
||||
char* ptr = strtok(theDir, ":");
|
||||
if (ptr != NULL)
|
||||
{
|
||||
globalDataPath = boost::filesystem::path(ptr);
|
||||
globalDataPath /= boost::filesystem::path("/");
|
||||
}
|
||||
}
|
||||
|
||||
return globalDataPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path LinuxPath::getRuntimeDataPath() const
|
||||
{
|
||||
return boost::filesystem::path("./data/");
|
||||
}
|
||||
|
||||
|
||||
} /* namespace Files */
|
||||
|
||||
#endif /* defined(__linux__) */
|
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Open Morrowind - an opensource Elder Scrolls III: Morrowind
|
||||
* engine implementation.
|
||||
*
|
||||
* Copyright (C) 2011 Open Morrowind Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** \file components/files/linuxpath.hpp */
|
||||
|
||||
#ifndef COMPONENTS_FILES_LINUXPATH_H
|
||||
#define COMPONENTS_FILES_LINUXPATH_H
|
||||
|
||||
#if defined(__linux__)
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
/**
|
||||
* \namespace Files
|
||||
*/
|
||||
namespace Files
|
||||
{
|
||||
|
||||
/**
|
||||
* \struct LinuxPath
|
||||
*/
|
||||
struct LinuxPath
|
||||
{
|
||||
/**
|
||||
* \brief Return path to the local configuration directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getLocalConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return path to the global (system) configuration directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getGlobalConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return path to the runtime configuration directory which is the
|
||||
* place where an application was started.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getRuntimeConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return path to the local data directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getLocalDataPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return path to the global (system) data directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getGlobalDataPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return runtime data path which is a location where
|
||||
* an application was started with 'data' suffix.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getRuntimeDataPath() const;
|
||||
};
|
||||
|
||||
} /* namespace Files */
|
||||
|
||||
#endif /* defined(__linux__) */
|
||||
|
||||
#endif /* COMPONENTS_FILES_LINUXPATH_H */
|
@ -0,0 +1,118 @@
|
||||
/**
|
||||
* Open Morrowind - an opensource Elder Scrolls III: Morrowind
|
||||
* engine implementation.
|
||||
*
|
||||
* Copyright (C) 2011 Open Morrowind Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** \file components/files/macospath.cpp */
|
||||
|
||||
#include "macospath.hpp"
|
||||
|
||||
#if defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__)
|
||||
|
||||
#include <cstdlib>
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* \namespace Files
|
||||
*/
|
||||
namespace Files
|
||||
{
|
||||
|
||||
boost::filesystem::path MacOsPath::getLocalConfigPath() const
|
||||
{
|
||||
boost::filesystem::path localConfigPath(".");
|
||||
boost::filesystem::path suffix("/");
|
||||
|
||||
const char* theDir = getenv("HOME");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
struct passwd* pwd = getpwuid(getuid());
|
||||
if (pwd != NULL)
|
||||
{
|
||||
theDir = pwd->pw_dir;
|
||||
}
|
||||
}
|
||||
if (theDir != NULL)
|
||||
{
|
||||
localConfigPath = boost::filesystem::path(theDir) / "Library/Preferences/";
|
||||
}
|
||||
|
||||
localConfigPath /= suffix;
|
||||
|
||||
return localConfigPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path MacOsPath::getGlobalConfigPath() const
|
||||
{
|
||||
boost::filesystem::path globalConfigPath("/Library/Preferences/");
|
||||
return globalConfigPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path MacOsPath::getRuntimeConfigPath() const
|
||||
{
|
||||
return boost::filesystem::path("./");
|
||||
}
|
||||
|
||||
boost::filesystem::path MacOsPath::getLocalDataPath() const
|
||||
{
|
||||
boost::filesystem::path localDataPath(".");
|
||||
boost::filesystem::path suffix("/");
|
||||
|
||||
const char* theDir = getenv("OPENMW_DATA");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
theDir = getenv("HOME");
|
||||
if (theDir == NULL)
|
||||
{
|
||||
struct passwd* pwd = getpwuid(getuid());
|
||||
if (pwd != NULL)
|
||||
{
|
||||
theDir = pwd->pw_dir;
|
||||
}
|
||||
}
|
||||
if (theDir != NULL)
|
||||
{
|
||||
suffix = boost::filesystem::path("/Library/Application Support/");
|
||||
}
|
||||
}
|
||||
|
||||
if (theDir != NULL)
|
||||
{
|
||||
localDataPath = boost::filesystem::path(theDir);
|
||||
}
|
||||
|
||||
localDataPath /= suffix;
|
||||
return localDataPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path MacOsPath::getGlobalDataPath() const
|
||||
{
|
||||
boost::filesystem::path globalDataPath("/Library/Application Support/");
|
||||
return globalDataPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path MacOsPath::getRuntimeDataPath() const
|
||||
{
|
||||
return boost::filesystem::path("./data/");
|
||||
}
|
||||
|
||||
|
||||
} /* namespace Files */
|
||||
|
||||
#endif /* defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__) */
|
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Open Morrowind - an opensource Elder Scrolls III: Morrowind
|
||||
* engine implementation.
|
||||
*
|
||||
* Copyright (C) 2011 Open Morrowind Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** \file components/files/macospath.hpp */
|
||||
|
||||
#ifndef COMPONENTS_FILES_MACOSPATH_H
|
||||
#define COMPONENTS_FILES_MACOSPATH_H
|
||||
|
||||
#if defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__)
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
/**
|
||||
* \namespace Files
|
||||
*/
|
||||
namespace Files
|
||||
{
|
||||
|
||||
/**
|
||||
* \struct MacOsPath
|
||||
*/
|
||||
struct MacOsPath
|
||||
{
|
||||
/**
|
||||
* \brief Return path to the local configuration directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getLocalConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return path to the global (system) configuration directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getGlobalConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return path to the runtime configuration directory which is the
|
||||
* place where an application was started.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getRuntimeConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return path to the local data directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getLocalDataPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return path to the global (system) data directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getGlobalDataPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return runtime data path which is a location where
|
||||
* an application was started with 'data' suffix.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getRuntimeDataPath() const;
|
||||
};
|
||||
|
||||
} /* namespace Files */
|
||||
|
||||
#endif /* defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__) */
|
||||
|
||||
#endif /* COMPONENTS_FILES_MACOSPATH_H */
|
@ -1,61 +0,0 @@
|
||||
#include "path.hpp"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <OgrePlatform.h>
|
||||
#include <string>
|
||||
|
||||
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
||||
#include <OSX/macUtils.h>
|
||||
#endif
|
||||
|
||||
#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
||||
#include <stdlib.h> //getenv
|
||||
#endif
|
||||
|
||||
std::string Files::getPath (PathTypeEnum parType, const std::string parApp, const std::string parFile)
|
||||
{
|
||||
std::string theBasePath;
|
||||
if (parType==Path_ConfigGlobal)
|
||||
{
|
||||
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
||||
boost::filesystem::path path(Ogre::macBundlePath());
|
||||
path = path.parent_path();
|
||||
theBasePath = path.string() + "/";
|
||||
#elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX
|
||||
theBasePath = "/etc/"+parApp+"/";
|
||||
#else
|
||||
theBasePath = "";
|
||||
#endif
|
||||
|
||||
}
|
||||
else if (parType==Path_ConfigUser)
|
||||
{
|
||||
#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
||||
const char* theDir;
|
||||
if ((theDir = getenv("OPENMW_HOME")) != NULL)
|
||||
{
|
||||
theBasePath = std::string(theDir)+"/";
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((theDir = getenv("XDG_CONFIG_HOME")))
|
||||
{
|
||||
theBasePath = std::string(theDir)+"/"+parApp+"/";
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((theDir = getenv("HOME")) == NULL)
|
||||
return parFile;
|
||||
theBasePath = std::string(theDir)+"/.config/"+parApp+"/";
|
||||
}
|
||||
}
|
||||
boost::filesystem::create_directories(boost::filesystem::path(theBasePath));
|
||||
#else
|
||||
theBasePath = "";
|
||||
#endif
|
||||
}
|
||||
|
||||
theBasePath.append(parFile);
|
||||
return theBasePath;
|
||||
}
|
@ -1,17 +1,232 @@
|
||||
/**
|
||||
* Open Morrowind - an opensource Elder Scrolls III: Morrowind
|
||||
* engine implementation.
|
||||
*
|
||||
* Copyright (C) 2011 Open Morrowind Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** \file components/files/path.hpp */
|
||||
|
||||
#ifndef COMPONENTS_FILES_PATH_HPP
|
||||
#define COMPONENTS_FILES_PATH_HPP
|
||||
|
||||
#include <string>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <components/files/linuxpath.hpp>
|
||||
namespace Files { typedef LinuxPath TargetPathType; }
|
||||
|
||||
#elif defined(__WIN32) || defined(__WINDOWS__)
|
||||
#include <components/files/windowspath.hpp>
|
||||
namespace Files { typedef WindowsPath TargetPathType; }
|
||||
|
||||
#elif defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__)
|
||||
#include <components/files/macospath.hpp>
|
||||
namespace Files { typedef MacOsPath TargetPathType; }
|
||||
|
||||
#else
|
||||
#error "Unknown platform!"
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* \namespace Files
|
||||
*/
|
||||
namespace Files
|
||||
{
|
||||
enum PathTypeEnum
|
||||
|
||||
/**
|
||||
* \struct Path
|
||||
*
|
||||
* \tparam P - Path strategy class type (depends on target system)
|
||||
*
|
||||
*/
|
||||
template
|
||||
<
|
||||
class P = TargetPathType
|
||||
>
|
||||
struct Path
|
||||
{
|
||||
typedef P PathType;
|
||||
|
||||
/**
|
||||
* \brief Path constructor.
|
||||
*
|
||||
* \param [in] application_name - Name of the application
|
||||
*/
|
||||
Path(const std::string& application_name)
|
||||
: mPath()
|
||||
, mLocalConfigPath(mPath.getLocalConfigPath())
|
||||
, mGlobalConfigPath(mPath.getGlobalConfigPath())
|
||||
, mRuntimeConfigPath(mPath.getRuntimeConfigPath())
|
||||
, mLocalDataPath(mPath.getLocalDataPath())
|
||||
, mGlobalDataPath(mPath.getGlobalDataPath())
|
||||
, mRuntimeDataPath(mPath.getRuntimeDataPath())
|
||||
{
|
||||
Path_ConfigUser,
|
||||
Path_ConfigGlobal
|
||||
};
|
||||
if (!application_name.empty())
|
||||
{
|
||||
boost::filesystem::path suffix(application_name + std::string("/"));
|
||||
|
||||
std::string getPath (PathTypeEnum parType, const std::string parApp, const std::string parFile);
|
||||
}
|
||||
mLocalConfigPath /= suffix;
|
||||
mGlobalConfigPath /= suffix;
|
||||
|
||||
#endif
|
||||
mLocalDataPath /= suffix;
|
||||
mGlobalDataPath /= suffix;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Return path pointing to the user local configuration directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
const boost::filesystem::path& getLocalConfigPath() const
|
||||
{
|
||||
return mLocalConfigPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Sets new local configuration path.
|
||||
*
|
||||
* \param [in] path - New path
|
||||
*/
|
||||
void setLocalConfigPath(const boost::filesystem::path& path)
|
||||
{
|
||||
mLocalConfigPath = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Return path pointing to the global (system) configuration directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
const boost::filesystem::path& getGlobalConfigPath() const
|
||||
{
|
||||
return mGlobalConfigPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Sets new global configuration path.
|
||||
*
|
||||
* \param [in] path - New path
|
||||
*/
|
||||
void setGlobalConfigPath(const boost::filesystem::path& path)
|
||||
{
|
||||
mGlobalConfigPath = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Return path pointing to the directory where application was started.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
const boost::filesystem::path& getRuntimeConfigPath() const
|
||||
{
|
||||
return mRuntimeConfigPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Sets new runtime configuration path.
|
||||
*
|
||||
* \param [in] path - New path
|
||||
*/
|
||||
void setRuntimeConfigPath(const boost::filesystem::path& path)
|
||||
{
|
||||
mRuntimeConfigPath = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Return path pointing to the user local data directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
const boost::filesystem::path& getLocalDataPath() const
|
||||
{
|
||||
return mLocalDataPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Sets new local data path.
|
||||
*
|
||||
* \param [in] path - New path
|
||||
*/
|
||||
void setLocalDataPath(const boost::filesystem::path& path)
|
||||
{
|
||||
mLocalDataPath = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Return path pointing to the global (system) data directory.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
const boost::filesystem::path& getGlobalDataPath() const
|
||||
{
|
||||
return mGlobalDataPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Sets new global (system) data directory.
|
||||
*
|
||||
* \param [in] path - New path
|
||||
*/
|
||||
void setGlobalDataPath(const boost::filesystem::path& path)
|
||||
{
|
||||
mGlobalDataPath = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Return path pointing to the directory where application was started.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
const boost::filesystem::path& getRuntimeDataPath() const
|
||||
{
|
||||
return mRuntimeDataPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Sets new runtime data directory.
|
||||
*
|
||||
* \param [in] path - New path
|
||||
*/
|
||||
void setRuntimeDataPath(const boost::filesystem::path& path)
|
||||
{
|
||||
mRuntimeDataPath = path;
|
||||
}
|
||||
|
||||
private:
|
||||
PathType mPath;
|
||||
|
||||
boost::filesystem::path mLocalConfigPath; /**< User local path to the configuration files */
|
||||
boost::filesystem::path mGlobalConfigPath; /**< Global path to the configuration files */
|
||||
boost::filesystem::path mRuntimeConfigPath; /**< Runtime path to the configuration files.
|
||||
By default it is the same directory where
|
||||
application was run */
|
||||
|
||||
boost::filesystem::path mLocalDataPath; /**< User local application data path (user plugins / mods / etc.) */
|
||||
boost::filesystem::path mGlobalDataPath; /**< Global application data path */
|
||||
boost::filesystem::path mRuntimeDataPath; /**< Runtime path to the configuration files.
|
||||
By default it is a 'data' directory in same
|
||||
directory where application was run */
|
||||
|
||||
};
|
||||
|
||||
|
||||
} /* namespace Files */
|
||||
|
||||
#endif /* COMPONENTS_FILES_PATH_HPP */
|
||||
|
@ -0,0 +1,72 @@
|
||||
#include "windowspath.hpp"
|
||||
|
||||
#if defined(_WIN32) || defined(__WINDOWS__)
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <windows.h>
|
||||
#include <shobj.h>
|
||||
|
||||
namespace Files
|
||||
{
|
||||
|
||||
boost::filesystem::path WindowsPath::getLocalConfigPath() const
|
||||
{
|
||||
boost::filesystem::path localConfigPath(".");
|
||||
boost::filesystem::path suffix("/");
|
||||
|
||||
TCHAR path[MAX_PATH];
|
||||
memset(path, 0, sizeof(path));
|
||||
|
||||
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path)))
|
||||
{
|
||||
PathAppend(path, TEXT("My Games"));
|
||||
localConfigPath = boost::filesystem::path(path);
|
||||
}
|
||||
|
||||
localConfigPath /= suffix;
|
||||
|
||||
return localConfigPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path WindowsPath::getGlobalConfigPath() const
|
||||
{
|
||||
boost::filesystem::path globalConfigPath(".");
|
||||
boost::filesystem::path suffix("/");
|
||||
|
||||
TCHAR path[MAX_PATH];
|
||||
memset(path, 0, sizeof(path));
|
||||
|
||||
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES | CSIDL_FLAG_CREATE, NULL, 0, path)))
|
||||
{
|
||||
globalConfigPath = boost::filesystem::path(path);
|
||||
}
|
||||
|
||||
globalConfigPath /= suffix;
|
||||
|
||||
return globalConfigPath;
|
||||
}
|
||||
|
||||
boost::filesystem::path WindowsPath::getRuntimeConfigPath() const
|
||||
{
|
||||
return boost::filesystem::path("./");
|
||||
}
|
||||
|
||||
boost::filesystem::path WindowsPath::getLocalDataPath() const
|
||||
{
|
||||
return getLocalConfigPath();
|
||||
}
|
||||
|
||||
boost::filesystem::path WindowsPath::getGlobalDataPath() const
|
||||
{
|
||||
return getGlobalConfigPath();
|
||||
}
|
||||
|
||||
boost::filesystem::path WindowsPath::getRuntimeDataPath() const
|
||||
{
|
||||
return boost::filesystem::path("./data/");
|
||||
}
|
||||
|
||||
} /* namespace Files */
|
||||
|
||||
#endif /* defined(_WIN32) || defined(__WINDOWS__) */
|
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Open Morrowind - an opensource Elder Scrolls III: Morrowind
|
||||
* engine implementation.
|
||||
*
|
||||
* Copyright (C) 2011 Open Morrowind Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** \file components/files/windowspath.hpp */
|
||||
|
||||
#ifndef COMPONENTS_FILES_WINDOWSPATH_HPP
|
||||
#define COMPONENTS_FILES_WINDOWSPATH_HPP
|
||||
|
||||
#if defined(_WIN32) || defined(__WINDOWS__)
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
/**
|
||||
* \namespace Files
|
||||
*/
|
||||
namespace Files
|
||||
{
|
||||
|
||||
/**
|
||||
* \struct WindowsPath
|
||||
*/
|
||||
struct WindowsPath
|
||||
{
|
||||
/**
|
||||
* \brief Returns "X:\Documents And Settings\<User name>\My Documents\My Games\"
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getLocalConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Returns "X:\Program Files\"
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getGlobalConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return runtime configuration path which is a location where
|
||||
* an application was started
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getRuntimeConfigPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return same path like getLocalConfigPath
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getLocalDataPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return same path like getGlobalConfigPath
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getGlobalDataPath() const;
|
||||
|
||||
/**
|
||||
* \brief Return runtime data path which is a location where
|
||||
* an application was started with 'data' suffix.
|
||||
*
|
||||
* \return boost::filesystem::path
|
||||
*/
|
||||
boost::filesystem::path getRuntimeDataPath() const;
|
||||
};
|
||||
|
||||
} /* namespace Files */
|
||||
|
||||
#endif /* defined(_WIN32) || defined(__WINDOWS__) */
|
||||
|
||||
#endif /* COMPONENTS_FILES_WINDOWSPATH_HPP */
|
@ -1,16 +0,0 @@
|
||||
#include "fileops.hpp"
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <string>
|
||||
|
||||
#include <OgrePrerequisites.h>
|
||||
|
||||
namespace Misc
|
||||
{
|
||||
|
||||
bool isFile(const char *name)
|
||||
{
|
||||
boost::filesystem::path cfg_file_path(name);
|
||||
return boost::filesystem::exists(cfg_file_path);
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
#ifndef MISC_FILEOPS_H
|
||||
#define MISC_FILEOPS_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Misc
|
||||
{
|
||||
|
||||
/// Check if a given path is an existing file (not a directory)
|
||||
bool isFile(const char *name);
|
||||
|
||||
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
||||
std::string macBundlePath();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue