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)
94 	: m_path(path)
95 {
96 }
97 
FilePath(const char * path)98 inline FilePath::FilePath (const char* path)
99 	: m_path(path)
100 {
101 }
102 
~FilePath()103 inline FilePath::~FilePath ()
104 {
105 }
106 
join(const FilePath & b)107 inline FilePath& FilePath::join (const FilePath& b)
108 {
109 	if (m_path == "")
110 		m_path = b.m_path;
111 	else
112 		m_path += separator + b.m_path;
113 	return *this;
114 }
115 
join(const FilePath & a,const FilePath & b)116 inline FilePath FilePath::join (const FilePath& a, const FilePath& b)
117 {
118 	return FilePath(a).join(b);
119 }
120 
getPath(void) const121 inline const char* FilePath::getPath (void) const
122 {
123 	return m_path.c_str();
124 }
125 
isSeparator(char c)126 inline bool FilePath::isSeparator (char c)
127 {
128 	return c == '/' || c == '\\';
129 }
130 
isRootPath(void) const131 inline bool FilePath::isRootPath (void) const
132 {
133 	return m_path.length() >= 1 && isSeparator(m_path[0]);
134 }
135 
isWinNetPath(void) const136 inline bool FilePath::isWinNetPath (void) const
137 {
138 	return m_path.length() >= 2 && isSeparator(m_path[0]) && isSeparator(m_path[1]);
139 }
140 
141 } // de
142 
143 #endif // _DEFILEPATH_HPP
144