64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* SedForLooser.cpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/04/21 15:56:56 by erey-bet #+# #+# */
|
|
/* Updated: 2023/05/18 16:20:42 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "SedForLooser.hpp"
|
|
#include <cerrno>
|
|
|
|
SedForLooser::SedForLooser(char *filename) {
|
|
this->filename = filename;
|
|
}
|
|
|
|
void SedForLooser::replace(std::string s1, std::string s2) {
|
|
std::ifstream infile(this->filename);
|
|
if (infile == NULL)
|
|
{
|
|
if (errno == EACCES)
|
|
std::cout << "You don't have the permissions to open the file." << std::endl;
|
|
else
|
|
std::cout << "Error to open the file." << std::endl;
|
|
return ;
|
|
}
|
|
|
|
std::ofstream outfile(strcat(this->filename, ".replace"));
|
|
if (infile == NULL)
|
|
{
|
|
if (errno == EACCES)
|
|
std::cout << "You don't have the permissions to open the file." << std::endl;
|
|
else
|
|
std::cout << "Error to open the file." << std::endl;
|
|
return ;
|
|
}
|
|
|
|
std::string text;
|
|
std::string line;
|
|
while (std::getline(infile, line))
|
|
text += line + "\n";
|
|
infile.close();
|
|
|
|
std::string result;
|
|
for (std::size_t i = 0; i < text.size(); i++)
|
|
{
|
|
for (std::size_t y = 0; y < s1.size(); y++)
|
|
{
|
|
if (text[i] != s1[y])
|
|
{
|
|
result += text[i];
|
|
break;
|
|
}
|
|
else if (y == s1.size() - 1)
|
|
result += s2;
|
|
}
|
|
}
|
|
|
|
outfile << result << std::endl;
|
|
}
|