#include "FileDialog.h" #include #include "../utils/dirent.h" #include "../utils/StringHelper.h" #include "../utils/ConverType.hpp" FileDialog::FileDialog() { } FileDialog::~FileDialog() { } void FileDialog::GetLogicalDrive(::stream::ResponseAny** rsp) { //std::vector vecDrives; char szbuf[MAX_PATH] = { 0 }; GetLogicalDriveStrings(MAX_PATH, szbuf); int nCount = 0; char* pDrive = szbuf; string driveStr; for (size_t nlen = strlen(szbuf); nlen == 3; nCount++) { std::string strDrive(pDrive); driveStr += strDrive + " "; pDrive += 4; nlen = strlen(pDrive); } stream::ComResponce result; result.set_data(driveStr); (*rsp)->mutable_data()->PackFrom(result); } inline bool ReplaceString(std::wstring& str, const std::wstring& oldStr, const std::wstring& newStr) { bool Finded = false; size_t pos = 0; while ((pos = str.find(oldStr, pos)) != std::wstring::npos) { Finded = true; str.replace(pos, oldStr.length(), newStr); pos += newStr.length(); } return Finded; } inline std::vector splitStringVector(const std::wstring& text, char delimiter) { std::vector arr; std::wstring::size_type start = 0; std::wstring::size_type end = text.find(delimiter, start); while (end != std::string::npos) { std::wstring token = text.substr(start, end - start); if (token != L"") arr.push_back(token); start = end + 1; end = text.find(delimiter, start); } arr.push_back(text.substr(start)); return arr; } void FileDialog::Request(const ReadData& rd, ::stream::ResponseAny** resp) { CALLFUNC type = (CALLFUNC)ConverType::TryToI(rd.nameKey); if (type == CALLFUNC::GetLogicalDrive) { GetLogicalDrive(resp); printf("GetLogicalDrive responded...\n"); } else if(type == CALLFUNC::ScanDir){ std::wstring vPath = StringHelper::Str2Wstr(rd.strValue); ScanDir(vPath); stream::ComResponce result; string dirc; for (auto start = m_CurrentPath_Decomposition.begin(); start != m_CurrentPath_Decomposition.end(); ++start) { dirc += StringHelper::Wstr2Str(*start) + " "; } result.set_directory(dirc); //当前目录 空格隔开 for (auto start = m_FileList.begin(); start != m_FileList.end(); ++start) { auto pFileInfo = result.add_fileinfo(); //目录下的文件夹 空格隔开 pFileInfo->set_type("" + start->type); pFileInfo->set_filename(StringHelper::Wstr2Str(start->fileName)); pFileInfo->set_filepath(StringHelper::Wstr2Str(start->filePath)); pFileInfo->set_ext(StringHelper::Wstr2Str(start->ext)); } (*resp)->mutable_data()->PackFrom(result); printf("ScanDir responded...\n"); } } bool FileDialog::ScanDir(std::wstring vPath) { struct _wdirent** files = 0; int i = 0; int n = 0; m_FileList.clear(); // get currentPath WDIR* currentDir = _wopendir(vPath.c_str()); if (currentDir == 0) // path not existing { vPath = L"."; // current app path currentDir = _wopendir(vPath.c_str()); } if (currentDir != 0) { std::wstring ws(currentDir->patt); m_CurrentPath = std::wstring(ws.begin(), ws.end()); ReplaceString(m_CurrentPath, L"\\*", L""); _wclosedir(currentDir); m_CurrentPath_Decomposition = splitStringVector(m_CurrentPath, '\\'); } else { return false; } n = wscandir(vPath.c_str(), &files, NULL, [](const void* a, const void* b) { return strcoll(((dirent*)a)->d_name, ((dirent*)b)->d_name); }); if (n >= 0) { for (i = 0; i < n; i++) { struct _wdirent* ent = files[i]; FileInfoStruct infos; infos.fileName = ent->d_name; if (ent->d_type == DT_REG) { infos.type = 'f'; } if (ent->d_type == DT_DIR) { infos.type = 'd'; } if (ent->d_type == DT_LNK) { infos.type = 'l'; } if (infos.type == 'f') { size_t lpt = infos.fileName.find_last_of(L"."); if (lpt != std::wstring::npos) infos.ext = infos.fileName.substr(lpt); } if (infos.fileName != L".") { switch (ent->d_type) { case DT_REG: infos.type = 'f'; break; case DT_DIR: infos.type = 'd'; break; case DT_LNK: infos.type = 'l'; break; } m_FileList.push_back(infos); } } for (i = 0; i < n; i++) { free(files[i]); } free(files); } std::sort(m_FileList.begin(), m_FileList.end(), [](FileInfoStruct& a, FileInfoStruct& b) { bool res; if (a.type != b.type) res = (a.type < b.type); else res = (a.fileName < b.fileName); return res; }); return true; }