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