d01 ex04 tout change et ajouts de tests

This commit is contained in:
Hugo LAMY
2022-02-08 13:58:59 +01:00
parent ea0f9b085a
commit eac083ff7e
18 changed files with 130 additions and 47 deletions

View File

@@ -14,29 +14,31 @@ 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;
std::string str;
char c_str[len + 1];
while (file.get(str, 1 + 1, EOF))
if (file.fail() || new_file.fail())
return;
file.get(c_str, len + 1, EOF);
str.assign(c_str);
while (!file.eof())
{
if (str[0] == this->_find[0])
if (this->_find.compare(str) == 0)
{
if (len == 1)
new_file << this->_replacement;
else
{
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;
}
new_file << this->_replacement;
file.get(c_str, len + 1, EOF);
str.assign(c_str);
continue;
}
else
new_file << str;
new_file << str[0];
str.erase(str.begin());
str.push_back(file.get());
}
for (int i = 0; str[i + 1]; i++)
new_file << str[i];
}