forked from Tr0n/client
Removed Panama & HybridCrypt, added experimental Folder and Zip archive providers
This commit is contained in:
111
src/EterPack/Folder.cpp
Normal file
111
src/EterPack/Folder.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
#include "Folder.h"
|
||||
#include "EterPackManager.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <Windows.h>
|
||||
#include "../eterBase/Stl.h"
|
||||
|
||||
void Folder::ListFiles(const std::string& directory, const std::string& relativePath = "") {
|
||||
WIN32_FIND_DATA findData;
|
||||
HANDLE hFind;
|
||||
|
||||
std::string searchPath = directory + "\\*";
|
||||
|
||||
// Start searching for files and directories
|
||||
hFind = FindFirstFile(searchPath.c_str(), &findData);
|
||||
if (hFind == INVALID_HANDLE_VALUE) {
|
||||
std::cerr << L"Failed to open directory: " << directory << L"\n";
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
const std::string fileOrDirName = findData.cFileName;
|
||||
|
||||
// Skip the "." and ".." directories
|
||||
if (fileOrDirName == "." || fileOrDirName == "..") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Construct the full and relative paths
|
||||
std::string fullPath = directory + "\\" + fileOrDirName;
|
||||
std::string currentRelativePath = relativePath + fileOrDirName;
|
||||
|
||||
// Check if the current entry is a directory
|
||||
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
// Recurse into the subdirectory
|
||||
ListFiles(fullPath, currentRelativePath + "\\");
|
||||
}
|
||||
else {
|
||||
if (currentRelativePath.rfind("ymir work\\", 0) == 0) {
|
||||
currentRelativePath = "d:\\" + currentRelativePath;
|
||||
}
|
||||
|
||||
currentRelativePath = CEterPackManager::Instance().ConvertFileName(currentRelativePath);
|
||||
|
||||
// Print the file's relative path
|
||||
this->fileList.insert(currentRelativePath);
|
||||
}
|
||||
} while (FindNextFile(hFind, &findData) != 0);
|
||||
|
||||
FindClose(hFind);
|
||||
}
|
||||
|
||||
|
||||
Folder::Folder(const std::string& folderPath)
|
||||
{
|
||||
this->folderPath = folderPath;
|
||||
ListFiles(folderPath);
|
||||
}
|
||||
|
||||
std::vector<std::string> Folder::listFiles()
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
std::copy(fileList.begin(), fileList.end(), std::back_inserter(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Folder::fileExists(const std::string& fileName)
|
||||
{
|
||||
auto it = fileList.find(fileName);
|
||||
|
||||
if (it == fileList.end())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Folder::getFile(const std::string& fileName, std::shared_ptr<std::vector<char>>& fileData)
|
||||
{
|
||||
std::string realFileName = fileName;
|
||||
std::string ymirPrefix = "d:/";
|
||||
|
||||
if (fileName.find(ymirPrefix) == 0)
|
||||
realFileName = realFileName.substr(ymirPrefix.length());
|
||||
|
||||
realFileName = this->folderPath + "/" + realFileName;
|
||||
|
||||
// open the file:
|
||||
std::ifstream file(realFileName, std::ios::binary);
|
||||
|
||||
// Stop eating new lines in binary mode!!!
|
||||
file.unsetf(std::ios::skipws);
|
||||
|
||||
// get its size:
|
||||
std::streampos fileSize;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
fileSize = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
// reserve capacity
|
||||
fileData->reserve(fileSize);
|
||||
|
||||
// read the data:
|
||||
fileData->insert(fileData->begin(),
|
||||
std::istream_iterator<BYTE>(file),
|
||||
std::istream_iterator<BYTE>());
|
||||
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user