FileLister: Added code that I received from Jeffrey Walton to handle directories and files better

This commit is contained in:
Daniel Marjamäki 2009-02-07 07:38:22 +00:00
parent 67ad1d8c64
commit eb5d11c1c3
1 changed files with 35 additions and 16 deletions

View File

@ -31,6 +31,8 @@
#endif #endif
#if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__) #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
#include <windows.h> #include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#endif #endif
std::string FileLister::simplifyPath(const char *originalPath) std::string FileLister::simplifyPath(const char *originalPath)
@ -157,27 +159,41 @@ void FileLister::RecursiveAddFiles(std::vector<std::string> &filenames, const st
void FileLister::RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive) void FileLister::RecursiveAddFiles(std::vector<std::string> &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::ostringstream bdir, oss;
std::string cleanedPath = path;
std::string cleanedPath = path;
std::replace(cleanedPath.begin(), cleanedPath.end(), '\\', '/'); std::replace(cleanedPath.begin(), cleanedPath.end(), '\\', '/');
oss << cleanedPath; 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 char c = cleanedPath[ cleanedPath.size()-1 ];
if (cleanedPath == ".") switch (c)
{
oss << "/*";
}
else if (cleanedPath[cleanedPath.length() - 1] == '/')
{ {
case '\\':
oss << '*';
bdir << cleanedPath; bdir << cleanedPath;
oss << "*"; break;
case '*':
bdir << cleanedPath.substr(0, cleanedPath.length() - 1);
break;
default:
oss << "\\*";
bdir << cleanedPath << '\\';
}
} }
else else
{ {
bdir << cleanedPath.substr(0, cleanedPath.rfind('/') + 1); std::string::size_type pos;
pos = path.find_last_of('\\');
if (std::string::npos != pos)
{
bdir << cleanedPath.substr(0, pos + 1);
} }
} }
@ -188,12 +204,12 @@ void FileLister::RecursiveAddFiles(std::vector<std::string> &filenames, const st
do do
{ {
std::ostringstream fname;
fname << bdir.str().c_str() << ffd.cFileName;
if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0') if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0')
continue; continue;
std::ostringstream fname;
fname << bdir.str().c_str() << ffd.cFileName;
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{ {
// File // File
@ -205,13 +221,16 @@ void FileLister::RecursiveAddFiles(std::vector<std::string> &filenames, const st
else if (recursive) else if (recursive)
{ {
// Directory // Directory
fname << "/";
FileLister::RecursiveAddFiles(filenames, fname.str().c_str(), recursive); FileLister::RecursiveAddFiles(filenames, fname.str().c_str(), recursive);
} }
} }
while (FindNextFile(hFind, &ffd) != FALSE); while (FindNextFile(hFind, &ffd) != FALSE);
if (INVALID_HANDLE_VALUE != hFind)
{
FindClose(hFind); FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
} }
#endif #endif