Skip to content

Commit ba4945a

Browse files
committed
Improvements to filelister under Windows:
- Passed string parameters as const references - Give correct string size to WinAPI functions (they take the number of characters, not the size in bytes (as used two times) and not the size in bytes plus 1 (as used one time)) - Fixed compiler warning C4293 by hiding the code for 32bit systems - store result of wcslen(ffd.cFileName) instead of calculating it twice
1 parent 6360afd commit ba4945a

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

cli/filelister.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,38 +57,37 @@ static bool TransformAnsiToUcs2(LPCSTR psAnsi, LPWSTR psUcs, UINT nUcs)
5757
return true;
5858
}
5959

60-
static BOOL MyIsDirectory(std::string path)
60+
static BOOL MyIsDirectory(const std::string& path)
6161
{
6262
WCHAR * unicodeCleanPath = new WCHAR[path.size() + 1];
63-
TransformAnsiToUcs2(path.c_str(), unicodeCleanPath,
64-
(path.size() * sizeof(WCHAR)) + 1);
63+
TransformAnsiToUcs2(path.c_str(), unicodeCleanPath, path.size() + 1);
6564
// See http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx
6665
BOOL res = PathIsDirectory(unicodeCleanPath);
6766
delete [] unicodeCleanPath;
6867
return res;
6968
}
7069

71-
static HANDLE MyFindFirstFile(std::string path, LPWIN32_FIND_DATA findData)
70+
static HANDLE MyFindFirstFile(const std::string& path, LPWIN32_FIND_DATA findData)
7271
{
7372
WCHAR * unicodeOss = new wchar_t[path.size() + 1];
74-
TransformAnsiToUcs2(path.c_str(), unicodeOss, (path.size() + 1) * sizeof(WCHAR));
73+
TransformAnsiToUcs2(path.c_str(), unicodeOss, path.size() + 1);
7574
HANDLE hFind = FindFirstFile(unicodeOss, findData);
7675
delete [] unicodeOss;
7776
return hFind;
7877
}
7978

80-
static BOOL MyFileExists(std::string path)
79+
static BOOL MyFileExists(const std::string& path)
8180
{
8281
WCHAR * unicodeOss = new wchar_t[path.size() + 1];
83-
TransformAnsiToUcs2(path.c_str(), unicodeOss, (path.size() + 1) * sizeof(WCHAR));
82+
TransformAnsiToUcs2(path.c_str(), unicodeOss, path.size() + 1);
8483
BOOL result = PathFileExists(unicodeOss);
8584
delete [] unicodeOss;
8685
return result;
8786
}
8887

8988
#else // defined(UNICODE)
9089

91-
static BOOL MyIsDirectory(std::string path)
90+
static BOOL MyIsDirectory(const std::string& path)
9291
{
9392
#ifdef __BORLANDC__
9493
return (GetFileAttributes(path.c_str()) & FILE_ATTRIBUTE_DIRECTORY);
@@ -98,13 +97,13 @@ static BOOL MyIsDirectory(std::string path)
9897
#endif
9998
}
10099

101-
static HANDLE MyFindFirstFile(std::string path, LPWIN32_FIND_DATA findData)
100+
static HANDLE MyFindFirstFile(const std::string& path, LPWIN32_FIND_DATA findData)
102101
{
103102
HANDLE hFind = FindFirstFile(path.c_str(), findData);
104103
return hFind;
105104
}
106105

107-
static BOOL MyFileExists(std::string path)
106+
static BOOL MyFileExists(const std::string& path)
108107
{
109108
#ifdef __BORLANDC__
110109
DWORD fa = GetFileAttributes(path.c_str());
@@ -162,8 +161,9 @@ void FileLister::recursiveAddFiles(std::map<std::string, size_t> &files, const s
162161
continue;
163162

164163
#if defined(UNICODE)
165-
char * ansiFfd = new char[wcslen(ffd.cFileName) + 1];
166-
TransformUcs2ToAnsi(ffd.cFileName, ansiFfd, wcslen(ffd.cFileName) + 1);
164+
size_t length = wcslen(ffd.cFileName);
165+
char * ansiFfd = new char[length + 1];
166+
TransformUcs2ToAnsi(ffd.cFileName, ansiFfd, length + 1);
167167
#else // defined(UNICODE)
168168
const char * ansiFfd = &ffd.cFileName[0];
169169
if (strchr(ansiFfd,'?')) {
@@ -178,13 +178,14 @@ void FileLister::recursiveAddFiles(std::map<std::string, size_t> &files, const s
178178
// File
179179

180180
// If recursive is not used, accept all files given by user
181-
if (Path::sameFileName(path,ansiFfd) || Path::acceptFile(ansiFfd)) {
181+
if (Path::sameFileName(path, ansiFfd) || Path::acceptFile(ansiFfd)) {
182182
const std::string nativename = Path::fromNativeSeparators(fname.str());
183183
// Limitation: file sizes are assumed to fit in a 'size_t'
184-
if (sizeof(size_t) > 4)
185-
files[nativename] = (static_cast<size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
186-
else
187-
files[nativename] = ffd.nFileSizeLow;
184+
#ifdef _WIN64
185+
files[nativename] = (static_cast<size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
186+
#else
187+
files[nativename] = ffd.nFileSizeLow;
188+
#endif
188189
}
189190
} else {
190191
// Directory

0 commit comments

Comments
 (0)