From e8aeea36e6b48e18d27f9d7857cd29524cc8aa1f Mon Sep 17 00:00:00 2001 From: eportnov Date: Tue, 13 Sep 2022 15:03:25 +0300 Subject: add writing files --- src/file/converter/file.cpp | 87 +++++++++++++++++++++++++++++++++++++++++ src/file/converter/file.hpp | 30 ++++++++++++++ src/file/converter/full.cpp | 17 ++++++++ src/file/converter/full.hpp | 25 ++++++++++++ src/file/converter/settings.cpp | 65 ++++++++++++++++++++++++++++++ src/file/converter/settings.hpp | 30 ++++++++++++++ 6 files changed, 254 insertions(+) create mode 100644 src/file/converter/file.cpp create mode 100644 src/file/converter/file.hpp create mode 100644 src/file/converter/full.cpp create mode 100644 src/file/converter/full.hpp create mode 100644 src/file/converter/settings.cpp create mode 100644 src/file/converter/settings.hpp (limited to 'src/file/converter') diff --git a/src/file/converter/file.cpp b/src/file/converter/file.cpp new file mode 100644 index 0000000..d0bb032 --- /dev/null +++ b/src/file/converter/file.cpp @@ -0,0 +1,87 @@ +#include "file.hpp" + +namespace smtp::file::converter +{ + manage::SettingsFields File::Convert( manage::SettingsFileDataType const& from ) const + { + manage::SettingsFields result; + + ApplyAuth( result, from ); + ApplySsl( result, from ); + ApplyUsername( result, from ); + ApplyPassword( result, from ); + ApplyHost( result, from ); + ApplyPort( result, from ); + + return result; + } + + void File::ApplyAuth( manage::SettingsFields &result, manage::SettingsFileDataType const& from ) const + { + static const std::string FIELD = "need_auth"; + + ApplyBool(from, FIELD, result.is_need_auth); + } + + void File::ApplySsl( manage::SettingsFields &result, manage::SettingsFileDataType const& from ) const + { + static const std::string FIELD = "need_ssl"; + + ApplyBool(from, FIELD, result.is_need_ssl); + } + + void File::ApplyBool( manage::SettingsFileDataType 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 File::ApplyUsername( manage::SettingsFields &result, manage::SettingsFileDataType const& from ) const + { + static const std::string FIELD = "username"; + + ApplyString( from, FIELD, result.username ); + } + + void File::ApplyPassword( manage::SettingsFields &result, manage::SettingsFileDataType const& from ) const + { + static const std::string FIELD = "password"; + + ApplyString( from, FIELD, result.password ); + } + + void File::ApplyHost( manage::SettingsFields &result, manage::SettingsFileDataType const& from ) const + { + static const std::string FIELD = "host"; + + ApplyString( from, FIELD, result.host ); + } + + void File::ApplyPort( manage::SettingsFields &result, manage::SettingsFileDataType const& from ) const + { + static const std::string FIELD = "port"; + + ApplyString(from, FIELD, result.port); + } + + void File::ApplyString( manage::SettingsFileDataType 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; + } +} diff --git a/src/file/converter/file.hpp b/src/file/converter/file.hpp new file mode 100644 index 0000000..63ff6dd --- /dev/null +++ b/src/file/converter/file.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +#include "managment/general.hpp" + +namespace smtp::file::converter +{ + class File + { + public: + File() = default; + ~File() = default; + + manage::SettingsFields Convert( manage::SettingsFileDataType const& from ) const; + private: + void ApplyAuth( manage::SettingsFields& result, manage::SettingsFileDataType const& from ) const; + void ApplySsl( manage::SettingsFields& result, manage::SettingsFileDataType const& from ) const; + void ApplyBool( manage::SettingsFileDataType const& from, std::string const& search_field, bool& field ) const; + + void ApplyUsername( manage::SettingsFields& result, manage::SettingsFileDataType const& from ) const; + void ApplyPassword( manage::SettingsFields& result, manage::SettingsFileDataType const& from ) const; + void ApplyHost( manage::SettingsFields& result, manage::SettingsFileDataType const& from ) const; + void ApplyPort( manage::SettingsFields& result, manage::SettingsFileDataType const& from ) const; + void ApplyString( manage::SettingsFileDataType const& from, std::string const& search_field, std::string& field ) const; + }; +} + + diff --git a/src/file/converter/full.cpp b/src/file/converter/full.cpp new file mode 100644 index 0000000..61f5a85 --- /dev/null +++ b/src/file/converter/full.cpp @@ -0,0 +1,17 @@ +#include "full.hpp" +#include "file.hpp" +#include "settings.hpp" + +namespace smtp::file::converter +{ + manage::SettingsFields Full::Convert( ParsedDataType const& from ) const + { + return File{}.Convert( from ); + } + + std::unordered_map Full::Convert( manage::SettingsFields const& from ) const + { + return Settings{}.Convert( from ); + + } +} diff --git a/src/file/converter/full.hpp b/src/file/converter/full.hpp new file mode 100644 index 0000000..5ffccd4 --- /dev/null +++ b/src/file/converter/full.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +#include "managment/general.hpp" + +namespace smtp::file::converter +{ + using ParsedDataType = std::unordered_map; + + class Full + { + public: + Full() = default; + ~Full() = default; + + manage::SettingsFields Convert( std::unordered_map const& from ) const; + std::unordered_map Convert( manage::SettingsFields const& from ) const; + private: + + }; +} + + diff --git a/src/file/converter/settings.cpp b/src/file/converter/settings.cpp new file mode 100644 index 0000000..809eef8 --- /dev/null +++ b/src/file/converter/settings.cpp @@ -0,0 +1,65 @@ +#include "settings.hpp" + +namespace smtp::file::converter +{ + + ParsedDataType Settings::Convert( manage::SettingsFields const& from ) const + { + ParsedDataType result; + + ApplyPort( from, result ); + ApplyHost( from, result ); + ApplyPassword( from, result ); + ApplyUsername( from, result ); + ApplySsl( from, result ); + ApplyAuth( from, result ); + + return result; + } + + void Settings::ApplyAuth( manage::SettingsFields const& from, ParsedDataType& result ) const + { + static const std::string FIELD = "need_auth"; + static const std::string TRUE_AS_STRING = "true"; + static const std::string FALSE_AS_STRING = "false"; + + from.is_need_auth ? result.insert({FIELD, TRUE_AS_STRING}) : result.insert({FIELD, FALSE_AS_STRING}); + } + + void Settings::ApplySsl( manage::SettingsFields const& from, ParsedDataType& result ) const + { + static const std::string FIELD = "need_ssl"; + static const std::string TRUE_AS_STRING = "true"; + static const std::string FALSE_AS_STRING = "false"; + + from.is_need_ssl ? result.insert({FIELD, TRUE_AS_STRING}) : result.insert({FIELD, FALSE_AS_STRING}); + } + + void Settings::ApplyUsername( manage::SettingsFields const& from, ParsedDataType& result ) const + { + static const std::string FIELD = "username"; + + result.insert({FIELD, from.username}); + } + + void Settings::ApplyPassword( manage::SettingsFields const& from, ParsedDataType& result ) const + { + static const std::string FIELD = "password"; + + result.insert({FIELD, from.password}); + } + + void Settings::ApplyHost( manage::SettingsFields const& from, ParsedDataType& result ) const + { + static const std::string FIELD = "host"; + + result.insert({FIELD, from.host}); + } + + void Settings::ApplyPort( manage::SettingsFields const& from, ParsedDataType& result ) const + { + static const std::string FIELD = "port"; + + result.insert({FIELD, from.port}); + } +} diff --git a/src/file/converter/settings.hpp b/src/file/converter/settings.hpp new file mode 100644 index 0000000..8f94942 --- /dev/null +++ b/src/file/converter/settings.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +#include "managment/general.hpp" + +namespace smtp::file::converter +{ + using ParsedDataType = std::unordered_map;; + + class Settings + { + public: + Settings() = default; + ~Settings() = default; + + std::unordered_map Convert( manage::SettingsFields const& from ) const; + private: + void ApplyAuth( manage::SettingsFields const& from, ParsedDataType& result ) const; + void ApplySsl( manage::SettingsFields const& from, ParsedDataType& result ) const; + + void ApplyUsername( manage::SettingsFields const& from, ParsedDataType& result ) const; + void ApplyPassword( manage::SettingsFields const& from, ParsedDataType& result ) const; + void ApplyHost( manage::SettingsFields const& from, ParsedDataType& result ) const; + void ApplyPort( manage::SettingsFields const& from, ParsedDataType& result ) const; + }; +} + + -- cgit v1.2.3