#include "Sed.hpp"

Sed::Sed(char *file, char *find, char *replacement)
	: _file( file )
	, _new_file( std::string(file).append(".replace") )
	, _find( find )
	, _replacement( replacement ) {

	return;
}
Sed::~Sed() {return;}

void	Sed::replace() {

	std::ifstream	file(this->_file.c_str());
	std::ofstream	new_file(this->_new_file.c_str());
	int				len = this->_find.length();
	std::string		str;
	char			c_str[len + 1];

	if (file.fail() || new_file.fail())
		return;

	file.get(c_str, len + 1, EOF);
	str.assign(c_str);
	while (!file.eof())
	{
		if (this->_find.compare(str) == 0)
		{
			new_file << this->_replacement;
			file.get(c_str, len + 1, EOF);
			str.assign(c_str);
			continue;
		}
		else
			new_file << str[0];
		str.erase(str.begin());
		str.push_back(file.get());
	}
	str.erase(str.end() - 1);
	new_file << str;

}

