Browse Source

Add plFileName::Join static method to join paths together correctly

Michael Hansen 12 years ago
parent
commit
c52a1f1037
  1. 20
      Sources/Plasma/CoreLib/plString.cpp
  2. 23
      Sources/Plasma/CoreLib/plString.h
  3. 13
      Sources/Plasma/PubUtilLib/plFile/hsFiles.h

20
Sources/Plasma/CoreLib/plString.cpp

@ -946,3 +946,23 @@ plFileName plFileName::StripFileExt() const
return *this; 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);
}

23
Sources/Plasma/CoreLib/plString.h

@ -657,6 +657,18 @@ private:
size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto); size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto);
#if HS_BUILD_FOR_WIN32
# define PATH_SEPARATOR '\\'
# define WPATH_SEPARATOR L'\\'
# define PATH_SEPARATOR_STR "\\"
# define WPATH_SEPARATOR_STR L"\\"
#else
# define PATH_SEPARATOR '/'
# define WPATH_SEPARATOR L'/'
# define PATH_SEPARATOR_STR "/"
# define WPATH_SEPARATOR_STR L"/"
#endif
/* plFileName - custom extension of plString for manipulating filenames */ /* plFileName - custom extension of plString for manipulating filenames */
class plFileName : public plString class plFileName : public plString
{ {
@ -672,6 +684,17 @@ public:
plFileName StripFileName() const; plFileName StripFileName() const;
plFileName StripFileExt() const; plFileName StripFileExt() const;
static plFileName Join(const plFileName &base, const plFileName &path);
// VS doesn't do variadic templates, and we don't want to use const char *
static plFileName Join(const plFileName &base, const plFileName &path,
const plFileName& path2)
{ return Join(Join(base, path), path2); }
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 #endif //plString_Defined

13
Sources/Plasma/PubUtilLib/plFile/hsFiles.h

@ -54,19 +54,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#endif #endif
#if HS_BUILD_FOR_WIN32
# define PATH_SEPARATOR '\\'
# define WPATH_SEPARATOR L'\\'
# define PATH_SEPARATOR_STR "\\"
# define WPATH_SEPARATOR_STR L"\\"
#elif HS_BUILD_FOR_UNIX
# define PATH_SEPARATOR '/'
# define WPATH_SEPARATOR L'/'
# define PATH_SEPARATOR_STR "/"
# define WPATH_SEPARATOR_STR L"/"
#endif
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
class hsFile { class hsFile {

Loading…
Cancel
Save