CPP05/ex02/src/ShrubberyCreationForm.cpp

79 lines
2.5 KiB
C++

#include "ShrubberyCreationForm.hpp"
#include <fstream>
#include <iostream>
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include "colors.h"
ShrubberyCreationForm::ShrubberyCreationForm(std::string target)
: AForm("Shrubbery Creation", 145, 137), _target(target)
{
std::cout << SHRUBBERY_CREATION_FORM PARAMETERIZED_CONSTRUCTOR << std::endl;
std::cout << SHRUBBERY_CREATION_FORM << *this << std::endl;
}
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &other)
: AForm(other), _target(other._target)
{
std::cout << SHRUBBERY_CREATION_FORM COPY_CONSTRUCTOR << std::endl;
std::cout << SHRUBBERY_CREATION_FORM << *this << std::endl;
}
ShrubberyCreationForm::~ShrubberyCreationForm()
{
std::cout << SHRUBBERY_CREATION_FORM DESTRUCTOR << std::endl;
}
void ShrubberyCreationForm::execute(const Bureaucrat &executor) const
{
if (!getIsSigned())
throw AForm::FormNotSignedException();
if (executor.getGrade() > getExecuteGrade())
throw AForm::GradeTooLowException();
std::ofstream ofs(_target + "_shrubbery");
if (!ofs)
{
std::cerr << "Error creating file: " << _target << "_shrubbery"
<< std::endl;
return;
}
ofs << " # #### ####\n"
<< " ### \\/#|### |/####\n"
<< " ##\\/#/ \\||/##/_/##/_#\n"
<< " ### \\/###|/ \\/ # ###\n"
<< " ##_\\_#\\_\\## | #/###_/_####\n"
<< " ## #### # \\ #| / #### ##/##\n"
<< " __#_--###` |{,###---###-~\n"
<< " \\ }{\n"
<< " }}{\n"
<< " }}{\n"
<< " {{}\n"
<< " , -=-~{ .-^- _\n"
<< " `}\n"
<< " {\n";
ofs.close();
std::cout << SHRUBBERY_CREATION_FORM << "Shrubbery created in file: " << _target << "_shrubbery"
<< std::endl;
}
const std::string ShrubberyCreationForm::getTarget() const
{
return _target;
}
std::ostream &operator<<(std::ostream &os, const ShrubberyCreationForm &form)
{
os << form.getName()
<< " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed")
<< ", Sign Grade: " << form.getSignGrade()
<< ", Execute Grade: " << form.getExecuteGrade()
<< ", Target: " << form.getTarget();
return os;
}