1
0
mirror of https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git synced 2025-07-18 11:19:10 +00:00

Merge pull request #259 from zrax/plFileName

Add plFileName
This commit is contained in:
2013-01-14 15:07:56 -08:00
15 changed files with 183 additions and 31 deletions

View File

@ -84,7 +84,7 @@ public:
virtual ~hsStream() { }
// Pre-filename-stringification shortcut:
bool Open_TEMP(const plString & filename, const char * mode = "rb") { return Open(filename.c_str(), mode); }
bool Open_TEMP(const plFileName & filename, const char * mode = "rb") { return Open(filename.c_str(), mode); }
virtual bool Open(const char *, const char * = "rb")=0;
virtual bool Open(const wchar_t *, const wchar_t * = L"rb")=0;

View File

@ -877,3 +877,91 @@ size_t ustrlen(const UniChar *ustr, size_t max)
;
return length;
}
/* plFileName */
plString plFileName::GetFileName() const
{
int end = FindLast('/');
if (end < 0)
end = FindLast('\\');
if (end < 0)
return *this;
return Substr(end + 1);
}
plString plFileName::GetFileExt() const
{
int dot = FindLast('.');
// Be sure not to get a dot in the directory!
int end = FindLast('/');
if (end < 0)
end = FindLast('\\');
if (dot > end)
return Substr(dot + 1);
return plString::Null;
}
plString plFileName::GetFileNameNoExt() const
{
int dot = FindLast('.');
int end = FindLast('/');
if (end < 0)
end = FindLast('\\');
// Be sure not to get a dot in the directory!
if (dot > end)
return Substr(end + 1, dot - end - 1);
return Substr(end + 1);
}
plFileName plFileName::StripFileName() const
{
int end = FindLast('/');
if (end < 0)
end = FindLast('\\');
if (end < 0)
return *this;
return Left(end);
}
plFileName plFileName::StripFileExt() const
{
int dot = FindLast('.');
// Be sure not to get a dot in the directory!
int end = FindLast('/');
if (end < 0)
end = FindLast('\\');
if (dot > end)
return Left(dot);
return *this;
}
plFileName plFileName::Join(const plFileName &base, const plFileName &path)
{
if (base.IsEmpty())
return path;
if (path.IsEmpty())
return base;
char last = base.CharAt(base.GetSize() - 1);
char first = path.CharAt(0);
if (last != '/' && last != '\\') {
if (first != '/' && first != '\\')
return plString::Format("%s" PATH_SEPARATOR_STR "%s", base.c_str(), path.c_str());
return base + path;
} else if (first != '/' && first != '\\') {
return base + path;
}
// Both have a slash, but we only need one
return base + path.Substr(1);
}

View File

@ -660,4 +660,82 @@ private:
/** \p strlen implementation for UniChar based C-style string buffers. */
size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto);
#if HS_BUILD_FOR_WIN32
# define PATH_SEPARATOR '\\'
# define PATH_SEPARATOR_STR "\\"
#else
# define PATH_SEPARATOR '/'
# define PATH_SEPARATOR_STR "/"
#endif
/** Subclass of plString with specific methods to help deal with common
* filename manipulation tasks.
*/
class plFileName : public plString
{
public:
/** Construct an empty filename. */
plFileName() { }
/** Construct a filename from the UTF-8 character data in \a cstr. */
plFileName(const char *cstr) : plString(cstr) { }
/** Construct a filename from the plString argument \a copy. */
plFileName(const plString &copy) : plString(copy) { }
/** Copy constructor. */
plFileName(const plFileName &copy) : plString(copy) { }
/** Return the name portion of the path (including extension).
* For example:
* <pre>plFileName("C:\\Path\\Filename.ext") => "Filename.ext"</pre>
*/
plString GetFileName() const;
/** Return the file extension from the filename.
* For example:
* <pre>plFileName("C:\\Path\\Filename.ext") => "ext"</pre>
*/
plString GetFileExt() const;
/** Return the name portion of the path, excluding its extension.
* For example:
* <pre>plFileName("C:\\Path\\Filename.ext") => "Filename"</pre>
*/
plString GetFileNameNoExt() const;
/** Return the path with the filename portion stripped off.
* For example:
* <pre>plFileName("C:\\Path\\Filename.ext") => "C:\\Path"</pre>
*/
plFileName StripFileName() const;
/** Return the filename with the extension stripped off.
* For example:
* <pre>plFileName("C:\\Path\\Filename.ext") => "C:\\Path\\Filename"</pre>
*/
plFileName StripFileExt() const;
/** Join two path components together with the correct path separator.
* For example:
* <pre>plFileName::Join("C:\\Path", "Filename.ext") => "C:\\Path\\Filename.ext"</pre>
*/
static plFileName Join(const plFileName &base, const plFileName &path);
/** Join three path components together with the correct path separator.
* \todo Make this more efficient.
*/
static plFileName Join(const plFileName &base, const plFileName &path,
const plFileName& path2)
{ return Join(Join(base, path), path2); }
/** Join four path components together with the correct path separator.
* \todo Make this more efficient.
*/
static plFileName Join(const plFileName &base, const plFileName &path,
const plFileName& path2, const plFileName &path3)
{ return Join(Join(Join(base, path), path2), path3); }
};
#endif //plString_Defined