summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx/src
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-03-18 23:41:20 +0100
committerAki <please@ignore.pl>2024-03-18 23:41:20 +0100
commit2066e4911948d11cac5a234d2f7773dc5f06ba96 (patch)
tree27c7672aad884d1307736b3d15e704d7d786b314 /FoundationEx/src
parent3df6ccddcbd881c2474746f5f1758b095c866a67 (diff)
downloadstarshatter-2066e4911948d11cac5a234d2f7773dc5f06ba96.zip
starshatter-2066e4911948d11cac5a234d2f7773dc5f06ba96.tar.gz
starshatter-2066e4911948d11cac5a234d2f7773dc5f06ba96.tar.bz2
Added filesystem-only starshatter::data DataLoader replacement
Step by step. The intent is to find a good spot between current data representations and the standard library and put the intermediate stage there. After it matures a bit, we can move further away.
Diffstat (limited to 'FoundationEx/src')
-rw-r--r--FoundationEx/src/data.cpp42
-rw-r--r--FoundationEx/src/reader.cpp4
-rw-r--r--FoundationEx/src/reader/buffer.h9
-rw-r--r--FoundationEx/src/reader/buffer.inl.h16
-rw-r--r--FoundationEx/src/reader/file.cpp129
-rw-r--r--FoundationEx/src/reader/file.h49
6 files changed, 239 insertions, 10 deletions
diff --git a/FoundationEx/src/data.cpp b/FoundationEx/src/data.cpp
new file mode 100644
index 0000000..643e823
--- /dev/null
+++ b/FoundationEx/src/data.cpp
@@ -0,0 +1,42 @@
+/* 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/data.h>
+
+#include <fstream>
+#include <memory>
+
+#include <starshatter/foundation/reader.h>
+
+#include "reader/file.h"
+
+
+
+namespace starshatter
+{
+namespace foundation
+{
+namespace data
+{
+
+
+bool
+toggle_filesystem(bool)
+{
+ return true;
+}
+
+
+Reader
+open(const char* pathname)
+{
+ return {std::make_unique<FileReader>(std::fstream(pathname, std::ios::binary | std::ios::in))};
+}
+
+
+} // namespace data
+} // namespace foundation
+} // namespace starshatter
diff --git a/FoundationEx/src/reader.cpp b/FoundationEx/src/reader.cpp
index 7bce34d..0a3a2ba 100644
--- a/FoundationEx/src/reader.cpp
+++ b/FoundationEx/src/reader.cpp
@@ -9,6 +9,8 @@
#include <memory>
#include <utility>
+#include <Text.h>
+
#include "reader/buffer.h"
@@ -75,7 +77,7 @@ 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(); }
+Text Reader::more() { return actual->more(); }
} // namespace foundation
diff --git a/FoundationEx/src/reader/buffer.h b/FoundationEx/src/reader/buffer.h
index c36310a..532a8f9 100644
--- a/FoundationEx/src/reader/buffer.h
+++ b/FoundationEx/src/reader/buffer.h
@@ -7,6 +7,7 @@
#pragma once
#include <starshatter/foundation/reader.h>
+#include <Text.h>
namespace starshatter
@@ -35,12 +36,12 @@ struct BufferReader : public BaseReader<char>
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;
+ Text more() override;
private:
- Source buffer;
- Count size;
- Count position;
+ Source buffer = {};
+ Count size = {};
+ Count position = {};
};
diff --git a/FoundationEx/src/reader/buffer.inl.h b/FoundationEx/src/reader/buffer.inl.h
index 3b739e4..c452878 100644
--- a/FoundationEx/src/reader/buffer.inl.h
+++ b/FoundationEx/src/reader/buffer.inl.h
@@ -8,8 +8,9 @@
#include <cstring>
#include <memory>
#include <utility>
+#include <vector>
-#include <starshatter/foundation/reader.h>
+#include <Text.h>
namespace starshatter
@@ -124,16 +125,21 @@ Count
BufferReader<Source>::peek(char* dest, Count bytes) const
{
bytes = std::min(bytes, available());
- std::copy(data(), data() + bytes, dest);
+ std::copy(at(buffer) + position, at(buffer) + position + bytes, dest);
return bytes;
}
template <typename Source>
-const char*
-BufferReader<Source>::data() const
+Text
+BufferReader<Source>::more()
{
- return at(buffer) + position;
+ const auto size = available();
+ if (size < 1)
+ return Text();
+ std::vector<char> tmp(size);
+ read(tmp.data(), size);
+ return Text(tmp.data(), size);
}
diff --git a/FoundationEx/src/reader/file.cpp b/FoundationEx/src/reader/file.cpp
new file mode 100644
index 0000000..f9e9ae2
--- /dev/null
+++ b/FoundationEx/src/reader/file.cpp
@@ -0,0 +1,129 @@
+/* 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 "file.h"
+
+#include <algorithm>
+#include <fstream>
+#include <ios>
+#include <limits>
+#include <utility>
+#include <vector>
+
+#include <Text.h>
+
+
+namespace starshatter
+{
+namespace foundation
+{
+
+
+FileReader::FileReader(std::fstream src) :
+ file {std::move(src)}
+{
+ file.seekg(0);
+ file.ignore(std::numeric_limits<std::streamsize>::max());
+ size = file.gcount();
+ file.clear();
+ file.seekg(0);
+ position = 0;
+}
+
+
+bool
+FileReader::valid() const
+{
+ return static_cast<bool>(file) && file.is_open();
+}
+
+
+Count
+FileReader::available() const
+{
+ return size - position;
+}
+
+
+Count
+FileReader::seek(Count pos)
+{
+ position = pos;
+ if (position > size)
+ position = size;
+ file.seekg(position);
+ return position;
+}
+
+
+Count
+FileReader::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;
+ }
+ if (offset < 0)
+ offset = 0;
+ return seek(static_cast<Count>(offset));
+}
+
+
+Count
+FileReader::read(char* dest)
+{
+ return read(dest, available());
+}
+
+
+Count
+FileReader::read(char* dest, Count bytes)
+{
+ bytes = std::min(bytes, available());
+ file.read(dest, bytes);
+ position += bytes;
+ return bytes;
+}
+
+
+Count
+FileReader::peek(char* dest) const
+{
+ return peek(dest, available());
+}
+
+
+Count
+FileReader::peek(char* dest, Count bytes) const
+{
+ bytes = std::min(bytes, available());
+ const auto before = file.tellg();
+ file.read(dest, bytes);
+ file.seekg(before);
+ return bytes;
+}
+
+
+Text
+FileReader::more()
+{
+ const auto size = available();
+ if (size < 1)
+ return Text();
+ std::vector<char> tmp(size);
+ read(tmp.data(), size);
+ return Text(tmp.data(), size);
+}
+
+
+} // namespace foundation
+} // namespace starshatter
diff --git a/FoundationEx/src/reader/file.h b/FoundationEx/src/reader/file.h
new file mode 100644
index 0000000..22810b6
--- /dev/null
+++ b/FoundationEx/src/reader/file.h
@@ -0,0 +1,49 @@
+/* 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 <fstream>
+
+#include <starshatter/foundation/reader.h>
+#include <Text.h>
+
+
+namespace starshatter
+{
+namespace foundation
+{
+
+
+struct FileReader : public BaseReader<char>
+{
+ FileReader() = default;
+ FileReader(std::fstream src);
+ FileReader(const FileReader& other) = delete;
+ FileReader& operator=(const FileReader& other) = delete;
+ FileReader(FileReader&& other) = default;
+ FileReader& operator=(FileReader&& other) = default;
+ ~FileReader() 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;
+ Text more() override;
+
+private:
+ mutable std::fstream file = {};
+ Count size = {};
+ Count position = {};
+};
+
+
+} // namespace foundation
+} // namespace starshatter