Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox_qt/src/libraries/core/PathConfig.cc @ 7421

Last change on this file since 7421 was 7421, checked in by rgrieder, 15 years ago

Basic stuff up and running for the Qt sandbox.
No GUI support yet.

  • Property svn:eol-style set to native
File size: 6.7 KB
RevLine 
[1505]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:
[2896]23 *      Reto Grieder
[1505]24 *   Co-authors:
[2896]25 *      ...
[1505]26 *
27 */
28
[5836]29#include "PathConfig.h"
[1505]30
[1756]31#include <cassert>
[2710]32#include <cstdlib>
33#include <cstdio>
[5836]34#include <vector>
[2710]35
36#ifdef ORXONOX_PLATFORM_WINDOWS
[2896]37#  ifndef WIN32_LEAN_AND_MEAN
38#    define WIN32_LEAN_AND_MEAN
39#  endif
[2710]40#  include <windows.h>
[3214]41#  undef min
42#  undef max
[2710]43#elif defined(ORXONOX_PLATFORM_APPLE)
44#  include <sys/param.h>
45#  include <mach-o/dyld.h>
46#else /* Linux */
47#  include <sys/types.h>
48#  include <unistd.h>
49#endif
50
51#include "SpecialConfig.h"
[2896]52#include "util/Debug.h"
[2710]53#include "util/Exception.h"
[1505]54
55namespace orxonox
56{
[3196]57    //! Static pointer to the singleton
[5836]58    PathConfig* PathConfig::singletonPtr_s  = 0;
[2662]59
[5836]60    PathConfig::PathConfig()
[3280]61    {
[5693]62        //////////////////////////
63        // FIND EXECUTABLE PATH //
64        //////////////////////////
65
[2710]66#ifdef ORXONOX_PLATFORM_WINDOWS
67        // get executable module
68        TCHAR buffer[1024];
69        if (GetModuleFileName(NULL, buffer, 1024) == 0)
70            ThrowException(General, "Could not retrieve executable path.");
71
72#elif defined(ORXONOX_PLATFORM_APPLE)
73        char buffer[1024];
74        unsigned long path_len = 1023;
75        if (_NSGetExecutablePath(buffer, &path_len))
76            ThrowException(General, "Could not retrieve executable path.");
77
78#else /* Linux */
79        /* written by Nicolai Haehnle <prefect_@gmx.net> */
80
81        /* Get our PID and build the name of the link in /proc */
82        char linkname[64]; /* /proc/<pid>/exe */
83        if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", getpid()) < 0)
84        {
85            /* This should only happen on large word systems. I'm not sure
86               what the proper response is here.
87               Since it really is an assert-like condition, aborting the
88               program seems to be in order. */
89            assert(false);
90        }
91
92        /* Now read the symbolic link */
93        char buffer[1024];
94        int ret;
95        ret = readlink(linkname, buffer, 1024);
96        /* In case of an error, leave the handling up to the caller */
97        if (ret == -1)
98            ThrowException(General, "Could not retrieve executable path.");
99
100        /* Ensure proper NUL termination */
101        buffer[ret] = 0;
102#endif
103
[7421]104        executablePath_ = QDir(buffer);
[2710]105#ifndef ORXONOX_PLATFORM_APPLE
[7421]106        executablePath_.cdUp(); // remove executable name
[2710]107#endif
108
[7421]109        if (executablePath_.exists("orxonox_dev_build.keep_me"))
[2710]110        {
111            COUT(1) << "Running from the build tree." << std::endl;
[5836]112            PathConfig::bDevRun_ = true;
[2710]113        }
114        else
115        {
[5693]116
[2710]117#ifdef INSTALL_COPYABLE // --> relative paths
[5693]118
[2710]119            // Also set the root path
[7421]120            QDir relativeExecutablePath(specialConfig::defaultRuntimePath);
[5836]121            rootPath_ = executablePath_;
[7421]122            while (rootPath_ / relativeExecutablePath != executablePath_)
123            {
124                if (!rootPath_.cdUp())
125                    ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?");
126            }
[2710]127
[5693]128#else
129
130            // There is no root path, so don't set it at all
131
132#endif
133        }
134    }
135
[5836]136    PathConfig::~PathConfig()
[5693]137    {
[5836]138    }
139
140    void PathConfig::setConfigurablePaths()
141    {
142        if (bDevRun_)
[5693]143        {
[5836]144            dataPath_         = specialConfig::dataDevDirectory;
145            configPath_       = specialConfig::configDevDirectory;
146            logPath_          = specialConfig::logDevDirectory;
[5693]147        }
148        else
149        {
150
151#ifdef INSTALL_COPYABLE // --> relative paths
152
[2710]153            // Using paths relative to the install prefix, complete them
[5836]154            dataPath_   = rootPath_ / specialConfig::defaultDataPath;
155            configPath_ = rootPath_ / specialConfig::defaultConfigPath;
156            logPath_    = rootPath_ / specialConfig::defaultLogPath;
[5693]157
[2710]158#else
159
[5836]160            dataPath_  = specialConfig::dataInstallDirectory;
[2710]161
162            // Get user directory
163#  ifdef ORXONOX_PLATFORM_UNIX /* Apple? */
164            char* userDataPathPtr(getenv("HOME"));
165#  else
166            char* userDataPathPtr(getenv("APPDATA"));
167#  endif
168            if (userDataPathPtr == NULL)
169                ThrowException(General, "Could not retrieve user data path.");
[7421]170            QDir userDataPath(userDataPathPtr);
[2710]171            userDataPath /= ".orxonox";
172
[5836]173            configPath_ = userDataPath / specialConfig::defaultConfigPath;
174            logPath_    = userDataPath / specialConfig::defaultLogPath;
[5693]175
[2710]176#endif
[5693]177
[2710]178        }
179
[5693]180        // Create directories to avoid problems when opening files in non existent folders.
[7421]181        std::vector<std::pair<QDir, std::string> > directories;
182        directories.push_back(std::make_pair(QDir(configPath_), "config"));
183        directories.push_back(std::make_pair(QDir(logPath_), "log"));
[2710]184
[7421]185        for (std::vector<std::pair<QDir, std::string> >::iterator it = directories.begin();
[2710]186            it != directories.end(); ++it)
187        {
[7421]188            if (!it->first.exists())
[2710]189            {
[7421]190                if (!it->first.mkpath("."))
191                    ThrowException(General, std::string("The ") + it->second + " directory could not be created.");
[2710]192                COUT(4) << "Created " << it->second << " directory" << std::endl;
193            }
194        }
195    }
[2896]196
[5836]197    /*static*/ std::string PathConfig::getRootPathString()
[3370]198    {
[7421]199        return getInstance().rootPath_.path().toStdString() + '/';
[3370]200    }
[5836]201
202    /*static*/ std::string PathConfig::getExecutablePathString()
203    {
[7421]204        return getInstance().executablePath_.path().toStdString() + '/';
[5836]205    }
206
207    /*static*/ std::string PathConfig::getDataPathString()
208    {
[7421]209        return getInstance().dataPath_.path().toStdString() + '/';
[5836]210    }
211
212    /*static*/ std::string PathConfig::getConfigPathString()
213    {
[7421]214        return getInstance().configPath_.path().toStdString() + '/';
[5836]215    }
216
217    /*static*/ std::string PathConfig::getLogPathString()
218    {
[7421]219        return getInstance().logPath_.path().toStdString() + '/';
[5836]220    }
[1505]221}
Note: See TracBrowser for help on using the repository browser.