// // Generic Bundles // Bundle::Bundle(const char *path, const char *execPath /* = NULL */) : mPath(path), mBundle(NULL) { if (execPath) // caller knows that one; set it mExecutablePath = execPath; secdebug("bundle", "%p Bundle from path %s(%s)", this, path, executablePath().c_str()); }
std::string GetExecutablePathWithName() { std::vector<char>executablePath(MAX_PATH); // Try to get the executable path with a buffer of MAX_PATH characters. DWORD result = ::GetModuleFileName( nullptr, &executablePath[0], static_cast<DWORD>(executablePath.size()) ); // As long the function returns the buffer size, it is indicating that the buffer // was too small. Keep enlarging the buffer by a factor of 2 until it fits. while (result == executablePath.size()) { executablePath.resize(executablePath.size() * 2); result = ::GetModuleFileName( nullptr, &executablePath[0], static_cast<DWORD>(executablePath.size()) ); } // If the function returned 0, something went wrong if (result == 0) { DEBUGWARNING(("Failed to find path of executable! Current working directory will be tried as path.")); } // We've got the path, construct a standard string from it return std::string(executablePath.begin(), executablePath.begin() + result); }
std::string utils::getExectablePath() { std::vector<char> executablePath(MAX_PATH); // Try to get the executable path with a buffer of MAX_PATH characters. DWORD result = ::GetModuleFileNameA( nullptr, &executablePath[0], static_cast<DWORD>(executablePath.size()) ); // As long the function returns the buffer size, it is indicating that the buffer // was too small. Keep enlarging the buffer by a factor of 2 until it fits. while(result == executablePath.size()) { executablePath.resize(executablePath.size() * 2); result = ::GetModuleFileNameA( nullptr, &executablePath[0], static_cast<DWORD>(executablePath.size()) ); } // If the function returned 0, something went wrong if(result == 0) { throw std::runtime_error("GetModuleFileName() failed"); } // We've got the path, construct a standard string from it return std::string(executablePath.begin(), executablePath.begin() + result); }
std::string GetExecutablePath() { #ifdef WIN32 HMODULE hModule = GetModuleHandle(NULL); if(hModule==NULL) throw std::runtime_error("Failed to obtain module handle."); CHAR path[MAX_PATH]; if(GetModuleFileName(hModule, path, MAX_PATH)==0) throw std::runtime_error("Failed to obtain excutable path."); std::string executablePath(path); std::string::size_type s = executablePath.find_last_of('\\'); if(s==std::string::npos) throw std::runtime_error("Failed to parse path returned by GetModuleFileName()."); executablePath = executablePath.substr(0, s); return executablePath; #else char buff[1024]; ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1); if (len == -1) throw std::runtime_error("Failed to retrieve executable path."); buff[len] = '\0'; std::string executablePath(buff); std::string::size_type s = executablePath.find_last_of('/'); if(s==std::string::npos) throw std::runtime_error("Failed to parse path returned by GetModuleFileName()."); executablePath = executablePath.substr(0, s); return executablePath; #endif }
std::string Configuration::validateConfigFile(std::string const &configFile) { static std::string exe_path = executablePath(); if(boost::filesystem::extension(configFile) != ".json") { throw Exception("Configuration cannot read non-json config files."); } if(boost::filesystem::exists(configFile)) { res_path = ""; } else { res_path = exe_path; } return res_path + configFile; }
int main( int argc, char** argv ) { //std::ofstream logFile( "Log.txt" ); //SET_LOUT( logFile ); //D_COMMAND( std::ofstream debugFile( "Debug.txt" ); ); //SET_DOUT( debugFile ); //XInitThreads(); ApplicationManager appManager; boost::filesystem::path executablePath(argv[0]); boost::filesystem::path dataDirName = GET_SETTINGS( "application.data_directory", std::string, (boost::filesystem::path(argv[0]).parent_path() / "data").string() ); //If we cannot locate data directory - try other posiible locations if (!boost::filesystem::exists(dataDirName) || !boost::filesystem::is_directory(dataDirName)) { std::vector<boost::filesystem::path> possibleDataDirs; possibleDataDirs.push_back(boost::filesystem::current_path() / "data"); possibleDataDirs.push_back(executablePath.parent_path() / "data"); possibleDataDirs.push_back(executablePath.parent_path().parent_path() / "data"); std::vector<boost::filesystem::path>::const_iterator it = possibleDataDirs.begin(); bool found = false; LOG( "Trying to locate 'data' directory:" ); while (!found && it != possibleDataDirs.end()) { LOG_CONT( "\tChecking: " << it->string() << " ... "); if (boost::filesystem::exists(*it) && boost::filesystem::is_directory(*it)) { dataDirName = *it; SET_SETTINGS( "application.data_directory", std::string, dataDirName.string() ); found = true; LOG( "SUCCESS" ); } else { LOG( "FAILED" ); } ++it; } if (!found) { BOOST_THROW_EXCEPTION( M4D::ErrorHandling::EDirNotFound() ); } } boost::filesystem::path dirName = GET_SETTINGS( "gui.icons_directory", std::string, ( dataDirName / "icons" ).string() ); appManager.setIconsDirectory(dirName); appManager.initialize( argc, argv ); try { //processCommandLine( argc, argv ); ViewerWindow viewer; appManager.setMainWindow( viewer ); createModules(); appManager.loadModules(); viewer.showMaximized(); return appManager.exec(); } catch ( std::exception &e ) { QMessageBox::critical ( NULL, "Exception", QString( e.what() ) ); } catch (...) { QMessageBox::critical ( NULL, "Exception", "Unknown error" ); } return 1; }