summaryrefslogtreecommitdiff
path: root/src/file/mail/file_decorator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/file/mail/file_decorator.cpp')
-rw-r--r--src/file/mail/file_decorator.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/file/mail/file_decorator.cpp b/src/file/mail/file_decorator.cpp
new file mode 100644
index 0000000..82111ac
--- /dev/null
+++ b/src/file/mail/file_decorator.cpp
@@ -0,0 +1,84 @@
+#include <fstream>
+
+#include "file_decorator.hpp"
+#include "logger/logger_set.hpp"
+
+namespace smtp::file::mail
+{
+
+ //
+ //Constructors
+ //
+
+ FileDecorator::FileDecorator( std::string const& path_file )
+ : mPathFile( path_file )
+ {
+ }
+
+ //
+ //Public methods
+ //
+
+ manage::MailsSet FileDecorator::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 {};
+ }
+
+ auto result = ReadFile( mail_file );
+
+ mail_file.close();
+ return result;
+ }
+
+ bool FileDecorator::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 };
+
+
+ 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 FileDecorator::ReadFile( std::ifstream& mail_file ) const
+ {
+ std::string line{};
+ manage::MailsSet result;
+
+ while ( std::getline( mail_file, line ) )
+ {
+ result.push_back( line );
+ }
+ return result;
+ }
+
+ bool FileDecorator::WriteFile( std::ofstream& mail_file, manage::MailsSet const& data ) const
+ {
+ bool result = true;
+
+ for( const auto& mail : data )
+ {
+ mail_file << mail << "\n";
+ }
+ return result;
+ }
+}