X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmisc%2Fsg_dir.cxx;h=95929229a57a6b01647351d42ef45c4f548f1f13;hb=e4e31be7d43569a92a5d9fa7e784381b66cbd95a;hp=e912b2d1af01e29585542faf12d1534f80ebca12;hpb=959f44502bb75df03494642b8c4546478f2c9c9b;p=simgear.git diff --git a/simgear/misc/sg_dir.cxx b/simgear/misc/sg_dir.cxx index e912b2d1..95929229 100644 --- a/simgear/misc/sg_dir.cxx +++ b/simgear/misc/sg_dir.cxx @@ -29,8 +29,6 @@ # include #endif -#include - #include #include @@ -108,6 +106,8 @@ PathList Dir::children(int types, const std::string& nameFilter) const return result; } + int filterLen = nameFilter.size(); + while (true) { struct dirent* entry = readdir(dp); if (!entry) { @@ -115,16 +115,17 @@ PathList Dir::children(int types, const std::string& nameFilter) const } // skip hidden files (names beginning with '.') unless requested - if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.')) { + if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') && + strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) { continue; } - struct stat s; - if (stat(file(entry->d_name).c_str(), &s)) { + SGPath f = file(entry->d_name); + if (!f.exists()) { continue; // stat() failed } - if (S_ISDIR(s.st_mode)) { + if (f.isDir()) { // directory handling if (!(types & TYPE_DIR)) { continue; @@ -135,7 +136,7 @@ PathList Dir::children(int types, const std::string& nameFilter) const continue; } } - } else if (S_ISREG(s.st_mode)) { + } else if (f.isFile()) { // regular file handling if (!(types & TYPE_FILE)) { continue; @@ -146,7 +147,13 @@ PathList Dir::children(int types, const std::string& nameFilter) const } if (!nameFilter.empty()) { - if (strstr(entry->d_name, nameFilter.c_str()) == NULL) { + int nameLen = strlen(entry->d_name); + if (nameLen < filterLen) { + continue; // name is shorter than the filter + } + + char* nameSuffix = entry->d_name + (nameLen - filterLen); + if (strcmp(nameSuffix, nameFilter.c_str())) { continue; } } @@ -162,23 +169,7 @@ PathList Dir::children(int types, const std::string& nameFilter) const 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 + return _path.isDir(); } SGPath Dir::file(const std::string& name) const