aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/stream.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2023-05-02 10:01:47 +0200
committerGitHub <[email protected]>2023-05-02 10:01:47 +0200
commit075d17f8ada47e990fe94606c3d21df409223465 (patch)
treee50549b766a2f3c354798a54ff73404217b4c9af /src/zencore/stream.cpp
parentfix: bundle shouldn't append content zip to zen (diff)
downloadzen-075d17f8ada47e990fe94606c3d21df409223465.tar.xz
zen-075d17f8ada47e990fe94606c3d21df409223465.zip
moved source directories into `/src` (#264)
* moved source directories into `/src` * updated bundle.lua for new `src` path * moved some docs, icon * removed old test trees
Diffstat (limited to 'src/zencore/stream.cpp')
-rw-r--r--src/zencore/stream.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/zencore/stream.cpp b/src/zencore/stream.cpp
new file mode 100644
index 000000000..3402e51be
--- /dev/null
+++ b/src/zencore/stream.cpp
@@ -0,0 +1,79 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <stdarg.h>
+#include <zencore/memory.h>
+#include <zencore/stream.h>
+#include <zencore/testing.h>
+
+#include <algorithm>
+#include <stdexcept>
+
+namespace zen {
+
+void
+BinaryWriter::Write(std::initializer_list<const MemoryView> Buffers)
+{
+ size_t TotalByteCount = 0;
+ for (const MemoryView& View : Buffers)
+ {
+ TotalByteCount += View.GetSize();
+ }
+ const size_t NeedEnd = m_Offset + TotalByteCount;
+ if (NeedEnd > m_Buffer.size())
+ {
+ m_Buffer.resize(NeedEnd);
+ }
+ for (const MemoryView& View : Buffers)
+ {
+ memcpy(m_Buffer.data() + m_Offset, View.GetData(), View.GetSize());
+ m_Offset += View.GetSize();
+ }
+}
+
+void
+BinaryWriter::Write(const void* data, size_t ByteCount, uint64_t Offset)
+{
+ const size_t NeedEnd = Offset + ByteCount;
+
+ if (NeedEnd > m_Buffer.size())
+ {
+ m_Buffer.resize(NeedEnd);
+ }
+
+ memcpy(m_Buffer.data() + Offset, data, ByteCount);
+}
+
+void
+BinaryWriter::Reset()
+{
+ m_Buffer.clear();
+ m_Offset = 0;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+// Testing related code follows...
+//
+
+#if ZEN_WITH_TESTS
+
+TEST_CASE("binary.writer.span")
+{
+ BinaryWriter Writer;
+ const MemoryView View1("apa", 3);
+ const MemoryView View2(" ", 1);
+ const MemoryView View3("banan", 5);
+ Writer.Write({View1, View2, View3});
+ MemoryView Result = Writer.GetView();
+ CHECK(Result.GetSize() == 9);
+ CHECK(memcmp(Result.GetData(), "apa banan", 9) == 0);
+}
+
+void
+stream_forcelink()
+{
+}
+
+#endif
+
+} // namespace zen