#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());
	char			str[this->_find.length() - 1];
	int				len = this->_find.length();
	char			tmp;

	while (file.get(str, 1 + 1, EOF))
	{
		if (str[0] == this->_find[0])
		{
			tmp = str[0];
			file.get(str, len, EOF);
			if (this->_find.compare(1, len - 1, str) == 0)
				new_file << this->_replacement;
			else
				new_file << tmp << str;
		}
		else
			new_file << str;
	}

}

