lugre_findpath.cpp
Go to the documentation of this file.00001 #include "lugre_prefix.h"
00002 #include "lugre_findpath.h"
00003 #include "lugre_robstring.h"
00004 #include <stdexcept>
00005
00006 #include <OgreConfigFile.h>
00007
00008 #ifndef LUGRE_BASE_CONFIG
00009 const std::string kLugreBaseConfig = "lugre.cfg";
00010 #else
00011 const std::string kLugreBaseConfig = (LUGRE_BASE_CONFIG);
00012 #endif
00013
00014 bool file_exists(const std::string &filename){
00015 try {
00016 std::fstream f(filename.c_str(), std::fstream::in);
00017 if(f.good()){
00018 return true;
00019 } else {
00020 return false;
00021 }
00022 }catch(...){
00023 return false;
00024 }
00025 }
00026
00027 #if LUGRE_PLATFORM == LUGRE_PLATFORM_WIN32
00028 #include <windows.h>
00029 bool directory_exists(const std::string &directoryname){
00030 DWORD dw = GetFileAttributes(directoryname.c_str());
00031 if(dw == INVALID_FILE_ATTRIBUTES){
00032 return false;
00033 } else {
00034 return (dw & FILE_ATTRIBUTE_DIRECTORY) > 0;
00035 }
00036 }
00037 #else
00038 #include <sys/types.h>
00039 #include <dirent.h>
00040
00041 bool directory_exists(const std::string &directoryname){
00042 DIR *dir = opendir(directoryname.c_str());
00043 if(dir){
00044 closedir(dir);
00045 return true;
00046 } else {
00047 return false;
00048 }
00049 }
00050 #endif
00051
00052 namespace Lugre {
00053
00054 #if LUGRE_PLATFORM == LUGRE_PLATFORM_PLATFORM_APPLE
00055 #include <CoreFoundation/CoreFoundation.h>
00056
00057
00058
00059
00060 std::string FindBasePaths::macBundlePath()
00061 {
00062 char path[1024];
00063 CFBundleRef mainBundle = CFBundleGetMainBundle();
00064 assert(mainBundle);
00065
00066 CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
00067 assert(mainBundleURL);
00068
00069 CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
00070 assert(cfStringRef);
00071
00072 CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
00073
00074 CFRelease(mainBundleURL);
00075 CFRelease(cfStringRef);
00076
00077 return std::string(path);
00078 }
00079
00080 const char* GetMacDefaultResourcesDir(){
00081 std::string path = strprintf("%s/Contents/Resources/",FindBasePaths::macBundlePath().c_str());
00082 static char static_path[1024];
00083 strncpy(static_path,path.c_str(),1024);
00084 return static_path;
00085 }
00086 #else
00087 std::string FindBasePaths::macBundlePath(){
00088 return std::string();
00089 }
00090
00091
00092 #endif
00093
00094
00095 FindBasePaths::FindBasePaths(){
00096
00097 #ifdef MAIN_WORKING_DIR
00098 mPossibleLugreLuaPaths.push_back(strprintf("%s/lugre/lua/",(MAIN_WORKING_DIR)));
00099 mPossibleMainLuaPaths.push_back(strprintf("%s/lua/main.lua",(MAIN_WORKING_DIR)));
00100 mMainWorkingDir = (MAIN_WORKING_DIR);
00101 #endif
00102
00103 #if LUGRE_PLATFORM == LUGRE_PLATFORM_APPLE
00104 mPossibleLugreLuaPaths.push_back(strprintf("%s/lugre/lua/",(GetMacDefaultResourcesDir())));
00105 mPossibleMainLuaPaths.push_back(strprintf("%s/lua/main.lua",(GetMacDefaultResourcesDir())));
00106 mMainWorkingDir = (GetMacDefaultResourcesDir());
00107 #endif
00108
00109
00110
00111 try {
00112 Ogre::ConfigFile f;
00113 f.load(kLugreBaseConfig);
00114 mPossibleLugreLuaPaths.push_back(f.getSetting("lugre_lua"));
00115 mPossibleMainLuaPaths.push_back(f.getSetting("main_lua"));
00116 mMainWorkingDir = f.getSetting("main_working_dir");
00117 } catch (...){
00118
00119 #if ! (LUGRE_PLATFORM == LUGRE_PLATFORM_APPLE)
00120 #ifndef MAIN_WORKING_DIR
00121 if(directory_exists("../bin/")){
00122 mMainWorkingDir = "../";
00123 } else {
00124 mMainWorkingDir = "./";
00125 }
00126 #endif
00127 #endif
00128 }
00129
00130
00131 if (mMainWorkingDir.size() > 0 && mMainWorkingDir[mMainWorkingDir.size()-1] != '/') mMainWorkingDir.append("/");
00132
00133
00134 mPossibleLugreLuaPaths.push_back("lugre/lua/");
00135 mPossibleLugreLuaPaths.push_back("../lugre/lua/");
00136
00137 mPossibleMainLuaPaths.push_back("lua/main.lua");
00138 mPossibleMainLuaPaths.push_back("../lua/main.lua");
00139 }
00140
00141 std::string FindBasePaths::getMainWorkingDir(){
00142 return mMainWorkingDir;
00143 }
00144
00145 std::string FindBasePaths::getLugreLuaPath(){
00146 for(StringList::const_iterator i = mPossibleLugreLuaPaths.begin(); i != mPossibleLugreLuaPaths.end(); ++i){
00147 std::cout << "probing: " << *i << std::endl;
00148 if(file_exists(strprintf("%s/lugre.lua",(*i).c_str()))){
00149 std::string res = *i;
00150
00151 if (res.size() > 0 && res[res.size()-1] != '/') res.append("/");
00152 return res;
00153 }
00154 }
00155 throw std::runtime_error("no lugre path found");
00156 }
00157
00158 std::string FindBasePaths::getMainLuaPath(){
00159 for(StringList::const_iterator i = mPossibleMainLuaPaths.begin(); i != mPossibleMainLuaPaths.end(); ++i){
00160 std::cout << "probing: " << *i << std::endl;
00161 if(file_exists(*i)){
00162 return *i;
00163 }
00164 }
00165 throw std::runtime_error("no main lua found");
00166 }
00167 }