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