1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_Path
21 #define TrenchBroom_Path
22 
23 #include "StringUtils.h"
24 
25 #include <vector>
26 
27 namespace TrenchBroom {
28     namespace IO {
29         class Path {
30         public:
31             typedef std::vector<Path> List;
32             static char separator();
33 
34             struct ToString {
35                 char m_separator;
36                 ToString(const char i_separator = separator()) :
m_separatorToString37                 m_separator(i_separator) {}
38 
operatorToString39                 String operator()(const Path& path) const {
40                     return path.asString(m_separator);
41                 }
42             };
43         private:
44             static const String& separators();
45 
46             StringList m_components;
47             bool m_absolute;
48 
49             Path(bool absolute, const StringList& components);
50         public:
51             Path(const String& path = "");
52 
53             Path operator+(const Path& rhs) const;
54             int compare(const Path& rhs) const;
55             bool operator==(const Path& rhs) const;
56             bool operator!= (const Path& rhs) const;
57             bool operator<(const Path& rhs) const;
58             bool operator>(const Path& rhs) const;
59 
60             String asString(const char sep = separator()) const;
61             String asString(const String& sep) const;
62             static StringList asStrings(const Path::List& paths, const char sep = separator());
63 
64             size_t length() const;
65             bool isEmpty() const;
66             Path firstComponent() const;
67             Path deleteFirstComponent() const;
68             Path lastComponent() const;
69             Path deleteLastComponent() const;
70             Path prefix(const size_t count) const;
71             Path suffix(const size_t count) const;
72             Path subPath(const size_t index, const size_t count) const;
73             const String extension() const;
74             Path deleteExtension() const;
75             Path addExtension(const String& extension) const;
76 
77             bool isAbsolute() const;
78             bool canMakeRelative(const Path& absolutePath) const;
79             Path makeAbsolute(const Path& relativePath) const;
80             Path makeRelative(const Path& absolutePath) const;
81             Path makeCanonical() const;
82             Path makeLowerCase() const;
83 
84             static List makeAbsoluteAndCanonical(const List& paths, const String& relativePath);
85         private:
86             static bool hasDriveSpec(const StringList& components);
87             static bool hasDriveSpec(const String& component);
88             StringList resolvePath(const bool absolute, const StringList& components) const;
89         };
90     }
91 }
92 
93 #endif /* defined(TrenchBroom_Path) */
94