mirror of
https://github.com/thunderbrewhq/thunderbrew
synced 2025-12-12 03:02:30 +00:00
chore: initial commit
This commit is contained in:
commit
70b00c5c38
965 changed files with 264882 additions and 0 deletions
125
src/util/SFile.cpp
Normal file
125
src/util/SFile.cpp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
#include "util/SFile.hpp"
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <storm/Memory.hpp>
|
||||
|
||||
// TODO Proper implementation
|
||||
int32_t SFile::Close(SFile* file) {
|
||||
delete file->m_filename;
|
||||
|
||||
file->m_stream->close();
|
||||
delete file->m_stream;
|
||||
|
||||
delete file;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO Proper implementation
|
||||
size_t SFile::GetFileSize(SFile* file, size_t* filesizeHigh) {
|
||||
return file->m_size;
|
||||
}
|
||||
|
||||
int32_t SFile::IsStreamingMode() {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
// TODO Proper implementation
|
||||
int32_t SFile::Load(SArchive* archive, const char* filename, void** buffer, size_t* bytes, size_t extraBytes, uint32_t flags, SOVERLAPPED* overlapped) {
|
||||
size_t pathlen = strlen(filename);
|
||||
char path[pathlen];
|
||||
strcpy(path, filename);
|
||||
|
||||
for (int32_t i = 0; i < pathlen; ++i) {
|
||||
if (path[i] == '\\') {
|
||||
path[i] = '/';
|
||||
}
|
||||
}
|
||||
|
||||
std::ifstream file (path, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
size_t size;
|
||||
char* data;
|
||||
|
||||
if (file.is_open()) {
|
||||
size = static_cast<size_t>(file.tellg());
|
||||
|
||||
if (bytes) {
|
||||
*bytes = size;
|
||||
}
|
||||
|
||||
data = new char[size + extraBytes];
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
file.read(data, size);
|
||||
file.close();
|
||||
|
||||
if (extraBytes) {
|
||||
memset(data + size, 0, extraBytes);
|
||||
}
|
||||
|
||||
*buffer = data;
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t SFile::Open(const char* filename, SFile** file) {
|
||||
return SFile::OpenEx(nullptr, filename, 0, file);
|
||||
}
|
||||
|
||||
// TODO Proper implementation
|
||||
int32_t SFile::OpenEx(SArchive* archive, const char* filename, uint32_t flags, SFile** file) {
|
||||
size_t pathlen = strlen(filename);
|
||||
char path[pathlen];
|
||||
strcpy(path, filename);
|
||||
|
||||
for (int32_t i = 0; i < pathlen; ++i) {
|
||||
if (path[i] == '\\') {
|
||||
path[i] = '/';
|
||||
}
|
||||
}
|
||||
|
||||
SFile* fileptr = new SFile;
|
||||
|
||||
fileptr->m_filename = strdup(filename);
|
||||
|
||||
std::ifstream* stream = new std::ifstream(path, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
|
||||
if (!stream->is_open()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
stream->seekg(0, std::ios::beg);
|
||||
|
||||
stream->ignore(std::numeric_limits<std::streamsize>::max());
|
||||
std::streamsize size = stream->gcount();
|
||||
stream->clear();
|
||||
|
||||
stream->seekg(0, std::ios::beg);
|
||||
|
||||
fileptr->m_stream = stream;
|
||||
fileptr->m_size = size;
|
||||
|
||||
*file = fileptr;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO Proper implementation
|
||||
int32_t SFile::Read(SFile* file, void* buffer, size_t bytestoread, size_t* bytesread, SOVERLAPPED* overlapped, TASYNCPARAMBLOCK* asyncparam) {
|
||||
file->m_stream->read((char*)buffer, bytestoread);
|
||||
|
||||
if (bytesread) {
|
||||
*bytesread = file->m_stream->gcount();
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32_t SFile::Unload(void* ptr) {
|
||||
SMemFree(ptr, __FILE__, __LINE__, 0);
|
||||
return 1;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue