Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v2/src/libraries/core/ConfigurablePaths.cc @ 10745

Last change on this file since 10745 was 10624, checked in by landauf, 9 years ago

merged branch core7 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
57// Differentiate Boost Filesystem v2 and v3
58#if (BOOST_FILESYSTEM_VERSION < 3)
59#  define BF_GENERIC_STRING string
60#else
61#  define BF_GENERIC_STRING generic_string
62#endif
63
64namespace orxonox
65{
66    namespace bf = boost::filesystem;
67
68    //! Static pointer to the singleton
69    ConfigurablePaths* ConfigurablePaths::singletonPtr_s  = 0;
70
71    SetCommandLineArgument(externalDataPath, "").information("Path to the external data files");
72    SetCommandLineArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");
73
74    ConfigurablePaths::ConfigurablePaths()
75        : dataPath_(*(new bf::path()))
76        , externalDataPath_(*(new bf::path()))
77        , configPath_(*(new bf::path()))
78        , logPath_(*(new bf::path()))
79    {
80    }
81
82    ConfigurablePaths::~ConfigurablePaths()
83    {
84        delete &dataPath_;
85        delete &externalDataPath_;
86        delete &configPath_;
87        delete &logPath_;
88    }
89
90    void ConfigurablePaths::setConfigurablePaths(const ApplicationPaths& applicationPaths)
91    {
92        if (applicationPaths.buildDirectoryRun())
93        {
94            dataPath_         = specialConfig::dataDevDirectory;
95            configPath_       = specialConfig::configDevDirectory;
96            logPath_          = specialConfig::logDevDirectory;
97
98            // Check for data path override by the command line
99            if (!CommandLineParser::getArgument("externalDataPath")->hasDefaultValue())
100                externalDataPath_ = CommandLineParser::getValue("externalDataPath").get<std::string>();
101            else
102                externalDataPath_ = specialConfig::externalDataDevDirectory;
103        }
104        else
105        {
106
107#ifdef INSTALL_COPYABLE // --> relative paths
108
109            // Using paths relative to the install prefix, complete them
110            dataPath_   = applicationPaths.getRootPath() / specialConfig::defaultDataPath;
111            configPath_ = applicationPaths.getRootPath() / specialConfig::defaultConfigPath;
112            logPath_    = applicationPaths.getRootPath() / specialConfig::defaultLogPath;
113
114#else
115
116            dataPath_  = specialConfig::dataInstallDirectory;
117
118            // Get user directory
119#ifdef ORXONOX_PLATFORM_UNIX
120            char* userDataPathPtr(getenv("HOME"));
121#else
122            char* userDataPathPtr(getenv("APPDATA"));
123#endif
124            if (userDataPathPtr == NULL)
125                ThrowException(General, "Could not retrieve user data path.");
126            bf::path userDataPath(userDataPathPtr);
127            userDataPath /= ".orxonox";
128
129            configPath_ = userDataPath / specialConfig::defaultConfigPath;
130            logPath_    = userDataPath / specialConfig::defaultLogPath;
131
132#endif
133
134        }
135
136        // Option to put all the config and log files in a separate folder
137        if (!CommandLineParser::getArgument("writingPathSuffix")->hasDefaultValue())
138        {
139            const std::string& directory(CommandLineParser::getValue("writingPathSuffix").get<std::string>());
140            configPath_ = configPath_ / directory;
141            logPath_    = logPath_    / directory;
142        }
143
144        // Create directories to avoid problems when opening files in non existent folders.
145        std::vector<std::pair<bf::path, std::string> > directories;
146        directories.push_back(std::make_pair(bf::path(configPath_), std::string("config")));
147        directories.push_back(std::make_pair(bf::path(logPath_), std::string("log")));
148
149        for (std::vector<std::pair<bf::path, std::string> >::iterator it = directories.begin();
150            it != directories.end(); ++it)
151        {
152            if (bf::exists(it->first) && !bf::is_directory(it->first))
153            {
154                ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \
155                                         Please remove " + it->first.BF_GENERIC_STRING());
156            }
157            if (bf::create_directories(it->first)) // function may not return true at all (bug?)
158            {
159                orxout(internal_info) << "Created " << it->second << " directory" << endl;
160            }
161        }
162    }
163
164    /*static*/ std::string ConfigurablePaths::getDataPathString()
165    {
166        return getInstance().dataPath_.BF_GENERIC_STRING() + '/';
167    }
168
169    /*static*/ std::string ConfigurablePaths::getExternalDataPathString()
170    {
171        return getInstance().externalDataPath_.BF_GENERIC_STRING() + '/';
172    }
173
174    /*static*/ std::string ConfigurablePaths::getConfigPathString()
175    {
176        return getInstance().configPath_.BF_GENERIC_STRING() + '/';
177    }
178
179    /*static*/ std::string ConfigurablePaths::getLogPathString()
180    {
181        return getInstance().logPath_.BF_GENERIC_STRING() + '/';
182    }
183}
Note: See TracBrowser for help on using the repository browser.