summaryrefslogtreecommitdiff
path: root/src/file/settings/file_decorator.cpp
diff options
context:
space:
mode:
authorclaiff <claiff@mail.ru>2022-10-05 16:03:08 +0300
committerclaiff <claiff@mail.ru>2022-10-05 16:03:08 +0300
commit8edef99a5b52643e0b919c424357752cbbc9a8dd (patch)
treec7ae9c113d29aa3350eca04f27a933bd3b5f769c /src/file/settings/file_decorator.cpp
parent2b03fece7f5895591eabd2f04baa2df19b4c3417 (diff)
parent3f1f70a3b945605c6abb7d23f46042b963db243a (diff)
downloadobmc-sila-smtp-8edef99a5b52643e0b919c424357752cbbc9a8dd.tar.xz
Merge branch 'refactor/3009'
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;
+ }
+}