diff options
Diffstat (limited to 'src/util/io_impl.hpp')
-rw-r--r-- | src/util/io_impl.hpp | 55 |
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; +} |