2021-09-08 15:18:24 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-01-16 01:51:31 +01:00
|
|
|
#include <hex/helpers/paths.hpp>
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
#if defined(OS_MACOS)
|
|
|
|
#define off64_t off_t
|
|
|
|
#define fopen64 fopen
|
|
|
|
#define fseeko64 fseek
|
|
|
|
#define ftello64 ftell
|
2021-10-09 23:07:58 +02:00
|
|
|
#define ftruncate64 ftruncate
|
2021-09-08 15:18:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
class File {
|
|
|
|
public:
|
|
|
|
enum class Mode {
|
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
Create
|
|
|
|
};
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
explicit File(const fs::path &path, Mode mode) noexcept;
|
|
|
|
File() noexcept;
|
2021-09-12 20:26:39 +02:00
|
|
|
File(const File&) = delete;
|
|
|
|
File(File &&other) noexcept;
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
~File();
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
File& operator=(File &&other) noexcept;
|
|
|
|
|
|
|
|
|
2021-09-22 17:56:06 +02:00
|
|
|
[[nodiscard]] bool isValid() const { return this->m_file != nullptr; }
|
2021-09-08 15:18:24 +02:00
|
|
|
|
|
|
|
void seek(u64 offset);
|
2021-09-22 17:56:06 +02:00
|
|
|
void close();
|
2021-09-08 15:18:24 +02:00
|
|
|
|
2021-09-11 14:41:18 +02:00
|
|
|
size_t readBuffer(u8 *buffer, size_t size);
|
2021-09-08 15:18:24 +02:00
|
|
|
std::vector<u8> readBytes(size_t numBytes = 0);
|
|
|
|
std::string readString(size_t numBytes = 0);
|
|
|
|
|
2021-09-11 14:41:18 +02:00
|
|
|
void write(const u8 *buffer, size_t size);
|
2021-09-08 15:18:24 +02:00
|
|
|
void write(const std::vector<u8> &bytes);
|
|
|
|
void write(const std::string &string);
|
|
|
|
|
2021-09-22 17:56:06 +02:00
|
|
|
[[nodiscard]] size_t getSize() const;
|
2021-09-08 15:18:24 +02:00
|
|
|
void setSize(u64 size);
|
|
|
|
|
2021-09-22 17:56:06 +02:00
|
|
|
void flush();
|
|
|
|
void remove();
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
auto getHandle() { return this->m_file; }
|
2022-01-16 01:51:31 +01:00
|
|
|
const fs::path& getPath() { return this->m_path; }
|
2021-09-08 15:18:24 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
FILE *m_file;
|
2022-01-16 01:51:31 +01:00
|
|
|
fs::path m_path;
|
2021-09-08 15:18:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|