From dcbaf61e4968734d9b3bc41f704ea04d54746cea Mon Sep 17 00:00:00 2001 From: eportnov Date: Tue, 13 Sep 2022 11:13:33 +0300 Subject: add file reading --- src/file/settings_converter.cpp | 94 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/file/settings_converter.cpp (limited to 'src/file/settings_converter.cpp') diff --git a/src/file/settings_converter.cpp b/src/file/settings_converter.cpp new file mode 100644 index 0000000..c64041f --- /dev/null +++ b/src/file/settings_converter.cpp @@ -0,0 +1,94 @@ +#include "settings_converter.hpp" + +namespace smtp::file +{ + + SettingsFields SettingsConverter::Convert( ParsedDataType const& from ) const + { + SettingsFields result; + + ApplyAuth( result, from ); + ApplySsl( result, from ); + ApplyUsername( result, from ); + ApplyPassword( result, from ); + ApplyHost( result, from ); + ApplyPort( result, from ); + + return result; + } + + std::unordered_map SettingsConverter::Convert( SettingsFields const& from ) const + { + std::unordered_map result; + return result; + } + + void SettingsConverter::ApplyAuth( SettingsFields &result, ParsedDataType const& from ) const + { + static const std::string FIELD = "need_auth"; + + ApplyBool(from, FIELD, result.is_need_auth); + } + + void SettingsConverter::ApplySsl( SettingsFields &result, ParsedDataType const& from ) const + { + static const std::string FIELD = "need_ssl"; + + ApplyBool(from, FIELD, result.is_need_ssl); + } + + void SettingsConverter::ApplyBool( ParsedDataType const& from, std::string const& search_field, bool& field ) const + { + static const std::string TRUE_AS_STRING = "true"; + static const std::string FALSE_AS_STRING = "false"; + + auto find = from.find( search_field ); + if( find == from.end() ) + { + return; + } + if( find->second != TRUE_AS_STRING && find->second != FALSE_AS_STRING ) + { + return; + } + field = ( find->second == TRUE_AS_STRING ) ? true : false; + } + + void SettingsConverter::ApplyUsername( SettingsFields &result, ParsedDataType const& from ) const + { + static const std::string FIELD = "username"; + + ApplyString( from, FIELD, result.username ); + } + + void SettingsConverter::ApplyPassword( SettingsFields &result, ParsedDataType const& from ) const + { + static const std::string FIELD = "password"; + + ApplyString( from, FIELD, result.password ); + } + + void SettingsConverter::ApplyHost( SettingsFields &result, ParsedDataType const& from ) const + { + static const std::string FIELD = "host"; + + ApplyString( from, FIELD, result.host ); + } + + void SettingsConverter::ApplyPort( SettingsFields &result, ParsedDataType const& from ) const + { + static const std::string FIELD = "port"; + + ApplyString(from, FIELD, result.port); + } + + void SettingsConverter::ApplyString( ParsedDataType const& from, std::string const& search_field, std::string& field ) const + { + auto find = from.find( search_field ); + if( find == from.end() ) + { + return; + } + field = find->second; + } +} -- cgit v1.2.3