Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FlappyOrx_HS17/src/libraries/core/ConfigurablePaths.cc @ 11703

Last change on this file since 11703 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "ConfigurablePaths.h"
30
31#include <cassert>
32#include <cstdlib>
33#include <cstdio>
34#include <vector>
35#include <boost/filesystem.hpp>
36
37#ifdef ORXONOX_PLATFORM_WINDOWS
38#  ifndef WIN32_LEAN_AND_MEAN
39#    define WIN32_LEAN_AND_MEAN
40#  endif
41#  include <windows.h>
42#  undef min
43#  undef max
44#elif defined(ORXONOX_PLATFORM_APPLE)
45#  include <sys/param.h>
46#  include <mach-o/dyld.h>
47#else /* Linux */
48#  include <sys/types.h>
49#  include <unistd.h>
50#endif
51
52#include "SpecialConfig.h"
53#include "util/Output.h"
54#include "util/Exception.h"
55#include "commandline/CommandLineIncludes.h"
56#include "core/ApplicationPaths.h"
57
58// Differentiate Boost Filesystem v2 and v3
59#if (BOOST_FILESYSTEM_VERSION < 3)
60#  define BF_GENERIC_STRING string
61#else
62#  define BF_GENERIC_STRING generic_string
63#endif
64
65namespace orxonox
66{
67    namespace bf = boost::filesystem;
68
69    //! Static pointer to the singleton
70    ConfigurablePaths* ConfigurablePaths::singletonPtr_s  = nullptr;
71
72    SetCommandLineArgument(externalDataPath, "").information("Path to the external data files");
73    SetCommandLineArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");
74
75    ConfigurablePaths::ConfigurablePaths()
76        : dataPath_(*(new bf::path()))
77        , externalDataPath_(*(new bf::path()))
78        , configPath_(*(new bf::path()))
79        , logPath_(*(new bf::path()))
80    {
81    }
82
83    ConfigurablePaths::~ConfigurablePaths()
84    {
85        delete &dataPath_;
86        delete &externalDataPath_;
87        delete &configPath_;
88        delete &logPath_;
89    }
90
91    void ConfigurablePaths::setConfigurablePaths(const ApplicationPaths& applicationPaths)
92    {
93        if (applicationPaths.buildDirectoryRun())
94        {
95            dataPath_         = specialConfig::dataDevDirectory;
96            configPath_       = specialConfig::configDevDirectory;
97            logPath_          = specialConfig::logDevDirectory;
98
99            // Check for data path override by the command line
100            if (!CommandLineParser::getArgument("externalDataPath")->hasDefaultValue())
101                externalDataPath_ = CommandLineParser::getValue("externalDataPath").get<std::string>();
102            else
103                externalDataPath_ = specialConfig::externalDataDevDirectory;
104        }
105        else
106        {
107
108#ifdef INSTALL_COPYABLE // --> relative paths
109
110            // Using paths relative to the install prefix, complete them
111            dataPath_   = applicationPaths.getRootPath() / specialConfig::defaultDataPath;
112            configPath_ = applicationPaths.getRootPath() / specialConfig::defaultConfigPath;
113            logPath_    = applicationPaths.getRootPath() / specialConfig::defaultLogPath;
114
115#else
116
117            dataPath_  = specialConfig::dataInstallDirectory;
118
119            // Get user directory
120#ifdef ORXONOX_PLATFORM_UNIX
121            char* userDataPathPtr(getenv("HOME"));
122#else
123            char* userDataPathPtr(getenv("APPDATA"));
124#endif
125            if (userDataPathPtr == nullptr)
126                ThrowException(General, "Could not retrieve user data path.");
127            bf::path userDataPath(userDataPathPtr);
128            userDataPath /= ".orxonox";
129
130            configPath_ = userDataPath / specialConfig::defaultConfigPath;
131            logPath_    = userDataPath / specialConfig::defaultLogPath;
132
133#endif
134
135        }
136
137        // Option to put all the config and log files in a separate folder
138        if (!CommandLineParser::getArgument("writingPathSuffix")->hasDefaultValue())
139        {
140            const std::string& directory(CommandLineParser::getValue("writingPathSuffix").get<std::string>());
141            configPath_ = configPath_ / directory;
142            logPath_    = logPath_    / directory;
143        }
144
145        // Create directories to avoid problems when opening files in non existent folders.
146        std::vector<std::pair<bf::path, std::string>> directories;
147        directories.emplace_back(bf::path(configPath_), std::string("config"));
148        directories.emplace_back(bf::path(logPath_), std::string("log"));
149
150        for (std::vector<std::pair<bf::path, std::string>>::iterator it = directories.begin();
151            it != directories.end(); ++it)
152        {
153            if (bf::exists(it->first) && !bf::is_directory(it->first))
154            {
155                ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \
156                                         Please remove " + it->first.BF_GENERIC_STRING());
157            }
158            if (bf::create_directories(it->first)) // function may not return true at all (bug?)
159            {
160                orxout(internal_info) << "Created " << it->second << " directory" << endl;
161            }
162        }
163    }
164
165    /*static*/ std::string ConfigurablePaths::getDataPathString()
166    {
167        return getInstance().dataPath_.BF_GENERIC_STRING() + '/';
168    }
169
170    /*static*/ std::string ConfigurablePaths::getExternalDataPathString()
171    {
172        return getInstance().externalDataPath_.BF_GENERIC_STRING() + '/';
173    }
174
175    /*static*/ std::string ConfigurablePaths::getConfigPathString()
176    {
177        return getInstance().configPath_.BF_GENERIC_STRING() + '/';
178    }
179
180    /*static*/ std::string ConfigurablePaths::getLogPathString()
181    {
182        return getInstance().logPath_.BF_GENERIC_STRING() + '/';
183    }
184}
Note: See TracBrowser for help on using the repository browser.