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 
72 	bool				isAbsolutePath		(void) const;
73 
74 	static bool			isSeparator			(char c);
75 
76 private:
77 	bool				isRootPath			(void) const;
78 	bool				isWinNetPath		(void) const;
79 	bool				beginsWithDrive		(void) const;
80 
81 	std::string			m_path;
82 };
83 
84 // \todo [2012-09-05 pyry] Move to delibs?
85 void	createDirectory				(const char* path);
86 void	createDirectoryAndParents	(const char* path);
87 
FilePath(void)88 inline FilePath::FilePath (void)
89 {
90 }
91 
FilePath(const std::string & path)92 inline FilePath::FilePath (const std::string& path)
93 	: m_path(path)
94 {
95 }
96 
FilePath(const char * path)97 inline FilePath::FilePath (const char* path)
98 	: m_path(path)
99 {
100 }
101 
~FilePath()102 inline FilePath::~FilePath ()
103 {
104 }
105 
join(const FilePath & b)106 inline FilePath& FilePath::join (const FilePath& b)
107 {
108 	if (m_path == "")
109 		m_path = b.m_path;
110 	else
111 		m_path += separator + b.m_path;
112 	return *this;
113 }
114 
join(const FilePath & a,const FilePath & b)115 inline FilePath FilePath::join (const FilePath& a, const FilePath& b)
116 {
117 	return FilePath(a).join(b);
118 }
119 
getPath(void) const120 inline const char* FilePath::getPath (void) const
121 {
122 	return m_path.c_str();
123 }
124 
isSeparator(char c)125 inline bool FilePath::isSeparator (char c)
126 {
127 	return c == '/' || c == '\\';
128 }
129 
isRootPath(void) const130 inline bool FilePath::isRootPath (void) const
131 {
132 	return m_path.length() >= 1 && isSeparator(m_path[0]);
133 }
134 
isWinNetPath(void) const135 inline bool FilePath::isWinNetPath (void) const
136 {
137 	return m_path.length() >= 2 && isSeparator(m_path[0]) && isSeparator(m_path[1]);
138 }
139 
140 } // de
141 
142 #endif // _DEFILEPATH_HPP
143