]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
Fix BTG writer for non-included index arrays.
[simgear.git] / simgear / misc / 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 #ifdef _WIN32
32 #  include <direct.h>
33 #endif
34 #include "sg_path.hxx"
35
36 using std::string;
37
38
39 /**
40  * define directory path separators
41  */
42
43 static const char sgDirPathSep = '/';
44 static const char sgDirPathSepBad = '\\';
45
46 #ifdef _WIN32
47 static const char sgSearchPathSep = ';';
48 #else
49 static const char sgSearchPathSep = ':';
50 #endif
51
52
53 // If Unix, replace all ":" with "/".  In windoze, allow the
54 // second character to be a ":" for things like c:\foo\bar
55
56 void
57 SGPath::fix()
58 {
59     for ( string::size_type i = 0; i < path.size(); ++i ) {
60 #if defined( WIN32 )
61         // for windoze, don't replace the ":" for the second character
62         if ( i == 1 ) {
63             continue;
64         }
65 #endif
66         if ( path[i] == sgDirPathSepBad ) {
67             path[i] = sgDirPathSep;
68         }
69     }
70 }
71
72
73 // default constructor
74 SGPath::SGPath()
75     : path(""),
76     _cached(false)
77 {
78 }
79
80
81 // create a path based on "path"
82 SGPath::SGPath( const std::string& p )
83     : path(p),
84     _cached(false)
85 {
86     fix();
87 }
88
89 // create a path based on "path" and a "subpath"
90 SGPath::SGPath( const SGPath& p, const std::string& r )
91     : path(p.path),
92     _cached(false)
93 {
94     append(r);
95     fix();
96 }
97
98 SGPath::SGPath(const SGPath& p) :
99   path(p.path),
100   _cached(p._cached),
101   _exists(p._exists),
102   _isDir(p._isDir),
103   _isFile(p._isFile)
104 {
105 }
106     
107 SGPath& SGPath::operator=(const SGPath& p)
108 {
109   path = p.path;
110   _cached = p._cached;
111   _exists = p._exists;
112   _isDir = p._isDir;
113   _isFile = p._isFile;
114   return *this;
115 }
116
117 // destructor
118 SGPath::~SGPath() {
119 }
120
121
122 // set path
123 void SGPath::set( const string& p ) {
124     path = p;
125     fix();
126     _cached = false;
127 }
128
129
130 // append another piece to the existing path
131 void SGPath::append( const string& p ) {
132     if ( path.size() == 0 ) {
133         path = p;
134     } else {
135         if ( p[0] != sgDirPathSep ) {
136             path += sgDirPathSep;
137         }
138         path += p;
139     }
140     fix();
141     _cached = false;
142 }
143
144 //add a new path component to the existing path string
145 void SGPath::add( const string& p ) {
146     append( sgSearchPathSep+p );
147 }
148
149
150 // concatenate a string to the end of the path without inserting a
151 // path separator
152 void SGPath::concat( const string& p ) {
153     if ( path.size() == 0 ) {
154         path = p;
155     } else {
156         path += p;
157     }
158     fix();
159     _cached = false;
160 }
161
162
163 // Get the file part of the path (everything after the last path sep)
164 string SGPath::file() const {
165     int index = path.rfind(sgDirPathSep);
166     if (index >= 0) {
167         return path.substr(index + 1);
168     } else {
169         return "";
170     }
171 }
172   
173
174 // get the directory part of the path.
175 string SGPath::dir() const {
176     int index = path.rfind(sgDirPathSep);
177     if (index >= 0) {
178         return path.substr(0, index);
179     } else {
180         return "";
181     }
182 }
183
184 // get the base part of the path (everything but the extension.)
185 string SGPath::base() const {
186     int index = path.rfind(".");
187     if ((index >= 0) && (path.find("/", index) == string::npos)) {
188         return path.substr(0, index);
189     } else {
190         return "";
191     }
192 }
193
194 // get the extension (everything after the final ".")
195 // but make sure no "/" follows the "." character (otherwise it
196 // is has to be a directory name containing a ".").
197 string SGPath::extension() const {
198     int index = path.rfind(".");
199     if ((index >= 0)  && (path.find("/", index) == string::npos)) {
200         return path.substr(index + 1);
201     } else {
202         return "";
203     }
204 }
205
206 void SGPath::validate() const
207 {
208   if (_cached) {
209     return;
210   }
211   
212 #ifdef _WIN32
213   struct _stat buf ;
214
215   bool remove_trailing = false;
216   if ( path.length() > 1 && path[path.length()-1] == '/' )
217       remove_trailing=true;
218   if (_stat (path.substr(0,remove_trailing?path.length()-1:path.length()).c_str(), &buf ) < 0) {
219     _exists = false;
220   } else {
221     _exists = true;
222     _isFile = ((S_IFREG & buf.st_mode ) !=0);
223     _isDir = ((S_IFDIR & buf.st_mode ) !=0);
224   }
225
226 #else
227   struct stat buf ;
228
229   if (stat(path.c_str(), &buf ) < 0) {
230     _exists = false;
231   } else {
232     _exists = true;
233     _isFile = ((S_ISREG(buf.st_mode )) != 0);
234     _isDir = ((S_ISDIR(buf.st_mode )) != 0);
235   }
236   
237 #endif
238   _cached = true;
239 }
240
241 bool SGPath::exists() const
242 {
243   validate();
244   return _exists;
245 }
246
247 bool SGPath::isDir() const
248 {
249   validate();
250   return _exists && _isDir;
251 }
252
253 bool SGPath::isFile() const
254 {
255   validate();
256   return _exists && _isFile;
257 }
258
259 #ifdef _WIN32
260 #  define sgMkDir(d,m)       _mkdir(d)
261 #else
262 #  define sgMkDir(d,m)       mkdir(d,m)
263 #endif
264
265
266 int SGPath::create_dir( mode_t mode ) {
267     string_list dirlist = sgPathSplit(dir());
268     if ( dirlist.empty() )
269         return -1;
270     string path = dirlist[0];
271     string_list path_elements = sgPathBranchSplit(path);
272     bool absolute = !path.empty() && path[0] == sgDirPathSep;
273
274     unsigned int i = 1;
275     SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
276     dir.concat( path_elements[0] );
277 #ifdef _WIN32
278     if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
279         dir.append( path_elements[1] );
280         i = 2;
281     }
282 #endif
283     struct stat info;
284     int r;
285     for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
286         dir.append(path_elements[i]);
287     }
288     if ( r == 0 ) {
289         return 0; // Directory already exists
290     }
291     if ( sgMkDir( dir.c_str(), mode) ) {
292         SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
293         return -2;
294     }
295     for(; i < path_elements.size(); i++) {
296         dir.append(path_elements[i]);
297         if ( sgMkDir( dir.c_str(), mode) ) {
298             SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
299             return -2;
300         }
301     }
302
303     return 0;
304 }
305
306 string_list sgPathBranchSplit( const string &dirpath ) {
307     string_list path_elements;
308     string element, path = dirpath;
309     while ( path.size() ) {
310         size_t p = path.find( sgDirPathSep );
311         if ( p != string::npos ) {
312             element = path.substr( 0, p );
313             path.erase( 0, p + 1 );
314         } else {
315             element = path;
316             path = "";
317         }
318         if ( element.size() )
319             path_elements.push_back( element );
320     }
321     return path_elements;
322 }
323
324
325 string_list sgPathSplit( const string &search_path ) {
326     string tmp = search_path;
327     string_list result;
328     result.clear();
329
330     bool done = false;
331
332     while ( !done ) {
333         int index = tmp.find(sgSearchPathSep);
334         if (index >= 0) {
335             result.push_back( tmp.substr(0, index) );
336             tmp = tmp.substr( index + 1 );
337         } else {
338             if ( !tmp.empty() )
339                 result.push_back( tmp );
340             done = true;
341         }
342     }
343
344     return result;
345 }
346
347 bool SGPath::isAbsolute() const
348 {
349   if (path.empty()) {
350     return false;
351   }
352   
353 #ifdef _WIN32
354   // detect '[A-Za-z]:/'
355   if (path.size() > 2) {
356     if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
357       return true;
358     }
359   }
360 #endif
361   
362   return (path[0] == sgDirPathSep);
363 }
364
365 bool SGPath::isNull() const
366 {
367   return path.empty() || (path == "");
368 }
369
370 std::string SGPath::str_native() const
371 {
372 #ifdef _WIN32
373     std::string s = str();
374     std::string::size_type pos;
375     std::string nativeSeparator;
376     nativeSeparator = sgDirPathSepBad;
377
378     while( (pos=s.find( sgDirPathSep )) != std::string::npos ) {
379         s.replace( pos, 1, nativeSeparator );
380     }
381     return s;
382 #else
383     return str();
384 #endif
385 }