@@ -34,11 +34,6 @@ namespace fs
34
34
35
35
#define _WIN32_WINNT 0x0501
36
36
#include < windows.h>
37
- #include < malloc.h>
38
- #include < tchar.h>
39
- #include < wchar.h>
40
-
41
- #define BUFSIZE MAX_PATH
42
37
43
38
std::vector<DirListNode> GetDirListing (std::string pathstring)
44
39
{
@@ -47,34 +42,18 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
47
42
WIN32_FIND_DATA FindFileData;
48
43
HANDLE hFind = INVALID_HANDLE_VALUE;
49
44
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 ());
71
45
46
+ std::string dirSpec = pathstring + " \\ *" ;
47
+
72
48
// Find the first file in the directory.
73
- hFind = FindFirstFile (DirSpec , &FindFileData);
49
+ hFind = FindFirstFile (dirSpec. c_str () , &FindFileData);
74
50
75
51
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
+ }
78
57
} else {
79
58
// NOTE:
80
59
// Be very sure to not include '..' in the results, it will
@@ -83,7 +62,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
83
62
DirListNode node;
84
63
node.name = FindFileData.cFileName ;
85
64
node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
86
- if (node.name != " ." && node.name != " .." )
65
+ if (node.name != " ." && node.name != " .." )
87
66
listing.push_back (node);
88
67
89
68
// List all the other files in the directory.
@@ -98,23 +77,12 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
98
77
dwError = GetLastError ();
99
78
FindClose (hFind);
100
79
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
+ }
106
85
}
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
-
118
86
return listing;
119
87
}
120
88
@@ -246,7 +214,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
246
214
// NOTE:
247
215
// Be very sure to not include '..' in the results, it will
248
216
// 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 )
250
218
continue ;
251
219
252
220
DirListNode node;
0 commit comments