Skip to content

Commit 2f8fbdb

Browse files
SmallJokerZeno-
authored andcommittedDec 9, 2014
Rewrite fs:GetDirListing(file) by kahrl
1 parent 5229a22 commit 2f8fbdb

File tree

1 file changed

+15
-47
lines changed

1 file changed

+15
-47
lines changed
 

‎src/filesys.cpp

+15-47
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ namespace fs
3434

3535
#define _WIN32_WINNT 0x0501
3636
#include <windows.h>
37-
#include <malloc.h>
38-
#include <tchar.h>
39-
#include <wchar.h>
40-
41-
#define BUFSIZE MAX_PATH
4237

4338
std::vector<DirListNode> GetDirListing(std::string pathstring)
4439
{
@@ -47,34 +42,18 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
4742
WIN32_FIND_DATA FindFileData;
4843
HANDLE hFind = INVALID_HANDLE_VALUE;
4944
DWORD dwError;
50-
LPTSTR DirSpec;
51-
INT retval;
52-
53-
DirSpec = (LPTSTR) malloc (BUFSIZE);
54-
55-
if(DirSpec == NULL) {
56-
errorstream<<"GetDirListing: Insufficient memory available"<<std::endl;
57-
retval = 1;
58-
goto Cleanup;
59-
}
60-
61-
// Check that the input is not larger than allowed.
62-
if (pathstring.size() > (BUFSIZE - 2)) {
63-
errorstream<<"GetDirListing: Input directory is too large."<<std::endl;
64-
retval = 3;
65-
goto Cleanup;
66-
}
67-
68-
//_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
69-
70-
sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
7145

46+
std::string dirSpec = pathstring + "\\*";
47+
7248
// Find the first file in the directory.
73-
hFind = FindFirstFile(DirSpec, &FindFileData);
49+
hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
7450

7551
if (hFind == INVALID_HANDLE_VALUE) {
76-
retval = (-1);
77-
goto Cleanup;
52+
dwError = GetLastError();
53+
if (dwError != ERROR_FILE_NOT_FOUND && dwError != ERROR_PATH_NOT_FOUND) {
54+
errorstream << "GetDirListing: FindFirstFile error."
55+
<< " Error is " << dwError << std::endl;
56+
}
7857
} else {
7958
// NOTE:
8059
// Be very sure to not include '..' in the results, it will
@@ -83,7 +62,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
8362
DirListNode node;
8463
node.name = FindFileData.cFileName;
8564
node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
86-
if(node.name != "." && node.name != "..")
65+
if (node.name != "." && node.name != "..")
8766
listing.push_back(node);
8867

8968
// List all the other files in the directory.
@@ -98,23 +77,12 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
9877
dwError = GetLastError();
9978
FindClose(hFind);
10079
if (dwError != ERROR_NO_MORE_FILES) {
101-
errorstream<<"GetDirListing: FindNextFile error. Error is "
102-
<<dwError<<std::endl;
103-
retval = (-1);
104-
goto Cleanup;
105-
}
80+
errorstream << "GetDirListing: FindNextFile error."
81+
<< " Error is " << dwError << std::endl;
82+
listing.clear();
83+
return listing;
84+
}
10685
}
107-
retval = 0;
108-
109-
Cleanup:
110-
free(DirSpec);
111-
112-
if(retval != 0) listing.clear();
113-
114-
//for(unsigned int i=0; i<listing.size(); i++){
115-
// infostream<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
116-
//}
117-
11886
return listing;
11987
}
12088

@@ -246,7 +214,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
246214
// NOTE:
247215
// Be very sure to not include '..' in the results, it will
248216
// result in an epic failure when deleting stuff.
249-
if(dirp->d_name == "." || dirp->d_name == "..")
217+
if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
250218
continue;
251219

252220
DirListNode node;

0 commit comments

Comments
 (0)
Please sign in to comment.