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