update: pushing the start ex04 of the module 01
This commit is contained in:
parent
2b197d8d05
commit
08a23cc42b
6 changed files with 291 additions and 0 deletions
109
cpp01/ex04/Makefile
Normal file
109
cpp01/ex04/Makefile
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
||||||
|
# Updated: 2024/11/05 15:36:01 by rparodi ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
# Variables
|
||||||
|
# Name
|
||||||
|
NAME = sed_is_for_loser
|
||||||
|
|
||||||
|
# Commands
|
||||||
|
CXX = c++
|
||||||
|
RM = rm -rf
|
||||||
|
|
||||||
|
# Flags
|
||||||
|
# Mandatory flags for 42 cpp
|
||||||
|
CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
|
||||||
|
# Flags to debug and have the dependences (can be removed for correction)
|
||||||
|
CXXFLAGS += -MMD -g3
|
||||||
|
# Flag to debug (TO REMOVE)
|
||||||
|
# CXXFLAGS += -D DEBUG=1
|
||||||
|
# Sources
|
||||||
|
SRC = sed.cpp \
|
||||||
|
main.cpp
|
||||||
|
|
||||||
|
# Objects
|
||||||
|
OBJDIRNAME = ./build
|
||||||
|
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o))
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
GREEN = \033[32m
|
||||||
|
GREY = \033[0;90m
|
||||||
|
RED = \033[0;31m
|
||||||
|
GOLD = \033[38;5;220m
|
||||||
|
END = \033[0m
|
||||||
|
|
||||||
|
# Rules
|
||||||
|
|
||||||
|
# All (make all)
|
||||||
|
all: header $(NAME) footer
|
||||||
|
|
||||||
|
# Clean (make clean)
|
||||||
|
clean:
|
||||||
|
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
|
||||||
|
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
|
||||||
|
@$(RM) $(OBJDIRNAME)
|
||||||
|
|
||||||
|
# Clean (make fclean)
|
||||||
|
fclean: clean
|
||||||
|
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
|
||||||
|
@$(RM) $(NAME)
|
||||||
|
@echo ""
|
||||||
|
|
||||||
|
# Restart (make re)
|
||||||
|
re: header fclean all
|
||||||
|
|
||||||
|
# Dependences for all
|
||||||
|
$(NAME): $(OBJ)
|
||||||
|
@mkdir -p $(OBJDIRNAME)
|
||||||
|
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
|
||||||
|
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
|
||||||
|
|
||||||
|
# Creating the objects
|
||||||
|
$(OBJDIRNAME)/%.o: %.cpp
|
||||||
|
@mkdir -p $(dir $@)
|
||||||
|
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
|
||||||
|
@$(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||||
|
|
||||||
|
# Header
|
||||||
|
header:
|
||||||
|
@clear
|
||||||
|
@printf '\n\n'
|
||||||
|
@printf '$(GOLD) ******* ****** ******* $(END)\n'
|
||||||
|
@printf '$(GOLD) ****** *** ******* $(END)\n'
|
||||||
|
@printf '$(GOLD) ******* * ******* $(END)\n'
|
||||||
|
@printf '$(GOLD) ****** ******* $(END)\n'
|
||||||
|
@printf '$(GOLD) ******* ******* $(END)\n'
|
||||||
|
@printf '$(GOLD) ******************* ******* * $(END)\n'
|
||||||
|
@printf '$(GOLD) ******************* ******* *** $(END)\n'
|
||||||
|
@printf '$(GOLD) ****** ******* ****** $(END)\n'
|
||||||
|
@printf '$(GOLD) ****** $(END)\n'
|
||||||
|
@printf '$(GOLD) ****** $(END)\n'
|
||||||
|
@printf '$(GREY) Made by rparodi$(END)\n\n'
|
||||||
|
|
||||||
|
# Footer
|
||||||
|
footer:
|
||||||
|
@printf "\n"
|
||||||
|
@printf "$(GOLD) ,_ _,$(END)\n"
|
||||||
|
@printf "$(GOLD) | \\___//|$(END)\n"
|
||||||
|
@printf "$(GOLD) |=6 6=|$(END)\n"
|
||||||
|
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
|
||||||
|
@printf "$(GOLD) ) \` ( ,$(END)\n"
|
||||||
|
@printf "$(GOLD) / \\ (('$(END)\n"
|
||||||
|
@printf "$(GOLD) | | ))$(END)\n"
|
||||||
|
@printf "$(GOLD) /| | | |\\_//$(END)\n"
|
||||||
|
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
|
||||||
|
@printf "$(GOLD) '\"' '\"'$(END)\n"
|
||||||
|
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
|
||||||
|
|
||||||
|
# Phony
|
||||||
|
.PHONY: all bonus clean fclean re
|
||||||
|
|
||||||
|
-include ${OBJ:.o=.d}
|
||||||
47
cpp01/ex04/color.hpp
Normal file
47
cpp01/ex04/color.hpp
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* color.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/17 15:27:54 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2024/10/17 15:48:22 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef COLOR_HPP
|
||||||
|
#define COLOR_HPP
|
||||||
|
|
||||||
|
#define CLR_RESET "\033[0m"
|
||||||
|
|
||||||
|
#define CLR_BLACK "\033[0;30m"
|
||||||
|
#define CLR_RED "\033[0;31m"
|
||||||
|
#define CLR_GREEN "\033[0;32m"
|
||||||
|
#define CLR_YELLOW "\033[0;33m"
|
||||||
|
#define CLR_BLUE "\033[0;34m"
|
||||||
|
#define CLR_MAGENTA "\033[0;35m"
|
||||||
|
#define CLR_CYAN "\033[0;36m"
|
||||||
|
#define CLR_WHITE "\033[0;37m"
|
||||||
|
#define CLR_GOLD "\033[38;5;220m"
|
||||||
|
#define CLR_GREY "\033[38;5;240m"
|
||||||
|
|
||||||
|
#define CLR_LIGHT_BLACK "\033[0;90m"
|
||||||
|
#define CLR_LIGHT_RED "\033[0;91m"
|
||||||
|
#define CLR_LIGHT_GREEN "\033[0;92m"
|
||||||
|
#define CLR_LIGHT_YELLOW "\033[0;93m"
|
||||||
|
#define CLR_LIGHT_BLUE "\033[0;94m"
|
||||||
|
#define CLR_LIGHT_MAGENTA "\033[0;95m"
|
||||||
|
#define CLR_LIGHT_CYAN "\033[0;96m"
|
||||||
|
#define CLR_LIGHT_WHITE "\033[0;97m"
|
||||||
|
|
||||||
|
#define CLR_BOLD_BLACK "\033[1;30m"
|
||||||
|
#define CLR_BOLD_RED "\033[1;31m"
|
||||||
|
#define CLR_BOLD_GREEN "\033[1;32m"
|
||||||
|
#define CLR_BOLD_YELLOW "\033[1;33m"
|
||||||
|
#define CLR_BOLD_BLUE "\033[1;34m"
|
||||||
|
#define CLR_BOLD_MAGENTA "\033[1;35m"
|
||||||
|
#define CLR_BOLD_CYAN "\033[1;36m"
|
||||||
|
#define CLR_BOLD_WHITE "\033[1;37m"
|
||||||
|
|
||||||
|
#endif
|
||||||
21
cpp01/ex04/err_msg.hpp
Normal file
21
cpp01/ex04/err_msg.hpp
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* err_msg.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/05 15:26:18 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2024/11/05 15:53:05 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef ERR_MSG_HPP
|
||||||
|
# define ERR_MSG_HPP
|
||||||
|
|
||||||
|
#include "color.hpp"
|
||||||
|
|
||||||
|
#define ERR_PREFIX "Error:\n"
|
||||||
|
#define ARGS_MISSING "Please give 3 arguments: <filename> <to_replace> <by_word> !"
|
||||||
|
#define FILE_CANT_OPEN ": The file cannot be open !"
|
||||||
|
#endif
|
||||||
32
cpp01/ex04/main.cpp
Normal file
32
cpp01/ex04/main.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/05 15:24:29 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2024/11/05 22:02:35 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "sed.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc != 4)
|
||||||
|
{
|
||||||
|
std::cerr << CLR_RED ERR_PREFIX ARGS_MISSING << std::endl;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
std::ifstream input(argv[1]);
|
||||||
|
if (!input)
|
||||||
|
{
|
||||||
|
std::cout << CLR_RED ERR_PREFIX << argv[1] << FILE_CANT_OPEN << CLR_RESET << std::endl;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
Sed new_sed = Sed(argv, &input);
|
||||||
|
std::ofstream output = output(new_sed.getFilename());
|
||||||
|
while (new_sed.line(output)){}
|
||||||
|
}
|
||||||
45
cpp01/ex04/sed.cpp
Normal file
45
cpp01/ex04/sed.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sed.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/05 15:58:02 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2024/11/05 21:58:30 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "sed.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <ios>
|
||||||
|
|
||||||
|
Sed::Sed(char **argv, std::ifstream *input)
|
||||||
|
{
|
||||||
|
_filename = argv[1];
|
||||||
|
_filename += ".replace";
|
||||||
|
_to_replace = argv[2];
|
||||||
|
_by_word = argv[3];
|
||||||
|
_input = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
Sed::~Sed()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *Sed::getFilename()
|
||||||
|
{
|
||||||
|
return (_filename.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Sed::line(std::ofstream output)
|
||||||
|
{
|
||||||
|
std::string to_ret;
|
||||||
|
|
||||||
|
if (!std::getline((*_input), to_ret))
|
||||||
|
return (false);
|
||||||
|
output << to_ret << std::endl;
|
||||||
|
std::cout << to_ret << std::endl;
|
||||||
|
return (true);
|
||||||
|
}
|
||||||
37
cpp01/ex04/sed.hpp
Normal file
37
cpp01/ex04/sed.hpp
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sed.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/05 15:27:45 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2024/11/05 21:58:59 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef SED_HPP
|
||||||
|
#define SED_HPP
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <streambuf>
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "err_msg.hpp"
|
||||||
|
|
||||||
|
class Sed
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Sed(char **argv, std::ifstream *input);
|
||||||
|
~Sed();
|
||||||
|
bool line(std::ofstream output);
|
||||||
|
const char *getFilename();
|
||||||
|
private:
|
||||||
|
std::string _filename;
|
||||||
|
std::string _to_replace;
|
||||||
|
std::string _by_word;
|
||||||
|
std::ifstream *_input;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue