Change SGPath::exists to use stat(), fix '.' and '..' handling on Windows, add simgear::Dir version of exists().
This commit is contained in:
@@ -80,6 +80,12 @@ PathList Dir::children(int types, const std::string& nameFilter) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||||
|
if (types & NO_DOT_OR_DOTDOT) {
|
||||||
|
if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!(types & TYPE_DIR)) {
|
if (!(types & TYPE_DIR)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -164,6 +170,27 @@ PathList Dir::children(int types, const std::string& nameFilter) const
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Dir::exists() const
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
struct _stat buf ;
|
||||||
|
|
||||||
|
if (_stat (_path.c_str(), &buf ) < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((S_IFDIR & buf.st_mode ) !=0);
|
||||||
|
#else
|
||||||
|
struct stat buf ;
|
||||||
|
|
||||||
|
if (stat(_path.c_str(), &buf ) < 0) {
|
||||||
|
return false ;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((S_ISDIR(buf.st_mode )) != 0);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
SGPath Dir::file(const std::string& name) const
|
SGPath Dir::file(const std::string& name) const
|
||||||
{
|
{
|
||||||
SGPath childPath = _path;
|
SGPath childPath = _path;
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ namespace simgear
|
|||||||
PathList children(int types = 0, const std::string& nameGlob = "") const;
|
PathList children(int types = 0, const std::string& nameGlob = "") const;
|
||||||
|
|
||||||
SGPath file(const std::string& name) const;
|
SGPath file(const std::string& name) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check that the directory at the path exists (and is a directory!)
|
||||||
|
*/
|
||||||
|
bool exists() const;
|
||||||
private:
|
private:
|
||||||
mutable SGPath _path;
|
mutable SGPath _path;
|
||||||
};
|
};
|
||||||
|
|||||||
+20
-11
@@ -28,7 +28,6 @@
|
|||||||
#include <simgear/debug/logstream.hxx>
|
#include <simgear/debug/logstream.hxx>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# include <direct.h>
|
# include <direct.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -49,9 +48,7 @@ static const char sgSearchPathSep = ':';
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// If Unix, replace all ":" with "/". If MacOS, replace all "/" with
|
// If Unix, replace all ":" with "/". In windoze, allow the
|
||||||
// ":" it should go without saying that neither of these characters
|
|
||||||
// should be used in file or directory names. 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
|
||||||
@@ -172,13 +169,25 @@ string SGPath::extension() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SGPath::exists() const {
|
bool SGPath::exists() const
|
||||||
FILE* fp = fopen( path.c_str(), "r");
|
{
|
||||||
if (fp == 0) {
|
#ifdef _WIN32
|
||||||
return false;
|
struct _stat buf;
|
||||||
}
|
|
||||||
fclose(fp);
|
if (_stat(path.c_str(), &buf) < 0) {
|
||||||
return true;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
struct stat buf ;
|
||||||
|
|
||||||
|
if (stat(path.c_str(), &buf) < 0) {
|
||||||
|
return false ;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|||||||
Reference in New Issue
Block a user