intra to gitea
This commit is contained in:
+103
@@ -0,0 +1,103 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2025/07/01 13:54:05 by lfirmin #+# #+# #
|
||||
# Updated: 2025/07/01 23:43:34 by lfirmin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
CXX = c++
|
||||
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
OBJDIR = obj
|
||||
SOURCES = Main.cpp
|
||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||
NAME = sed
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(OBJDIR):
|
||||
@echo "📁 Creating obj directory..."
|
||||
@mkdir -p $(OBJDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
|
||||
@echo "🧠 Compiling $< ..."
|
||||
@$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
@echo "✅ $@ ready!"
|
||||
|
||||
$(NAME): $(OBJECTS)
|
||||
@echo "🔗 Linking $(NAME) ..."
|
||||
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
|
||||
@echo "🎉 $(NAME) is ready to hunt brains!"
|
||||
|
||||
clean:
|
||||
@echo "🧹 Cleaning object files..."
|
||||
@rm -rf $(OBJDIR)
|
||||
@echo "✨ Objects cleaned!"
|
||||
|
||||
fclean: clean
|
||||
@echo "🗑️ Removing $(NAME)..."
|
||||
@rm -f $(NAME)
|
||||
@echo "💀 Full clean complete!"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
Reference in New Issue
Block a user