summaryrefslogtreecommitdiff
path: root/src/file/mail.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/file/mail.cpp')
-rw-r--r--src/file/mail.cpp60
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;
}
}