feat: add streamed_io.
This commit is contained in:
17
src/data_format/io/streamed_io.cpp
Normal file
17
src/data_format/io/streamed_io.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "data_format/io/streamed_io.h"
|
||||
|
||||
namespace di {
|
||||
|
||||
void StreamedIO::read(const fs::path& path) {
|
||||
m_file_stream.open(path, std::ios::binary);
|
||||
if (!m_file_stream) {
|
||||
throw std::runtime_error("Failed to open file!");
|
||||
}
|
||||
}
|
||||
|
||||
void StreamedIO::write(const fs::path& path) const {
|
||||
std::fstream ofs(path, std::ios::binary);
|
||||
ofs << m_file_stream.rdbuf();
|
||||
}
|
||||
|
||||
} // namespace di
|
||||
43
src/data_format/io/streamed_io.h
Normal file
43
src/data_format/io/streamed_io.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "data_format/io/io_base.h"
|
||||
|
||||
namespace di {
|
||||
|
||||
class StreamedIO : public IOBase {
|
||||
public:
|
||||
void read(const fs::path& path) override;
|
||||
void write(const fs::path& path) const override;
|
||||
|
||||
constexpr auto next() { return m_file_stream.peek(); }
|
||||
|
||||
// Avoid confusion with the function that opens a file, so use eat
|
||||
// instead of read. Maybe come up with another name for write as well.
|
||||
//
|
||||
// TODO: write<T>() method.
|
||||
|
||||
template <typename T>
|
||||
constexpr T eat() {
|
||||
T value;
|
||||
m_file_stream.read((char*)&value, sizeof(T));
|
||||
return value;
|
||||
}
|
||||
|
||||
template <std::unsigned_integral T>
|
||||
constexpr T eat_varint() {
|
||||
T res = 0;
|
||||
int shift = 0;
|
||||
while (true) {
|
||||
auto byte = m_file_stream.get();
|
||||
res |= static_cast<T>(byte & 0x7F) << shift;
|
||||
if ((byte & 0x80) == 0) break;
|
||||
shift += 7;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
std::ifstream m_file_stream;
|
||||
};
|
||||
|
||||
} // namespace di
|
||||
Reference in New Issue
Block a user