]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
Add VS2010 project files
[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
37 /**
38  * define directory path separators
39  */
40
41 static const char sgDirPathSep = '/';
42 static const char sgDirPathSepBad = '\\';
43
44 #ifdef _WIN32
45 static const char sgSearchPathSep = ';';
46 #else
47 static const char sgSearchPathSep = ':';
48 #endif
49
50
51 // If Unix, replace all ":" with "/".  In windoze, allow the
52 // second character to be a ":" for things like c:\foo\bar
53
54 void
55 SGPath::fix()
56 {
57     for ( string::size_type i = 0; i < path.size(); ++i ) {
58 #if defined( WIN32 )
59         // for windoze, don't replace the ":" for the second character
60         if ( i == 1 ) {
61             continue;
62         }
63 #endif
64         if ( path[i] == sgDirPathSepBad ) {
65             path[i] = sgDirPathSep;
66         }
67     }
68 }
69
70
71 // default constructor
72 SGPath::SGPath()
73     : path("")
74 {
75 }
76
77
78 // create a path based on "path"
79 SGPath::SGPath( const std::string& p )
80     : path(p)
81 {
82     fix();
83 }
84
85
86 // destructor
87 SGPath::~SGPath() {
88 }
89
90
91 // set path
92 void SGPath::set( const string& p ) {
93     path = p;
94     fix();
95 }
96
97
98 // append another piece to the existing path
99 void SGPath::append( const string& p ) {
100     if ( path.size() == 0 ) {
101         path = p;
102     } else {
103         if ( p[0] != sgDirPathSep ) {
104             path += sgDirPathSep;
105         }
106         path += p;
107     }
108     fix();
109 }
110
111 //add a new path component to the existing path string
112 void SGPath::add( const string& p ) {
113     append( sgSearchPathSep+p );
114 }
115
116
117 // concatenate a string to the end of the path without inserting a
118 // path separator
119 void SGPath::concat( const string& p ) {
120     if ( path.size() == 0 ) {
121         path = p;
122     } else {
123         path += p;
124     }
125     fix();
126 }
127
128
129 // Get the file part of the path (everything after the last path sep)
130 string SGPath::file() const {
131     int index = path.rfind(sgDirPathSep);
132     if (index >= 0) {
133         return path.substr(index + 1);
134     } else {
135         return "";
136     }
137 }
138   
139
140 // get the directory part of the path.
141 string SGPath::dir() const {
142     int index = path.rfind(sgDirPathSep);
143     if (index >= 0) {
144         return path.substr(0, index);
145     } else {
146         return "";
147     }
148 }
149
150 // get the base part of the path (everything but the extension.)
151 string SGPath::base() const {
152     int index = path.rfind(".");
153     if ((index >= 0) && (path.find("/", index) == string::npos)) {
154         return path.substr(0, index);
155     } else {
156         return "";
157     }
158 }
159
160 // get the extension (everything after the final ".")
161 // but make sure no "/" follows the "." character (otherwise it
162 // is has to be a directory name containing a ".").
163 string SGPath::extension() const {
164     int index = path.rfind(".");
165     if ((index >= 0)  && (path.find("/", index) == string::npos)) {
166         return path.substr(index + 1);
167     } else {
168         return "";
169     }
170 }
171
172 bool SGPath::exists() const
173 {
174 #ifdef _WIN32
175   struct _stat buf;
176
177   if (_stat(path.c_str(), &buf) < 0) {
178     return false;
179   }
180   
181   return true;
182 #else
183   struct stat buf ;
184
185   if (stat(path.c_str(), &buf) < 0) {
186     return false ;
187   }
188   
189   return true;
190 #endif
191 }
192
193 #ifdef _WIN32
194 #  define sgMkDir(d,m)       _mkdir(d)
195 #else
196 #  define sgMkDir(d,m)       mkdir(d,m)
197 #endif
198
199
200 int SGPath::create_dir( mode_t mode ) {
201     string_list dirlist = sgPathSplit(dir());
202     if ( dirlist.empty() )
203         return -1;
204     string path = dirlist[0];
205     string_list path_elements = sgPathBranchSplit(path);
206     bool absolute = !path.empty() && path[0] == sgDirPathSep;
207
208     unsigned int i = 1;
209     SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
210     dir.concat( path_elements[0] );
211 #ifdef _WIN32
212     if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
213         dir.append( path_elements[1] );
214         i = 2;
215     }
216 #endif
217     struct stat info;
218     int r;
219     for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
220         dir.append(path_elements[i]);
221     }
222     if ( r == 0 ) {
223         return 0; // Directory already exists
224     }
225     if ( sgMkDir( dir.c_str(), mode) ) {
226         SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
227         return -2;
228     }
229     for(; i < path_elements.size(); i++) {
230         dir.append(path_elements[i]);
231         if ( sgMkDir( dir.c_str(), mode) ) {
232             SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
233             return -2;
234         }
235     }
236
237     return 0;
238 }
239
240 string_list sgPathBranchSplit( const string &dirpath ) {
241     string_list path_elements;
242     string element, path = dirpath;
243     while ( path.size() ) {
244         size_t p = path.find( sgDirPathSep );
245         if ( p != string::npos ) {
246             element = path.substr( 0, p );
247             path.erase( 0, p + 1 );
248         } else {
249             element = path;
250             path = "";
251         }
252         if ( element.size() )
253             path_elements.push_back( element );
254     }
255     return path_elements;
256 }
257
258
259 string_list sgPathSplit( const string &search_path ) {
260     string tmp = search_path;
261     string_list result;
262     result.clear();
263
264     bool done = false;
265
266     while ( !done ) {
267         int index = tmp.find(sgSearchPathSep);
268         if (index >= 0) {
269             result.push_back( tmp.substr(0, index) );
270             tmp = tmp.substr( index + 1 );
271         } else {
272             if ( !tmp.empty() )
273                 result.push_back( tmp );
274             done = true;
275         }
276     }
277
278     return result;
279 }