43 lines
1.1 KiB
C++
43 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <fstream>
|
|
#include <sp/io/IOInterface.h>
|
|
|
|
namespace sp {
|
|
|
|
struct FileTag {};
|
|
|
|
template <>
|
|
class IOInterface<FileTag> {
|
|
private:
|
|
std::unique_ptr<std::ifstream> m_FileInput;
|
|
std::unique_ptr<std::ofstream> m_FileOutput;
|
|
|
|
public:
|
|
IOInterface(const std::string& fileInput, const std::string& fileOutput) {
|
|
if (!fileInput.empty())
|
|
m_FileInput = std::make_unique<std::ifstream>(fileInput);
|
|
if (!fileOutput.empty())
|
|
m_FileOutput = std::make_unique<std::ofstream>(fileOutput);
|
|
}
|
|
IOInterface(IOInterface&& other) : m_FileOutput(std::move(other.m_FileOutput)), m_FileInput(std::move(other.m_FileInput)) {}
|
|
|
|
DataBuffer Read(std::size_t a_Amount) {
|
|
DataBuffer buffer;
|
|
buffer.Resize(a_Amount);
|
|
assert(m_FileInput != nullptr);
|
|
m_FileInput->read(reinterpret_cast<char*>(buffer.data()), a_Amount);
|
|
return buffer;
|
|
}
|
|
|
|
void Write(const sp::DataBuffer& a_Data) {
|
|
assert(m_FileOutput != nullptr);
|
|
m_FileOutput->write(reinterpret_cast<const char*>(a_Data.data()), a_Data.GetSize());
|
|
m_FileOutput->flush();
|
|
}
|
|
};
|
|
|
|
using FileIO = IOInterface<FileTag>;
|
|
|
|
} // namespace sp
|