Browse Source

Add plFileName string specialization for filename manipulation

Michael Hansen 12 years ago
parent
commit
01e25e5d8d
  1. 2
      Sources/Plasma/CoreLib/hsStream.h
  2. 68
      Sources/Plasma/CoreLib/plString.cpp
  3. 18
      Sources/Plasma/CoreLib/plString.h

2
Sources/Plasma/CoreLib/hsStream.h

@ -84,7 +84,7 @@ public:
virtual ~hsStream() { } virtual ~hsStream() { }
// Pre-filename-stringification shortcut: // 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 char *, const char * = "rb")=0;
virtual bool Open(const wchar_t *, const wchar_t * = L"rb")=0; virtual bool Open(const wchar_t *, const wchar_t * = L"rb")=0;

68
Sources/Plasma/CoreLib/plString.cpp

@ -878,3 +878,71 @@ size_t ustrlen(const UniChar *ustr, size_t max)
; ;
return length; 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;
}

18
Sources/Plasma/CoreLib/plString.h

@ -656,4 +656,22 @@ private:
/** \p strlen implementation for UniChar based C-style string buffers. */ /** \p strlen implementation for UniChar based C-style string buffers. */
size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto); 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 &copy) : plString(copy) { }
plFileName(const plFileName &copy) : plString(copy) { }
plString GetFileName() const;
plString GetFileExt() const;
plString GetFileNameNoExt() const;
plFileName StripFileName() const;
plFileName StripFileExt() const;
};
#endif //plString_Defined #endif //plString_Defined

Loading…
Cancel
Save