]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.hxx
Add isAbsolute/isRelative predicates to SGPath.
[simgear.git] / simgear / misc / sg_path.hxx
1 /**
2  * \file sg_path.hxx
3  * Routines to abstract out path separator differences between MacOS
4  * and the rest of the world.
5  */
6
7 // Written by Curtis L. Olson, started April 1999.
8 //
9 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Library General Public
13 // License as published by the Free Software Foundation; either
14 // version 2 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 //
25 // $Id$
26
27
28 #ifndef _SG_PATH_HXX
29 #define _SG_PATH_HXX
30
31 #include <sys/types.h>
32
33 #include <simgear/compiler.h>
34 #include <string>
35
36 #include <simgear/math/sg_types.hxx>
37
38 #ifdef _MSC_VER
39   typedef int mode_t;
40 #endif
41
42 /**
43  * A class to hide path separator difference across platforms and assist
44  * in managing file system path names.
45  *
46  * Paths can be input in any platform format and will be converted
47  * automatically to the proper format.
48  */
49
50 class SGPath {
51
52 private:
53
54     std::string path;
55
56 public:
57
58     /** Default constructor */
59     SGPath();
60
61     SGPath(const SGPath& p);
62     
63     SGPath& operator=(const SGPath& p);
64
65     /**
66      * Construct a path based on the starting path provided.
67      * @param p initial path
68      */
69     SGPath( const std::string& p );
70
71     /** Destructor */
72     ~SGPath();
73
74     /**
75      * Set path to a new value
76      * @param p new path
77      */
78     void set( const std::string& p );
79     SGPath& operator= ( const char* p ) { this->set(p); return *this; }
80
81     /**
82      * Append another piece to the existing path.  Inserts a path
83      * separator between the existing component and the new component.
84      * @param p additional path component */
85     void append( const std::string& p );
86
87     /**
88      * Append a new piece to the existing path.  Inserts a search path
89      * separator to the existing path and the new patch component.
90      * @param p additional path component */
91     void add( const std::string& p );
92
93     /**
94      * Concatenate a string to the end of the path without inserting a
95      * path separator.
96      * @param p additional path suffix
97      */
98     void concat( const std::string& p );
99
100     /**
101      * Get the file part of the path (everything after the last path sep)
102      * @return file string
103      */
104     std::string file() const;
105   
106     /**
107      * Get the directory part of the path.
108      * @return directory string
109      */
110     std::string dir() const;
111   
112     /**
113      * Get the base part of the path (everything but the extension.)
114      * @return the base string
115      */
116     std::string base() const;
117
118     /**
119      * Get the extension part of the path (everything after the final ".")
120      * @return the extension string
121      */
122     std::string extension() const;
123
124     /**
125      * Get the path string
126      * @return path string
127      */
128     std::string str() const { return path; }
129
130     /**
131      * Get the path string
132      * @return path in "C" string (ptr to char array) form.
133      */
134     const char* c_str() { return path.c_str(); }
135
136     /**
137      * Determine if file exists by attempting to fopen it.
138      * @return true if file exists, otherwise returns false.
139      */
140     bool exists() const;
141
142     /**
143      * Create the designated directory.
144      * @return 0 on success, or <0 on failure.
145      */
146     int create_dir(mode_t mode);
147
148     bool isFile() const;
149     bool isDir() const;
150     
151     /**
152      * Opposite sense to isAbsolute
153      */
154     bool isRelative() const { return !isAbsolute(); }
155     
156     /**
157      * Is this an absolute path?
158      * I.e starts with a directory seperator, or a single character + colon
159      */
160     bool isAbsolute() const;
161 private:
162
163     void fix();
164
165     void validate() const;
166
167     mutable bool _cached;
168     mutable bool _exists;
169     mutable bool _isDir;
170     mutable bool _isFile;
171 };
172
173
174 /**
175  * Split a directory string into a list of it's parent directories.
176  */
177 string_list sgPathBranchSplit( const std::string &path );
178
179 /**
180  * Split a directory search path into a vector of individual paths
181  */
182 string_list sgPathSplit( const std::string &search_path );
183
184
185 #endif // _SG_PATH_HXX
186
187