+
+#if HS_BUILD_FOR_WIN32
+# define PATH_SEPARATOR '\\'
+# define PATH_SEPARATOR_STR "\\"
+#else
+# define PATH_SEPARATOR '/'
+# define PATH_SEPARATOR_STR "/"
+#endif
+
+/** Represents a filename or path, including utilities for manipulating,
+ * splitting, and joining path components.
+ * \sa plFileInfo
+ */
+class plFileName
+{
+public:
+ /** Construct an empty filename. */
+ plFileName() { }
+
+ /** Construct a filename from the UTF-8 character data in \a cstr. */
+ plFileName(const char *cstr) : fName(cstr) { }
+
+ /** Construct a filename from the plString argument \a copy. */
+ plFileName(const plString ©) : fName(copy) { }
+
+ /** Copy constructor. */
+ plFileName(const plFileName ©) : fName(copy.fName) { }
+
+ /** Assignment operator. Same as plFileName(const char *). */
+ plFileName &operator=(const char *cstr)
+ {
+ fName.operator=(cstr);
+ return *this;
+ }
+
+ /** Assignment operator. Same as plFileName(const plString &). */
+ plFileName &operator=(const plString ©)
+ {
+ fName.operator=(copy);
+ return *this;
+ }
+
+ /** Assignment operator. Same as plFileName(const plFileName &). */
+ plFileName &operator=(const plFileName ©)
+ {
+ fName.operator=(copy.fName);
+ return *this;
+ }
+
+ /** Comparison operator. */
+ bool operator==(const char *other) const { return fName.operator==(other); }
+
+ /** Comparison operator. */
+ bool operator==(const plFileName &other) const { return fName.operator==(other.fName); }
+
+ /** Inverse of operator==(const char *other) const. */
+ bool operator!=(const char *other) const { return fName.operator!=(other); }
+
+ /** Inverse of operator==(const plFileName &other) const. */
+ bool operator!=(const plFileName &other) const { return fName.operator!=(other.fName); }
+
+ /** Operator overload for use in containers which depend on \c std::less. */
+ bool operator<(const plFileName &other) const { return fName.Compare(other.fName) < 0; }
+
+ /** Functor which compares two filenames case-insensitively for sorting. */
+ struct less_i
+ {
+ bool operator()(const plFileName &_L, const plFileName &_R) const
+ { return _L.fName.Compare(_R.fName, plString::kCaseInsensitive) < 0; }
+ };
+
+ /** Return whether this filename is valid (not empty). */
+ bool IsValid() const { return !fName.IsEmpty(); }
+
+ /** Return the length of the filename string (UTF-8). */
+ size_t GetSize() const { return fName.GetSize(); }
+
+ /** Convert the filename to a string. This does not resolve relative
+ * paths or normalize slashes, it just returns the stored name string.
+ */
+ const plString &AsString() const { return fName; }
+
+ /** Return the name portion of the path (including extension).
+ * For example:
+ * plFileName("C:\\Path\\Filename.ext") => "Filename.ext"
+ */
+ plString GetFileName() const;
+
+ /** Return the file extension from the filename.
+ * For example:
+ * plFileName("C:\\Path\\Filename.ext") => "ext"
+ */
+ plString GetFileExt() const;
+
+ /** Return the name portion of the path, excluding its extension.
+ * For example:
+ * plFileName("C:\\Path\\Filename.ext") => "Filename"
+ */
+ plString GetFileNameNoExt() const;
+
+ /** Return the path with the filename portion stripped off.
+ * For example:
+ * plFileName("C:\\Path\\Filename.ext") => "C:\\Path"
+ */
+ plFileName StripFileName() const;
+
+ /** Return the filename with the extension stripped off.
+ * For example:
+ * plFileName("C:\\Path\\Filename.ext") => "C:\\Path\\Filename"
+ */
+ plFileName StripFileExt() const;
+
+ /** Normalize slashes to a particular format. By default, we use the
+ * OS's native slash format.
+ * For example:
+ * plFileName("C:\\Path/Filename.ext").Normalize('\\') => "C:\\Path\\Filename.ext"
+ */
+ plFileName Normalize(char slash = PATH_SEPARATOR) const;
+
+ /** Expand relative filenames and ./.. pieces to an absolute path. */
+ plFileName AbsolutePath() const;
+
+ /** Join two path components together with the correct path separator.
+ * For example:
+ * plFileName::Join("C:\\Path", "Filename.ext") => "C:\\Path\\Filename.ext"
+ */
+ 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); }
+
+private:
+ plString fName;
+};
+
+
+/** Structure to get information about a file by name.
+ * \sa plFileName
+ */
+class plFileInfo
+{
+public:
+ /** Construct an invalid plFileInfo which points to no file. */
+ plFileInfo()
+ : fFileSize(-1), fCreateTime(), fModifyTime(), fFlags() { }
+
+ /** Construct a plFileInfo and fill it with info about the specified
+ * file, if it exists.
+ */
+ explicit plFileInfo(const plFileName &filename);
+
+ /** Retrieve the filename associated with this info structure. */
+ const plFileName &FileName() const { return fName; }
+
+ /** Return whether the plFileInfo has been initialized. */
+ bool IsValid() const { return fName.IsValid(); }
+
+ /** Determine whether the file exists on the filesystem. */
+ bool Exists() const { return (fFlags & kEntryExists); }
+
+ /** Returns the size of the file on the disk, in bytes. */
+ int64_t FileSize() const { return fFileSize; }
+
+ /** Returns the creation time of the file. */
+ uint64_t CreateTime() const { return fCreateTime; }
+
+ /** Returns the last modification time of the file. */
+ uint64_t ModifyTime() const { return fModifyTime; }
+
+ /** Returns \p true if this file is a directory. */
+ bool IsDirectory() const { return (fFlags & kIsDirectory); }
+
+ /** Returns \p true if this file is a regular file. */
+ bool IsFile() const { return (fFlags & kIsNormalFile); }
+
+private:
+ plFileName fName;
+ int64_t fFileSize;
+ uint64_t fCreateTime, fModifyTime;
+
+ enum {
+ kEntryExists = (1<<0),
+ kIsDirectory = (1<<1),
+ kIsNormalFile = (1<<2),
+ };
+ uint32_t fFlags;
+};
+
+
+namespace plFileSystem
+{
+ /** Get the current working directory of the application. */
+ plFileName GetCWD();
+
+ /** Open a file using the correct platform fopen API. */
+ FILE *Open(const plFileName &filename, const char *mode);
+
+ /** Delete a file from the filesystem. */
+ bool Unlink(const plFileName &filename);
+
+ /** Move or rename a file. */
+ bool Move(const plFileName &from, const plFileName &to);
+
+ /** Copy a file to a new location. */
+ bool Copy(const plFileName &from, const plFileName &to);
+
+ /** Create a directory. If \a checkParents is \p true, this will also
+ * check the whole path and create any parent directories as needed.
+ */
+ bool CreateDir(const plFileName &dir, bool checkParents = false);
+
+ /** Get the User's data directory. If it doesn't exist, this will
+ * create it.
+ */
+ plFileName GetUserDataPath();
+
+ /** Get the Init script direcotory. If it doesn't exist, this will
+ * create it. */
+ plFileName GetInitPath();
+
+ /** Get the Log output directory. If it doesn't exist, this will
+ * create it. */
+ plFileName GetLogPath();
+}
+
+#endif // plFileSystem_Defined
diff --git a/Sources/Plasma/CoreLib/plString.cpp b/Sources/Plasma/CoreLib/plString.cpp
index 5f390933..5b0be23b 100644
--- a/Sources/Plasma/CoreLib/plString.cpp
+++ b/Sources/Plasma/CoreLib/plString.cpp
@@ -877,110 +877,3 @@ size_t ustrlen(const UniChar *ustr, size_t max)
;
return length;
}
-
-
-/* plFileName */
-static_assert(sizeof(plFileName) == sizeof(plString),
- "plFileName should be a thin wrapper around plString");
-
-plString plFileName::GetFileName() const
-{
- int end = fName.FindLast('/');
- if (end < 0)
- end = fName.FindLast('\\');
- if (end < 0)
- return fName;
-
- return fName.Substr(end + 1);
-}
-
-plString plFileName::GetFileExt() const
-{
- int dot = fName.FindLast('.');
-
- // Be sure not to get a dot in the directory!
- int end = fName.FindLast('/');
- if (end < 0)
- end = fName.FindLast('\\');
-
- if (dot > end)
- return fName.Substr(dot + 1);
-
- return plString::Null;
-}
-
-plString plFileName::GetFileNameNoExt() const
-{
- int dot = fName.FindLast('.');
-
- int end = fName.FindLast('/');
- if (end < 0)
- end = fName.FindLast('\\');
-
- // Be sure not to get a dot in the directory!
- if (dot > end)
- return fName.Substr(end + 1, dot - end - 1);
- return fName.Substr(end + 1);
-}
-
-plFileName plFileName::StripFileName() const
-{
- int end = fName.FindLast('/');
- if (end < 0)
- end = fName.FindLast('\\');
- if (end < 0)
- return *this;
-
- return fName.Left(end);
-}
-
-plFileName plFileName::StripFileExt() const
-{
- int dot = fName.FindLast('.');
-
- // Be sure not to get a dot in the directory!
- int end = fName.FindLast('/');
- if (end < 0)
- end = fName.FindLast('\\');
-
- if (dot > end)
- return fName.Left(dot);
-
- return *this;
-}
-
-plFileName plFileName::Normalize(char slash) const
-{
- plStringBuffer norm;
- char *norm_p = norm.CreateWritableBuffer(fName.GetSize());
- for (const char *p = fName.c_str(); *p; ++p) {
- if (*p == '/' || *p == '\\')
- *norm_p++ = slash;
- else
- *norm_p++ = *p;
- }
- *norm_p = 0;
- return plString(norm);
-}
-
-plFileName plFileName::Join(const plFileName &base, const plFileName &path)
-{
- if (!base.IsValid())
- return path;
- if (!path.IsValid())
- return base;
-
- char last = base.fName.CharAt(base.GetSize() - 1);
- char first = path.fName.CharAt(0);
- if (last != '/' && last != '\\') {
- if (first != '/' && first != '\\') {
- return plString::Format("%s" PATH_SEPARATOR_STR "%s",
- base.fName.c_str(), path.fName.c_str());
- }
- return base.fName + path.fName;
- } else if (first != '/' && first != '\\') {
- return base.fName + path.fName;
- }
- // Both have a slash, but we only need one
- return base.fName + path.fName.Substr(1);
-}
diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h
index 410315cd..38ac1a51 100644
--- a/Sources/Plasma/CoreLib/plString.h
+++ b/Sources/Plasma/CoreLib/plString.h
@@ -223,7 +223,7 @@ public:
* \note This constructor expects the input to be UTF-8 encoded. For
* conversion from ISO-8859-1 8-bit data, use FromIso8859_1().
*/
- plString(const char *cstr) { IConvertFromUtf8(cstr, kSizeAuto); }
+ plString(const char *cstr, size_t size = kSizeAuto) { IConvertFromUtf8(cstr, size); }
/** Copy constructor. */
plString(const plString ©) : fUtf8Buffer(copy.fUtf8Buffer) { }
@@ -539,13 +539,6 @@ public:
static plString Fill(size_t count, char c);
public:
- /** Functor which compares two strings case-sensitively for sorting. */
- struct less
- {
- bool operator()(const plString &_L, const plString &_R) const
- { return _L.Compare(_R, kCaseSensitive) < 0; }
- };
-
/** Functor which compares two strings case-insensitively for sorting. */
struct less_i
{
@@ -553,13 +546,6 @@ public:
{ return _L.Compare(_R, kCaseInsensitive) < 0; }
};
- /** Functor which compares two strings case-sensitively for equality. */
- struct equal
- {
- bool operator()(const plString &_L, const plString &_R) const
- { return _L.Compare(_R, kCaseSensitive) == 0; }
- };
-
/** Functor which compares two strings case-insensitively for equality. */
struct equal_i
{
@@ -660,124 +646,4 @@ 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:
- /** Construct an empty filename. */
- plFileName() { }
-
- /** Construct a filename from the UTF-8 character data in \a cstr. */
- plFileName(const char *cstr) : fName(cstr) { }
-
- /** Construct a filename from the plString argument \a copy. */
- plFileName(const plString ©) : fName(copy) { }
-
- /** Copy constructor. */
- plFileName(const plFileName ©) : fName(copy.fName) { }
-
- /** Assignment operator. Same as plFileName(const char *). */
- plFileName &operator=(const char *cstr)
- {
- fName.operator=(cstr);
- return *this;
- }
-
- /** Assignment operator. Same as plFileName(const plString &). */
- plFileName &operator=(const plString ©)
- {
- fName.operator=(copy);
- return *this;
- }
-
- /** Assignment operator. Same as plFileName(const plFileName &). */
- plFileName &operator=(const plFileName ©)
- {
- fName.operator=(copy.fName);
- return *this;
- }
-
- /** Return whether this filename is valid (not empty). */
- bool IsValid() const { return !fName.IsEmpty(); }
-
- /** Return the length of the filename string (UTF-8). */
- size_t GetSize() const { return fName.GetSize(); }
-
- /** Convert the filename to a string. This does not resolve relative
- * paths or normalize slashes, it just returns the stored name string.
- */
- const plString &AsString() const { return fName; }
-
- /** Return the name portion of the path (including extension).
- * For example:
- * plFileName("C:\\Path\\Filename.ext") => "Filename.ext"
- */
- plString GetFileName() const;
-
- /** Return the file extension from the filename.
- * For example:
- * plFileName("C:\\Path\\Filename.ext") => "ext"
- */
- plString GetFileExt() const;
-
- /** Return the name portion of the path, excluding its extension.
- * For example:
- * plFileName("C:\\Path\\Filename.ext") => "Filename"
- */
- plString GetFileNameNoExt() const;
-
- /** Return the path with the filename portion stripped off.
- * For example:
- * plFileName("C:\\Path\\Filename.ext") => "C:\\Path"
- */
- plFileName StripFileName() const;
-
- /** Return the filename with the extension stripped off.
- * For example:
- * plFileName("C:\\Path\\Filename.ext") => "C:\\Path\\Filename"
- */
- plFileName StripFileExt() const;
-
- /** Normalize slashes to a particular format. By default, we use the
- * OS's native slash format.
- * For example:
- * plFileName("C:\\Path/Filename.ext").Normalize("\\") => "C:\\Path\\Filename.ext"
- */
- plFileName Normalize(char slash = PATH_SEPARATOR) const;
-
- /** Join two path components together with the correct path separator.
- * For example:
- * plFileName::Join("C:\\Path", "Filename.ext") => "C:\\Path\\Filename.ext"
- */
- 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); }
-
-private:
- plString fName;
-};
-
#endif //plString_Defined