Changeset 7421 for sandbox_qt/src/libraries/core/PathConfig.cc
- Timestamp:
- Sep 12, 2010, 12:47:30 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sandbox_qt/src/libraries/core/PathConfig.cc
r6417 r7421 33 33 #include <cstdio> 34 34 #include <vector> 35 #include <boost/version.hpp>36 #include <boost/filesystem.hpp>37 35 38 36 #ifdef ORXONOX_PLATFORM_WINDOWS … … 54 52 #include "util/Debug.h" 55 53 #include "util/Exception.h" 56 #include "CommandLineParser.h"57 58 // Boost 1.36 has some issues with deprecated functions that have been omitted59 #if (BOOST_VERSION == 103600)60 # define BOOST_LEAF_FUNCTION filename61 #else62 # define BOOST_LEAF_FUNCTION leaf63 #endif64 54 65 55 namespace orxonox 66 56 { 67 namespace bf = boost::filesystem;68 69 57 //! Static pointer to the singleton 70 58 PathConfig* PathConfig::singletonPtr_s = 0; 71 59 72 SetCommandLineArgument(externalDataPath, "").information("Path to the external data files");73 SetCommandLineOnlyArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");74 75 60 PathConfig::PathConfig() 76 : rootPath_(*(new bf::path()))77 , executablePath_(*(new bf::path()))78 , modulePath_(*(new bf::path()))79 , dataPath_(*(new bf::path()))80 , externalDataPath_(*(new bf::path()))81 , configPath_(*(new bf::path()))82 , logPath_(*(new bf::path()))83 , bDevRun_(false)84 61 { 85 62 ////////////////////////// … … 125 102 #endif 126 103 127 executablePath_ = bf::path(buffer);104 executablePath_ = QDir(buffer); 128 105 #ifndef ORXONOX_PLATFORM_APPLE 129 executablePath_ = executablePath_.branch_path(); // remove executable name 130 #endif 131 132 ///////////////////// 133 // SET MODULE PATH // 134 ///////////////////// 135 136 if (bf::exists(executablePath_ / "orxonox_dev_build.keep_me")) 106 executablePath_.cdUp(); // remove executable name 107 #endif 108 109 if (executablePath_.exists("orxonox_dev_build.keep_me")) 137 110 { 138 111 COUT(1) << "Running from the build tree." << std::endl; 139 112 PathConfig::bDevRun_ = true; 140 modulePath_ = specialConfig::moduleDevDirectory;141 113 } 142 114 else … … 146 118 147 119 // Also set the root path 148 bf::pathrelativeExecutablePath(specialConfig::defaultRuntimePath);120 QDir relativeExecutablePath(specialConfig::defaultRuntimePath); 149 121 rootPath_ = executablePath_; 150 while (!bf::equivalent(rootPath_ / relativeExecutablePath, executablePath_) && !rootPath_.empty()) 151 rootPath_ = rootPath_.branch_path(); 152 if (rootPath_.empty()) 153 ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?"); 154 155 // Module path is fixed as well 156 modulePath_ = rootPath_ / specialConfig::defaultModulePath; 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 } 157 127 158 128 #else 159 129 160 130 // There is no root path, so don't set it at all 161 // Module path is fixed as well162 modulePath_ = specialConfig::moduleInstallDirectory;163 131 164 132 #endif … … 168 136 PathConfig::~PathConfig() 169 137 { 170 delete &rootPath_;171 delete &executablePath_;172 delete &modulePath_;173 delete &dataPath_;174 delete &externalDataPath_;175 delete &configPath_;176 delete &logPath_;177 138 } 178 139 … … 184 145 configPath_ = specialConfig::configDevDirectory; 185 146 logPath_ = specialConfig::logDevDirectory; 186 187 // Check for data path override by the command line188 if (!CommandLineParser::getArgument("externalDataPath")->hasDefaultValue())189 externalDataPath_ = CommandLineParser::getValue("externalDataPath").getString();190 else191 externalDataPath_ = specialConfig::externalDataDevDirectory;192 147 } 193 148 else … … 213 168 if (userDataPathPtr == NULL) 214 169 ThrowException(General, "Could not retrieve user data path."); 215 bf::pathuserDataPath(userDataPathPtr);170 QDir userDataPath(userDataPathPtr); 216 171 userDataPath /= ".orxonox"; 217 172 … … 223 178 } 224 179 225 // Option to put all the config and log files in a separate folder226 if (!CommandLineParser::getArgument("writingPathSuffix")->hasDefaultValue())227 {228 const std::string& directory(CommandLineParser::getValue("writingPathSuffix").getString());229 configPath_ = configPath_ / directory;230 logPath_ = logPath_ / directory;231 }232 233 180 // Create directories to avoid problems when opening files in non existent folders. 234 std::vector<std::pair< bf::path, std::string> > directories;235 directories.push_back(std::make_pair( bf::path(configPath_), "config"));236 directories.push_back(std::make_pair( bf::path(logPath_), "log"));237 238 for (std::vector<std::pair< bf::path, std::string> >::iterator it = directories.begin();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")); 184 185 for (std::vector<std::pair<QDir, std::string> >::iterator it = directories.begin(); 239 186 it != directories.end(); ++it) 240 187 { 241 if ( bf::exists(it->first) && !bf::is_directory(it->first))188 if (!it->first.exists()) 242 189 { 243 ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \ 244 Please remove " + it->first.string()); 245 } 246 if (bf::create_directories(it->first)) // function may not return true at all (bug?) 247 { 190 if (!it->first.mkpath(".")) 191 ThrowException(General, std::string("The ") + it->second + " directory could not be created."); 248 192 COUT(4) << "Created " << it->second << " directory" << std::endl; 249 193 } … … 251 195 } 252 196 253 std::vector<std::string> PathConfig::getModulePaths()254 {255 std::vector<std::string> modulePaths;256 257 // We search for helper files with the following extension258 const std::string& moduleextension = specialConfig::moduleExtension;259 size_t moduleextensionlength = moduleextension.size();260 261 // Add that path to the PATH variable in case a module depends on another one262 std::string pathVariable(getenv("PATH"));263 putenv(const_cast<char*>(("PATH=" + pathVariable + ';' + modulePath_.string()).c_str()));264 265 // Make sure the path exists, otherwise don't load modules266 if (!boost::filesystem::exists(modulePath_))267 return modulePaths;268 269 boost::filesystem::directory_iterator file(modulePath_);270 boost::filesystem::directory_iterator end;271 272 // Iterate through all files273 while (file != end)274 {275 const std::string& filename = file->BOOST_LEAF_FUNCTION();276 277 // Check if the file ends with the exension in question278 if (filename.size() > moduleextensionlength)279 {280 if (filename.substr(filename.size() - moduleextensionlength) == moduleextension)281 {282 // We've found a helper file283 const std::string& library = filename.substr(0, filename.size() - moduleextensionlength);284 modulePaths.push_back((modulePath_ / library).file_string());285 }286 }287 ++file;288 }289 290 return modulePaths;291 }292 293 197 /*static*/ std::string PathConfig::getRootPathString() 294 198 { 295 return getInstance().rootPath_. string() + '/';199 return getInstance().rootPath_.path().toStdString() + '/'; 296 200 } 297 201 298 202 /*static*/ std::string PathConfig::getExecutablePathString() 299 203 { 300 return getInstance().executablePath_. string() + '/';204 return getInstance().executablePath_.path().toStdString() + '/'; 301 205 } 302 206 303 207 /*static*/ std::string PathConfig::getDataPathString() 304 208 { 305 return getInstance().dataPath_.string() + '/'; 306 } 307 308 /*static*/ std::string PathConfig::getExternalDataPathString() 309 { 310 return getInstance().externalDataPath_.string() + '/'; 209 return getInstance().dataPath_.path().toStdString() + '/'; 311 210 } 312 211 313 212 /*static*/ std::string PathConfig::getConfigPathString() 314 213 { 315 return getInstance().configPath_. string() + '/';214 return getInstance().configPath_.path().toStdString() + '/'; 316 215 } 317 216 318 217 /*static*/ std::string PathConfig::getLogPathString() 319 218 { 320 return getInstance().logPath_.string() + '/'; 321 } 322 323 /*static*/ std::string PathConfig::getModulePathString() 324 { 325 return getInstance().modulePath_.string() + '/'; 219 return getInstance().logPath_.path().toStdString() + '/'; 326 220 } 327 221 }
Note: See TracChangeset
for help on using the changeset viewer.