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