CXFilePath
From cxwiki
Contents
Native Encodings
Win32
Windows filepaths are UTF-16 encoded, use backslash ("\") separators, have virtually unlimited length and support a wide range of characters. Unfortunately, the Windows shell imposes a lot of legacy restrictions on paths, such that a typical path is limited to 230 characters, allows a forward slash ("/") separator in most but not all cases, doesn't allow certain characters to appear at all, doesn't allow certain characters to appear at certain locations in the path, and treats certain filenames as special devices. Further complicating this scenario, many Win32 API functions accept shell paths.
The CXFilePath abstract-to-native mappings resolve this as follows:
- If the filepath is longer than 230 characters, and represents a windows network path (ie. is prefixed with "//"), it is converted to a UNC path by stripping the two leading slashes and then prefixing "\\?\UNC\".
- If the filepath is longer than 230 characters, and is an absolute path, it is converted to a UNC path by prefixing "\\?\UNC\"
- Otherwise, the filepath is used as-is.
All forward slash character ("/") are converted to backslash characters ("\").
Posix
Construction
Various constructors are available to allow a CXFilePath to be build from a C String, string classes, etc.
//
CXFilePath(void);
//
CXFilePath(const char* __nullable path);
CXFilePath(const CXString& path);
CXFilePath(const CXStringEdit& path);
template <class OP> inline CXFilePath(const CXFilePathBase<OP>& other);
Nullness
CXFilePath does not distinguish between null and empty states. An empty filepath cannot be used to reference a file (although it can be appended as a relative filepath, to no effect) and so it is typically treated as a sentinel for a null / invalid / unset value.
// Returns true if this CXFilePath object has a non-zero length.
operator bool(void) const;
// Returns true if this CXFilePath is empty.
bool operator !(void) const;
Path Operations
// Returns the filename portion of this path.
CXString GetFilename(void) const;
// Removes and returns the leftmost path element (root).
CXString StripLeftElement(bool bShouldStripFilenameElement = true);
// Remove and return the rightmost path element (filename).
CXString StripRightElement(void);
// Returns the path minus its rightmost path element.
CXString GetParentPath(void) const;
// Returns true if this object represents an absolute path.
// TBD.
bool IsAbsolutePath(void) const;
// Returns true if this object represents the filesystem root.
// TBD.
bool IsRoot(void) const;
// Returns true if this object contains a file name but no path separators.
bool IsFilenameOnly(void) const;
// Attempt to resolve any './' and '../' references, symlinks, etc.
// TBD.
bool Resolve(void);
String Manipulation
//
operator const char* __nonnull (void) const;
//
operator CXStringArgument(void) const;
//
const char& operator[](int pos) const;
//
size_t Length(void) const;
//
operator NSString* __nonnull (void) const;
//
const CXString& AsString(void) const;
//
const char* __nonnull c_str(void) const;
//
char GetFirstChar(void) const;
Comparison
//
bool operator==(const QUAL &p_path) const;
//
bool operator==(const CXString &p_str) const;
//
bool operator==(const char* __nonnull p_str) const;
//
bool operator!=(const QUAL &p_path) const;
//
bool operator!=(const CXString &p_str) const;
//
bool operator!=(const char* __nonnull p_str) const;
// sort operator
bool operator<(const QUAL &other) const;
Helper Methods
// convert to lowercase; useful for filename matching on case-insensitive systems
void ToLower(void);
//
static String GetSafeFilename(const char* __nonnull filename);
//
static CXFilePath ResolvePathRelativeTo(const CXFilePath& relativeOrAbsolutePath, const CXFilePath& basePathForRelative);
//
static CXFilePath GetPathRelativeTo(const CXFilePath& absolutePath, CXFilePath basePathForRelative);
//
static bool IsPathRelativeTo(const CXFilePath& absolutePath, const CXFilePath& basePathForRelative);
//
CXString AsFileURIString(void) const;
Streamers
//
template <CX_STREAMER_TMPL_DECL> CX_STREAMER_QUAL& operator << (CX_STREAMER_QUAL& stream, const CXFilePath& src);
//
template <CX_STREAMER_TMPL_DECL> CX_STREAMER_QUAL& operator >> (CX_STREAMER_QUAL& stream, CXFilePath& dst);
Stringification
//
void CXStringify(CXString& o_result, const CXFilePath& value);