summaryrefslogtreecommitdiff
path: root/src/file/settings/file_decorator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/file/settings/file_decorator.cpp')
-rw-r--r--src/file/settings/file_decorator.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/file/settings/file_decorator.cpp b/src/file/settings/file_decorator.cpp
new file mode 100644
index 0000000..38b42ce
--- /dev/null
+++ b/src/file/settings/file_decorator.cpp
@@ -0,0 +1,77 @@
+#include <fstream>
+
+#include "file_decorator.hpp"
+#include "logger/logger_set.hpp"
+#include "parser.hpp"
+#include "converter/file_to_string.hpp"
+
+namespace smtp::file::settings
+{
+
+ //
+ //Constructors
+ //
+
+ FileDecorator::FileDecorator( std::string const& path_file )
+ : mPathFile( path_file )
+ {
+
+ }
+
+ //
+ //Public methods
+ //
+
+ manage::SettingsFileDataType FileDecorator::Read() const
+ {
+ static const std::string METHOD_NAME = "Read settings";
+
+ std::ifstream settings_file{mPathFile, std::fstream::in};
+ if( !settings_file.is_open() )
+ {
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to read " + mPathFile );
+ return {};
+ }
+ auto result = GetDataFromFile( settings_file );
+
+ settings_file.close();
+ return result;
+ }
+
+ bool FileDecorator::Write( manage::SettingsFileDataType const& data ) const
+ {
+ static const std::string METHOD_NAME = "Write settings";
+
+ std::ofstream settings_file{mPathFile, std::fstream::out | std::fstream::trunc};
+ if( !settings_file.is_open())
+ {
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to write " + mPathFile );
+ return false;
+ }
+ auto settings_as_list = converter::FileToString{}.Convert(data);
+ for( const auto& settings_as_string : settings_as_list )
+ {
+ settings_file << settings_as_string << "\n";
+ }
+ settings_file.close();
+ return true;
+ }
+
+ //
+ //Private methods
+ //
+
+ manage::SettingsFileDataType FileDecorator::GetDataFromFile( std::ifstream& settings_file ) const
+ {
+ std::string line{};
+ manage::SettingsFileDataType result;
+ parser::Settings parser;
+
+ while( std::getline( settings_file, line ))
+ {
+ auto parsed_data = parser.Parse( line );
+ result.insert( parsed_data );
+ }
+ return result;
+ }
+}