path.hpp (2894B)
1 /* ReaPack: Package manager for REAPER 2 * Copyright (C) 2015-2025 Christian Fillion 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef REAPACK_PATH_HPP 19 #define REAPACK_PATH_HPP 20 21 #include <list> 22 #include <ostream> 23 #include <string> 24 25 class UseRootPath; 26 27 class Path { 28 public: 29 enum Attribute { 30 Absolute = 1<<0, 31 UNC = 1<<1, 32 }; 33 34 static const Path DATA; 35 static const Path CACHE; 36 static const Path CONFIG; 37 static const Path REGISTRY; 38 39 static const Path &root() { return s_root; } 40 41 Path(const std::string &path = {}); 42 43 void append(const std::string &parts, bool traversal = true); 44 void append(const Path &other); 45 void remove(size_t pos, size_t count); 46 void removeLast(); 47 void clear(); 48 49 bool empty() const { return m_parts.empty(); } 50 size_t size() const { return m_parts.size(); } 51 int attributes() const { return m_attributes; } 52 bool test(Attribute f) const { return (m_attributes & f) != 0; } 53 54 Path dirname() const; 55 std::string front() const; 56 std::string basename() const; 57 std::string join(bool nativeSeparator = true) const; 58 bool startsWith(const Path &) const; 59 60 Path prependRoot() const; 61 Path removeRoot() const; 62 63 std::list<std::string>::const_iterator begin() const { return m_parts.begin(); } 64 std::list<std::string>::const_iterator end() const { return m_parts.end(); } 65 66 bool operator==(const Path &) const; 67 bool operator!=(const Path &) const; 68 bool operator<(const Path &) const; 69 Path operator+(const std::string &) const; 70 Path operator+(const Path &) const; 71 const Path &operator+=(const std::string &); 72 const Path &operator+=(const Path &); 73 std::string &operator[](size_t); 74 const std::string &operator[](size_t) const; 75 76 private: 77 static Path s_root; 78 friend UseRootPath; 79 80 const std::string &at(size_t) const; 81 82 std::list<std::string> m_parts; 83 int m_attributes; 84 }; 85 86 inline std::ostream &operator<<(std::ostream &os, const Path &p) 87 { 88 return os << p.join(); 89 }; 90 91 class UseRootPath { 92 public: 93 UseRootPath(const Path &); 94 ~UseRootPath(); 95 96 private: 97 Path m_backup; 98 }; 99 100 class TempPath { 101 public: 102 TempPath(const Path &target); 103 104 const Path &target() const { return m_target; } 105 const Path &temp() const { return m_temp; } 106 107 private: 108 Path m_target; 109 Path m_temp; 110 }; 111 112 #endif