summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx/src
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-03-18 00:39:44 +0100
committerAki <please@ignore.pl>2024-03-18 00:40:11 +0100
commit2aa26759e12da36d68641ea3abda9b53292a2cbd (patch)
tree99174f52d82082b5d6307c819748ec53ac2b50d2 /FoundationEx/src
parent7117768feb2b2acb8d2498229cbaa4afbad377f1 (diff)
downloadstarshatter-2aa26759e12da36d68641ea3abda9b53292a2cbd.zip
starshatter-2aa26759e12da36d68641ea3abda9b53292a2cbd.tar.gz
starshatter-2aa26759e12da36d68641ea3abda9b53292a2cbd.tar.bz2
Bulk busy work towards Reader replacement to inject into DataLoader
Diffstat (limited to 'FoundationEx/src')
-rw-r--r--FoundationEx/src/Reader_.cpp (renamed from FoundationEx/src/Reader.cpp)0
-rw-r--r--FoundationEx/src/reader.cpp82
-rw-r--r--FoundationEx/src/reader/buffer.h51
-rw-r--r--FoundationEx/src/reader/buffer.inl.h141
4 files changed, 274 insertions, 0 deletions
diff --git a/FoundationEx/src/Reader.cpp b/FoundationEx/src/Reader_.cpp
index c3e33d5..c3e33d5 100644
--- a/FoundationEx/src/Reader.cpp
+++ b/FoundationEx/src/Reader_.cpp
diff --git a/FoundationEx/src/reader.cpp b/FoundationEx/src/reader.cpp
new file mode 100644
index 0000000..7bce34d
--- /dev/null
+++ b/FoundationEx/src/reader.cpp
@@ -0,0 +1,82 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+*/
+
+#include <starshatter/foundation/reader.h>
+
+#include <memory>
+#include <utility>
+
+#include "reader/buffer.h"
+
+
+namespace starshatter
+{
+namespace foundation
+{
+
+
+Reader::Reader()
+{
+}
+
+
+Reader::Reader(Reader&& other) :
+ actual {std::move(other.actual)}
+{
+}
+
+
+Reader&
+Reader::operator=(Reader&& other)
+{
+ actual = std::move(other.actual);
+ return *this;
+}
+
+
+Reader::Reader(Base src) :
+ actual {std::move(src)}
+{
+}
+
+
+Reader::Reader(Chars src) :
+ actual {std::make_unique<BufferReader<Chars>>(std::move(src))}
+{
+}
+
+
+Reader::Reader(Chars src, Count bytes) :
+ actual {std::make_unique<BufferReader<Chars>>(std::move(src), bytes)}
+{
+}
+
+
+Reader::Reader(const char* src) :
+ actual {std::make_unique<BufferReader<const char*>>(src)}
+{
+}
+
+
+Reader::Reader(const char* src, Count bytes) :
+ actual {std::make_unique<BufferReader<const char*>>(src, bytes)}
+{
+}
+
+
+bool Reader::valid() const { return static_cast<bool>(actual); }
+Count Reader::available() const { return actual->available(); }
+Count Reader::seek(Count pos) { return actual->seek(pos); }
+Count Reader::seek(Offset offset, Direction dir) { return actual->seek(offset, dir); }
+Count Reader::read(char* dest) { return actual->read(dest); }
+Count Reader::read(char* dest, Count bytes) { return actual->read(dest, bytes); }
+Count Reader::peek(char* dest) const { return actual->peek(dest); }
+Count Reader::peek(char* dest, Count bytes) const { return actual->peek(dest, bytes); }
+const char* Reader::data() const { return actual->data(); }
+
+
+} // namespace foundation
+} // namespace starshatter
diff --git a/FoundationEx/src/reader/buffer.h b/FoundationEx/src/reader/buffer.h
new file mode 100644
index 0000000..c36310a
--- /dev/null
+++ b/FoundationEx/src/reader/buffer.h
@@ -0,0 +1,51 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+*/
+
+#pragma once
+
+#include <starshatter/foundation/reader.h>
+
+
+namespace starshatter
+{
+namespace foundation
+{
+
+
+template <typename Source>
+struct BufferReader : public BaseReader<char>
+{
+ BufferReader();
+ BufferReader(Source src);
+ BufferReader(Source src, Count bytes);
+ BufferReader(const BufferReader& other) = default;
+ BufferReader& operator=(const BufferReader& other) = default;
+ BufferReader(BufferReader&& other) = default;
+ BufferReader& operator=(BufferReader&& other) = default;
+ ~BufferReader() override = default;
+
+ bool valid() const override;
+ Count available() const override;
+ Count seek(Count pos) override;
+ Count seek(Offset offset, Direction dir) override;
+ Count read(char* dest) override;
+ Count read(char* dest, Count bytes) override;
+ Count peek(char* dest) const override;
+ Count peek(char* dest, Count bytes) const override;
+ const char* data() const override;
+
+private:
+ Source buffer;
+ Count size;
+ Count position;
+};
+
+
+} // namespace foundation
+} // namespace starshatter
+
+
+#include "buffer.inl.h"
diff --git a/FoundationEx/src/reader/buffer.inl.h b/FoundationEx/src/reader/buffer.inl.h
new file mode 100644
index 0000000..3b739e4
--- /dev/null
+++ b/FoundationEx/src/reader/buffer.inl.h
@@ -0,0 +1,141 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+*/
+
+#include <algorithm>
+#include <cstring>
+#include <memory>
+#include <utility>
+
+#include <starshatter/foundation/reader.h>
+
+
+namespace starshatter
+{
+namespace foundation
+{
+
+
+// If extension is needed, use type_traits or update to C++20 and use concepts
+inline bool ok(const char* data) { return data != nullptr; }
+inline bool ok(const std::unique_ptr<const char[]>& data) { return static_cast<bool>(data); }
+inline const char* at(const char* data) { return data; }
+inline const char* at(const std::unique_ptr<const char[]>& data) { return data.get(); }
+
+
+template <typename Source>
+BufferReader<Source>::BufferReader()
+{
+}
+
+
+template <typename Source>
+BufferReader<Source>::BufferReader(Source src) :
+ buffer {std::move(src)},
+ size {std::strlen(at(buffer)) + 1}
+{
+}
+
+
+template <typename Source>
+BufferReader<Source>::BufferReader(Source src, Count bytes) :
+ buffer {std::move(src)},
+ size {bytes}
+{
+}
+
+
+template <typename Source>
+bool
+BufferReader<Source>::valid() const
+{
+ return ok(buffer);
+}
+
+
+template <typename Source>
+Count
+BufferReader<Source>::available() const
+{
+ return size - position;
+}
+
+
+template <typename Source>
+Count
+BufferReader<Source>::seek(Count pos)
+{
+ position = pos;
+ if (position > size)
+ position = size;
+ return position;
+}
+
+
+template <typename Source>
+Count
+BufferReader<Source>::seek(Offset offset, Direction dir)
+{
+ switch (dir) {
+ case Direction::Start:
+ break; // no-op
+ case Direction::End:
+ offset = size + offset;
+ break;
+ case Direction::Current:
+ offset = position + offset;
+ break;
+ }
+ position = static_cast<Count>(std::clamp<Offset>(offset, 0, size));
+ return position;
+}
+
+
+template <typename Source>
+Count
+BufferReader<Source>::read(char* dest)
+{
+ return read(dest, available());
+}
+
+
+template <typename Source>
+Count
+BufferReader<Source>::read(char* dest, Count bytes)
+{
+ bytes = peek(dest, bytes);
+ position += bytes;
+ return bytes;
+}
+
+
+template <typename Source>
+Count
+BufferReader<Source>::peek(char* dest) const
+{
+ return peek(dest, available());
+}
+
+
+template <typename Source>
+Count
+BufferReader<Source>::peek(char* dest, Count bytes) const
+{
+ bytes = std::min(bytes, available());
+ std::copy(data(), data() + bytes, dest);
+ return bytes;
+}
+
+
+template <typename Source>
+const char*
+BufferReader<Source>::data() const
+{
+ return at(buffer) + position;
+}
+
+
+} // namespace foundation
+} // namespace starshatter