From: Lukas Jiriste Date: Fri, 11 Oct 2024 11:31:50 +0000 (+0200) Subject: Add solution to ex06 (without Makefile) X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=0e9b94d6b0d9ad69cd10134faad36551c27a92e8;p=42%2FCPP_Module_01 Add solution to ex06 (without Makefile) I am not really satisfied with the solution I came up with, but I am quite fed up with this exercise, because I see little value in it. --- diff --git a/ex06/Harl.cpp b/ex06/Harl.cpp new file mode 100644 index 0000000..eeb8097 --- /dev/null +++ b/ex06/Harl.cpp @@ -0,0 +1,103 @@ +#include "Harl.h" +#include +#include +#include +#include + +Harl::Harl(int filter) +{ + m_filter = filter; + m_levels[0] = "DEBUG"; + m_speech[0] = &Harl::debug; + m_levels[1] = "INFO"; + m_speech[1] = &Harl::info; + m_levels[2] = "WARNING"; + m_speech[2] = &Harl::warning; + m_levels[3] = "ERROR"; + m_speech[3] = &Harl::error; +} + +Harl::Harl(const Harl &harl) +{ + *this = harl; +} + +Harl::~Harl() +{ +} + +Harl &Harl::operator=(const Harl &harl) +{ + m_levels[0] = harl.m_levels[0]; + m_speech[0] = harl.m_speech[0]; + m_levels[1] = harl.m_levels[1]; + m_speech[1] = harl.m_speech[1]; + m_levels[2] = harl.m_levels[2]; + m_speech[2] = harl.m_speech[2]; + m_levels[3] = harl.m_levels[3]; + m_speech[3] = harl.m_speech[3]; + m_filter = harl.m_filter; + return (*this); +} + +ssize_t Harl::get_level_num(std::string level) +{ + for (ssize_t i(0); i < 4; ++i) + if (m_levels[i] == level) + return (i); + return (-1); +} + +void Harl::complain(std::string level) +{ + ssize_t i; + + i = get_level_num(level); + if (i < 0 || i >= m_filter) + { + if (i >= 0) + std::cout << '[' << level << "]\n"; + switch (i) + { + case 0: + debug(); + break ; + case 1: + info(); + break ; + case 2: + warning(); + break ; + case 3: + error(); + break ; + default: + std::cout << "Harl does not know this level.\n\n"; + break ; + } + } +} + +void Harl::debug(void) +{ + std::cout << "I love having extra bacon for my " + "7XL-double-cheese-triple-pickle-special-ketchup burger. I really do!\n\n"; +} + +void Harl::info(void) +{ + 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!\n\n"; +} + +void Harl::warning(void) +{ + std::cout << "I think I deserve to have some extra bacon for free. " + "I’ve been coming for ears whereas you started working here since last month.\n\n"; +} + +void Harl::error(void) +{ + std::cout << "This is unacceptable! I want to speak to the manager now.\n\n"; +} diff --git a/ex06/Harl.h b/ex06/Harl.h new file mode 100644 index 0000000..5a331b2 --- /dev/null +++ b/ex06/Harl.h @@ -0,0 +1,29 @@ +#ifndef HARL_H +# define HARL_H + +# include +# include + +class Harl +{ + private: + void debug(void); + void info(void); + void warning(void); + void error(void); + + void (Harl::*m_speech[4])(void); + std::string m_levels[4]; + int m_filter; + + public: + Harl(int filter = 0); + Harl(const Harl &harl); + ~Harl(); + + Harl &operator=(const Harl &harl); + void complain(std::string level); + ssize_t get_level_num(std::string level); +}; + +#endif // HARL_H diff --git a/ex06/main.cpp b/ex06/main.cpp new file mode 100644 index 0000000..7da8356 --- /dev/null +++ b/ex06/main.cpp @@ -0,0 +1,28 @@ +#include "Harl.h" +#include + +void test_harl(ssize_t filter) +{ + Harl harl(filter); + + harl.complain("INFO"); + harl.complain("DEBUG"); + harl.complain("WARNING"); + harl.complain("ERROR"); + harl.complain("DEBUG"); + harl.complain("INFO"); + harl.complain("ERROR"); +} + +int main(int argc, char **argv) +{ + Harl harl; + + if (argc != 2 || harl.get_level_num(argv[1]) == -1) + { + std::cout << "[ Probably complaining about insignificant problems ]\n"; + return (1); + } + test_harl(harl.get_level_num(argv[1])); + return (0); +}