Commit b763e438 authored by Jan Lepper's avatar Jan Lepper

add sfs::File

parent 639580e7
......@@ -19,12 +19,33 @@
#ifndef IL2GE_SFS_H
#define IL2GE_SFS_H
#include <file.h>
#include <vector>
#include <cstdint>
#include <string>
namespace sfs
{
class File : public util::File
{
int m_fd = -1;
long m_pos = 0;
long m_size = 0;
std::string m_path;
public:
File(std::string path);
~File() override;
int read(char *out, int bytes) override;
void skip(int bytes) override;
void rewind() override;
bool eof() override;
void readAll(std::vector<char>&) override;
int getSize() override;
};
void init();
bool readFile(const std::string &filename, std::vector<char> &out);
__int64 getHash(const char *filename);
......
......@@ -164,4 +164,85 @@ void *get_openf_wrapper()
}
File::File(std::string path) : m_path(path)
{
m_fd = open(path.c_str());
if (m_fd == -1)
throw std::runtime_error("Failed to open " + path);
m_size = g_lseek_func(m_fd, 0, SEEK_END);
g_lseek_func(m_fd, 0, SEEK_SET);
}
File::~File()
{
g_close_func(m_fd);
}
int File::read(char *out, int bytes)
{
assert(m_pos < m_size);
auto ret = g_lseek_func(m_fd, m_pos, SEEK_SET);
assert(ret == m_pos);
ret = g_read_func(m_fd, out, bytes);
assert(ret != -1);
assert(ret >= 0);
m_pos += ret;
assert(m_pos <= m_size);
return ret;
}
void File::skip(int bytes)
{
m_pos += bytes;
assert(m_pos >= 0);
auto ret = g_lseek_func(m_fd, m_pos, SEEK_SET);
assert(ret == m_pos);
}
void File::rewind()
{
m_pos = 0;
g_lseek_func(m_fd, m_pos, SEEK_SET);
}
bool File::eof()
{
return m_pos >= m_size;
}
void File::readAll(std::vector<char> &out)
{
assert(m_pos == 0);
assert(!eof());
bool success = false;
out.resize(m_size);
auto ret = read(out.data(), out.size());
assert(ret == m_size);
rewind();
}
int File::getSize()
{
return m_size;
}
} // namespace sfs
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment