diff options
Diffstat (limited to 'src/file')
-rw-r--r-- | src/file/mail.cpp | 41 | ||||
-rw-r--r-- | src/file/mail.hpp | 22 | ||||
-rw-r--r-- | src/file/parser/settings.cpp | 20 | ||||
-rw-r--r-- | src/file/parser/settings.hpp | 19 | ||||
-rw-r--r-- | src/file/settings.cpp | 50 | ||||
-rw-r--r-- | src/file/settings.hpp | 31 | ||||
-rw-r--r-- | src/file/settings_converter.cpp | 94 | ||||
-rw-r--r-- | src/file/settings_converter.hpp | 41 |
8 files changed, 318 insertions, 0 deletions
diff --git a/src/file/mail.cpp b/src/file/mail.cpp new file mode 100644 index 0000000..14e6228 --- /dev/null +++ b/src/file/mail.cpp @@ -0,0 +1,41 @@ +#include <fstream> + +#include "mail.hpp" + +namespace smtp::file +{ + Mail::Mail( std::string const& path_file ) + : PathFile( path_file ) + { + + } + + std::list<std::string> Mail::Read() const + { + std::ifstream mail_file{ PathFile }; + if ( !mail_file.is_open() ) + { + //TODO new file + // std::cerr << "Failed to open pcie_devices database \n"; + } + std::string line{}; + std::list<std::string> result; + + while ( std::getline( mail_file, line ) ) + { + auto parsed_data = GetMailFromLine( line ); + if( parsed_data ) + { + result.push_back( *parsed_data ); + } + } + return result; + } + + std::optional<std::string> Mail::GetMailFromLine( std::string const& line ) const + { + //TODO parsing + return line; + } + +} diff --git a/src/file/mail.hpp b/src/file/mail.hpp new file mode 100644 index 0000000..0f5ade5 --- /dev/null +++ b/src/file/mail.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include <list> +#include <string> +#include <optional> + +namespace smtp::file +{ + class Mail + { + public: + explicit Mail( std::string const& path_file ); + ~Mail() = default; + + std::list<std::string> Read() const; + private: + std::optional<std::string> GetMailFromLine( std::string const& line ) const; + + std::string PathFile; + }; + +} diff --git a/src/file/parser/settings.cpp b/src/file/parser/settings.cpp new file mode 100644 index 0000000..590ed72 --- /dev/null +++ b/src/file/parser/settings.cpp @@ -0,0 +1,20 @@ +#include "settings.hpp" + +namespace smtp::file::parser +{ + ParseResult Settings::Parse( std::string const& line ) const + { + static constexpr char DELIMITER = '='; + static constexpr size_t ZERO_POSITION = 0; + static constexpr size_t NEXT_POSITION = 1; + + auto position = line.find(DELIMITER); + if( position == std::string::npos) + { + return {}; + } + auto first_part = line.substr( ZERO_POSITION, position ); + auto second_part = line.substr(position + NEXT_POSITION, line.length()); + return {first_part, second_part}; + } +} diff --git a/src/file/parser/settings.hpp b/src/file/parser/settings.hpp new file mode 100644 index 0000000..e7de2f6 --- /dev/null +++ b/src/file/parser/settings.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include <utility> +#include <string> + +namespace smtp::file::parser +{ + using ParseResult = std::pair<std::string,std::string>; + + class Settings + { + public: + Settings() = default; + ~Settings() = default; + + ParseResult Parse( std::string const& line ) const; + }; + +} diff --git a/src/file/settings.cpp b/src/file/settings.cpp new file mode 100644 index 0000000..bcb2f3e --- /dev/null +++ b/src/file/settings.cpp @@ -0,0 +1,50 @@ +#include <fstream> + +#include "settings.hpp" + +namespace smtp::file +{ + Settings::Settings( std::string const& path_file, parser::Settings const& parser, SettingsConverter const& converter) + : mPathFile( path_file ) + , mParser( parser ) + , mConverter( converter ) + { + + } + + SettingsFields Settings::Read() const + { + auto parsed_store = GetParsedStore(); + return mConverter.Convert( parsed_store ); + } + + void Settings::Write( SettingsFields const& settings_fields ) const + { + auto parsed_data = mConverter.Convert( settings_fields ); + SetParsedData(); + } + + ParsedStoreType Settings::GetParsedStore() const + { + std::ifstream settings_file{ mPathFile }; + if ( !settings_file.is_open() ) + { + //TODO new file + // std::cerr << "Failed to open pcie_devices database \n"; + } + std::string line{}; + ParsedStoreType result; + + while ( std::getline( settings_file, line ) ) + { + auto parsed_data = mParser.Parse(line); + result.insert( parsed_data ); + } + return result; + } + + void Settings::SetParsedData() const + { + + } +} diff --git a/src/file/settings.hpp b/src/file/settings.hpp new file mode 100644 index 0000000..62182b1 --- /dev/null +++ b/src/file/settings.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include <list> +#include <string> + +#include "settings_converter.hpp" +#include "parser/settings.hpp" + +namespace smtp::file +{ + using ParsedStoreType = std::unordered_map<std::string, std::string>; + + class Settings + { + public: + explicit Settings( std::string const& path_file, parser::Settings const& parser, SettingsConverter const& converter ); + ~Settings() = default; + + SettingsFields Read() const; + void Write( SettingsFields const& settings_fields ) const; + private: + ParsedStoreType GetParsedStore() const; + void SetParsedData() const; + + std::string mPathFile; + parser::Settings mParser; + SettingsConverter mConverter; + + }; + +} 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<std::string, std::string> SettingsConverter::Convert( SettingsFields const& from ) const + { + std::unordered_map<std::string, std::string> 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; + } +} diff --git a/src/file/settings_converter.hpp b/src/file/settings_converter.hpp new file mode 100644 index 0000000..36041c3 --- /dev/null +++ b/src/file/settings_converter.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include <unordered_map> +#include <string> + +namespace smtp::file +{ + struct SettingsFields + { + bool is_need_auth; + bool is_need_ssl; + std::string username; + std::string password; + std::string host; + std::string port; + }; + + using ParsedDataType = std::unordered_map<std::string, std::string>;; + + class SettingsConverter + { + public: + SettingsConverter() = default; + ~SettingsConverter() = default; + + SettingsFields Convert( std::unordered_map<std::string, std::string> const& from ) const; + std::unordered_map<std::string, std::string> Convert( SettingsFields const& from ) const; + private: + void ApplyAuth( SettingsFields& result, ParsedDataType const& from ) const; + void ApplySsl( SettingsFields& result, ParsedDataType const& from ) const; + void ApplyBool( ParsedDataType const& from, std::string const& search_field, bool& field ) const; + + void ApplyUsername( SettingsFields& result, ParsedDataType const& from ) const; + void ApplyPassword( SettingsFields& result, ParsedDataType const& from ) const; + void ApplyHost( SettingsFields& result, ParsedDataType const& from ) const; + void ApplyPort( SettingsFields& result, ParsedDataType const& from ) const; + void ApplyString( ParsedDataType const& from, std::string const& search_field, std::string& field ) const; + }; +} + + |