2021-05-05 23:31:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
enum class PathType {
|
|
|
|
UNDEFINED = 0,
|
2021-06-06 08:50:58 +00:00
|
|
|
NATIVE = 1, // Can be relative.
|
|
|
|
CONTENT_URI = 2, // Android only. Can only be absolute!
|
2021-05-05 23:31:38 +00:00
|
|
|
HTTP = 3, // http://, https://
|
|
|
|
};
|
|
|
|
|
|
|
|
// Windows paths are always stored with '/' slashes in a Path.
|
|
|
|
// On .ToWString(), they are flipped back to '\'.
|
|
|
|
|
|
|
|
class Path {
|
|
|
|
private:
|
|
|
|
void Init(const std::string &str);
|
|
|
|
|
|
|
|
public:
|
|
|
|
Path() : type_(PathType::UNDEFINED) {}
|
|
|
|
explicit Path(const std::string &str);
|
|
|
|
|
|
|
|
#if PPSSPP_PLATFORM(WINDOWS)
|
|
|
|
explicit Path(const std::wstring &str);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PathType Type() const {
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Valid() const { return !path_.empty(); }
|
2021-05-09 17:06:02 +00:00
|
|
|
bool IsRoot() const { return path_ == "/"; } // Special value - only path that can end in a slash.
|
2021-05-05 23:31:38 +00:00
|
|
|
|
|
|
|
// Some std::string emulation for simplicity.
|
|
|
|
bool empty() const { return !Valid(); }
|
|
|
|
void clear() {
|
|
|
|
type_ = PathType::UNDEFINED;
|
|
|
|
path_.clear();
|
|
|
|
}
|
2021-05-09 13:02:46 +00:00
|
|
|
size_t size() const {
|
|
|
|
return path_.size();
|
|
|
|
}
|
|
|
|
|
2021-05-29 20:55:43 +00:00
|
|
|
// WARNING: Potentially unsafe usage, if it's not NATIVE.
|
2021-05-05 23:31:38 +00:00
|
|
|
const char *c_str() const {
|
|
|
|
return path_.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAbsolute() const;
|
|
|
|
|
|
|
|
// Returns a path extended with a subdirectory.
|
|
|
|
Path operator /(const std::string &subdir) const;
|
|
|
|
|
|
|
|
// Navigates down into a subdir.
|
|
|
|
void operator /=(const std::string &subdir);
|
|
|
|
|
|
|
|
// File extension manipulation.
|
|
|
|
Path WithExtraExtension(const std::string &ext) const;
|
2021-05-09 13:02:46 +00:00
|
|
|
Path WithReplacedExtension(const std::string &oldExtension, const std::string &newExtension) const;
|
2021-05-09 16:38:48 +00:00
|
|
|
Path WithReplacedExtension(const std::string &newExtension) const;
|
2021-05-05 23:31:38 +00:00
|
|
|
|
|
|
|
// Removes the last component.
|
2021-05-09 17:06:02 +00:00
|
|
|
std::string GetFilename() const; // Really, GetLastComponent. Could be a file or directory. Includes the extension.
|
2021-05-09 16:38:48 +00:00
|
|
|
std::string GetFileExtension() const; // Always lowercase return. Includes the dot.
|
|
|
|
std::string GetDirectory() const;
|
2021-05-05 23:31:38 +00:00
|
|
|
|
|
|
|
const std::string &ToString() const;
|
|
|
|
|
|
|
|
#if PPSSPP_PLATFORM(WINDOWS)
|
|
|
|
std::wstring ToWString() const;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string ToVisualString() const;
|
|
|
|
|
|
|
|
bool CanNavigateUp() const;
|
2021-05-09 16:38:48 +00:00
|
|
|
Path NavigateUp() const;
|
|
|
|
|
2021-06-06 08:19:17 +00:00
|
|
|
// Navigates as far up as possible from this path. If not possible to navigate upwards, returns the same path.
|
|
|
|
// Not actually always the root of the volume, especially on systems like Mac and Linux where things are often mounted.
|
|
|
|
Path GetRootVolume() const;
|
2021-06-01 22:05:04 +00:00
|
|
|
|
|
|
|
std::string PathTo(const Path &child);
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
bool operator ==(const Path &other) const {
|
|
|
|
return path_ == other.path_ && type_ == other.type_;
|
|
|
|
}
|
|
|
|
bool operator !=(const Path &other) const {
|
|
|
|
return path_ != other.path_ || type_ != other.type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FilePathContains(const std::string &needle) const;
|
|
|
|
|
|
|
|
bool StartsWith(const Path &other) const;
|
|
|
|
|
2021-05-09 13:02:23 +00:00
|
|
|
bool operator <(const Path &other) const {
|
|
|
|
return path_ < other.path_;
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
private:
|
|
|
|
// The internal representation is currently always the plain string.
|
|
|
|
// For CPU efficiency we could keep an AndroidStorageContentURI too,
|
|
|
|
// but I don't think the encode/decode cost is significant. We simply create
|
|
|
|
// those for processing instead.
|
|
|
|
std::string path_;
|
|
|
|
|
|
|
|
PathType type_;
|
|
|
|
};
|