Fix line endings
This commit is contained in:
parent
02462d1752
commit
d314b6a552
@ -1,368 +1,368 @@
|
|||||||
// sg_path.cxx -- routines to abstract out path separator differences
|
// sg_path.cxx -- routines to abstract out path separator differences
|
||||||
// between MacOS and the rest of the world
|
// between MacOS and the rest of the world
|
||||||
//
|
//
|
||||||
// Written by Curtis L. Olson, started April 1999.
|
// Written by Curtis L. Olson, started April 1999.
|
||||||
//
|
//
|
||||||
// Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
|
// Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
|
||||||
//
|
//
|
||||||
// This library is free software; you can redistribute it and/or
|
// This library is free software; you can redistribute it and/or
|
||||||
// modify it under the terms of the GNU Library General Public
|
// modify it under the terms of the GNU Library General Public
|
||||||
// License as published by the Free Software Foundation; either
|
// License as published by the Free Software Foundation; either
|
||||||
// version 2 of the License, or (at your option) any later version.
|
// version 2 of the License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This library is distributed in the hope that it will be useful,
|
// This library is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
// Library General Public License for more details.
|
// Library General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program; if not, write to the Free Software
|
// along with this program; if not, write to the Free Software
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
//
|
//
|
||||||
// $Id$
|
// $Id$
|
||||||
|
|
||||||
|
|
||||||
#include <simgear/compiler.h>
|
#include <simgear/compiler.h>
|
||||||
|
|
||||||
#include <simgear_config.h>
|
#include <simgear_config.h>
|
||||||
#include <simgear/debug/logstream.hxx>
|
#include <simgear/debug/logstream.hxx>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# include <direct.h>
|
# include <direct.h>
|
||||||
#endif
|
#endif
|
||||||
#include "sg_path.hxx"
|
#include "sg_path.hxx"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* define directory path separators
|
* define directory path separators
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static const char sgDirPathSep = '/';
|
static const char sgDirPathSep = '/';
|
||||||
static const char sgDirPathSepBad = '\\';
|
static const char sgDirPathSepBad = '\\';
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static const char sgSearchPathSep = ';';
|
static const char sgSearchPathSep = ';';
|
||||||
#else
|
#else
|
||||||
static const char sgSearchPathSep = ':';
|
static const char sgSearchPathSep = ':';
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// If Unix, replace all ":" with "/". In windoze, allow the
|
// If Unix, replace all ":" with "/". In windoze, allow the
|
||||||
// second character to be a ":" for things like c:\foo\bar
|
// second character to be a ":" for things like c:\foo\bar
|
||||||
|
|
||||||
void
|
void
|
||||||
SGPath::fix()
|
SGPath::fix()
|
||||||
{
|
{
|
||||||
for ( string::size_type i = 0; i < path.size(); ++i ) {
|
for ( string::size_type i = 0; i < path.size(); ++i ) {
|
||||||
#if defined( WIN32 )
|
#if defined( WIN32 )
|
||||||
// for windoze, don't replace the ":" for the second character
|
// for windoze, don't replace the ":" for the second character
|
||||||
if ( i == 1 ) {
|
if ( i == 1 ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if ( path[i] == sgDirPathSepBad ) {
|
if ( path[i] == sgDirPathSepBad ) {
|
||||||
path[i] = sgDirPathSep;
|
path[i] = sgDirPathSep;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// default constructor
|
// default constructor
|
||||||
SGPath::SGPath()
|
SGPath::SGPath()
|
||||||
: path(""),
|
: path(""),
|
||||||
_cached(false)
|
_cached(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// create a path based on "path"
|
// create a path based on "path"
|
||||||
SGPath::SGPath( const std::string& p )
|
SGPath::SGPath( const std::string& p )
|
||||||
: path(p),
|
: path(p),
|
||||||
_cached(false)
|
_cached(false)
|
||||||
{
|
{
|
||||||
fix();
|
fix();
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a path based on "path" and a "subpath"
|
// create a path based on "path" and a "subpath"
|
||||||
SGPath::SGPath( const SGPath& p, const std::string& r )
|
SGPath::SGPath( const SGPath& p, const std::string& r )
|
||||||
: path(p.path),
|
: path(p.path),
|
||||||
_cached(false)
|
_cached(false)
|
||||||
{
|
{
|
||||||
append(r);
|
append(r);
|
||||||
fix();
|
fix();
|
||||||
}
|
}
|
||||||
|
|
||||||
SGPath::SGPath(const SGPath& p) :
|
SGPath::SGPath(const SGPath& p) :
|
||||||
path(p.path),
|
path(p.path),
|
||||||
_cached(p._cached),
|
_cached(p._cached),
|
||||||
_exists(p._exists),
|
_exists(p._exists),
|
||||||
_isDir(p._isDir),
|
_isDir(p._isDir),
|
||||||
_isFile(p._isFile)
|
_isFile(p._isFile)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SGPath& SGPath::operator=(const SGPath& p)
|
SGPath& SGPath::operator=(const SGPath& p)
|
||||||
{
|
{
|
||||||
path = p.path;
|
path = p.path;
|
||||||
_cached = p._cached;
|
_cached = p._cached;
|
||||||
_exists = p._exists;
|
_exists = p._exists;
|
||||||
_isDir = p._isDir;
|
_isDir = p._isDir;
|
||||||
_isFile = p._isFile;
|
_isFile = p._isFile;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// destructor
|
// destructor
|
||||||
SGPath::~SGPath() {
|
SGPath::~SGPath() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// set path
|
// set path
|
||||||
void SGPath::set( const string& p ) {
|
void SGPath::set( const string& p ) {
|
||||||
path = p;
|
path = p;
|
||||||
fix();
|
fix();
|
||||||
_cached = false;
|
_cached = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// append another piece to the existing path
|
// append another piece to the existing path
|
||||||
void SGPath::append( const string& p ) {
|
void SGPath::append( const string& p ) {
|
||||||
if ( path.size() == 0 ) {
|
if ( path.size() == 0 ) {
|
||||||
path = p;
|
path = p;
|
||||||
} else {
|
} else {
|
||||||
if ( p[0] != sgDirPathSep ) {
|
if ( p[0] != sgDirPathSep ) {
|
||||||
path += sgDirPathSep;
|
path += sgDirPathSep;
|
||||||
}
|
}
|
||||||
path += p;
|
path += p;
|
||||||
}
|
}
|
||||||
fix();
|
fix();
|
||||||
_cached = false;
|
_cached = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//add a new path component to the existing path string
|
//add a new path component to the existing path string
|
||||||
void SGPath::add( const string& p ) {
|
void SGPath::add( const string& p ) {
|
||||||
append( sgSearchPathSep+p );
|
append( sgSearchPathSep+p );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// concatenate a string to the end of the path without inserting a
|
// concatenate a string to the end of the path without inserting a
|
||||||
// path separator
|
// path separator
|
||||||
void SGPath::concat( const string& p ) {
|
void SGPath::concat( const string& p ) {
|
||||||
if ( path.size() == 0 ) {
|
if ( path.size() == 0 ) {
|
||||||
path = p;
|
path = p;
|
||||||
} else {
|
} else {
|
||||||
path += p;
|
path += p;
|
||||||
}
|
}
|
||||||
fix();
|
fix();
|
||||||
_cached = false;
|
_cached = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get the file part of the path (everything after the last path sep)
|
// Get the file part of the path (everything after the last path sep)
|
||||||
string SGPath::file() const {
|
string SGPath::file() const {
|
||||||
int index = path.rfind(sgDirPathSep);
|
int index = path.rfind(sgDirPathSep);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
return path.substr(index + 1);
|
return path.substr(index + 1);
|
||||||
} else {
|
} else {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// get the directory part of the path.
|
// get the directory part of the path.
|
||||||
string SGPath::dir() const {
|
string SGPath::dir() const {
|
||||||
int index = path.rfind(sgDirPathSep);
|
int index = path.rfind(sgDirPathSep);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
return path.substr(0, index);
|
return path.substr(0, index);
|
||||||
} else {
|
} else {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the base part of the path (everything but the extension.)
|
// get the base part of the path (everything but the extension.)
|
||||||
string SGPath::base() const {
|
string SGPath::base() const {
|
||||||
int index = path.rfind(".");
|
int index = path.rfind(".");
|
||||||
if ((index >= 0) && (path.find("/", index) == string::npos)) {
|
if ((index >= 0) && (path.find("/", index) == string::npos)) {
|
||||||
return path.substr(0, index);
|
return path.substr(0, index);
|
||||||
} else {
|
} else {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the extension (everything after the final ".")
|
// get the extension (everything after the final ".")
|
||||||
// but make sure no "/" follows the "." character (otherwise it
|
// but make sure no "/" follows the "." character (otherwise it
|
||||||
// is has to be a directory name containing a ".").
|
// is has to be a directory name containing a ".").
|
||||||
string SGPath::extension() const {
|
string SGPath::extension() const {
|
||||||
int index = path.rfind(".");
|
int index = path.rfind(".");
|
||||||
if ((index >= 0) && (path.find("/", index) == string::npos)) {
|
if ((index >= 0) && (path.find("/", index) == string::npos)) {
|
||||||
return path.substr(index + 1);
|
return path.substr(index + 1);
|
||||||
} else {
|
} else {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SGPath::validate() const
|
void SGPath::validate() const
|
||||||
{
|
{
|
||||||
if (_cached) {
|
if (_cached) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
struct _stat buf ;
|
struct _stat buf ;
|
||||||
|
|
||||||
bool remove_trailing = false;
|
bool remove_trailing = false;
|
||||||
if ( path.length() > 1 && path[path.length()-1] == '/' )
|
if ( path.length() > 1 && path[path.length()-1] == '/' )
|
||||||
remove_trailing=true;
|
remove_trailing=true;
|
||||||
if (_stat (path.substr(0,remove_trailing?path.length()-1:path.length()).c_str(), &buf ) < 0) {
|
if (_stat (path.substr(0,remove_trailing?path.length()-1:path.length()).c_str(), &buf ) < 0) {
|
||||||
_exists = false;
|
_exists = false;
|
||||||
} else {
|
} else {
|
||||||
_exists = true;
|
_exists = true;
|
||||||
_isFile = ((S_IFREG & buf.st_mode ) !=0);
|
_isFile = ((S_IFREG & buf.st_mode ) !=0);
|
||||||
_isDir = ((S_IFDIR & buf.st_mode ) !=0);
|
_isDir = ((S_IFDIR & buf.st_mode ) !=0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
struct stat buf ;
|
struct stat buf ;
|
||||||
|
|
||||||
if (stat(path.c_str(), &buf ) < 0) {
|
if (stat(path.c_str(), &buf ) < 0) {
|
||||||
_exists = false;
|
_exists = false;
|
||||||
} else {
|
} else {
|
||||||
_exists = true;
|
_exists = true;
|
||||||
_isFile = ((S_ISREG(buf.st_mode )) != 0);
|
_isFile = ((S_ISREG(buf.st_mode )) != 0);
|
||||||
_isDir = ((S_ISDIR(buf.st_mode )) != 0);
|
_isDir = ((S_ISDIR(buf.st_mode )) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
_cached = true;
|
_cached = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SGPath::exists() const
|
bool SGPath::exists() const
|
||||||
{
|
{
|
||||||
validate();
|
validate();
|
||||||
return _exists;
|
return _exists;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SGPath::isDir() const
|
bool SGPath::isDir() const
|
||||||
{
|
{
|
||||||
validate();
|
validate();
|
||||||
return _exists && _isDir;
|
return _exists && _isDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SGPath::isFile() const
|
bool SGPath::isFile() const
|
||||||
{
|
{
|
||||||
validate();
|
validate();
|
||||||
return _exists && _isFile;
|
return _exists && _isFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# define sgMkDir(d,m) _mkdir(d)
|
# define sgMkDir(d,m) _mkdir(d)
|
||||||
#else
|
#else
|
||||||
# define sgMkDir(d,m) mkdir(d,m)
|
# define sgMkDir(d,m) mkdir(d,m)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
int SGPath::create_dir( mode_t mode ) {
|
int SGPath::create_dir( mode_t mode ) {
|
||||||
string_list dirlist = sgPathSplit(dir());
|
string_list dirlist = sgPathSplit(dir());
|
||||||
if ( dirlist.empty() )
|
if ( dirlist.empty() )
|
||||||
return -1;
|
return -1;
|
||||||
string path = dirlist[0];
|
string path = dirlist[0];
|
||||||
string_list path_elements = sgPathBranchSplit(path);
|
string_list path_elements = sgPathBranchSplit(path);
|
||||||
bool absolute = !path.empty() && path[0] == sgDirPathSep;
|
bool absolute = !path.empty() && path[0] == sgDirPathSep;
|
||||||
|
|
||||||
unsigned int i = 1;
|
unsigned int i = 1;
|
||||||
SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
|
SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
|
||||||
dir.concat( path_elements[0] );
|
dir.concat( path_elements[0] );
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
|
if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
|
||||||
dir.append( path_elements[1] );
|
dir.append( path_elements[1] );
|
||||||
i = 2;
|
i = 2;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
struct stat info;
|
struct stat info;
|
||||||
int r;
|
int r;
|
||||||
for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
|
for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
|
||||||
dir.append(path_elements[i]);
|
dir.append(path_elements[i]);
|
||||||
}
|
}
|
||||||
if ( r == 0 ) {
|
if ( r == 0 ) {
|
||||||
return 0; // Directory already exists
|
return 0; // Directory already exists
|
||||||
}
|
}
|
||||||
if ( sgMkDir( dir.c_str(), mode) ) {
|
if ( sgMkDir( dir.c_str(), mode) ) {
|
||||||
SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
|
SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
for(; i < path_elements.size(); i++) {
|
for(; i < path_elements.size(); i++) {
|
||||||
dir.append(path_elements[i]);
|
dir.append(path_elements[i]);
|
||||||
if ( sgMkDir( dir.c_str(), mode) ) {
|
if ( sgMkDir( dir.c_str(), mode) ) {
|
||||||
SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
|
SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_list sgPathBranchSplit( const string &dirpath ) {
|
string_list sgPathBranchSplit( const string &dirpath ) {
|
||||||
string_list path_elements;
|
string_list path_elements;
|
||||||
string element, path = dirpath;
|
string element, path = dirpath;
|
||||||
while ( path.size() ) {
|
while ( path.size() ) {
|
||||||
size_t p = path.find( sgDirPathSep );
|
size_t p = path.find( sgDirPathSep );
|
||||||
if ( p != string::npos ) {
|
if ( p != string::npos ) {
|
||||||
element = path.substr( 0, p );
|
element = path.substr( 0, p );
|
||||||
path.erase( 0, p + 1 );
|
path.erase( 0, p + 1 );
|
||||||
} else {
|
} else {
|
||||||
element = path;
|
element = path;
|
||||||
path = "";
|
path = "";
|
||||||
}
|
}
|
||||||
if ( element.size() )
|
if ( element.size() )
|
||||||
path_elements.push_back( element );
|
path_elements.push_back( element );
|
||||||
}
|
}
|
||||||
return path_elements;
|
return path_elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string_list sgPathSplit( const string &search_path ) {
|
string_list sgPathSplit( const string &search_path ) {
|
||||||
string tmp = search_path;
|
string tmp = search_path;
|
||||||
string_list result;
|
string_list result;
|
||||||
result.clear();
|
result.clear();
|
||||||
|
|
||||||
bool done = false;
|
bool done = false;
|
||||||
|
|
||||||
while ( !done ) {
|
while ( !done ) {
|
||||||
int index = tmp.find(sgSearchPathSep);
|
int index = tmp.find(sgSearchPathSep);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
result.push_back( tmp.substr(0, index) );
|
result.push_back( tmp.substr(0, index) );
|
||||||
tmp = tmp.substr( index + 1 );
|
tmp = tmp.substr( index + 1 );
|
||||||
} else {
|
} else {
|
||||||
if ( !tmp.empty() )
|
if ( !tmp.empty() )
|
||||||
result.push_back( tmp );
|
result.push_back( tmp );
|
||||||
done = true;
|
done = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SGPath::isAbsolute() const
|
bool SGPath::isAbsolute() const
|
||||||
{
|
{
|
||||||
if (path.empty()) {
|
if (path.empty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// detect '[A-Za-z]:/'
|
// detect '[A-Za-z]:/'
|
||||||
if (path.size() > 2) {
|
if (path.size() > 2) {
|
||||||
if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
|
if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return (path[0] == sgDirPathSep);
|
return (path[0] == sgDirPathSep);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SGPath::isNull() const
|
bool SGPath::isNull() const
|
||||||
{
|
{
|
||||||
return path.empty() || (path == "");
|
return path.empty() || (path == "");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user