Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 30, 2015, 12:22:27 PM (9 years ago)
Author:
landauf
Message:

moved static application paths (root, executable, modules) into new class named ApplicationPaths
moved configurable data paths (data, log, config) into new class named ConfigurablePaths
removed PathConfig

File:
1 moved

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/ConfigurablePaths.cc

    r10457 r10509  
    2727 */
    2828
    29 #include "PathConfig.h"
     29#include "ConfigurablePaths.h"
    3030
    3131#include <cassert>
     
    5757// Differentiate Boost Filesystem v2 and v3
    5858#if (BOOST_FILESYSTEM_VERSION < 3)
    59 #  define BF_LEAF leaf
    6059#  define BF_GENERIC_STRING string
    61 #  define BF_NATIVE_STRING file_string
    6260#else
    63 #  define BF_LEAF path().filename().string
    6461#  define BF_GENERIC_STRING generic_string
    65 #  define BF_NATIVE_STRING string
    6662#endif
    6763
     
    7167
    7268    //! Static pointer to the singleton
    73     PathConfig* PathConfig::singletonPtr_s  = 0;
     69    ConfigurablePaths* ConfigurablePaths::singletonPtr_s  = 0;
    7470
    7571    SetCommandLineArgument(externalDataPath, "").information("Path to the external data files");
    7672    SetCommandLineArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");
    7773
    78     PathConfig::PathConfig()
    79         : rootPath_(*(new bf::path()))
    80         , executablePath_(*(new bf::path()))
    81         , modulePath_(*(new bf::path()))
    82         , dataPath_(*(new bf::path()))
     74    ConfigurablePaths::ConfigurablePaths()
     75        : dataPath_(*(new bf::path()))
    8376        , externalDataPath_(*(new bf::path()))
    8477        , configPath_(*(new bf::path()))
    8578        , logPath_(*(new bf::path()))
    86         , bBuildDirectoryRun_(false)
    8779    {
    88         //////////////////////////
    89         // FIND EXECUTABLE PATH //
    90         //////////////////////////
    91 
    92 #ifdef ORXONOX_PLATFORM_WINDOWS
    93         // get executable module
    94         TCHAR buffer[1024];
    95         if (GetModuleFileName(NULL, buffer, 1024) == 0)
    96             ThrowException(General, "Could not retrieve executable path.");
    97 
    98 #elif defined(ORXONOX_PLATFORM_APPLE)
    99         char buffer[1024];
    100         uint32_t path_len = 1023;
    101         if (_NSGetExecutablePath(buffer, &path_len))
    102             ThrowException(General, "Could not retrieve executable path.");
    103 
    104 #else /* Linux */
    105         /* written by Nicolai Haehnle <prefect_@gmx.net> */
    106 
    107         /* Get our PID and build the name of the link in /proc */
    108         char linkname[64]; /* /proc/<pid>/exe */
    109         if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", getpid()) < 0)
    110         {
    111             /* This should only happen on large word systems. I'm not sure
    112                what the proper response is here.
    113                Since it really is an assert-like condition, aborting the
    114                program seems to be in order. */
    115             assert(false);
    116         }
    117 
    118         /* Now read the symbolic link */
    119         char buffer[1024];
    120         int ret;
    121         ret = readlink(linkname, buffer, 1024);
    122         /* In case of an error, leave the handling up to the caller */
    123         if (ret == -1)
    124             ThrowException(General, "Could not retrieve executable path.");
    125 
    126         /* Ensure proper NUL termination */
    127         buffer[ret] = 0;
    128 #endif
    129 
    130         // Remove executable filename
    131         executablePath_ = bf::path(buffer).branch_path();
    132 
    133         /////////////////////
    134         // SET MODULE PATH //
    135         /////////////////////
    136 
    137         if (bf::exists(executablePath_ / "orxonox_dev_build.keep_me"))
    138         {
    139             orxout(internal_info) << "Running from the build tree." << endl;
    140             PathConfig::bBuildDirectoryRun_ = true;
    141             modulePath_ = specialConfig::moduleDevDirectory;
    142         }
    143         else
    144         {
    145 
    146 #ifdef INSTALL_COPYABLE // --> relative paths
    147 
    148             // Also set the root path
    149             bf::path relativeExecutablePath(specialConfig::defaultRuntimePath);
    150             rootPath_ = executablePath_;
    151             while (!bf::equivalent(rootPath_ / relativeExecutablePath, executablePath_) && !rootPath_.empty())
    152                 rootPath_ = rootPath_.branch_path();
    153             if (rootPath_.empty())
    154                 ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?");
    155 
    156             // Module path is fixed as well
    157             modulePath_ = rootPath_ / specialConfig::defaultModulePath;
    158 
    159 #else
    160 
    161             // There is no root path, so don't set it at all
    162             // Module path is fixed as well
    163             modulePath_ = specialConfig::moduleInstallDirectory;
    164 
    165 #endif
    166         }
    16780    }
    16881
    169     PathConfig::~PathConfig()
     82    ConfigurablePaths::~ConfigurablePaths()
    17083    {
    171         delete &rootPath_;
    172         delete &executablePath_;
    173         delete &modulePath_;
    17484        delete &dataPath_;
    17585        delete &externalDataPath_;
     
    17888    }
    17989
    180     void PathConfig::setConfigurablePaths()
     90    void ConfigurablePaths::setConfigurablePaths(const ApplicationPaths& applicationPaths)
    18191    {
    182         if (bBuildDirectoryRun_)
     92        if (applicationPaths.buildDirectoryRun())
    18393        {
    18494            dataPath_         = specialConfig::dataDevDirectory;
     
    198108
    199109            // Using paths relative to the install prefix, complete them
    200             dataPath_   = rootPath_ / specialConfig::defaultDataPath;
    201             configPath_ = rootPath_ / specialConfig::defaultConfigPath;
    202             logPath_    = rootPath_ / specialConfig::defaultLogPath;
     110            dataPath_   = applicationPaths.getRootPath() / specialConfig::defaultDataPath;
     111            configPath_ = applicationPaths.getRootPath() / specialConfig::defaultConfigPath;
     112            logPath_    = applicationPaths.getRootPath() / specialConfig::defaultLogPath;
    203113
    204114#else
     
    252162    }
    253163
    254     std::vector<std::string> PathConfig::getModulePaths()
    255     {
    256         std::vector<std::string> modulePaths;
    257 
    258         // We search for helper files with the following extension
    259         const std::string& moduleextension = specialConfig::moduleExtension;
    260         size_t moduleextensionlength = moduleextension.size();
    261 
    262         // Make sure the path exists, otherwise don't load modules
    263         if (!boost::filesystem::exists(modulePath_))
    264             return modulePaths;
    265 
    266         boost::filesystem::directory_iterator file(modulePath_);
    267         boost::filesystem::directory_iterator end;
    268 
    269         // Iterate through all files
    270         while (file != end)
    271         {
    272             std::string filename = file->BF_LEAF();
    273 
    274             // Check if the file ends with the extension in question
    275             if (filename.size() > moduleextensionlength)
    276             {
    277                 if (filename.substr(filename.size() - moduleextensionlength) == moduleextension)
    278                 {
    279                     // We've found a helper file
    280                     const std::string& library = filename.substr(0, filename.size() - moduleextensionlength);
    281                     modulePaths.push_back(getModulePathString() + library);
    282                 }
    283             }
    284             ++file;
    285         }
    286 
    287         return modulePaths;
    288     }
    289 
    290     /*static*/ std::string PathConfig::getRootPathString()
    291     {
    292         return getInstance().rootPath_.BF_GENERIC_STRING() + '/';
    293     }
    294 
    295     /*static*/ std::string PathConfig::getExecutablePathString()
    296     {
    297         return getInstance().executablePath_.BF_GENERIC_STRING() + '/';
    298     }
    299 
    300     /*static*/ std::string PathConfig::getDataPathString()
     164    /*static*/ std::string ConfigurablePaths::getDataPathString()
    301165    {
    302166        return getInstance().dataPath_.BF_GENERIC_STRING() + '/';
    303167    }
    304168
    305     /*static*/ std::string PathConfig::getExternalDataPathString()
     169    /*static*/ std::string ConfigurablePaths::getExternalDataPathString()
    306170    {
    307171        return getInstance().externalDataPath_.BF_GENERIC_STRING() + '/';
    308172    }
    309173
    310     /*static*/ std::string PathConfig::getConfigPathString()
     174    /*static*/ std::string ConfigurablePaths::getConfigPathString()
    311175    {
    312176        return getInstance().configPath_.BF_GENERIC_STRING() + '/';
    313177    }
    314178
    315     /*static*/ std::string PathConfig::getLogPathString()
     179    /*static*/ std::string ConfigurablePaths::getLogPathString()
    316180    {
    317181        return getInstance().logPath_.BF_GENERIC_STRING() + '/';
    318182    }
    319 
    320     /*static*/ std::string PathConfig::getModulePathString()
    321     {
    322         return getInstance().modulePath_.BF_GENERIC_STRING() + '/';
    323     }
    324183}
Note: See TracChangeset for help on using the changeset viewer.