]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.hxx
2921ac27b4a6f5126c7e5fa651f3cc94da5d0cdd
[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
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     string path;
55
56 public:
57
58     /** Default constructor */
59     SGPath();
60
61     /**
62      * Construct a path based on the starting path provided.
63      * @param p initial path
64      */
65     SGPath( const string& p );
66
67     /** Destructor */
68     ~SGPath();
69
70     /**
71      * Set path to a new value
72      * @param p new path
73      */
74     void set( const string& p );
75     SGPath& operator= ( const char* p ) { this->set(p); return *this; }
76
77     /**
78      * Append another piece to the existing path.  Inserts a path
79      * separator between the existing component and the new component.
80      * @param p additional path component */
81     void append( const string& p );
82
83     /**
84      * Append a new piece to the existing path.  Inserts a search path
85      * separator to the existing path and the new patch component.
86      * @param p additional path component */
87     void add( const string& p );
88
89     /**
90      * Concatenate a string to the end of the path without inserting a
91      * path separator.
92      * @param p addtional path suffix
93      */
94     void concat( const string& p );
95
96     /**
97      * Get the file part of the path (everything after the last path sep)
98      * @return file string
99      */
100     string file() const;
101   
102     /**
103      * Get the directory part of the path.
104      * @return directory string
105      */
106     string dir() const;
107   
108     /**
109      * Get the base part of the path (everything but the extension.)
110      * @return the base string
111      */
112     string base() const;
113
114     /**
115      * Get the extention part of the path (everything after the final ".")
116      * @return the extention string
117      */
118     string extension() const;
119
120     /** Get the path string
121      * @return path string
122      */
123     string str() const { return path; }
124
125     /** Get the path string
126      * @return path in "C" string (ptr to char array) form.
127      */
128     const char* c_str() { return path.c_str(); }
129
130     /**
131      * Determine if file exists by attempting to fopen it.
132      * @return true if file exists, otherwise returns false.
133      */
134     bool exists() const;
135
136     /**
137      * Create the designated directory.
138      */
139     void create_dir(mode_t mode);
140
141 private:
142
143     void fix();
144
145 };
146
147
148 /**
149  * Split a directory search path into a vector of individual paths
150  */
151 string_list sgPathSplit( const string &search_path );
152
153
154 #endif // _SG_PATH_HXX
155
156