old-nall/vfs/vfs.hpp

76 lines
1.7 KiB
C++
Raw Permalink Normal View History

2016-07-03 11:35:22 +00:00
#pragma once
#include <nall/range.hpp>
#include <nall/shared-pointer.hpp>
namespace nall { namespace vfs {
struct file {
enum class mode : uint { read, write, modify, create };
enum class index : uint { absolute, relative };
virtual ~file() = default;
2016-08-09 18:27:40 +00:00
virtual auto size() const -> uintmax = 0;
virtual auto offset() const -> uintmax = 0;
2016-07-03 11:35:22 +00:00
2016-08-09 18:27:40 +00:00
virtual auto seek(intmax offset, index = index::absolute) -> void = 0;
2016-07-03 11:35:22 +00:00
virtual auto read() -> uint8_t = 0;
virtual auto write(uint8_t data) -> void = 0;
virtual auto flush() -> void {}
auto end() const -> bool {
return offset() >= size();
}
2016-08-09 18:27:40 +00:00
auto read(void* vdata, uintmax bytes) -> void {
2016-07-03 11:35:22 +00:00
auto data = (uint8_t*)vdata;
while(bytes--) *data++ = read();
}
2016-08-09 18:27:40 +00:00
auto readl(uint bytes) -> uintmax {
uintmax data = 0;
for(auto n : range(bytes)) data |= (uintmax)read() << n * 8;
2016-07-03 11:35:22 +00:00
return data;
}
2016-08-09 18:27:40 +00:00
auto readm(uint bytes) -> uintmax {
uintmax data = 0;
2016-07-03 11:35:22 +00:00
for(auto n : range(bytes)) data = data << 8 | read();
return data;
}
auto reads() -> string {
string s;
s.resize(size());
read(s.get<uint8_t>(), s.size());
return s;
}
2016-08-09 18:27:40 +00:00
auto write(const void* vdata, uintmax bytes) -> void {
2016-07-03 11:35:22 +00:00
auto data = (const uint8_t*)vdata;
while(bytes--) write(*data++);
}
2016-08-09 18:27:40 +00:00
auto writel(uintmax data, uint bytes) -> void {
2016-07-03 11:35:22 +00:00
for(auto n : range(bytes)) write(data), data >>= 8;
}
2016-08-09 18:27:40 +00:00
auto writem(uintmax data, uint bytes) -> void {
2016-07-03 11:35:22 +00:00
for(auto n : rrange(bytes)) write(data >> n * 8);
}
auto writes(const string& s) -> void {
write(s.data<uint8_t>(), s.size());
}
};
}}
namespace nall { namespace vfs { namespace shared {
using file = shared_pointer<vfs::file>;
}}}
#include <nall/vfs/fs/file.hpp>
#include <nall/vfs/memory/file.hpp>