]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.hxx
Move SGReadFileCallback from model.cxx to public class ModelRegistry
[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 STL_STRING
35
36 #include <simgear/math/sg_types.hxx>
37
38 SG_USING_STD(string);
39
40 #ifdef _MSC_VER
41   typedef int mode_t;
42 #endif
43
44 /**
45  * A class to hide path separator difference across platforms and assist
46  * in managing file system path names.
47  *
48  * Paths can be input in any platform format and will be converted
49  * automatically to the proper format.
50  */
51
52 class SGPath {
53
54 private:
55
56     string path;
57
58 public:
59
60     /** Default constructor */
61     SGPath();
62
63     /**
64      * Construct a path based on the starting path provided.
65      * @param p initial path
66      */
67     SGPath( const string& p );
68
69     /** Destructor */
70     ~SGPath();
71
72     /**
73      * Set path to a new value
74      * @param p new path
75      */
76     void set( const string& p );
77     SGPath& operator= ( const char* p ) { this->set(p); return *this; }
78
79     /**
80      * Append another piece to the existing path.  Inserts a path
81      * separator between the existing component and the new component.
82      * @param p additional path component */
83     void append( const string& p );
84
85     /**
86      * Append a new piece to the existing path.  Inserts a search path
87      * separator to the existing path and the new patch component.
88      * @param p additional path component */
89     void add( const string& p );
90
91     /**
92      * Concatenate a string to the end of the path without inserting a
93      * path separator.
94      * @param p addtional path suffix
95      */
96     void concat( const string& p );
97
98     /**
99      * Get the file part of the path (everything after the last path sep)
100      * @return file string
101      */
102     string file() const;
103   
104     /**
105      * Get the directory part of the path.
106      * @return directory string
107      */
108     string dir() const;
109   
110     /**
111      * Get the base part of the path (everything but the extension.)
112      * @return the base string
113      */
114     string base() const;
115
116     /**
117      * Get the extention part of the path (everything after the final ".")
118      * @return the extention string
119      */
120     string extension() const;
121
122     /**
123      * Get the path string
124      * @return path string
125      */
126     string str() const { return path; }
127
128     /**
129      * Get the path string
130      * @return path in "C" string (ptr to char array) form.
131      */
132     const char* c_str() { return path.c_str(); }
133
134     /**
135      * Determine if file exists by attempting to fopen it.
136      * @return true if file exists, otherwise returns false.
137      */
138     bool exists() const;
139
140     /**
141      * Create the designated directory.
142      */
143     void create_dir(mode_t mode);
144
145 private:
146
147     void fix();
148
149 };
150
151
152 /**
153  * Split a directory string into a list of it's parent directories.
154  */
155 string_list sgPathBranchSplit( const string &path );
156
157 /**
158  * Split a directory search path into a vector of individual paths
159  */
160 string_list sgPathSplit( const string &search_path );
161
162
163 #endif // _SG_PATH_HXX
164
165