]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
Move SGReadFileCallback from model.cxx to public class ModelRegistry
[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 #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     if ( dirlist.empty() )
199         return;
200     string path = dirlist[0];
201     string_list path_elements = sgPathBranchSplit(path);
202     bool absolute = !path.empty() && path[0] == sgDirPathSep;
203
204     unsigned int i = 1;
205     SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
206     dir.concat( path_elements[0] );
207 #if defined( _MSC_VER) || defined(__MINGW32__)
208     if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
209         dir.append( path_elements[1] );
210         i = 2;
211     }
212 #endif
213     struct stat info;
214     int r;
215     for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
216         dir.append(path_elements[i]);
217     }
218     if ( r == 0 ) {
219         return; // Directory already exists
220     }
221     if ( sgMkDir( dir.c_str(), mode) ) {
222         SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
223         return;
224     }
225     for(;i < path_elements.size(); i++) {
226         dir.append(path_elements[i]);
227         if ( sgMkDir( dir.c_str(), mode) ) {
228             SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
229             break;
230         }
231     }
232 }
233
234 string_list sgPathBranchSplit( const string &dirpath ) {
235     string_list path_elements;
236     string element, path = dirpath;
237     while ( path.size() ) {
238         size_t p = path.find( sgDirPathSep );
239         if ( p != string::npos ) {
240             element = path.substr( 0, p );
241             path.erase( 0, p + 1 );
242         } else {
243             element = path;
244             path = "";
245         }
246         if ( element.size() )
247             path_elements.push_back( element );
248     }
249     return path_elements;
250 }
251
252
253 string_list sgPathSplit( const string &search_path ) {
254     string tmp = search_path;
255     string_list result;
256     result.clear();
257
258     bool done = false;
259
260     while ( !done ) {
261         int index = tmp.find(sgSearchPathSep);
262         if (index >= 0) {
263             result.push_back( tmp.substr(0, index) );
264             tmp = tmp.substr( index + 1 );
265         } else {
266             if ( !tmp.empty() )
267                 result.push_back( tmp );
268             done = true;
269         }
270     }
271
272     return result;
273 }