Rewrite fs:GetDirListing(file) by kahrl

This commit is contained in:
SmallJoker 2014-12-10 02:58:12 +10:00 committed by Craig Robbins
parent 5229a229a6
commit 2f8fbdb9f5

@ -34,11 +34,6 @@ namespace fs
#define _WIN32_WINNT 0x0501 #define _WIN32_WINNT 0x0501
#include <windows.h> #include <windows.h>
#include <malloc.h>
#include <tchar.h>
#include <wchar.h>
#define BUFSIZE MAX_PATH
std::vector<DirListNode> GetDirListing(std::string pathstring) std::vector<DirListNode> GetDirListing(std::string pathstring)
{ {
@ -47,34 +42,18 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
WIN32_FIND_DATA FindFileData; WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE; HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError; DWORD dwError;
LPTSTR DirSpec;
INT retval;
DirSpec = (LPTSTR) malloc (BUFSIZE); std::string dirSpec = pathstring + "\\*";
if(DirSpec == NULL) {
errorstream<<"GetDirListing: Insufficient memory available"<<std::endl;
retval = 1;
goto Cleanup;
}
// Check that the input is not larger than allowed.
if (pathstring.size() > (BUFSIZE - 2)) {
errorstream<<"GetDirListing: Input directory is too large."<<std::endl;
retval = 3;
goto Cleanup;
}
//_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
// Find the first file in the directory. // Find the first file in the directory.
hFind = FindFirstFile(DirSpec, &FindFileData); hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) { if (hFind == INVALID_HANDLE_VALUE) {
retval = (-1); dwError = GetLastError();
goto Cleanup; if (dwError != ERROR_FILE_NOT_FOUND && dwError != ERROR_PATH_NOT_FOUND) {
errorstream << "GetDirListing: FindFirstFile error."
<< " Error is " << dwError << std::endl;
}
} else { } else {
// NOTE: // NOTE:
// Be very sure to not include '..' in the results, it will // Be very sure to not include '..' in the results, it will
@ -83,7 +62,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
DirListNode node; DirListNode node;
node.name = FindFileData.cFileName; node.name = FindFileData.cFileName;
node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
if(node.name != "." && node.name != "..") if (node.name != "." && node.name != "..")
listing.push_back(node); listing.push_back(node);
// List all the other files in the directory. // List all the other files in the directory.
@ -98,23 +77,12 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
dwError = GetLastError(); dwError = GetLastError();
FindClose(hFind); FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES) { if (dwError != ERROR_NO_MORE_FILES) {
errorstream<<"GetDirListing: FindNextFile error. Error is " errorstream << "GetDirListing: FindNextFile error."
<<dwError<<std::endl; << " Error is " << dwError << std::endl;
retval = (-1); listing.clear();
goto Cleanup; return listing;
} }
} }
retval = 0;
Cleanup:
free(DirSpec);
if(retval != 0) listing.clear();
//for(unsigned int i=0; i<listing.size(); i++){
// infostream<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
//}
return listing; return listing;
} }
@ -246,7 +214,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
// NOTE: // NOTE:
// Be very sure to not include '..' in the results, it will // Be very sure to not include '..' in the results, it will
// result in an epic failure when deleting stuff. // result in an epic failure when deleting stuff.
if(dirp->d_name == "." || dirp->d_name == "..") if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
continue; continue;
DirListNode node; DirListNode node;