「✨」 feat(Sed): Should be finished
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,3 +7,4 @@ zombie
|
||||
zombieHorde
|
||||
thisisabrain
|
||||
violence
|
||||
sed
|
||||
|
@ -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.replace
Normal file
2
ex04/asdf.replace
Normal file
@ -0,0 +1,2 @@
|
||||
fff ll fheiugfoghklh
|
||||
uoghpiuheqwhugppoisdyfh
|
@ -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
50
ex04/sed.cpp
Normal 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
27
ex04/sed.h
Normal 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);
|
||||
};
|
Reference in New Issue
Block a user