| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- #pragma once
- namespace BansheeEngine
- {
- /**
- * @brief Class for storing and manipulating file paths. Paths may be parsed
- * from and to raw strings according to various platform specific path
- * types.
- *
- * @note In order to allow the system to easily distinguish between file and directory
- * paths, try to ensure that all directory paths end with a separator (\ or / depending
- * on platform). System won't fail if you don't but it will be easier to misuse.
- */
- class BS_UTILITY_EXPORT Path
- {
- public:
- enum class PathType
- {
- Windows,
- Unix,
- Default
- };
- public:
- Path();
- /**
- * @brief Constructs a path by parsing the provided path string.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- Path(const WString& pathStr, PathType type = PathType::Default);
- /**
- * @brief Constructs a path by parsing the provided path string.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- Path(const String& pathStr, PathType type = PathType::Default);
- /**
- * @brief Constructs a path by parsing the provided path null terminated string.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- Path(wchar_t* pathStr, PathType type = PathType::Default);
- /**
- * @brief Constructs a path by parsing the provided path null terminated string.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- Path(const char* pathStr, PathType type = PathType::Default);
- Path(const Path& other);
- /**
- * @brief Assigns a path by parsing the provided path string. Path will be
- * parsed according to the rules of the platform the application is
- * being compiled to.
- */
- Path& operator= (const WString& pathStr);
- /**
- * @brief Assigns a path by parsing the provided path string. Path will be
- * parsed according to the rules of the platform the application is
- * being compiled to.
- */
- Path& operator= (const String& pathStr);
- /**
- * @brief Assigns a path by parsing the provided path null terminated string.
- * Path will be parsed according to the rules of the platform the
- * application is being compiled to.
- */
- Path& operator= (const wchar_t* pathStr);
- /**
- * @brief Assigns a path by parsing the provided path null terminated string.
- * Path will be parsed according to the rules of the platform the
- * application is being compiled to.
- */
- Path& operator= (const char* pathStr);
- Path& operator= (const Path& path);
- /**
- * @brief Compares two paths and returns true if they match. Comparison is
- * case insensitive and paths will be compared as-is, without canonization.
- */
- bool operator== (const Path& path) const { return equals(path); }
- /**
- * @brief Compares two paths and returns true if they don't match. Comparison is
- * case insensitive and paths will be compared as-is, without canonization.
- */
- bool operator!= (const Path& path) const { return !equals(path); }
- /**
- * @brief Gets a directory name with the specified index from the path.
- */
- const WString& operator[] (UINT32 idx) const { return getWDirectory(idx); }
- /**
- * @brief Swap internal data with another Path object.
- */
- void swap(Path& path);
- /**
- * @brief Create a path from another Path object.
- */
- void assign(const Path& path);
- /**
- * @brief Constructs a path by parsing the provided path string.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- void assign(const WString& pathStr, PathType type = PathType::Default);
- /**
- * @brief Constructs a path by parsing the provided path string.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- void assign(const String& pathStr, PathType type = PathType::Default);
- /**
- * @brief Constructs a path by parsing the provided path null terminated string.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- void assign(const wchar_t* pathStr, PathType type = PathType::Default);
- /**
- * @brief Constructs a path by parsing the provided path null terminated string.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- void assign(const char* pathStr, PathType type = PathType::Default);
- /**
- * @brief Converts the path in a string according to platform path rules.
- *
- * @type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- WString toWString(PathType type = PathType::Default) const;
- /**
- * @brief Converts the path in a string according to platform path rules.
- *
- * @type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- String toString(PathType type = PathType::Default) const;
- /**
- * @brief Checks is the path a directory (contains no file-name).
- */
- bool isDirectory() const { return mFilename.empty(); }
- /**
- * @brief Checks does the path point to a file.
- */
- bool isFile() const { return !mFilename.empty(); }
- /**
- * @brief Checks is the contained path absolute.
- */
- bool isAbsolute() const { return mIsAbsolute; }
- /**
- * @brief Returns parent path. If current path points to a file
- * the parent path will be the folder where the file is located.
- * Or if it contains a directory the parent will be the parent directory.
- * If no parent exists, same path will be returned.
- */
- Path getParent() const;
- /**
- * @brief Returns an absolute path by appending the current path to the provided base.
- * If path was already absolute no changes are made and copy of current path
- * is returned.
- * If base is not absolute, then the returned path will be made relative to base,
- * but will not be absolute.
- */
- Path getAbsolute(const Path& base) const;
- /**
- * @brief Returns a relative path by making the current path relative to the provided base.
- * Base must be a part of the current path. If base is not a part
- * of the path no changes are made and a copy of the current path
- * is returned.
- */
- Path getRelative(const Path& base) const;
- /**
- * @brief Returns the path as a path to directory. If path was pointing
- * to a file, the filename is removed, otherwise no changes are made
- * and exact copy is returned.
- */
- Path getDirectory() const;
- /**
- * @brief Makes the path the parent of the current path. If current path points to a file
- * the parent path will be the folder where the file is located.
- * Or if it contains a directory the parent will be the parent directory.
- * If no parent exists, same path will be returned.
- */
- Path& makeParent();
- /**
- * @brief Makes the current path absolute by appending it to base.
- * If path was already absolute no changes are made and copy of current path
- * is returned.
- * If base is not absolute, then the returned path will be made relative to base,
- * but will not be absolute.
- */
- Path& makeAbsolute(const Path& base);
- /**
- * @brief Makes the current path relative to the provided base.
- * Base must be a part of the current path. If base is not a part
- * of the path no changes are made and a copy of the current path
- * is returned.
- */
- Path& makeRelative(const Path& base);
- /**
- * @brief Appends another path to the end of this path.
- */
- Path& append(const Path& path);
- /**
- * @brief Checks if the current path contains the provided path.
- * Comparison is case insensitive and paths will be compared
- * as-is, without canonization.
- */
- bool includes(const Path& child) const;
- /**
- * @brief Compares two paths and returns true if they match. Comparison is
- * case insensitive and paths will be compared as-is, without canonization.
- */
- bool equals(const Path& other) const;
- /**
- * @brief Change or set the filename in the path.
- */
- void setFilename(const WString& filename) { mFilename = filename; }
- /**
- * @brief Change or set the filename in the path.
- */
- void setFilename(const String& filename) { mFilename = BansheeEngine::toWString(filename); }
- /**
- * @brief Change or set the base name in the path. Base name changes the
- * filename by changing its base to the provided value but keeping extension intact.
- */
- void setBasename(const WString& basename);
- /**
- * @brief Change or set the base name in the path. Base name changes the
- * filename by changing its base to the provided value but keeping extension intact.
- */
- void setBasename(const String& basename);
- /**
- * @brief Change or set the extension of the filename in the path.
- *
- * @param extension Extension with a leading ".".
- */
- void setExtension(const WString& extension);
- /**
- * @brief Change or set the extension of the filename in the path.
- *
- * @param extension Extension with a leading ".".
- */
- void setExtension(const String& extension);
- /**
- * @brief Returns a filename in the path.
- *
- * @param extension If true, returned filename will contain an extension.
- */
- WString getWFilename(bool extension = true) const;
- /**
- * @brief Returns a filename in the path.
- *
- * @param extension If true, returned filename will contain an extension.
- */
- String getFilename(bool extension = true) const;
- /**
- * @brief Returns file extension with the leading ".".
- */
- WString getWExtension() const;
- /**
- * @brief Returns file extension with the leading ".".
- */
- String getExtension() const;
- /**
- * @brief Gets the number of directories in the path.
- */
- UINT32 getNumDirectories() const { return (UINT32)mDirectories.size(); }
- /**
- * @brief Gets a directory name with the specified index from the path.
- */
- const WString& getWDirectory(UINT32 idx) const;
- /**
- * @brief Gets a directory name with the specified index from the path.
- */
- String getDirectory(UINT32 idx) const;
- /**
- * @brief Returns path device (e.g. drive, volume, etc.) if one exists in the path.
- */
- const WString& getWDevice() const { return mDevice; }
- /**
- * @brief Returns path device (e.g. drive, volume, etc.) if one exists in the path.
- */
- String getDevice() const { return BansheeEngine::toString(mDevice); }
- /**
- * @brief Returns path node (e.g. network name) if one exists in the path.
- */
- const WString& getWNode() const { return mNode; }
- /**
- * @brief Returns path node (e.g. network name) if one exists in the path.
- */
- String getNode() const { return BansheeEngine::toString(mNode); }
- /**
- * @brief Gets last element in the path, filename if it exists, otherwise the last directory.
- * If no directories exist returns device or node.
- *
- * @param type Determines format of node or device, in case they are returned. When default,
- * format for the active platform will be used, otherwise the format defined
- * by the parameter will be used.
- */
- WString getWTail(PathType type = PathType::Default) const;
- /**
- * @brief Gets last element in the path, filename if it exists, otherwise the last directory.
- * If no directories exist returns device or node.
- *
- * @param type Determines format of node or device, in case they are returned. When default,
- * format for the active platform will be used, otherwise the format defined
- * by the parameter will be used.
- */
- String getTail(PathType type = PathType::Default) const;
- /**
- * @brief Clears the path to nothing.
- */
- void clear();
- /**
- * @brief Returns true if no path has been set.
- */
- bool isEmpty() const { return mDirectories.empty() && mFilename.empty() && mDevice.empty() && mNode.empty(); }
- /**
- * @brief Concatenates two paths.
- */
- Path operator+ (const Path& rhs) const;
- /**
- * @brief Concatenates two paths.
- */
- Path& operator+= (const Path& rhs);
- /**
- * @brief Compares two path elements (i.e. filenames, directory names, etc.)
- */
- static bool comparePathElem(const WString& left, const WString& right);
- /**
- * @brief Combines two paths and returns the result. Right path should be relative.
- */
- static Path combine(const Path& left, const Path& right);
- static const Path BLANK;
- private:
- /**
- * @brief Constructs a path by parsing the provided raw string data.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- void assign(const wchar_t* pathStr, UINT32 numChars, PathType type = PathType::Default);
- /**
- * @brief Constructs a path by parsing the provided raw string data.
- * Throws exception if provided path is not valid.
- *
- * @param type If set to default path will be parsed according to the
- * rules of the platform the application is being compiled to.
- * Otherwise it will be parsed according to provided type.
- */
- void assign(const char* pathStr, UINT32 numChars, PathType type = PathType::Default);
- /**
- * @brief Parses a Windows path and stores the parsed data internally.
- * Throws an exception if parsing fails.
- */
- template<class T>
- void parseWindows(const T* pathStr, UINT32 numChars)
- {
- clear();
- UINT32 idx = 0;
- BasicStringStream<T> tempStream;
- if (idx < numChars)
- {
- if (pathStr[idx] == '\\' || pathStr[idx] == '/')
- {
- mIsAbsolute = true;
- idx++;
- }
- }
- if (idx < numChars)
- {
- // Path starts with a node, a drive letter or is relative
- if (mIsAbsolute && pathStr[idx] == '\\' || pathStr[idx] == '/') // Node
- {
- idx++;
- tempStream.str(BasicString<T>());
- tempStream.clear();
- while (idx < numChars && pathStr[idx] != '\\' && pathStr[idx] != '/')
- tempStream << pathStr[idx++];
- setNode(tempStream.str());
- if (idx < numChars)
- idx++;
- }
- else // A drive letter or not absolute
- {
- T drive = pathStr[idx];
- idx++;
- if (idx < numChars && pathStr[idx] == ':')
- {
- if (mIsAbsolute || !((drive >= 'a' && drive <= 'z') || (drive >= 'A' && drive <= 'Z')))
- throwInvalidPathException(BasicString<T>(pathStr, numChars));
- mIsAbsolute = true;
- setDevice(BansheeEngine::toWString(drive));
- idx++;
- if (idx >= numChars || (pathStr[idx] != '\\' && pathStr[idx] != '/'))
- throwInvalidPathException(BasicString<T>(pathStr, numChars));
- idx++;
- }
- else
- idx--;
- }
- while (idx < numChars)
- {
- tempStream.str(BasicString<T>());
- tempStream.clear();
- while (idx < numChars && pathStr[idx] != '\\' && pathStr[idx] != '/')
- {
- tempStream << pathStr[idx];
- idx++;
- }
- if (idx < numChars)
- pushDirectory(tempStream.str());
- else
- setFilename(tempStream.str());
- idx++;
- }
- }
- }
- /**
- * @brief Parses a Unix path and stores the parsed data internally.
- * Throws an exception if parsing fails.
- */
- template<class T>
- void parseUnix(const T* pathStr, UINT32 numChars)
- {
- clear();
- UINT32 idx = 0;
- BasicStringStream<T> tempStream;
- if (idx < numChars)
- {
- if (pathStr[idx] == '/')
- {
- mIsAbsolute = true;
- idx++;
- }
- else if (pathStr[idx] == '~')
- {
- idx++;
- if (idx >= numChars || pathStr[idx] == '/')
- {
- pushDirectory(BansheeEngine::toWString('~'));
- mIsAbsolute = true;
- }
- else
- idx--;
- }
- while (idx < numChars)
- {
- tempStream.str(BasicString<T>());
- tempStream.clear();
- while (idx < numChars && pathStr[idx] != '/')
- {
- tempStream << pathStr[idx];
- idx++;
- }
- if (idx < numChars)
- {
- if (mDirectories.empty())
- {
- BasicString<T> deviceStr = tempStream.str();
- if (!deviceStr.empty() && *(deviceStr.rbegin()) == ':')
- {
- setDevice(deviceStr.substr(0, deviceStr.length() - 1));
- mIsAbsolute = true;
- }
- else
- {
- pushDirectory(deviceStr);
- }
- }
- else
- {
- pushDirectory(tempStream.str());
- }
- }
- else
- {
- setFilename(tempStream.str());
- }
- idx++;
- }
- }
- }
- void setNode(const WString& node) { mNode = node; }
- void setNode(const String& node) { mNode = BansheeEngine::toWString(node); }
- void setDevice(const WString& device) { mDevice = device; }
- void setDevice(const String& device) { mDevice = BansheeEngine::toWString(device); }
- /**
- * @brief Build a Windows path string from internal path data.
- */
- WString buildWindows() const;
- /**
- * @brief Build a Unix path string from internal path data.
- */
- WString buildUnix() const;
- /**
- * @brief Add new directory to the end of the path.
- */
- void pushDirectory(const WString& dir);
- /**
- * @brief Add new directory to the end of the path.
- */
- void pushDirectory(const String& dir);
- /**
- * @brief Helper method that throws invalid path exception.
- */
- void throwInvalidPathException(const WString& path) const;
- /**
- * @brief Helper method that throws invalid path exception.
- */
- void throwInvalidPathException(const String& path) const;
- private:
- friend struct RTTIPlainType<Path>; // For serialization
- friend struct ::std::hash<BansheeEngine::Path>;
- Vector<WString> mDirectories;
- WString mDevice;
- WString mFilename;
- WString mNode;
- bool mIsAbsolute;
- };
- /**
- * @brief RTTIPlainType specialization for Path that allows paths be serialized as
- * value types.
- *
- * @see RTTIPlainType
- */
- template<> struct RTTIPlainType<Path>
- {
- enum { id = TID_Path }; enum { hasDynamicSize = 1 };
- static void toMemory(const Path& data, char* memory)
- {
- UINT32 size = getDynamicSize(data);
- memcpy(memory, &size, sizeof(UINT32));
- memory += sizeof(UINT32);
- memory = rttiWriteElem(data.mDevice, memory);
- memory = rttiWriteElem(data.mNode, memory);
- memory = rttiWriteElem(data.mFilename, memory);
- memory = rttiWriteElem(data.mIsAbsolute, memory);
- memory = rttiWriteElem(data.mDirectories, memory);
- }
- static UINT32 fromMemory(Path& data, char* memory)
- {
- UINT32 size;
- memcpy(&size, memory, sizeof(UINT32));
- memory += sizeof(UINT32);
- memory = rttiReadElem(data.mDevice, memory);
- memory = rttiReadElem(data.mNode, memory);
- memory = rttiReadElem(data.mFilename, memory);
- memory = rttiReadElem(data.mIsAbsolute, memory);
- memory = rttiReadElem(data.mDirectories, memory);
- return size;
- }
- static UINT32 getDynamicSize(const Path& data)
- {
- UINT64 dataSize = rttiGetElemSize(data.mDevice) + rttiGetElemSize(data.mNode) + rttiGetElemSize(data.mFilename) +
- rttiGetElemSize(data.mIsAbsolute) + rttiGetElemSize(data.mDirectories) + sizeof(UINT32);
- #if BS_DEBUG_MODE
- if (dataSize > std::numeric_limits<UINT32>::max())
- {
- __string_throwDataOverflowException();
- }
- #endif
- return (UINT32)dataSize;
- }
- };
- }
- /**
- * @brief Hash value generator for Path.
- */
- template<>
- struct std::hash<BansheeEngine::Path>
- {
- size_t operator()(const BansheeEngine::Path& path) const
- {
- size_t hash = 0;
- BansheeEngine::hash_combine(hash, path.mFilename);
- BansheeEngine::hash_combine(hash, path.mDevice);
- BansheeEngine::hash_combine(hash, path.mNode);
- for (auto& dir : path.mDirectories)
- BansheeEngine::hash_combine(hash, dir);
- return hash;
- }
- };
|