Common: Add functions to decompose a Path into its components
This commit is contained in:
parent
cb8718dcdc
commit
f2849282a6
2 changed files with 42 additions and 2 deletions
|
@ -32,7 +32,10 @@ Path::Path(const char *str, char separator) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Path::Path(const String &str, char separator) {
|
Path::Path(const String &str, char separator) {
|
||||||
set(str.c_str(), separator);
|
if (separator == DIR_SEPARATOR)
|
||||||
|
_str = str;
|
||||||
|
else
|
||||||
|
set(str.c_str(), separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
String Path::toString(char separator) const {
|
String Path::toString(char separator) const {
|
||||||
|
@ -46,6 +49,24 @@ String Path::toString(char separator) const {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Path Path::getParent() const {
|
||||||
|
if (_str.size() < 2)
|
||||||
|
return Path();
|
||||||
|
size_t separatorPos = _str.findLastOf(DIR_SEPARATOR, _str.size() - 2);
|
||||||
|
if (separatorPos == Common::String::npos)
|
||||||
|
return Path();
|
||||||
|
return Path(_str.substr(0, separatorPos + 1), DIR_SEPARATOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
Path Path::getLastComponent() const {
|
||||||
|
if (_str.size() < 2)
|
||||||
|
return *this;
|
||||||
|
size_t separatorPos = _str.findLastOf(DIR_SEPARATOR, _str.size() - 2);
|
||||||
|
if (separatorPos == Common::String::npos)
|
||||||
|
return *this;
|
||||||
|
return Path(_str.substr(separatorPos + 1), DIR_SEPARATOR);
|
||||||
|
}
|
||||||
|
|
||||||
bool Path::operator==(const Path &x) const {
|
bool Path::operator==(const Path &x) const {
|
||||||
return _str == x.rawString();
|
return _str == x.rawString();
|
||||||
}
|
}
|
||||||
|
@ -84,7 +105,10 @@ Path &Path::appendInPlace(const Path &x) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Path &Path::appendInPlace(const String &str, char separator) {
|
Path &Path::appendInPlace(const String &str, char separator) {
|
||||||
appendInPlace(str.c_str(), separator);
|
if (separator == DIR_SEPARATOR)
|
||||||
|
_str += str;
|
||||||
|
else
|
||||||
|
appendInPlace(str.c_str(), separator);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,22 @@ public:
|
||||||
*/
|
*/
|
||||||
String toString(char separator = '/') const;
|
String toString(char separator = '/') const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Path for the parent directory of this path.
|
||||||
|
*
|
||||||
|
* Appending the getLastComponent() of a path to getParent() returns a path
|
||||||
|
* identical to the original path.
|
||||||
|
*/
|
||||||
|
Path getParent() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the last component of this path.
|
||||||
|
*
|
||||||
|
* Appending the getLastComponent() of a path to getParent() returns a path
|
||||||
|
* identical to the original path.
|
||||||
|
*/
|
||||||
|
Path getLastComponent() const;
|
||||||
|
|
||||||
/** Check whether this path is identical to path @p x. */
|
/** Check whether this path is identical to path @p x. */
|
||||||
bool operator==(const Path &x) const;
|
bool operator==(const Path &x) const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue