1
0
This repository has been archived on 2024-11-25. You can view files and clone it, but cannot push or open issues or pull requests.

51 lines
1.7 KiB
C++
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/07 16:45:25 by adjoly #+# #+# */
/* Updated: 2024/11/09 18:49:49 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);
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();
}