1 #ifndef _DEFILEPATH_HPP
2 #define _DEFILEPATH_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Filesystem path class.
24 *//*--------------------------------------------------------------------*/
25
26 #include "deDefs.hpp"
27
28 #include <string>
29 #include <vector>
30
31 namespace de
32 {
33
34 void FilePath_selfTest(void);
35
36 class FilePath
37 {
38 public:
39 enum Type
40 {
41 TYPE_UNKNOWN = 0, /*!< Non-existent or unknown object. */
42 TYPE_FILE, /*!< File. */
43 TYPE_DIRECTORY, /*!< Directory. */
44
45 TYPE_LAST
46 };
47
48 static const std::string separator; /*!< Path separator. */
49
50 FilePath(void);
51 FilePath(const std::string &path);
52 FilePath(const char *path);
53 FilePath(const std::vector<std::string> &components);
54 ~FilePath(void);
55
56 bool exists(void) const;
57 Type getType(void) const;
58
59 const char *getPath(void) const;
60 std::string getBaseName(void) const;
61 std::string getDirName(void) const;
62 std::string getFileExtension(void) const;
63
64 static FilePath join(const FilePath &a, const FilePath &b);
65 FilePath &join(const FilePath &b);
66
67 static FilePath normalize(const FilePath &path);
68 FilePath &normalize(void);
69
70 void split(std::vector<std::string> &components) const;
71 static FilePath join(const std::vector<std::string> &components);
72
73 bool isAbsolutePath(void) const;
74
75 static bool isSeparator(char c);
76
77 private:
78 bool isRootPath(void) const;
79 bool isWinNetPath(void) const;
80 bool beginsWithDrive(void) const;
81
82 std::string m_path;
83 };
84
85 // \todo [2012-09-05 pyry] Move to delibs?
86 void createDirectory(const char *path);
87 void createDirectoryAndParents(const char *path);
88
FilePath(void)89 inline FilePath::FilePath(void)
90 {
91 }
92
FilePath(const std::string & path)93 inline FilePath::FilePath(const std::string &path) : m_path(path)
94 {
95 }
96
FilePath(const char * path)97 inline FilePath::FilePath(const char *path) : m_path(path)
98 {
99 }
100
~FilePath()101 inline FilePath::~FilePath()
102 {
103 }
104
join(const FilePath & b)105 inline FilePath &FilePath::join(const FilePath &b)
106 {
107 if (m_path == "")
108 m_path = b.m_path;
109 else
110 m_path += separator + b.m_path;
111 return *this;
112 }
113
join(const FilePath & a,const FilePath & b)114 inline FilePath FilePath::join(const FilePath &a, const FilePath &b)
115 {
116 return FilePath(a).join(b);
117 }
118
getPath(void) const119 inline const char *FilePath::getPath(void) const
120 {
121 return m_path.c_str();
122 }
123
isSeparator(char c)124 inline bool FilePath::isSeparator(char c)
125 {
126 return c == '/' || c == '\\';
127 }
128
isRootPath(void) const129 inline bool FilePath::isRootPath(void) const
130 {
131 return m_path.length() >= 1 && isSeparator(m_path[0]);
132 }
133
isWinNetPath(void) const134 inline bool FilePath::isWinNetPath(void) const
135 {
136 return m_path.length() >= 2 && isSeparator(m_path[0]) && isSeparator(m_path[1]);
137 }
138
139 } // namespace de
140
141 #endif // _DEFILEPATH_HPP
142