From bbd61977f14c30ce93a2bcf5a3551708d36a29c8 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 23 Jul 2010 07:54:10 +0100 Subject: [PATCH] Change SGPath::exists to use stat(), fix '.' and '..' handling on Windows, add simgear::Dir version of exists(). --- simgear/misc/sg_dir.cxx | 27 +++++++++++++++++++++++++++ simgear/misc/sg_dir.hxx | 5 +++++ simgear/misc/sg_path.cxx | 31 ++++++++++++++++++++----------- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/simgear/misc/sg_dir.cxx b/simgear/misc/sg_dir.cxx index 92e5af6f..9c5aa78a 100644 --- a/simgear/misc/sg_dir.cxx +++ b/simgear/misc/sg_dir.cxx @@ -80,6 +80,12 @@ PathList Dir::children(int types, const std::string& nameFilter) const } if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + if (types & NO_DOT_OR_DOTDOT) { + if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) { + continue; + } + } + if (!(types & TYPE_DIR)) { continue; } @@ -164,6 +170,27 @@ PathList Dir::children(int types, const std::string& nameFilter) const 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 childPath = _path; diff --git a/simgear/misc/sg_dir.hxx b/simgear/misc/sg_dir.hxx index 5ff51c9c..0ebc2bf7 100644 --- a/simgear/misc/sg_dir.hxx +++ b/simgear/misc/sg_dir.hxx @@ -52,6 +52,11 @@ namespace simgear PathList children(int types = 0, const std::string& nameGlob = "") const; SGPath file(const std::string& name) const; + + /** + * Check that the directory at the path exists (and is a directory!) + */ + bool exists() const; private: mutable SGPath _path; }; diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index 8e34c987..bf0438cc 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -28,7 +28,6 @@ #include #include #include -#include #ifdef _WIN32 # include #endif @@ -49,9 +48,7 @@ static const char sgSearchPathSep = ':'; #endif -// If Unix, replace all ":" with "/". If MacOS, replace all "/" with -// ":" it should go without saying that neither of these characters -// should be used in file or directory names. In windoze, allow the +// If Unix, replace all ":" with "/". In windoze, allow the // second character to be a ":" for things like c:\foo\bar void @@ -172,13 +169,25 @@ string SGPath::extension() const { } } -bool SGPath::exists() const { - FILE* fp = fopen( path.c_str(), "r"); - if (fp == 0) { - return false; - } - fclose(fp); - return true; +bool SGPath::exists() const +{ +#ifdef _WIN32 + struct _stat buf; + + if (_stat(path.c_str(), &buf) < 0) { + return false; + } + + return true; +#else + struct stat buf ; + + if (stat(path.c_str(), &buf) < 0) { + return false ; + } + + return true; +#endif } #ifdef _WIN32 -- 2.39.5