diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index 34fac020..09dc7c79 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -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; diff --git a/Sources/Plasma/CoreLib/plString.cpp b/Sources/Plasma/CoreLib/plString.cpp index cd24209b..833cd11b 100644 --- a/Sources/Plasma/CoreLib/plString.cpp +++ b/Sources/Plasma/CoreLib/plString.cpp @@ -878,3 +878,71 @@ 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 + 1); +} + +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; +} diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h index 4a60b427..85b20710 100644 --- a/Sources/Plasma/CoreLib/plString.h +++ b/Sources/Plasma/CoreLib/plString.h @@ -656,4 +656,22 @@ private: /** \p strlen implementation for UniChar based C-style string buffers. */ size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto); + +/* plFileName - custom extension of plString for manipulating filenames */ +class plFileName : public plString +{ +public: + plFileName() { } + plFileName(const char *cstr) : plString(cstr) { } + plFileName(const plString ©) : plString(copy) { } + plFileName(const plFileName ©) : plString(copy) { } + + plString GetFileName() const; + plString GetFileExt() const; + plString GetFileNameNoExt() const; + + plFileName StripFileName() const; + plFileName StripFileExt() const; +}; + #endif //plString_Defined