add file input/output
This commit is contained in:
@@ -10,23 +10,30 @@ struct FileTag {};
|
||||
template <>
|
||||
class IOInterface<FileTag> {
|
||||
private:
|
||||
std::ofstream m_FileOutput;
|
||||
std::ifstream m_FileInput;
|
||||
std::unique_ptr<std::ifstream> m_FileInput;
|
||||
std::unique_ptr<std::ofstream> m_FileOutput;
|
||||
|
||||
public:
|
||||
IOInterface(const std::string& filePath) : m_FileOutput(filePath), m_FileInput(filePath) {}
|
||||
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);
|
||||
m_FileInput.read(reinterpret_cast<char*>(buffer.data()), 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) {
|
||||
m_FileOutput.write(reinterpret_cast<const char*>(a_Data.data()), a_Data.GetSize());
|
||||
m_FileOutput.flush();
|
||||
assert(m_FileOutput != nullptr);
|
||||
m_FileOutput->write(reinterpret_cast<const char*>(a_Data.data()), a_Data.GetSize());
|
||||
m_FileOutput->flush();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ using FileStream = sp::IOStream<sp::FileTag, sp::PacketDispatcher, sp::PacketFac
|
||||
int main() {
|
||||
auto handler = std::make_shared<CustomPacketHandler>();
|
||||
|
||||
FileStream stream(sp::FileIO{"test.txt"});
|
||||
FileStream stream(sp::FileIO{"test.txt", "text.txt"});
|
||||
stream.GetDispatcher().RegisterHandler(PacketId::Disconnect, handler);
|
||||
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user