From 5fa62d64ee2b6e02a18e68e080c085c4a8f0de78 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Sun, 11 Jan 2026 22:19:00 +0100 Subject: [PATCH] added io functionality --- src/io/io.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/io/io.hpp | 13 +++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/io/io.cpp b/src/io/io.cpp index e69de29..444c436 100644 --- a/src/io/io.cpp +++ b/src/io/io.cpp @@ -0,0 +1,36 @@ +#include "io/io.hpp" +#include +#include +#include +#include + +std::string read_file(const std::string &FILE_PATH) { + std::ifstream file(FILE_PATH); + if (!file) { + throw std::runtime_error("Error while opening file at " + FILE_PATH); + } + std::stringstream buffer; + buffer << file.rdbuf(); + + if (file.bad()) { + throw std::runtime_error("Error while reading file at " + FILE_PATH); + } + + return buffer.str(); +} + +void write_file(const std::string &FILE_PATH, const std::string &CONTENT) { + std::ofstream out(FILE_PATH); + if (!out) { + throw std::runtime_error("Error while opening file for writing at " + + FILE_PATH); + } + + out << CONTENT; + + if (!out) { + throw std::runtime_error("Error while writing to the " + FILE_PATH); + } + + out.close(); +} diff --git a/src/io/io.hpp b/src/io/io.hpp index e69de29..a43df36 100644 --- a/src/io/io.hpp +++ b/src/io/io.hpp @@ -0,0 +1,13 @@ +#include + +// @brief Read file from given path +// @param FILE PATH Path to the file +// @return File content +// @throws std::runtime_error if any errors +std::string read_file(const std::string &FILE_PATH); + +/// @brief Writes the given content into a file. +/// @param FILE_PATH Path to the file to write. +/// @param CONTENT Content to write into the file. +/// @throws std::runtime_error if the file cannot be opened or written. +void write_file(const std::string &FILE_PATH, const std::string &CONTENT);