diff --git a/ex06/.gitignore b/ex06/.gitignore new file mode 100644 index 0000000..11d101f --- /dev/null +++ b/ex06/.gitignore @@ -0,0 +1 @@ +harlFilter diff --git a/ex06/Harl.cpp b/ex06/Harl.cpp new file mode 100644 index 0000000..c7349db --- /dev/null +++ b/ex06/Harl.cpp @@ -0,0 +1,75 @@ +#include "Harl.hpp" +#include + +typedef void (Harl::*HarlComplaint)(void); + +int getLevelIdx(std::string level) { + static const std::string levels[] = { + "DEBUG", + "INFO", + "WARNING", + "ERROR", + }; + for (int i = 0; i < 4; i++) { + if (level == levels[i]) + return i; + } + return -1; +} + +void Harl::complain(std::string level) { + int idx = getLevelIdx(level); + switch (idx) { + case 0: + std::cout << "[ DEBUG ]" << std::endl; + debug(); + std::cout << std::endl; + case 1: + std::cout << "[ INFO ]" << std::endl; + info(); + std::cout << std::endl; + case 2: + std::cout << "[ WARNING ]" << std::endl; + warning(); + std::cout << std::endl; + case 3: + std::cout << "[ ERROR ]" << std::endl; + error(); + std::cout << std::endl; + break; + default: + wrongLevel(); + break; + } +} + +void Harl::debug() { + std::cout + << "I love having extra bacon for my " + "7XL-double-cheese-triple-pickle-special-ketchup burger. I really do!" + << std::endl; +} + +void Harl::info() { + std::cout + << "I cannot believe adding extra bacon costs more money. You didn’t put " + "enough bacon in my burger! If you did, I wouldn’t be asking for more!" + << std::endl; +} + +void Harl::warning() { + std::cout + << "I think I deserve to have some extra bacon for free. I’ve been " + "coming for years, whereas you started working here just last month." + << std::endl; +} + +void Harl::error() { + std::cout << "This is unacceptable! I want to speak to the manager now." + << std::endl; +} + +void Harl::wrongLevel() { + std::cout << "[ Probably complaining about insignificant problems ]" + << std::endl; +} diff --git a/ex06/Harl.hpp b/ex06/Harl.hpp new file mode 100644 index 0000000..405f48e --- /dev/null +++ b/ex06/Harl.hpp @@ -0,0 +1,19 @@ +#ifndef HARL_HPP +#define HARL_HPP + +#include + +class Harl { +public: + void complain(std::string level); + +private: + void debug(void); + void info(void); + void warning(void); + void error(void); + + void wrongLevel(void); +}; + +#endif diff --git a/ex06/Makefile b/ex06/Makefile new file mode 100644 index 0000000..f731e60 --- /dev/null +++ b/ex06/Makefile @@ -0,0 +1,39 @@ +NAME = harlFilter +ifeq ($(CPPFLAGS),) + CPPFLAGS = -Wall -Wextra -Werror -std=c++98 -g +endif +ifeq ($(CXX),) + CXX = c++ +endif +# g++ is the default on 42 computers +ifeq ($(CXX),g++) + CXX = c++ +endif +srcs = \ + Harl.cpp \ + +main_objs = main.o $(srcs:.cpp=.o) +all_objs = $(main_objs) +deps = $(all_objs:.o=.d) + +all: $(NAME) + +-include $(deps) + +$(NAME): $(main_objs) + $(CXX) $(CPPFLAGS) -o $@ $^ + +%.o: %.cpp + $(CXX) -c $(CPPFLAGS) -o $*.o $*.cpp + $(CXX) -MM $(CPPFLAGS) -MT $*.o $*.cpp > $*.d + +clean: + find . -name '*.o' -print -delete + find . -name '*.d' -print -delete + +fclean: clean + rm -f $(NAME) + +re: + +make fclean + +make all diff --git a/ex06/main.cpp b/ex06/main.cpp new file mode 100644 index 0000000..e450aa0 --- /dev/null +++ b/ex06/main.cpp @@ -0,0 +1,12 @@ +#include "Harl.hpp" + +int main(int argc, char *argv[]) { + std::string level; + if (argc != 2) + level = "invalid"; + else + level = argv[1]; + Harl harl; + harl.complain(level); + return 0; +}