1
0

」 feat(Sed): Should be finished

This commit is contained in:
2024-11-07 18:47:39 +01:00
parent 7903a6e7a0
commit dcda44e695
8 changed files with 101 additions and 6 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ zombie
zombieHorde
thisisabrain
violence
sed

View File

@ -6,17 +6,18 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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))

2
ex04/asdf Normal file
View File

@ -0,0 +1,2 @@
fff asdf fheiugfoghklh
uoghpiuheqwhugppoisdyfh

2
ex04/asdf.replace Normal file
View File

@ -0,0 +1,2 @@
fff ll fheiugfoghklh
uoghpiuheqwhugppoisdyfh

View File

@ -6,10 +6,22 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
#include <string>
int main(int ac, char **av) {
if (!(ac > 2 && ac <= 4))
return (std::cerr << "Invalid number of args please only give ./sed <infile> <s1> <s2>" << 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);
}

50
ex04/sed.cpp Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/07 16:45:25 by adjoly #+# #+# */
/* Updated: 2024/11/07 18:45:45 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#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();
}

27
ex04/sed.h Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sed.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
class Sed {
private:
std::string _outFileName;
std::string _content;
public:
Sed(std::string &inFile, std::string &s1, std::string &s2);
~Sed(void);
};

View File

@ -31,7 +31,7 @@
git
gdb
inputs.pogit.packages.${pkgs.system}.default
bear
python312Packages.compiledb
];
};
});