CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/File/Path.h
Views: 1401
#pragma once12#include "ppsspp_config.h"34#include <string>5#include <string_view>67#if defined(__APPLE__)89#if TARGET_OS_IPHONE10#define HOST_IS_CASE_SENSITIVE 111#elif TARGET_IPHONE_SIMULATOR12#define HOST_IS_CASE_SENSITIVE 013#else14// Mac OSX case sensitivity defaults off, but is user configurable (when15// creating a filesytem), so assume the worst:16#define HOST_IS_CASE_SENSITIVE 117#endif1819#elif defined(_WIN32)20#define HOST_IS_CASE_SENSITIVE 02122#else // Android, Linux, BSD (and the rest?)23#define HOST_IS_CASE_SENSITIVE 12425#endif2627enum class PathType {28UNDEFINED = 0,29NATIVE = 1, // Can be relative.30CONTENT_URI = 2, // Android only. Can only be absolute!31HTTP = 3, // http://, https://32};3334// Windows paths are always stored with '/' slashes in a Path.35// On .ToWString(), they are flipped back to '\'.3637class Path {38private:39void Init(std::string_view str);4041public:42Path() : type_(PathType::UNDEFINED) {}43explicit Path(std::string_view str);4445#if PPSSPP_PLATFORM(WINDOWS)46explicit Path(const std::wstring &str);47#endif4849PathType Type() const {50return type_;51}52bool IsLocalType() const {53return type_ == PathType::NATIVE || type_ == PathType::CONTENT_URI;54}5556bool Valid() const { return !path_.empty(); }57bool IsRoot() const { return path_ == "/"; } // Special value - only path that can end in a slash.5859// Some std::string emulation for simplicity.60bool empty() const { return !Valid(); }61void clear() {62type_ = PathType::UNDEFINED;63path_.clear();64}65size_t size() const {66return path_.size();67}6869// WARNING: Potentially unsafe usage, if it's not NATIVE.70const char *c_str() const {71return path_.c_str();72}7374bool IsAbsolute() const;7576// Returns a path extended with a subdirectory.77Path operator /(std::string_view subdir) const;7879// Navigates down into a subdir.80void operator /=(std::string_view subdir);8182// File extension manipulation.83Path WithExtraExtension(std::string_view ext) const;84Path WithReplacedExtension(const std::string &oldExtension, const std::string &newExtension) const;85Path WithReplacedExtension(const std::string &newExtension) const;8687std::string GetFilename() const; // Really, GetLastComponent. Could be a file or directory. Includes the extension.88std::string GetFileExtension() const; // Always lowercase return. Includes the dot.89// Removes the last component.90std::string GetDirectory() const;9192const std::string &ToString() const;9394#if PPSSPP_PLATFORM(WINDOWS)95std::wstring ToWString() const;96std::string ToCString() const; // Flips the slashes back to Windows standard, but string still UTF-8.97#else98std::string ToCString() const {99return ToString();100}101#endif102103// Pass in a relative root to turn the path into a relative path - if it is one!104std::string ToVisualString(const char *relativeRoot = nullptr) const;105106bool CanNavigateUp() const;107Path NavigateUp() const;108109// Navigates as far up as possible from this path. If not possible to navigate upwards, returns the same path.110// Not actually always the root of the volume, especially on systems like Mac and Linux where things are often mounted.111// For Android directory trees, navigates to the root of the tree.112Path GetRootVolume() const;113114bool ComputePathTo(const Path &other, std::string &path) const;115116bool operator ==(const Path &other) const {117return path_ == other.path_ && type_ == other.type_;118}119bool operator !=(const Path &other) const {120return path_ != other.path_ || type_ != other.type_;121}122123bool FilePathContainsNoCase(std::string_view needle) const;124125bool StartsWith(const Path &other) const;126127bool operator <(const Path &other) const {128return path_ < other.path_;129}130bool operator >(const Path &other) const {131return path_ > other.path_;132}133134private:135// The internal representation is currently always the plain string.136// For CPU efficiency we could keep an AndroidStorageContentURI too,137// but I don't think the encode/decode cost is significant. We simply create138// those for processing instead.139std::string path_;140141PathType type_;142};143144// Utility function for parsing out file extensions.145std::string GetExtFromString(std::string_view str);146147// Utility function for fixing the case of paths. Only present on Unix-like systems.148149#if HOST_IS_CASE_SENSITIVE150151enum FixPathCaseBehavior {152FPC_FILE_MUST_EXIST, // all path components must exist (rmdir, move from)153FPC_PATH_MUST_EXIST, // all except the last one must exist - still tries to fix last one (fopen, move to)154FPC_PARTIAL_ALLOWED, // don't care how many exist (mkdir recursive)155};156157bool FixPathCase(const Path &basePath, std::string &path, FixPathCaseBehavior behavior);158159#endif160161162