FileDir FileDir::getApplicationDir() { #ifdef WIN32 unsigned int pathSize=512; ByteArray tmpPath; tmpPath.resize( pathSize ); GetModuleFileName( 0, tmpPath.data(), pathSize); FilePath tmp = tmpPath.data(); tmp = tmp.getUpDir(); //delete tmpPath; return tmp; #elif defined(__linux__) char exe_path[PATH_MAX] = {0}; char * dir_path; sprintf(exe_path, "/proc/%d/exe", getpid()); readlink(exe_path, exe_path, sizeof(exe_path)); dir_path = dirname(exe_path); return FilePath(dir_path); #elif defined(__APPLE__) char exe_path[PROC_PIDPATHINFO_MAXSIZE]; int ret = proc_pidpath(getpid(), exe_path, sizeof(exe_path)); if (ret <= 0) { THROW("Cannot get application executable file path"); } return FilePath(dirname(exe_path)); #endif return FilePath( "." ); }
//! flatten a path and file name for example: "/you/me/../." becomes "/you" FilePath FilePath::flattenFilename( const FilePath& root ) const { std::string directory = addEndSlash().toString(); directory = StringHelper::replace( directory, "\\", "/" ); FilePath dir; FilePath subdir; int lastpos = 0; std::string::size_type pos = 0; bool lastWasRealDir=false; while( ( pos = directory.find( '/', lastpos) ) != std::string::npos ) { subdir = FilePath( directory.substr(lastpos, pos - lastpos + 1) ); if( subdir.toString() == "../" ) { if (lastWasRealDir) { dir = dir.getUpDir(); dir = dir.getUpDir(); lastWasRealDir=( dir.toString().size()!=0); } else { dir = FilePath( dir.toString() + subdir.toString() ); lastWasRealDir=false; } } else if( subdir.toString() == "/") { dir = root; } else if( subdir.toString() != "./" ) { dir = FilePath( dir.toString() + subdir.toString() ); lastWasRealDir=true; } lastpos = pos + 1; } return dir; }
FileDir FileDir::getApplicationDir() { #ifdef WIN32 unsigned int pathSize=512; ByteArray tmpPath; tmpPath.resize( pathSize ); GetModuleFileName( 0, tmpPath.data(), pathSize); FilePath tmp = tmpPath.data(); tmp = tmp.getUpDir(); //delete tmpPath; return tmp; #elif defined(__linux__) char exe_path[PATH_MAX] = {0}; char * dir_path; sprintf(exe_path, "/proc/%d/exe", getpid()); readlink(exe_path, exe_path, sizeof(exe_path)); dir_path = dirname(exe_path); return FilePath(dir_path); #endif // __GNUC__ return FilePath( "" ); }