// Copyright (c) 2014-2021 Thomas Fussell // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE // // @license: http://www.opensource.org/licenses/mit-license.php // @author: see AUTHORS file #pragma once #include #include #include #include namespace xlnt { /// /// Encapsulates a path that points to location in a filesystem. /// class XLNT_API path { public: /// /// The system-specific path separator character (e.g. '/' or '\'). /// static char system_separator(); /// /// Construct an empty path. /// path(); /// /// Counstruct a path from a string representing the path. /// explicit path(const std::string &path_string); /// /// Construct a path from a string with an explicit directory seprator. /// path(const std::string &path_string, char sep); // general attributes /// /// Return true iff this path doesn't begin with / (or a drive letter on Windows). /// bool is_relative() const; /// /// Return true iff path::is_relative() is false. /// bool is_absolute() const; /// /// Return true iff this path is the root directory. /// bool is_root() const; /// /// Return a new path that points to the directory containing the current path /// Return the path unchanged if this path is the absolute or relative root. /// path parent() const; /// /// Return the last component of this path. /// std::string filename() const; /// /// Return the part of the path following the last dot in the filename. /// std::string extension() const; /// /// Return a pair of strings resulting from splitting the filename on the last dot. /// std::pair split_extension() const; // conversion /// /// Create a string representing this path separated by the provided /// separator or the system-default separator if not provided. /// std::vector split() const; /// /// Create a string representing this path separated by the provided /// separator or the system-default separator if not provided. /// const std::string &string() const; #ifdef _MSC_VER /// /// Create a wstring representing this path separated by the provided /// separator or the system-default separator if not provided. /// std::wstring wstring() const; #endif /// /// If this path is relative, append each component of this path /// to base_path and return the resulting absolute path. Otherwise, /// the the current path will be returned and base_path will be ignored. /// path resolve(const path &base_path) const; /// /// The inverse of path::resolve. Creates a relative path from an absolute /// path by removing the common root between base_path and this path. /// If the current path is already relative, return it unchanged. /// path relative_to(const path &base_path) const; // filesystem attributes /// /// Return true iff the file or directory pointed to by this path /// exists on the filesystem. /// bool exists() const; /// /// Return true if the file or directory pointed to by this path /// is a directory. /// bool is_directory() const; /// /// Return true if the file or directory pointed to by this path /// is a regular file. /// bool is_file() const; // filesystem /// /// Open the file pointed to by this path and return a string containing /// the files contents. /// std::string read_contents() const; // mutators /// /// Append the provided part to this path and return the result. /// path append(const std::string &to_append) const; /// /// Append the provided part to this path and return the result. /// path append(const path &to_append) const; /// /// Returns true if left path is equal to right path. /// bool operator==(const path &other) const; /// /// Returns true if left path is equal to right path. /// bool operator!=(const path &other) const; private: /// /// Returns the character that separates directories in the path. /// On POSIX style filesystems, this is always '/'. /// On Windows, this is the character that separates the drive letter from /// the rest of the path for absolute paths with a drive letter, '/' if the path /// is absolute and starts with '/', and '/' or '\' for relative paths /// depending on which splits the path into more directory components. /// char guess_separator() const; /// /// A string that represents this path. /// std::string internal_; }; } // namespace xlnt namespace std { /// /// Template specialization to allow xlnt:path to be used as a key in a std container. /// template <> struct hash { /// /// Returns a hashed represenation of the given path. /// size_t operator()(const xlnt::path &p) const { static hash hasher; return hasher(p.string()); } }; } // namespace std