Files
XCEngine/engine/src/Resources/FileArchive.cpp

98 lines
2.2 KiB
C++

#include "Resources/FileArchive.h"
#include <cstdio>
namespace XCEngine {
namespace Resources {
FileArchive::FileArchive() = default;
FileArchive::~FileArchive() {
Close();
}
bool FileArchive::Open(const Containers::String& path) {
m_archivePath = path;
m_isValid = true;
return true;
}
void FileArchive::Close() {
m_archivePath = "";
m_isValid = false;
}
bool FileArchive::Read(const Containers::String& fileName, void* buffer, size_t size, size_t offset) const {
if (!m_isValid) {
return false;
}
Containers::String fullPath = m_archivePath;
if (!fullPath.EndsWith("/") && !fullPath.EndsWith("\\")) {
fullPath += "/";
}
fullPath += fileName;
FILE* file = std::fopen(fullPath.CStr(), "rb");
if (!file) {
return false;
}
if (offset > 0) {
std::fseek(file, static_cast<long>(offset), SEEK_SET);
}
size_t bytesRead = std::fread(buffer, 1, size, file);
std::fclose(file);
return bytesRead == size;
}
size_t FileArchive::GetSize(const Containers::String& fileName) const {
if (!m_isValid) {
return 0;
}
Containers::String fullPath = m_archivePath;
if (!fullPath.EndsWith("/") && !fullPath.EndsWith("\\")) {
fullPath += "/";
}
fullPath += fileName;
FILE* file = std::fopen(fullPath.CStr(), "rb");
if (!file) {
return 0;
}
std::fseek(file, 0, SEEK_END);
size_t size = static_cast<size_t>(std::ftell(file));
std::fclose(file);
return size;
}
bool FileArchive::Exists(const Containers::String& fileName) const {
if (!m_isValid) {
return false;
}
Containers::String fullPath = m_archivePath;
if (!fullPath.EndsWith("/") && !fullPath.EndsWith("\\")) {
fullPath += "/";
}
fullPath += fileName;
FILE* file = std::fopen(fullPath.CStr(), "rb");
if (file) {
std::fclose(file);
return true;
}
return false;
}
void FileArchive::Enumerate(const Containers::String& pattern, Containers::Array<Containers::String>& outFiles) const {
outFiles.Clear();
// TODO: Implement pattern-based enumeration
}
} // namespace Resources
} // namespace XCEngine