wudd/source/fs/CFile.hpp

70 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
2021-10-09 00:58:55 +02:00
#include <cstdio>
2021-10-09 00:58:55 +02:00
#include <string>
#include <cstring>
2021-10-09 00:58:55 +02:00
#include <fcntl.h>
#include <unistd.h>
#include <wut_types.h>
#include <utils/logger.h>
2021-10-09 00:58:55 +02:00
class CFile {
public:
enum eOpenTypes {
ReadOnly,
WriteOnly,
ReadWrite,
Append
};
CFile();
CFile(const std::string &filepath, eOpenTypes mode);
CFile(const uint8_t *memory, int32_t memsize);
virtual ~CFile();
int32_t open(const std::string &filepath, eOpenTypes mode);
int32_t open(const uint8_t *memory, int32_t memsize);
[[nodiscard]] BOOL isOpen() const {
if (iFd >= 0) {
2021-10-09 00:58:55 +02:00
return true;
}
2021-10-09 00:58:55 +02:00
if (mem_file) {
2021-10-09 00:58:55 +02:00
return true;
}
2021-10-09 00:58:55 +02:00
return false;
}
void close();
2021-10-09 00:58:55 +02:00
virtual int32_t read(uint8_t *ptr, size_t size);
2021-10-09 00:58:55 +02:00
virtual int32_t write(const uint8_t *ptr, size_t size);
2021-10-09 00:58:55 +02:00
int32_t fwrite(const char *format, ...);
virtual int32_t seek(int64_t offset, int32_t origin);
2021-10-09 00:58:55 +02:00
[[nodiscard]] virtual uint64_t tell() const {
2021-10-09 00:58:55 +02:00
return pos;
};
[[nodiscard]] uint64_t size() const {
2021-10-09 00:58:55 +02:00
return filesize;
};
void rewind() {
this->seek(0, SEEK_SET);
};
protected:
int32_t iFd;
const uint8_t *mem_file{};
uint64_t filesize{};
uint64_t pos{};
};