cpp01/ex04/Main.cpp

104 lines
3.1 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/01 23:30:01 by lfirmin #+# #+# */
/* Updated: 2025/07/01 23:48:50 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include <string>
#include <iostream>
#include <fstream>
static int open_files(std::string nameInputFile, std::string nameOutputfile,
std::ifstream *inputFile, std::ofstream *outputFile)
{
(*inputFile).open(nameInputFile.c_str(), std::fstream::in);
if (!inputFile->is_open()) // ou if (!(*inputFile))
{
std::cerr << "Failed to open input file!" << std::endl;
return (1);
}
(*outputFile).open(nameOutputfile.c_str(), std::fstream::out);
if (!outputFile->is_open()) // ou if (!(*outputFile))
{
std::cerr << "Failed to open output file!" << std::endl;
(*inputFile).close();
return (1);
}
return (0);
}
static void read_and_replace(char **argv, std::ifstream *inputFile, std::ofstream *outputFile)
{
std::string to_find;
std::string to_replace;
std::string line;
std::string::size_type found;
size_t end_last_found;
std::string replaced_line;
to_find = *(argv + 2);
to_replace = *(argv + 3);
end_last_found = 0;
while(std::getline(*inputFile, line))
{
if (to_find.empty())
{
if (!(*inputFile).eof())
*outputFile << line << std::endl;
else
*outputFile << line;
}
else
{
replaced_line.clear();
end_last_found = 0;
found = line.find(to_find);
if (found != std::string::npos)
{
while (found != std::string::npos)
{
replaced_line.append(line,end_last_found,found - end_last_found);
replaced_line += to_replace;
end_last_found = found + to_find.length();
found = line.find(to_find, end_last_found);
if (found == std::string::npos)
replaced_line.append(line, end_last_found,line.length());
}
}
else
replaced_line = line;
if (!(*inputFile).eof())
*outputFile << replaced_line << std::endl;
else
*outputFile << replaced_line;
}
}
}
int main(int argc, char **argv)
{
std::string nameInputFile;
std::string nameOutputfile;
std::ifstream inputFile;
std::ofstream outputFile;
if (argc != 4)
return (std::cerr << "./sed <Filename> <S1> <S2>" << std::endl, 0);
nameInputFile = argv[1];
nameOutputfile = nameOutputfile + argv[1] + ".replace";
if (open_files(nameInputFile, nameOutputfile, &inputFile, &outputFile))
return (1);
read_and_replace(argv, &inputFile, &outputFile);
inputFile.close();
outputFile.close();
return (0);
}