From dcda44e695642c26de583d6a700838f0f8443822 Mon Sep 17 00:00:00 2001 From: Adam JOLY Date: Thu, 7 Nov 2024 18:47:39 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(Sed):=20Shou?= =?UTF-8?q?ld=20be=20finished?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + ex04/Makefile | 7 ++++--- ex04/asdf | 2 ++ ex04/asdf.replace | 2 ++ ex04/main.cpp | 16 +++++++++++++-- ex04/sed.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++ ex04/sed.h | 27 +++++++++++++++++++++++++ flake.nix | 2 +- 8 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 ex04/asdf create mode 100644 ex04/asdf.replace create mode 100644 ex04/sed.cpp create mode 100644 ex04/sed.h diff --git a/.gitignore b/.gitignore index 66599d1..39be181 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ zombie zombieHorde thisisabrain violence +sed diff --git a/ex04/Makefile b/ex04/Makefile index 8d4ff45..c8cc415 100644 --- a/ex04/Makefile +++ b/ex04/Makefile @@ -6,17 +6,18 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2024/11/05 18:10:30 by adjoly ### ########.fr # +# Updated: 2024/11/07 18:45:24 by adjoly ### ########.fr # # # # **************************************************************************** # -NAME = violence +NAME = sed CC = c++ OBJSDIR = obj/ -SRCS = main.cpp +SRCS = main.cpp \ + sed.cpp OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) diff --git a/ex04/asdf b/ex04/asdf new file mode 100644 index 0000000..6fd9726 --- /dev/null +++ b/ex04/asdf @@ -0,0 +1,2 @@ +fff asdf fheiugfoghklh +uoghpiuheqwhugppoisdyfh diff --git a/ex04/asdf.replace b/ex04/asdf.replace new file mode 100644 index 0000000..cdcc676 --- /dev/null +++ b/ex04/asdf.replace @@ -0,0 +1,2 @@ +fff ll fheiugfoghklh +uoghpiuheqwhugppoisdyfh diff --git a/ex04/main.cpp b/ex04/main.cpp index fce8542..38b83ac 100644 --- a/ex04/main.cpp +++ b/ex04/main.cpp @@ -6,10 +6,22 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/05 18:11:15 by adjoly #+# #+# */ -/* Updated: 2024/11/05 18:11:41 by adjoly ### ########.fr */ +/* Updated: 2024/11/07 18:46:40 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -int main(int ac, char **av) { +#include "sed.h" +#include +#include +int main(int ac, char **av) { + if (!(ac > 2 && ac <= 4)) + return (std::cerr << "Invalid number of args please only give ./sed " << std::endl, 1); + + std::string infile = av[1]; + std::string s1 = av[2]; + std::string s2 = av[3]; + Sed sed(infile, s1, s2); + + return (0); } diff --git a/ex04/sed.cpp b/ex04/sed.cpp new file mode 100644 index 0000000..ca1a354 --- /dev/null +++ b/ex04/sed.cpp @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/07 16:45:25 by adjoly #+# #+# */ +/* Updated: 2024/11/07 18:45:45 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include "sed.h" + +Sed::Sed(std::string &inFile, std::string &s1, std::string &s2): + _outFileName(inFile + ".replace") { + std::ifstream infile(inFile.c_str()); + size_t pos = 0; + std::stringstream buf; + + if (!infile) { + std::cerr << "Could not open infile" << std::endl; + return ; + } + buf << infile.rdbuf(); + _content = buf.str(); + infile.close(); + pos = _content.find(s1, pos + s2.length()); + while (pos < _content.length()) + { + _content.erase(pos, s1.length()); + _content.insert(pos, s2); + pos = _content.find(s1, pos + s2.length()); + } +} + +Sed::~Sed(void) { + std::ofstream outfile(this->_outFileName.c_str()); + + if (!outfile) { + std::cout << "Cannot write to outfile" << std::endl; + return ; + } + outfile << _content; + outfile.close(); +} diff --git a/ex04/sed.h b/ex04/sed.h new file mode 100644 index 0000000..cdf3419 --- /dev/null +++ b/ex04/sed.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sed.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/07 16:10:36 by adjoly #+# #+# */ +/* Updated: 2024/11/07 18:36:27 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +# define fileExtension ".replace" + +# include + +class Sed { + private: + std::string _outFileName; + std::string _content; + + public: + Sed(std::string &inFile, std::string &s1, std::string &s2); + ~Sed(void); +}; diff --git a/flake.nix b/flake.nix index cb53ef0..3ce3ed9 100644 --- a/flake.nix +++ b/flake.nix @@ -31,7 +31,7 @@ git gdb inputs.pogit.packages.${pkgs.system}.default - bear + python312Packages.compiledb ]; }; });