]> git.mxchange.org Git - simgear.git/blob - sg_path.cxx
4cfc3576578eff19677f7d2249d89c6797c4b9e4
[simgear.git] / sg_path.cxx
1 // sg_path.cxx -- routines to abstract out path separator differences
2 //               between MacOS and the rest of the world
3 //
4 // Written by Curtis L. Olson, started April 1999.
5 //
6 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #include <simgear/compiler.h>
26
27 #include <simgear_config.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <sys/stat.h>
32 #if defined( _MSC_VER) || defined(__MINGW32__)
33 #  include <direct.h>
34 #endif
35 #include "sg_path.hxx"
36
37
38 /**
39  * define directory path separators
40  */
41
42 #if defined( macintosh )
43 static const char sgDirPathSep = ':';
44 static const char sgDirPathSepBad = '/';
45 #else
46 static const char sgDirPathSep = '/';
47 static const char sgDirPathSepBad = '\\';
48 #endif
49
50 #if defined( WIN32 ) && !defined(__CYGWIN__)
51 static const char sgSearchPathSep = ';';
52 #else
53 static const char sgSearchPathSep = ':';
54 #endif
55
56
57 // If Unix, replace all ":" with "/".  If MacOS, replace all "/" with
58 // ":" it should go without saying that neither of these characters
59 // should be used in file or directory names.  In windoze, allow the
60 // second character to be a ":" for things like c:\foo\bar
61
62 void
63 SGPath::fix()
64 {
65     for ( string::size_type i = 0; i < path.size(); ++i ) {
66 #if defined( WIN32 )
67         // for windoze, don't replace the ":" for the second character
68         if ( i == 1 ) {
69             continue;
70         }
71 #endif
72         if ( path[i] == sgDirPathSepBad ) {
73             path[i] = sgDirPathSep;
74         }
75     }
76 }
77
78
79 // default constructor
80 SGPath::SGPath()
81     : path("")
82 {
83 }
84
85
86 // create a path based on "path"
87 SGPath::SGPath( const std::string& p )
88     : path(p)
89 {
90     fix();
91 }
92
93
94 // destructor
95 SGPath::~SGPath() {
96 }
97
98
99 // set path
100 void SGPath::set( const string& p ) {
101     path = p;
102     fix();
103 }
104
105
106 // append another piece to the existing path
107 void SGPath::append( const string& p ) {
108     if ( path.size() == 0 ) {
109         path = p;
110     } else {
111         if ( p[0] != sgDirPathSep ) {
112             path += sgDirPathSep;
113         }
114         path += p;
115     }
116     fix();
117 }
118
119 //add a new path component to the existing path string
120 void SGPath::add( const string& p ) {
121     append( sgSearchPathSep+p );
122 }
123
124
125 // concatenate a string to the end of the path without inserting a
126 // path separator
127 void SGPath::concat( const string& p ) {
128     if ( path.size() == 0 ) {
129         path = p;
130     } else {
131         path += p;
132     }
133     fix();
134 }
135
136
137 // Get the file part of the path (everything after the last path sep)
138 string SGPath::file() const {
139     int index = path.rfind(sgDirPathSep);
140     if (index >= 0) {
141         return path.substr(index + 1);
142     } else {
143         return "";
144     }
145 }
146   
147
148 // get the directory part of the path.
149 string SGPath::dir() const {
150     int index = path.rfind(sgDirPathSep);
151     if (index >= 0) {
152         return path.substr(0, index);
153     } else {
154         return "";
155     }
156 }
157
158 // get the base part of the path (everything but the extension.)
159 string SGPath::base() const {
160     int index = path.rfind(".");
161     if ((index >= 0) && (path.find("/", index) == string::npos)) {
162         return path.substr(0, index);
163     } else {
164         return "";
165     }
166 }
167
168 // get the extention (everything after the final ".")
169 // but make sure no "/" follows the "." character (otherwise it
170 // is has to be a directory name containing a ".").
171 string SGPath::extension() const {
172     int index = path.rfind(".");
173     if ((index >= 0)  && (path.find("/", index) == string::npos)) {
174         return path.substr(index + 1);
175     } else {
176         return "";
177     }
178 }
179
180 bool SGPath::exists() const {
181     FILE* fp = fopen( path.c_str(), "r");
182     if (fp == 0) {
183         return false;
184     }
185     fclose(fp);
186     return true;
187 }
188
189 #if defined( _MSC_VER) || defined(__MINGW32__)
190 #  define sgMkDir(d,m)       _mkdir(d)
191 #else
192 #  define sgMkDir(d,m)       mkdir(d,m)
193 #endif
194
195
196 void SGPath::create_dir( mode_t mode ) {
197     string_list dirlist = sgPathSplit(dir());
198     string path = dirlist[0];
199     string_list path_elements = sgPathBranchSplit(path);
200     bool absolute = !path.empty() && path[0] == sgDirPathSep;
201
202     unsigned int i = 1;
203     SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
204     dir.concat( path_elements[0] );
205 #if defined( _MSC_VER) || defined(__MINGW32__)
206     if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
207         dir.append( path_elements[1] );
208         i = 2;
209     }
210 #endif
211     struct stat info;
212     int r;
213     for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
214         dir.append(path_elements[i]);
215     }
216     if ( r == 0 ) {
217         return; // Directory already exists
218     }
219     if ( sgMkDir( dir.c_str(), mode) ) {
220         SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
221         return;
222     }
223     for(;i < path_elements.size(); i++) {
224         dir.append(path_elements[i]);
225         if ( sgMkDir( dir.c_str(), mode) ) {
226             SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
227             break;
228         }
229     }
230 }
231
232 string_list sgPathBranchSplit( const string &dirpath ) {
233     string_list path_elements;
234     string element, path = dirpath;
235     while ( path.size() ) {
236         size_t p = path.find( sgDirPathSep );
237         if ( p != string::npos ) {
238             element = path.substr( 0, p );
239             path.erase( 0, p + 1 );
240         } else {
241             element = path;
242             path = "";
243         }
244         if ( element.size() )
245             path_elements.push_back( element );
246     }
247     return path_elements;
248 }
249
250
251 string_list sgPathSplit( const string &search_path ) {
252     string tmp = search_path;
253     string_list result;
254     result.clear();
255
256     bool done = false;
257
258     while ( !done ) {
259         int index = tmp.find(sgSearchPathSep);
260         if (index >= 0) {
261             result.push_back( tmp.substr(0, index) );
262             tmp = tmp.substr( index + 1 );
263         } else {
264             if ( !tmp.empty() )
265                 result.push_back( tmp );
266             done = true;
267         }
268     }
269
270     return result;
271 }