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