aboutsummaryrefslogtreecommitdiff
path: root/src/rez/file.cpp
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-12-04 13:58:02 +0100
committerXavier Del Campo Romero <xavi92@disroot.org>2025-12-15 23:04:39 +0100
commit600ff28dd73f2cf17725382b68a4b1b2573f2e34 (patch)
treea105c686458d8998438652aeca6299cf9000edcd /src/rez/file.cpp
downloadglobalops-main.tar.gz
First commitHEADabcmain
Diffstat (limited to 'src/rez/file.cpp')
-rw-r--r--src/rez/file.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/rez/file.cpp b/src/rez/file.cpp
new file mode 100644
index 0000000..9a6a1f3
--- /dev/null
+++ b/src/rez/file.cpp
@@ -0,0 +1,100 @@
+#include <rez/file.h>
+#include <rez/entry.h>
+#include <rez/resource.h>
+#include <cstddef>
+#include <iostream>
+
+std::string rez::file::path() const
+{
+ return resource.name();
+}
+
+long rez::file::tell()
+{
+ return offset;
+}
+
+int rez::file::seek(long offset)
+{
+ if (offset < 0 || offset > resource.entry.size)
+ return -1;
+ else if (io.seek(resource.entry.pos + offset))
+ return -1;
+
+ this->offset = offset;
+ return 0;
+}
+
+int rez::file::eof() const
+{
+ return offset >= resource.entry.size;
+}
+
+int rez::file::read(void *b, size_t n)
+{
+ const rez::entry &entry = resource.entry;
+ unsigned long roffset = offset + entry.pos,
+ end = entry.pos + entry.size, remaining = end - roffset;
+
+ if (n > remaining)
+ {
+ std::cerr << path() << ": requested " << n << " bytes, only "
+ << remaining << " remaining" << std::endl;
+ return -1;
+ }
+
+ if (seek(offset) || io.read(b, n))
+ return -1;
+
+ offset += n;
+ return 0;
+}
+
+int rez::file::read(unsigned char &b)
+{
+ return read(&b, sizeof b);
+}
+
+int rez::file::read(std::string &s, size_t len)
+{
+ for (size_t i = 0; i < len; i++)
+ {
+ char b;
+
+ if (read(&b, sizeof b))
+ return -1;
+
+ s += b;
+ }
+
+ return 0;
+}
+
+int rez::file::read_le(uint16_t &w)
+{
+ unsigned char b[sizeof w];
+
+ if (read(b, sizeof b))
+ return -1;
+
+ w = b[0] | (b[1] << 8);
+ return 0;
+}
+
+int rez::file::read_le(uint32_t &w)
+{
+ unsigned char b[sizeof w];
+
+ if (read(b, sizeof b))
+ return -1;
+
+ w = b[0] | (b[1] << 8u) | (b[2] << 16u) | (b[3] << 24u);
+ return 0;
+}
+
+rez::file::file(const rez::resource &resource, rez::io &io) :
+ resource(resource),
+ offset(0),
+ io(io)
+{
+}