From afd6bd90af0171028f4918c9f575ac3d928bb141 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 6 Nov 2024 16:14:44 +0100 Subject: [PATCH] update: finishing the ex04 of the module 01 --- cpp01/ex04/main.cpp | 12 +++++++++--- cpp01/ex04/sed.cpp | 26 +++++++++++++++++++------- cpp01/ex04/sed.hpp | 4 ++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/cpp01/ex04/main.cpp b/cpp01/ex04/main.cpp index c0a1e1e..2493842 100644 --- a/cpp01/ex04/main.cpp +++ b/cpp01/ex04/main.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/05 15:24:29 by rparodi #+# #+# */ -/* Updated: 2024/11/05 22:02:35 by rparodi ### ########.fr */ +/* Updated: 2024/11/06 16:09:06 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,12 @@ int main(int argc, char *argv[]) return (1); } Sed new_sed = Sed(argv, &input); - std::ofstream output = output(new_sed.getFilename()); - while (new_sed.line(output)){} + std::ofstream output(new_sed.getFilename()); + if (!output.is_open()) { + std::cerr << "Error: Could not open the file for writing." << std::endl; + return 1; + } + while (new_sed.line(output)) + { + } } diff --git a/cpp01/ex04/sed.cpp b/cpp01/ex04/sed.cpp index a25e148..5efef75 100644 --- a/cpp01/ex04/sed.cpp +++ b/cpp01/ex04/sed.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/05 15:58:02 by rparodi #+# #+# */ -/* Updated: 2024/11/05 21:58:30 by rparodi ### ########.fr */ +/* Updated: 2024/11/06 16:14:03 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,13 +33,25 @@ const char *Sed::getFilename() return (_filename.c_str()); } -bool Sed::line(std::ofstream output) +bool Sed::line(std::ofstream& output) { std::string to_ret; - if (!std::getline((*_input), to_ret)) - return (false); - output << to_ret << std::endl; - std::cout << to_ret << std::endl; - return (true); + if (!std::getline(*_input, to_ret)) { + return false; + } + + std::string result; + std::size_t pos = 0; + std::size_t found = 0; + + while ((found = to_ret.find(_to_replace, pos)) != std::string::npos) { + result += to_ret.substr(pos, found - pos) + _by_word; + pos = found + _to_replace.length(); + } + + result += to_ret.substr(pos); + + output << result << std::endl; + return true; } diff --git a/cpp01/ex04/sed.hpp b/cpp01/ex04/sed.hpp index 87197ee..d2e481c 100644 --- a/cpp01/ex04/sed.hpp +++ b/cpp01/ex04/sed.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/05 15:27:45 by rparodi #+# #+# */ -/* Updated: 2024/11/05 21:58:59 by rparodi ### ########.fr */ +/* Updated: 2024/11/06 15:56:45 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ class Sed public: Sed(char **argv, std::ifstream *input); ~Sed(); - bool line(std::ofstream output); + bool line(std::ofstream& output); const char *getFilename(); private: std::string _filename;