vfs.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include <nall/range.hpp>
  3. #include <nall/shared-pointer.hpp>
  4. namespace nall::vfs {
  5. struct file {
  6. enum class mode : uint { read, write, modify, create };
  7. enum class index : uint { absolute, relative };
  8. virtual ~file() = default;
  9. virtual auto size() const -> uintmax = 0;
  10. virtual auto offset() const -> uintmax = 0;
  11. virtual auto seek(intmax offset, index = index::absolute) -> void = 0;
  12. virtual auto read() -> uint8_t = 0;
  13. virtual auto write(uint8_t data) -> void = 0;
  14. virtual auto flush() -> void {}
  15. auto end() const -> bool {
  16. return offset() >= size();
  17. }
  18. auto read(void* vdata, uintmax bytes) -> void {
  19. auto data = (uint8_t*)vdata;
  20. while(bytes--) *data++ = read();
  21. }
  22. auto readl(uint bytes) -> uintmax {
  23. uintmax data = 0;
  24. for(auto n : range(bytes)) data |= (uintmax)read() << n * 8;
  25. return data;
  26. }
  27. auto readm(uint bytes) -> uintmax {
  28. uintmax data = 0;
  29. for(auto n : range(bytes)) data = data << 8 | read();
  30. return data;
  31. }
  32. auto reads() -> string {
  33. string s;
  34. s.resize(size());
  35. read(s.get<uint8_t>(), s.size());
  36. return s;
  37. }
  38. auto write(const void* vdata, uintmax bytes) -> void {
  39. auto data = (const uint8_t*)vdata;
  40. while(bytes--) write(*data++);
  41. }
  42. auto writel(uintmax data, uint bytes) -> void {
  43. for(auto n : range(bytes)) write(data), data >>= 8;
  44. }
  45. auto writem(uintmax data, uint bytes) -> void {
  46. for(auto n : reverse(range(bytes))) write(data >> n * 8);
  47. }
  48. auto writes(const string& s) -> void {
  49. write(s.data<uint8_t>(), s.size());
  50. }
  51. };
  52. }
  53. #include <nall/vfs/cdrom.hpp>
  54. #include <nall/vfs/disk.hpp>
  55. #include <nall/vfs/memory.hpp>