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