void FastOS_FileInterface::EmptyDirectory( const char *dir, const char *keepFile /* = nullptr */ ) { FastOS_StatInfo statInfo; if (!FastOS_File::Stat(dir, &statInfo)) return; // Fail if the directory does not exist FastOS_DirectoryScan dirScan( dir ); while (dirScan.ReadNext()) { if (strcmp(dirScan.GetName(), ".") != 0 && strcmp(dirScan.GetName(), "..") != 0 && (keepFile == nullptr || strcmp(dirScan.GetName(), keepFile) != 0)) { std::string name = dir; name += GetPathSeparator(); name += dirScan.GetName(); if (dirScan.IsDirectory()) { EmptyAndRemoveDirectory(name.c_str()); } else { if ( ! FastOS_File::Delete(name.c_str()) ) { std::ostringstream os; os << "Failed deleting file '" << name << "' due to " << getLastErrorString(); throw std::runtime_error(os.str().c_str()); } } } } }
/** * init app, script and data paths */ bool InitPaths(const char* argv0) { PROFILE_FUNC(); //The method currently only works if the path is explicitly specified //during startup or if UG4_ROOT is defined. // extract the application path. char* ug4Root = getenv("UG4_ROOT"); const char* pathSep = GetPathSeparator(); std::string strRoot = ""; if(ug4Root){ strRoot = ug4Root; } else{ std::string tPath = argv0; size_t pos = tPath.find_last_of(pathSep); if (pos != std::string::npos) tPath = tPath.substr(0, pos); else tPath = "."; strRoot = tPath + pathSep + ".."; } if(!PathProvider::has_path(ROOT_PATH)) PathProvider::set_path(ROOT_PATH, strRoot); if(!PathProvider::has_path(BIN_PATH)) PathProvider::set_path(BIN_PATH, strRoot + pathSep + "bin"); if(!PathProvider::has_path(SCRIPT_PATH)) PathProvider::set_path(SCRIPT_PATH, strRoot + pathSep + "ugcore/scripts"); if(!PathProvider::has_path(DATA_PATH)) PathProvider::set_path(DATA_PATH, strRoot + pathSep + "data"); if(!PathProvider::has_path(GRID_PATH)) PathProvider::set_path(GRID_PATH, strRoot + pathSep + "data" + pathSep + "grids"); if(!PathProvider::has_path(PLUGIN_PATH)) PathProvider::set_path(PLUGIN_PATH, strRoot + pathSep + "bin" + pathSep + "plugins"); if(!PathProvider::has_path(APPS_PATH)) PathProvider::set_path(APPS_PATH, strRoot + pathSep + "apps"); // log the paths UG_DLOG(MAIN, 1, "app path set to: " << PathProvider::get_path(BIN_PATH) << std::endl << "script path set to: " << PathProvider::get_path(SCRIPT_PATH) << std::endl << "data path set to: " << PathProvider::get_path(DATA_PATH) << std::endl); /* if(!script::FileExists(PathProvider::get_path(BIN_PATH).c_str()) || !script::FileExists(PathProvider::get_path(SCRIPT_PATH).c_str()) || !script::FileExists(PathProvider::get_path(DATA_PATH).c_str())) { UG_LOG("WARNING: paths were not initialized correctly.\n"); return false; } */ return true; }
/** * init app, script and data paths for a given root path */ void SetRootPath(const std::string& strRoot) { PROFILE_FUNC(); const char* pathSep = GetPathSeparator(); PathProvider::set_path(ROOT_PATH, strRoot); PathProvider::set_path(BIN_PATH, strRoot + pathSep + "bin"); PathProvider::set_path(SCRIPT_PATH, strRoot + pathSep + "scripts"); PathProvider::set_path(DATA_PATH, strRoot + pathSep + "data"); PathProvider::set_path(GRID_PATH, strRoot + pathSep + "data" + pathSep + "grids"); PathProvider::set_path(PLUGIN_PATH, strRoot + pathSep + "bin" + pathSep + "plugins"); PathProvider::set_path(APPS_PATH, strRoot + pathSep + "apps"); }
StringVector GetSystemPATH(const String& PATH) { StringVector Results; const Char8 Sep = GetPathSeparator(); String OneEntry; for( String::const_iterator Current = PATH.begin() ; PATH.end()!=Current ; ++Current ) { if(Sep==*Current) { Results.push_back(OneEntry); OneEntry.clear(); }else{ OneEntry += *Current; } } return Results; }
bool PrependPathToEnvironmentVar(const char *variableName, const char *path) { std::string oldValue = GetEnvironmentVar(variableName); const char *newValue = nullptr; std::string buf; if (oldValue.empty()) { newValue = path; } else { buf = path; buf += GetPathSeparator(); buf += oldValue; newValue = buf.c_str(); } return SetEnvironmentVar(variableName, newValue); }
bool cFile::CreateFolderRecursive(const AString & a_FolderPath) { // Special case: Fail if the path is empty if (a_FolderPath.empty()) { return false; } // Go through each path element and create the folder: auto len = a_FolderPath.length(); auto PathSep = GetPathSeparator()[0]; for (decltype(len) i = 0; i < len; i++) { if (a_FolderPath[i] == PathSep) { CreateFolder(a_FolderPath.substr(0, i)); } } CreateFolder(a_FolderPath); // Check the result by querying whether the final path exists: return IsFolder(a_FolderPath); }
bool cFile::DeleteFolderContents(const AString & a_FolderName) { auto Contents = cFile::GetFolderContents(a_FolderName); for (const auto & item: Contents) { // Skip "." and ".." altogether: if ((item == ".") || (item == "..")) { continue; } // Remove the item: auto WholePath = a_FolderName + GetPathSeparator() + item; if (IsFolder(WholePath)) { if (!DeleteFolderContents(WholePath)) { return false; } if (!DeleteFolder(WholePath)) { return false; } } else { if (!DeleteFile(WholePath)) { return false; } } } // for item - Contents[] // All deletes succeeded return true; }
void GetFilePath(char *dest, const char *filename) { static const char *working_directory = GetDataPath(); strcpy(dest, working_directory); strcat(dest, GetPathSeparator()); strcat(dest, filename); }