diff options
Diffstat (limited to 'src/file')
-rw-r--r-- | src/file/mail.cpp | 10 | ||||
-rw-r--r-- | src/file/settings.cpp | 100 |
2 files changed, 59 insertions, 51 deletions
diff --git a/src/file/mail.cpp b/src/file/mail.cpp index 2b1ff14..2a2231b 100644 --- a/src/file/mail.cpp +++ b/src/file/mail.cpp @@ -1,7 +1,7 @@ #include <fstream> #include "mail.hpp" -#include "management/logger.hpp" +#include "logger/logger_set.hpp" namespace smtp::file { @@ -13,10 +13,12 @@ namespace smtp::file manage::MailsSet Mail::Read() const { + static const std::string METHOD_NAME = "Read mails"; + std::ifstream mail_file{ mPathFile, std::fstream::in }; if ( !mail_file.is_open() ) { - manage::Logger::LogError( "Unable to open file to read " + mPathFile ); + logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to read " + mPathFile ); return {}; } std::string line{}; @@ -35,10 +37,12 @@ namespace smtp::file bool Mail::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() ) { - manage::Logger::LogError( "Unable to open file to write " + mPathFile ); + logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to write " + mPathFile ); return false; } for( const auto& mail : data ) diff --git a/src/file/settings.cpp b/src/file/settings.cpp index 710b67f..70f3cf7 100644 --- a/src/file/settings.cpp +++ b/src/file/settings.cpp @@ -1,58 +1,60 @@ #include <fstream> #include "settings.hpp" -#include "management/logger.hpp" +#include "logger/logger_set.hpp" namespace smtp::file { // //Constructors // - Settings::Settings( std::string const& path_file, checker::RegistratorSettings const& registrator_errors ) - : mPathFile( path_file ) - , mRegistratorErrors( registrator_errors ) - { + Settings::Settings( std::string const& path_file, checker::RegistratorSettings const& registrator_errors ) + : mPathFile( path_file ) + , mRegistratorErrors( registrator_errors ) + { - } + } // //Public methods // - manage::SettingsFields Settings::Read() const - { - auto parsed_store = GetParsedStore(); - return converter::Full{}.Convert( parsed_store ); - } + manage::SettingsFields Settings::Read() const + { + auto parsed_store = GetParsedStore(); + return converter::Full{}.Convert( parsed_store ); + } - bool Settings::Write( manage::SettingsFields const& settings_fields ) const - { - auto parsed_data = converter::Full{}.Convert( settings_fields ); + bool Settings::Write( manage::SettingsFields const& settings_fields ) const + { + auto parsed_data = converter::Full{}.Convert( settings_fields ); return mRegistratorErrors.Check( parsed_data ) && SetParsedData( parsed_data ); - } + } // //Private methods // manage::SettingsFileDataType Settings::GetParsedStore() const - { - std::ifstream settings_file{ mPathFile, std::fstream::in }; - if ( !settings_file.is_open() ) - { - manage::Logger::LogError( "Unable to open file to read " + mPathFile ); - return {}; - } + { + 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 mRegistratorErrors.Check(result) ? result : manage::SettingsFileDataType{}; - } + return mRegistratorErrors.Check( result ) ? result : manage::SettingsFileDataType{}; + } manage::SettingsFileDataType Settings::GetDataFromFile( std::ifstream& settings_file ) const { std::string line{}; manage::SettingsFileDataType result; - while ( std::getline( settings_file, line ) ) + while( std::getline( settings_file, line )) { auto parsed_data = parser::Settings{}.Parse( line ); result.insert( parsed_data ); @@ -61,28 +63,30 @@ namespace smtp::file } bool Settings::SetParsedData( manage::SettingsFileDataType const& parsed_data ) const - { - std::ofstream settings_file{ mPathFile, std::fstream::out | std::fstream::trunc }; - if ( !settings_file.is_open() ) - { - manage::Logger::LogError( "Unable to open file to write " + mPathFile ); - return false; - } - for( const auto& data : parsed_data ) - { - auto line = BuildParam( data ); - settings_file << line << "\n"; - } - settings_file.close(); - return true; - } + { + static const std::string METHOD_NAME = "Write settings"; - std::string Settings::BuildParam( std::pair<std::string, std::string> const& data) const - { - std::string result; - result += data.first; - result += '='; - result += data.second; - return result; - } + 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; + } + for( const auto& data: parsed_data ) + { + auto line = BuildParam( data ); + settings_file << line << "\n"; + } + settings_file.close(); + return true; + } + + std::string Settings::BuildParam( std::pair < std::string, std::string > const& data ) const + { + std::string result; + result += data.first; + result += '='; + result += data.second; + return result; + } } |