summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx/src/reader
diff options
context:
space:
mode:
Diffstat (limited to 'FoundationEx/src/reader')
-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
4 files changed, 194 insertions, 9 deletions
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