d05 ex03 handle weird request as long as they contains form name
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
# include "colors.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <algorithm> // trasnform
|
||||
# include <cctype> // tolower
|
||||
|
||||
# include <AForm.hpp>
|
||||
# include <ShrubberyCreationForm.hpp>
|
||||
@@ -34,6 +36,8 @@ private:
|
||||
static AForm * makePresidential(std::string const &target);
|
||||
static AForm * makeRobotomy(std::string const &target);
|
||||
|
||||
bool isValidForm(std::string name, unsigned int i) const;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
BIN
d05/ex03/interns
Executable file
BIN
d05/ex03/interns
Executable file
Binary file not shown.
@@ -142,39 +142,49 @@ int main() {
|
||||
AForm * f2;
|
||||
AForm * f3;
|
||||
AForm * f4;
|
||||
AForm * f5;
|
||||
|
||||
std::cout << B_BLUE "intern tries to create a Shrubbery form" RESET "\n";
|
||||
std::cout << B_BLUE "\nintern tries to create a Shrubbery form" RESET "\n";
|
||||
f1 = i.makeForm("Shrubbery", "smoking");
|
||||
if (f1)
|
||||
std::cout << *f1 << '\n';
|
||||
else
|
||||
std::cout << "intern make an error and cannot create the form\n";
|
||||
std::cout << "intern makes an error and cannot create the form\n";
|
||||
|
||||
std::cout << B_BLUE "\nintern tries to create a Robotomy form" RESET "\n";
|
||||
f2 = i.makeForm("Robotomy", "building");
|
||||
if (f2)
|
||||
std::cout << *f2 << '\n';
|
||||
else
|
||||
std::cout << "intern make an error and cannot create the form\n";
|
||||
std::cout << "intern makes an error and cannot create the form\n";
|
||||
|
||||
std::cout << B_BLUE "\nintern tries to create a Presidential form" RESET "\n";
|
||||
f3 = i.makeForm("Presidential", "building");
|
||||
if (f3)
|
||||
std::cout << *f3 << '\n';
|
||||
else
|
||||
std::cout << "intern make an error and cannot create the form\n";
|
||||
std::cout << "intern makes an error and cannot create the form\n";
|
||||
|
||||
std::cout << B_BLUE "\nintern tries to create a blueberry form" RESET "\n";
|
||||
f4 = i.makeForm("blueberry", "giv_me_gold");
|
||||
if (f4)
|
||||
std::cout << *f4 << '\n';
|
||||
else
|
||||
std::cout << "intern make an error and cannot create the form\n";
|
||||
std::cout << "intern makes an error and cannot create the form\n";
|
||||
|
||||
std::cout << B_BLUE "\nintern tries to create a \"robotomy request\" form" RESET "\n";
|
||||
f5 = i.makeForm("robotomy request", "try_hard");
|
||||
if (f5)
|
||||
std::cout << *f5 << '\n';
|
||||
else
|
||||
std::cout << "intern makes an error and cannot create the form\n";
|
||||
|
||||
std::cout << '\n';
|
||||
delete f1;
|
||||
delete f2;
|
||||
delete f3;
|
||||
delete f4;
|
||||
delete f5;
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
|
||||
17
d05/ex03/sekoia_shrubbery
Normal file
17
d05/ex03/sekoia_shrubbery
Normal file
@@ -0,0 +1,17 @@
|
||||
* *
|
||||
* * *
|
||||
* * * * *
|
||||
* * * * *
|
||||
* * * * * * *
|
||||
* * * * * .# * *
|
||||
* * * #. .# * *
|
||||
* "#. #: #" * * *
|
||||
* * * "#. ##" *
|
||||
* "###
|
||||
"##
|
||||
##.
|
||||
.##:
|
||||
:###
|
||||
;###
|
||||
,####.
|
||||
/\/\/\/\/\/.######.\/\/\/\/\
|
||||
@@ -7,9 +7,9 @@
|
||||
*********************************************/
|
||||
|
||||
const t_formModel Intern::_chooseForm[] = {
|
||||
{"Shrubbery", Intern::makeShrubbery},
|
||||
{"Presidential", Intern::makePresidential},
|
||||
{"Robotomy", Intern::makeRobotomy}
|
||||
{"shrubbery", Intern::makeShrubbery},
|
||||
{"presidential", Intern::makePresidential},
|
||||
{"robotomy", Intern::makeRobotomy}
|
||||
};
|
||||
|
||||
/*********************************************
|
||||
@@ -44,18 +44,6 @@ Intern & Intern::operator=( Intern const & rhs __attribute__((unused))) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
//std::ostream & operator<<(std::ostream & o, Intern const & rhs)
|
||||
//{
|
||||
// o << rhs.getFoo();
|
||||
// return (o);
|
||||
//}
|
||||
|
||||
/*********************************************
|
||||
* ACCESSORS
|
||||
*********************************************/
|
||||
|
||||
//std::string Intern::getFoo() const {return _foo;}
|
||||
|
||||
/*********************************************
|
||||
* PRIVATE MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
@@ -64,7 +52,7 @@ AForm * Intern::makeForm(std::string formName, std::string formTarget) const {
|
||||
unsigned int len = sizeof _chooseForm / sizeof _chooseForm[0];
|
||||
|
||||
for (unsigned int i = 0; i < len; i++)
|
||||
if (!formName.compare(_chooseForm[i].name))
|
||||
if (isValidForm(formName, i))
|
||||
{
|
||||
std::cout << "Intern creates " << formName << "\n";
|
||||
return _chooseForm[i].create(formTarget);
|
||||
@@ -73,6 +61,16 @@ AForm * Intern::makeForm(std::string formName, std::string formTarget) const {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool Intern::isValidForm(std::string name, unsigned int i) const {
|
||||
// to lowercase :
|
||||
// https://stackoverflow.com/questions/313970/how-to-convert-an-instance-of-stdstring-to-lower-case
|
||||
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
|
||||
|
||||
if (name.find(_chooseForm[i].name) == std::string::npos)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
@@ -84,10 +82,3 @@ AForm * Intern::makePresidential(std::string const &target) {
|
||||
AForm * Intern::makeRobotomy(std::string const &target) {
|
||||
return new RobotomyRequestForm(target);}
|
||||
|
||||
/*********************************************
|
||||
* NESTED CLASS
|
||||
*********************************************/
|
||||
|
||||
//void Intern::Class::function() {}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user