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