diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-07-27 19:06:17 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-07-27 19:06:17 (GMT) |
| commit | 5befe5c25f8f188e7588de44ab2c8bead22ae999 (patch) | |
| tree | 643b02af217770c1a3156be03e01442557795760 /src/client/Client.cpp | |
| parent | f8ca8af387b8611c18ca7c5357efd19c8bc28941 (diff) | |
| download | powder-5befe5c25f8f188e7588de44ab2c8bead22ae999.zip powder-5befe5c25f8f188e7588de44ab2c8bead22ae999.tar.gz | |
Local file browser + some more interesting things like Progress bar UI component
Diffstat (limited to 'src/client/Client.cpp')
| -rw-r--r-- | src/client/Client.cpp | 101 |
1 files changed, 98 insertions, 3 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 2e74c48..a9661ca 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -22,13 +22,19 @@ #include "Misc.h" #include "interface/Point.h" - #include "client/SaveInfo.h" - #include "ClientListener.h" - #include "Update.h" +extern "C" +{ +#if defined(WIN32) && !defined(__GNUC__) +#include <io.h> +#else +#include <dirent.h> +#endif +} + Client::Client(): authUser(0, ""), updateAvailable(false) @@ -117,6 +123,95 @@ Client::Client(): versionCheckRequest = http_async_req_start(NULL, SERVER "/Download/Version.json", NULL, 0, 1); } +std::vector<std::string> Client::DirectorySearch(std::string directory, std::string search, std::string extension) +{ + std::vector<std::string> extensions; + extensions.push_back(extension); + return DirectorySearch(directory, search, extensions); +} + +std::vector<std::string> Client::DirectorySearch(std::string directory, std::string search, std::vector<std::string> extensions) +{ + std::vector<std::string> results; + + //Get full file listing + std::vector<std::string> directoryList; +#if defined(WIN32) && !defined(__GNUC__) + //Windows + struct _finddata_t currentFile; + intptr_t findFileHandle; + std::string fileMatch = directory + "*.*"; + findFileHandle = _findfirst(fileMatch.c_str(), ¤tFile); + if (findFileHandle == -1L) + { + printf("Unable to open directory\n"); + return std::vector<std::string>(); + } + do + { + std::string currentFileName = std::string(currentFile.name); + if(currentFileName.length()>4) + directoryList.push_back(directory+currentFileName); + } + while (_findnext(findFileHandle, ¤tFile) == 0); + _findclose(findFileHandle); +#else + //Linux or MinGW + struct dirent * directoryEntry; + DIR *directoryHandle = opendir(directory.c_str()); + if(!directoryHandle) + { + printf("Unable to open directory\n"); + return std::vector<std::string>(); + } + while(directoryEntry = readdir(directoryHandle)) + { + std::string currentFileName = std::string(directoryEntry->d_name); + if(currentFileName.length()>4) + directoryList.push_back(directory+currentFileName); + } + closedir(directoryHandle); +#endif + + //Filter results + return directoryList; + return results; +} + +std::vector<unsigned char> Client::ReadFile(std::string filename) +{ + try + { + std::ifstream fileStream; + fileStream.open(string(filename).c_str(), ios::binary); + if(fileStream.is_open()) + { + fileStream.seekg(0, ios::end); + size_t fileSize = fileStream.tellg(); + fileStream.seekg(0); + + unsigned char * tempData = new unsigned char[fileSize]; + fileStream.read((char *)tempData, fileSize); + fileStream.close(); + + std::vector<unsigned char> fileData; + fileData.insert(fileData.end(), tempData, tempData+fileSize); + delete[] tempData; + + return fileData; + } + else + { + return std::vector<unsigned char>(); + } + } + catch(std::exception & e) + { + std::cerr << "Readfile: " << e.what() << std::endl; + throw; + } +} + void Client::Tick() { //Check status on version check request |
