1 // sg_path.cxx -- routines to abstract out path separator differences
2 // between MacOS and the rest of the world
4 // Written by Curtis L. Olson, started April 1999.
6 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
25 #include <simgear/compiler.h>
27 #include <simgear_config.h>
28 #include <simgear/debug/logstream.hxx>
36 #include "sg_path.hxx"
38 #include <boost/algorithm/string/case_conv.hpp>
43 * define directory path separators
46 static const char sgDirPathSep = '/';
47 static const char sgDirPathSepBad = '\\';
50 static const char sgSearchPathSep = ';';
52 static const char sgSearchPathSep = ':';
56 // If Unix, replace all ":" with "/". In windoze, allow the
57 // second character to be a ":" for things like c:\foo\bar
62 for ( string::size_type i = 0; i < path.size(); ++i ) {
64 // for windoze, don't replace the ":" for the second character
69 if ( path[i] == sgDirPathSepBad ) {
70 path[i] = sgDirPathSep;
76 // default constructor
85 // create a path based on "path"
86 SGPath::SGPath( const std::string& p )
94 // create a path based on "path" and a "subpath"
95 SGPath::SGPath( const SGPath& p, const std::string& r )
98 _cacheEnabled(p._cacheEnabled)
104 SGPath::SGPath(const SGPath& p) :
107 _cacheEnabled(p._cacheEnabled),
115 SGPath& SGPath::operator=(const SGPath& p)
119 _cacheEnabled = p._cacheEnabled;
123 _modTime = p._modTime;
133 void SGPath::set( const string& p ) {
139 void SGPath::set_cached(bool cached)
141 _cacheEnabled = cached;
144 // append another piece to the existing path
145 void SGPath::append( const string& p ) {
146 if ( path.size() == 0 ) {
149 if ( p[0] != sgDirPathSep ) {
150 path += sgDirPathSep;
158 //add a new path component to the existing path string
159 void SGPath::add( const string& p ) {
160 append( sgSearchPathSep+p );
164 // concatenate a string to the end of the path without inserting a
166 void SGPath::concat( const string& p ) {
167 if ( path.size() == 0 ) {
177 // Get the file part of the path (everything after the last path sep)
178 string SGPath::file() const
180 string::size_type index = path.rfind(sgDirPathSep);
181 if (index != string::npos) {
182 return path.substr(index + 1);
189 // get the directory part of the path.
190 string SGPath::dir() const {
191 int index = path.rfind(sgDirPathSep);
193 return path.substr(0, index);
199 // get the base part of the path (everything but the extension.)
200 string SGPath::base() const
202 string::size_type index = path.rfind(".");
203 string::size_type lastSep = path.rfind(sgDirPathSep);
205 // tolerate dots inside directory names
206 if ((lastSep != string::npos) && (index < lastSep)) {
210 if (index != string::npos) {
211 return path.substr(0, index);
217 string SGPath::file_base() const
219 string::size_type index = path.rfind(sgDirPathSep);
220 if (index == string::npos) {
221 index = 0; // no separator in the name
223 ++index; // skip past the separator
226 string::size_type firstDot = path.find(".", index);
227 if (firstDot == string::npos) {
228 return path.substr(index); // no extensions
231 return path.substr(index, firstDot - index);
234 // get the extension (everything after the final ".")
235 // but make sure no "/" follows the "." character (otherwise it
236 // is has to be a directory name containing a ".").
237 string SGPath::extension() const {
238 int index = path.rfind(".");
239 if ((index >= 0) && (path.find("/", index) == string::npos)) {
240 return path.substr(index + 1);
246 string SGPath::lower_extension() const {
247 return boost::to_lower_copy(extension());
250 string SGPath::complete_lower_extension() const
252 string::size_type index = path.rfind(sgDirPathSep);
253 if (index == string::npos) {
254 index = 0; // no separator in the name
256 ++index; // skip past the separator
259 string::size_type firstDot = path.find(".", index);
260 if ((firstDot != string::npos) && (path.find(sgDirPathSep, firstDot) == string::npos)) {
261 return boost::to_lower_copy(path.substr(firstDot + 1));
267 void SGPath::validate() const
269 if (_cached && _cacheEnabled) {
276 bool remove_trailing = false;
277 if ( path.length() > 1 && path[path.length()-1] == '/' )
278 remove_trailing=true;
279 if (_stat (path.substr(0,remove_trailing?path.length()-1:path.length()).c_str(), &buf ) < 0) {
283 _isFile = ((S_IFREG & buf.st_mode ) !=0);
284 _isDir = ((S_IFDIR & buf.st_mode ) !=0);
285 _modTime = buf.st_mtime;
291 if (stat(path.c_str(), &buf ) < 0) {
295 _isFile = ((S_ISREG(buf.st_mode )) != 0);
296 _isDir = ((S_ISDIR(buf.st_mode )) != 0);
297 _modTime = buf.st_mtime;
304 bool SGPath::exists() const
310 bool SGPath::isDir() const
313 return _exists && _isDir;
316 bool SGPath::isFile() const
319 return _exists && _isFile;
323 # define sgMkDir(d,m) _mkdir(d)
325 # define sgMkDir(d,m) mkdir(d,m)
329 int SGPath::create_dir( mode_t mode ) {
330 string_list dirlist = sgPathSplit(dir());
331 if ( dirlist.empty() )
333 string path = dirlist[0];
334 string_list path_elements = sgPathBranchSplit(path);
335 bool absolute = !path.empty() && path[0] == sgDirPathSep;
338 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
339 dir.concat( path_elements[0] );
341 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
342 dir.append( path_elements[1] );
348 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
349 dir.append(path_elements[i]);
352 return 0; // Directory already exists
354 if ( sgMkDir( dir.c_str(), mode) ) {
355 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
358 for(; i < path_elements.size(); i++) {
359 dir.append(path_elements[i]);
360 if ( sgMkDir( dir.c_str(), mode) ) {
361 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
369 string_list sgPathBranchSplit( const string &dirpath ) {
370 string_list path_elements;
371 string element, path = dirpath;
372 while ( path.size() ) {
373 size_t p = path.find( sgDirPathSep );
374 if ( p != string::npos ) {
375 element = path.substr( 0, p );
376 path.erase( 0, p + 1 );
381 if ( element.size() )
382 path_elements.push_back( element );
384 return path_elements;
388 string_list sgPathSplit( const string &search_path ) {
389 string tmp = search_path;
396 int index = tmp.find(sgSearchPathSep);
398 result.push_back( tmp.substr(0, index) );
399 tmp = tmp.substr( index + 1 );
402 result.push_back( tmp );
410 bool SGPath::isAbsolute() const
417 // detect '[A-Za-z]:/'
418 if (path.size() > 2) {
419 if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
425 return (path[0] == sgDirPathSep);
428 bool SGPath::isNull() const
430 return path.empty() || (path == "");
433 std::string SGPath::str_native() const
436 std::string s = str();
437 std::string::size_type pos;
438 std::string nativeSeparator;
439 nativeSeparator = sgDirPathSepBad;
441 while( (pos=s.find( sgDirPathSep )) != std::string::npos ) {
442 s.replace( pos, 1, nativeSeparator );
450 bool SGPath::remove()
452 int err = ::unlink(c_str());
454 SG_LOG(SG_IO, SG_WARN, "file remove failed: (" << str() << ") " << strerror(errno));
459 time_t SGPath::modTime() const
465 bool SGPath::operator==(const SGPath& other) const
467 return (path == other.path);
470 bool SGPath::operator!=(const SGPath& other) const
472 return (path != other.path);
475 bool SGPath::rename(const SGPath& newName)
477 if (::rename(c_str(), newName.c_str()) != 0) {
478 SG_LOG(SG_IO, SG_WARN, "renamed failed: from " << str() << " to " << newName.str()
479 << " reason: " << strerror(errno));