aboutsummaryrefslogtreecommitdiff
path: root/src/util/io_impl.hpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-12 13:44:58 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:57:01 +0200
commit927e065f9829045247be7c0b3296408b6f577c1f (patch)
tree7da3d9cd5aa4070414a8708a582f6c3ab3e1e708 /src/util/io_impl.hpp
parenteb66598c326862fd9dfc1899be4eac93f81a8023 (diff)
feat: add reading RLE files
Diffstat (limited to 'src/util/io_impl.hpp')
-rw-r--r--src/util/io_impl.hpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/util/io_impl.hpp b/src/util/io_impl.hpp
new file mode 100644
index 0000000..14f4ded
--- /dev/null
+++ b/src/util/io_impl.hpp
@@ -0,0 +1,55 @@
+#pragma once
+
+#include "io.hpp"
+
+#include "errors/io.hpp"
+
+#include <fstream>
+
+template <typename ContainerType>
+requires Container<ContainerType> && HasPushBack<ContainerType> &&
+ std::same_as<typename ContainerType::value_type, std::string>
+auto read_file_lines(const std::filesystem::path &path) -> ContainerType
+{
+ if (!std::filesystem::exists(path))
+ {
+ throw NoSuchFileOrDirectoryError(path);
+ }
+
+ const auto file_status = std::filesystem::status(path);
+
+ const auto file_type = file_status.type();
+
+ if (file_type != std::filesystem::file_type::regular)
+ {
+ throw WrongFileTypeError(path, std::filesystem::file_type::regular, file_type);
+ }
+
+ auto file = std::ifstream();
+
+ file.open(path);
+
+ if (!file.is_open())
+ {
+ throw FileNotOpenError(path);
+ }
+
+ auto content_lines = ContainerType();
+
+ while (file)
+ {
+ auto line = std::string();
+ std::getline(file, line);
+
+ std::erase(line, '\r');
+
+ content_lines.push_back(line);
+ }
+
+ if (!content_lines.empty() && content_lines.back().empty())
+ {
+ content_lines.pop_back();
+ }
+
+ return content_lines;
+}