#include #include "mail.hpp" #include "logger/logger_set.hpp" namespace smtp::file { Mail::Mail( std::string const& path_file, checker::RegistratorMails const& registrator_errors ) : mPathFile( path_file ) , mRegistratorErrors( registrator_errors ) { } manage::MailsSet Mail::Read() const { static const std::string METHOD_NAME = "Read mails"; std::ifstream mail_file{ mPathFile, std::fstream::in }; if ( !mail_file.is_open() ) { logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to read " + mPathFile ); return {}; } std::string line{}; manage::MailsSet result; while ( std::getline( mail_file, line ) ) { if( mRegistratorErrors.Check( line ) ) { result.push_back( line ); } } mail_file.close(); return result; } bool Mail::Write( manage::MailsSet const& data ) const { static const std::string METHOD_NAME = "Write mails"; std::ofstream mail_file{ mPathFile, std::fstream::out | std::fstream::trunc }; bool result = true; if ( !mail_file.is_open() ) { logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to write " + mPathFile ); return false; } for( const auto& mail : data ) { if( mRegistratorErrors.Check( mail ) ) { mail_file << mail << "\n"; } else { result = false; } } mail_file.close(); return result; } }