From eb5d11c1c36ccf8895083ac585cc5f2a3e363b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 7 Feb 2009 07:38:22 +0000 Subject: [PATCH] FileLister: Added code that I received from Jeffrey Walton to handle directories and files better --- src/filelister.cpp | 51 +++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/filelister.cpp b/src/filelister.cpp index 421ccb384..9eb2e73bf 100644 --- a/src/filelister.cpp +++ b/src/filelister.cpp @@ -31,6 +31,8 @@ #endif #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__) #include +#include +#pragma comment(lib, "shlwapi.lib") #endif std::string FileLister::simplifyPath(const char *originalPath) @@ -157,27 +159,41 @@ void FileLister::RecursiveAddFiles(std::vector &filenames, const st void FileLister::RecursiveAddFiles(std::vector &filenames, const std::string &path, bool recursive) { + // oss is the search string passed into FindFirst and FindNext. + // bdir is the base directory which is used to form pathnames. + // It always has a trailing backslash available for concatenation. std::ostringstream bdir, oss; - std::string cleanedPath = path; + std::string cleanedPath = path; std::replace(cleanedPath.begin(), cleanedPath.end(), '\\', '/'); + oss << cleanedPath; - if (cleanedPath.length() > 0) + // See http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx + if (PathIsDirectory(cleanedPath.c_str())) { - // Windows doesn't recognize "." as current folder by default - if (cleanedPath == ".") - { - oss << "/*"; - } - else if (cleanedPath[cleanedPath.length() - 1] == '/') + char c = cleanedPath[ cleanedPath.size()-1 ]; + switch (c) { + case '\\': + oss << '*'; bdir << cleanedPath; - oss << "*"; + break; + case '*': + bdir << cleanedPath.substr(0, cleanedPath.length() - 1); + break; + default: + oss << "\\*"; + bdir << cleanedPath << '\\'; } - else + } + else + { + std::string::size_type pos; + pos = path.find_last_of('\\'); + if (std::string::npos != pos) { - bdir << cleanedPath.substr(0, cleanedPath.rfind('/') + 1); + bdir << cleanedPath.substr(0, pos + 1); } } @@ -188,12 +204,12 @@ void FileLister::RecursiveAddFiles(std::vector &filenames, const st do { - std::ostringstream fname; - fname << bdir.str().c_str() << ffd.cFileName; - if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0') continue; + std::ostringstream fname; + fname << bdir.str().c_str() << ffd.cFileName; + if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { // File @@ -205,13 +221,16 @@ void FileLister::RecursiveAddFiles(std::vector &filenames, const st else if (recursive) { // Directory - fname << "/"; FileLister::RecursiveAddFiles(filenames, fname.str().c_str(), recursive); } } while (FindNextFile(hFind, &ffd) != FALSE); - FindClose(hFind); + if (INVALID_HANDLE_VALUE != hFind) + { + FindClose(hFind); + hFind = INVALID_HANDLE_VALUE; + } } #endif