51 lines
1.7 KiB
C++
51 lines
1.7 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* 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();
|
|
}
|