diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 387843693..f2283fb9e 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #ifdef MACOSX # include "common/macosx.h" @@ -50,16 +49,6 @@ #include "client/http/RequestManager.h" #include "gui/preview/Comment.h" -extern "C" -{ -#if defined(WIN) && !defined(__GNUC__) -#include -#else -#include -#endif -} - - Client::Client(): messageOfTheDay("Fetching the message of the day..."), versionCheckRequest(nullptr), @@ -991,22 +980,13 @@ void Client::updateStamps() void Client::RescanStamps() { - DIR * directory; - struct dirent * entry; - directory = opendir("stamps"); - if (directory != NULL) + stampIDs.clear(); + for (auto &stamp : Platform::DirectorySearch("stamps", "", { ".stm" })) { - stampIDs.clear(); - while ((entry = readdir(directory))) - { - ByteString name = entry->d_name; - if(name != ".." && name != "." && name.EndsWith(".stm") && name.size() == 14) - stampIDs.push_front(name.Substr(0, 10)); - } - closedir(directory); - stampIDs.sort(std::greater()); - updateStamps(); + stampIDs.push_front(stamp.Substr(0, 10)); } + stampIDs.sort(std::greater()); + updateStamps(); } int Client::GetStampsCount() diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index ac0857ac4..06ca8ec15 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -22,6 +21,7 @@ # include # include # include +# include #endif #ifdef MACOSX # include @@ -291,11 +291,10 @@ std::vector DirectorySearch(ByteString directory, ByteString search, { //Get full file listing //Normalise directory string, ensure / or \ is present - if (*directory.rbegin() != '/' && *directory.rbegin() != '\\') + if (!directory.size() || (directory.back() != '/' && directory.back() != '\\')) directory += PATH_SEP; std::vector directoryList; -#if defined(WIN) && !defined(__GNUC__) - //Windows +#ifdef WIN struct _wfinddata_t currentFile; intptr_t findFileHandle; ByteString fileMatch = directory + "*.*"; @@ -306,14 +305,11 @@ std::vector DirectorySearch(ByteString directory, ByteString search, } do { - ByteString currentFileName = Platform::WinNarrow(currentFile.name); - if (currentFileName.length() > 4) - directoryList.push_back(currentFileName); + directoryList.push_back(Platform::WinNarrow(currentFile.name)); } while (_wfindnext(findFileHandle, ¤tFile) == 0); _findclose(findFileHandle); #else - //Linux or MinGW struct dirent * directoryEntry; DIR *directoryHandle = opendir(directory.c_str()); if (!directoryHandle) @@ -322,9 +318,7 @@ std::vector DirectorySearch(ByteString directory, ByteString search, } while ((directoryEntry = readdir(directoryHandle))) { - ByteString currentFileName = ByteString(directoryEntry->d_name); - if (currentFileName.length()>4) - directoryList.push_back(currentFileName); + directoryList.push_back(ByteString(directoryEntry->d_name)); } closedir(directoryHandle); #endif @@ -336,12 +330,12 @@ std::vector DirectorySearch(ByteString directory, ByteString search, { ByteString filename = *iter, tempfilename = *iter; bool extensionMatch = !extensions.size(); - for (std::vector::iterator extIter = extensions.begin(), extEnd = extensions.end(); extIter != extEnd; ++extIter) + for (auto &extension : extensions) { - if (filename.EndsWith(*extIter)) + if (filename.size() >= extension.size() && filename.EndsWith(extension)) { extensionMatch = true; - tempfilename = filename.SubstrFromEnd(0, (*extIter).size()).ToLower(); + tempfilename = filename.SubstrFromEnd(0, extension.size()).ToLower(); break; } } diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 8605e283f..2d809ca48 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -59,7 +59,6 @@ extern "C" #include #endif #include -#include } #include "eventcompat.lua.h" @@ -3783,32 +3782,17 @@ void LuaScriptInterface::initFileSystemAPI() int LuaScriptInterface::fileSystem_list(lua_State * l) { auto directoryName = tpt_lua_checkByteString(l, 1); - - int index = 1; lua_newtable(l); - - DIR * directory; - struct dirent * entry; - - // FIXME: winapi - directory = opendir(directoryName.c_str()); - if (directory != NULL) + int index = 0; + for (auto &name : Platform::DirectorySearch(directoryName, "", {})) { - while ((entry = readdir(directory))) + if (name != "." && name != "..") { - if(strncmp(entry->d_name, "..", 3) && strncmp(entry->d_name, ".", 2)) - { - lua_pushstring(l, entry->d_name); - lua_rawseti(l, -2, index++); - } + index += 1; + lua_pushstring(l, name.c_str()); + lua_rawseti(l, -2, index); } - closedir(directory); } - else - { - lua_pushnil(l); - } - return 1; }