diff options
author | eportnov <eportnov@ibs.ru> | 2022-10-03 11:38:05 +0300 |
---|---|---|
committer | eportnov <eportnov@ibs.ru> | 2022-10-03 11:38:05 +0300 |
commit | 2b03fece7f5895591eabd2f04baa2df19b4c3417 (patch) | |
tree | f68d57d9b472cbe1186f235034b97a1be75cac08 /src/file/mail.cpp | |
parent | 7e2843c706c1a6e033662c45957a76e01d167438 (diff) | |
parent | e41b247e61ce4d4cc96badab3a14bf413e4f46f2 (diff) | |
download | obmc-sila-smtp-2b03fece7f5895591eabd2f04baa2df19b4c3417.tar.xz |
Merge branch 'refactor/3009'
Diffstat (limited to 'src/file/mail.cpp')
-rw-r--r-- | src/file/mail.cpp | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/src/file/mail.cpp b/src/file/mail.cpp index 403ed84..a7e30ea 100644 --- a/src/file/mail.cpp +++ b/src/file/mail.cpp @@ -5,12 +5,21 @@ namespace smtp::file { + + // + //Constructors + // + Mail::Mail( std::string const& path_file, checker::RegistratorMails const& registrator_errors ) : mPathFile( path_file ) , mRegistratorErrors( registrator_errors ) { } + // + //Public methods + // + manage::MailsSet Mail::Read() const { static const std::string METHOD_NAME = "Read mails"; @@ -21,16 +30,9 @@ namespace smtp::file 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 ); - } - } + auto result = ReadFile( mail_file ); + mail_file.close(); return result; } @@ -40,25 +42,53 @@ namespace smtp::file 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; } + + auto result = WriteFile( mail_file, data ); + + mail_file.close(); + return result; + } + + // + //Private methods + // + + manage::MailsSet Mail::ReadFile( std::ifstream& mail_file ) const + { + std::string line{}; + manage::MailsSet result; + + while ( std::getline( mail_file, line ) ) + { + if( mRegistratorErrors.Check( line ) ) + { + result.push_back( line ); + } + } + return result; + } + + bool Mail::WriteFile( std::ofstream& mail_file, manage::MailsSet const& data ) const + { + bool result = true; for( const auto& mail : data ) { - if( mRegistratorErrors.Check( mail ) ) - { - mail_file << mail << "\n"; - } + if( mRegistratorErrors.Check( mail ) ) + { + mail_file << mail << "\n"; + } else { result = false; } } - mail_file.close(); return result; } } |