mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
added io functionality
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#include "io/io.hpp"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <string>
|
||||
|
||||
// @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);
|
||||
|
||||
Reference in New Issue
Block a user