Skip to content

Commit 52b35ac

Browse files
perminovVSminggo
authored andcommitted
[Win32,WinRT] Fix listFiles and listFilesRecursively use unicode. (#17813)
* [Win32,WinRT] Fix listFiles and listFilesRecursively use unicode. 1. Incorrect convertation to unicode if (length != fullpath.size()) correct only ASCII char. 2. For convert unicode exist api, to avoid errors. 3. Equal first char on '.' - error Need check full name file on "." and "..", otherwise exclude file and folder begin '.' Now not use macro UNICODE, for easy use unicode convert api(Otherwise, you need to connect different versions WinRT and Win32). * Fix minggo remark. * Move to specific platform section.
1 parent 4e92bca commit 52b35ac

File tree

5 files changed

+303
-127
lines changed

5 files changed

+303
-127
lines changed

cocos/platform/CCFileUtils.cpp

Lines changed: 99 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ THE SOFTWARE.
3434
//#include "base/ccUtils.h"
3535

3636
#include "tinyxml2/tinyxml2.h"
37-
#include "tinydir/tinydir.h"
3837
#ifdef MINIZIP_FROM_SYSTEM
3938
#include <minizip/unzip.h>
4039
#else // from our embedded sources
@@ -1167,65 +1166,6 @@ void FileUtils::getFileSize(const std::string &filepath, std::function<void(long
11671166
}, std::move(callback));
11681167
}
11691168

1170-
std::vector<std::string> FileUtils::listFiles(const std::string& dirPath) const
1171-
{
1172-
std::string fullpath = fullPathForFilename(dirPath);
1173-
std::vector<std::string> files;
1174-
if (isDirectoryExist(fullpath))
1175-
{
1176-
tinydir_dir dir;
1177-
#ifdef UNICODE
1178-
unsigned int length = MultiByteToWideChar(CP_UTF8, 0, &fullpath[0], (int)fullpath.size(), NULL, 0);
1179-
if (length != fullpath.size())
1180-
{
1181-
return files;
1182-
}
1183-
std::wstring fullpathstr(length, 0);
1184-
MultiByteToWideChar(CP_UTF8, 0, &fullpath[0], (int)fullpath.size(), &fullpathstr[0], length);
1185-
#else
1186-
std::string fullpathstr = fullpath;
1187-
#endif
1188-
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
1189-
{
1190-
while (dir.has_next)
1191-
{
1192-
tinydir_file file;
1193-
if (tinydir_readfile(&dir, &file) == -1)
1194-
{
1195-
// Error getting file
1196-
break;
1197-
}
1198-
1199-
#ifdef UNICODE
1200-
std::wstring path = file.path;
1201-
length = WideCharToMultiByte(CP_UTF8, 0, &path[0], (int)path.size(), NULL, 0, NULL, NULL);
1202-
std::string filepath;
1203-
if (length > 0)
1204-
{
1205-
filepath.resize(length);
1206-
WideCharToMultiByte(CP_UTF8, 0, &path[0], (int)path.size(), &filepath[0], length, NULL, NULL);
1207-
}
1208-
#else
1209-
std::string filepath = file.path;
1210-
#endif
1211-
if (file.is_dir)
1212-
{
1213-
filepath.append("/");
1214-
}
1215-
files.push_back(filepath);
1216-
1217-
if (tinydir_next(&dir) == -1)
1218-
{
1219-
// Error getting next file
1220-
break;
1221-
}
1222-
}
1223-
}
1224-
tinydir_close(&dir);
1225-
}
1226-
return files;
1227-
}
1228-
12291169
void FileUtils::listFilesAsync(const std::string& dirPath, std::function<void(std::vector<std::string>)> callback) const
12301170
{
12311171
auto fullPath = fullPathForFilename(dirPath);
@@ -1234,71 +1174,6 @@ void FileUtils::listFilesAsync(const std::string& dirPath, std::function<void(st
12341174
}, std::move(callback));
12351175
}
12361176

1237-
void FileUtils::listFilesRecursively(const std::string& dirPath, std::vector<std::string> *files) const
1238-
{
1239-
std::string fullpath = fullPathForFilename(dirPath);
1240-
if (isDirectoryExist(fullpath))
1241-
{
1242-
tinydir_dir dir;
1243-
#ifdef UNICODE
1244-
unsigned int length = MultiByteToWideChar(CP_UTF8, 0, &fullpath[0], (int)fullpath.size(), NULL, 0);
1245-
if (length != fullpath.size())
1246-
{
1247-
return;
1248-
}
1249-
std::wstring fullpathstr(length, 0);
1250-
MultiByteToWideChar(CP_UTF8, 0, &fullpath[0], (int)fullpath.size(), &fullpathstr[0], length);
1251-
#else
1252-
std::string fullpathstr = fullpath;
1253-
#endif
1254-
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
1255-
{
1256-
while (dir.has_next)
1257-
{
1258-
tinydir_file file;
1259-
if (tinydir_readfile(&dir, &file) == -1)
1260-
{
1261-
// Error getting file
1262-
break;
1263-
}
1264-
1265-
#ifdef UNICODE
1266-
std::wstring path = file.path;
1267-
length = WideCharToMultiByte(CP_UTF8, 0, &path[0], (int)path.size(), NULL, 0, NULL, NULL);
1268-
std::string filepath;
1269-
if (length > 0)
1270-
{
1271-
filepath.resize(length);
1272-
WideCharToMultiByte(CP_UTF8, 0, &path[0], (int)path.size(), &filepath[0], length, NULL, NULL);
1273-
}
1274-
#else
1275-
std::string filepath = file.path;
1276-
#endif
1277-
if (file.name[0] != '.')
1278-
{
1279-
if (file.is_dir)
1280-
{
1281-
filepath.append("/");
1282-
files->push_back(filepath);
1283-
listFilesRecursively(filepath, files);
1284-
}
1285-
else
1286-
{
1287-
files->push_back(filepath);
1288-
}
1289-
}
1290-
1291-
if (tinydir_next(&dir) == -1)
1292-
{
1293-
// Error getting next file
1294-
break;
1295-
}
1296-
}
1297-
}
1298-
tinydir_close(&dir);
1299-
}
1300-
}
1301-
13021177
void FileUtils::listFilesRecursivelyAsync(const std::string& dirPath, std::function<void(std::vector<std::string>)> callback) const
13031178
{
13041179
auto fullPath = fullPathForFilename(dirPath);
@@ -1359,7 +1234,20 @@ long FileUtils::getFileSize(const std::string &filepath)
13591234
return 0;
13601235
}
13611236

1237+
std::vector<std::string> FileUtils::listFiles(const std::string& dirPath) const
1238+
{
1239+
CCASSERT(false, "FileUtils not support listFiles");
1240+
return std::vector<std::string>();
1241+
}
1242+
1243+
void FileUtils::listFilesRecursively(const std::string& dirPath, std::vector<std::string> *files) const
1244+
{
1245+
CCASSERT(false, "FileUtils not support listFilesRecursively");
1246+
return;
1247+
}
1248+
13621249
#else
1250+
#include "tinydir/tinydir.h"
13631251
// default implements for unix like os
13641252
#include <sys/types.h>
13651253
#include <errno.h>
@@ -1491,7 +1379,6 @@ std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
14911379
return filenameUtf8;
14921380
}
14931381

1494-
14951382
long FileUtils::getFileSize(const std::string &filepath)
14961383
{
14971384
CCASSERT(!filepath.empty(), "Invalid path");
@@ -1519,6 +1406,92 @@ long FileUtils::getFileSize(const std::string &filepath)
15191406
return (long)(info.st_size);
15201407
}
15211408
}
1409+
1410+
std::vector<std::string> FileUtils::listFiles(const std::string& dirPath) const
1411+
{
1412+
std::vector<std::string> files;
1413+
std::string fullpath = fullPathForFilename(dirPath);
1414+
if (isDirectoryExist(fullpath))
1415+
{
1416+
tinydir_dir dir;
1417+
std::string fullpathstr = fullpath;
1418+
1419+
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
1420+
{
1421+
while (dir.has_next)
1422+
{
1423+
tinydir_file file;
1424+
if (tinydir_readfile(&dir, &file) == -1)
1425+
{
1426+
// Error getting file
1427+
break;
1428+
}
1429+
std::string filepath = file.path;
1430+
1431+
if (file.is_dir)
1432+
{
1433+
filepath.append("/");
1434+
}
1435+
files.push_back(filepath);
1436+
1437+
if (tinydir_next(&dir) == -1)
1438+
{
1439+
// Error getting next file
1440+
break;
1441+
}
1442+
}
1443+
}
1444+
tinydir_close(&dir);
1445+
}
1446+
return files;
1447+
}
1448+
1449+
void FileUtils::listFilesRecursively(const std::string& dirPath, std::vector<std::string> *files) const
1450+
{
1451+
std::string fullpath = fullPathForFilename(dirPath);
1452+
if (isDirectoryExist(fullpath))
1453+
{
1454+
tinydir_dir dir;
1455+
std::string fullpathstr = fullpath;
1456+
1457+
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
1458+
{
1459+
while (dir.has_next)
1460+
{
1461+
tinydir_file file;
1462+
if (tinydir_readfile(&dir, &file) == -1)
1463+
{
1464+
// Error getting file
1465+
break;
1466+
}
1467+
std::string fileName = file.name;
1468+
1469+
if (fileName != "." && fileName != "..")
1470+
{
1471+
std::string filepath = file.path;
1472+
if (file.is_dir)
1473+
{
1474+
filepath.append("/");
1475+
files->push_back(filepath);
1476+
listFilesRecursively(filepath, files);
1477+
}
1478+
else
1479+
{
1480+
files->push_back(filepath);
1481+
}
1482+
}
1483+
1484+
if (tinydir_next(&dir) == -1)
1485+
{
1486+
// Error getting next file
1487+
break;
1488+
}
1489+
}
1490+
}
1491+
tinydir_close(&dir);
1492+
}
1493+
}
1494+
15221495
#endif
15231496

15241497
//////////////////////////////////////////////////////////////////////////

cocos/platform/win32/CCFileUtils-win32.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ THE SOFTWARE.
2929
#include "platform/win32/CCFileUtils-win32.h"
3030
#include "platform/win32/CCUtils-win32.h"
3131
#include "platform/CCCommon.h"
32+
#include "tinydir/tinydir.h"
3233
#include <Shlobj.h>
3334
#include <cstdlib>
3435
#include <regex>
@@ -219,6 +220,91 @@ std::string FileUtilsWin32::getFullPathForDirectoryAndFilename(const std::string
219220
return FileUtils::getFullPathForDirectoryAndFilename(unixDirectory, unixFilename);
220221
}
221222

223+
void FileUtilsWin32::listFilesRecursively(const std::string& dirPath, std::vector<std::string> *files) const
224+
{
225+
std::string fullpath = fullPathForFilename(dirPath);
226+
if (isDirectoryExist(fullpath))
227+
{
228+
tinydir_dir dir;
229+
std::wstring fullpathstr = StringUtf8ToWideChar(fullpath);
230+
231+
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
232+
{
233+
while (dir.has_next)
234+
{
235+
tinydir_file file;
236+
if (tinydir_readfile(&dir, &file) == -1)
237+
{
238+
// Error getting file
239+
break;
240+
}
241+
std::string fileName = StringWideCharToUtf8(file.name);
242+
243+
if (fileName != "." && fileName != "..")
244+
{
245+
std::string filepath = StringWideCharToUtf8(file.path);
246+
if (file.is_dir)
247+
{
248+
filepath.append("/");
249+
files->push_back(filepath);
250+
listFilesRecursively(filepath, files);
251+
}
252+
else
253+
{
254+
files->push_back(filepath);
255+
}
256+
}
257+
258+
if (tinydir_next(&dir) == -1)
259+
{
260+
// Error getting next file
261+
break;
262+
}
263+
}
264+
}
265+
tinydir_close(&dir);
266+
}
267+
}
268+
269+
std::vector<std::string> FileUtilsWin32::listFiles(const std::string& dirPath) const
270+
{
271+
std::string fullpath = fullPathForFilename(dirPath);
272+
std::vector<std::string> files;
273+
if (isDirectoryExist(fullpath))
274+
{
275+
tinydir_dir dir;
276+
std::wstring fullpathstr = StringUtf8ToWideChar(fullpath);
277+
278+
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
279+
{
280+
while (dir.has_next)
281+
{
282+
tinydir_file file;
283+
if (tinydir_readfile(&dir, &file) == -1)
284+
{
285+
// Error getting file
286+
break;
287+
}
288+
289+
std::string filepath = StringWideCharToUtf8(file.path);
290+
if (file.is_dir)
291+
{
292+
filepath.append("/");
293+
}
294+
files.push_back(filepath);
295+
296+
if (tinydir_next(&dir) == -1)
297+
{
298+
// Error getting next file
299+
break;
300+
}
301+
}
302+
}
303+
tinydir_close(&dir);
304+
}
305+
return files;
306+
}
307+
222308
string FileUtilsWin32::getWritablePath() const
223309
{
224310
if (_writablePath.length())

cocos/platform/win32/CCFileUtils-win32.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ class CC_DLL FileUtilsWin32 : public FileUtils
132132
* @return The full path of the file, if the file can't be found, it will return an empty string.
133133
*/
134134
virtual std::string getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const override;
135+
136+
/**
137+
* List all files in a directory.
138+
*
139+
* @param dirPath The path of the directory, it could be a relative or an absolute path.
140+
* @return File paths in a string vector
141+
*/
142+
virtual std::vector<std::string> listFiles(const std::string& dirPath) const;
143+
144+
/**
145+
* List all files recursively in a directory.
146+
*
147+
* @param dirPath The path of the directory, it could be a relative or an absolute path.
148+
* @return File paths in a string vector
149+
*/
150+
virtual void listFilesRecursively(const std::string& dirPath, std::vector<std::string> *files) const;
135151
};
136152

137153
// end of platform group

0 commit comments

Comments
 (0)