]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.hxx
Fix BTG writer for non-included index arrays.
[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     /** Copy contructor */
62     SGPath(const SGPath& p);
63     
64     SGPath& operator=(const SGPath& p);
65
66     /**
67      * Construct a path based on the starting path provided.
68      * @param p initial path
69      */
70     SGPath( const std::string& p );
71
72     /**
73      * Construct a path based on the starting path provided and a relative subpath
74      * @param p initial path
75      * @param r relative subpath
76      */
77     SGPath( const SGPath& p, const std::string& r );
78
79     /** Destructor */
80     ~SGPath();
81
82     /**
83      * Set path to a new value
84      * @param p new path
85      */
86     void set( const std::string& p );
87     SGPath& operator= ( const char* p ) { this->set(p); return *this; }
88
89     /**
90      * Append another piece to the existing path.  Inserts a path
91      * separator between the existing component and the new component.
92      * @param p additional path component */
93     void append( const std::string& p );
94
95     /**
96      * Append a new piece to the existing path.  Inserts a search path
97      * separator to the existing path and the new patch component.
98      * @param p additional path component */
99     void add( const std::string& p );
100
101     /**
102      * Concatenate a string to the end of the path without inserting a
103      * path separator.
104      * @param p additional path suffix
105      */
106     void concat( const std::string& p );
107
108     /**
109      * Get the file part of the path (everything after the last path sep)
110      * @return file string
111      */
112     std::string file() const;
113   
114     /**
115      * Get the directory part of the path.
116      * @return directory string
117      */
118     std::string dir() const;
119   
120     /**
121      * Get the base part of the path (everything but the extension.)
122      * @return the base string
123      */
124     std::string base() const;
125
126     /**
127      * Get the extension part of the path (everything after the final ".")
128      * @return the extension string
129      */
130     std::string extension() const;
131
132     /**
133      * Get the path string
134      * @return path string
135      */
136     std::string str() const { return path; }
137
138     /**
139      * Get the path string
140      * @return path in "C" string (ptr to char array) form.
141      */
142     const char* c_str() const { return path.c_str(); }
143
144     /**
145      * Get the path string in OS native form
146      */
147     std::string str_native() const;
148
149     /**
150      * Determine if file exists by attempting to fopen it.
151      * @return true if file exists, otherwise returns false.
152      */
153     bool exists() const;
154
155     /**
156      * Create the designated directory.
157      * @return 0 on success, or <0 on failure.
158      */
159     int create_dir(mode_t mode);
160
161     bool isFile() const;
162     bool isDir() const;
163     
164     /**
165      * Opposite sense to isAbsolute
166      */
167     bool isRelative() const { return !isAbsolute(); }
168     
169     /**
170      * Is this an absolute path?
171      * I.e starts with a directory seperator, or a single character + colon
172      */
173     bool isAbsolute() const;
174     
175     /**
176      * check for default constructed path
177      */
178     bool isNull() const;
179 private:
180
181     void fix();
182
183     void validate() const;
184
185     mutable bool _cached;
186     mutable bool _exists;
187     mutable bool _isDir;
188     mutable bool _isFile;
189 };
190
191
192 /**
193  * Split a directory string into a list of it's parent directories.
194  */
195 string_list sgPathBranchSplit( const std::string &path );
196
197 /**
198  * Split a directory search path into a vector of individual paths
199  */
200 string_list sgPathSplit( const std::string &search_path );
201
202
203 #endif // _SG_PATH_HXX
204
205