summaryrefslogtreecommitdiff
path: root/src/file/converter/file.cpp
blob: d0bb0327095d33c1cb1bf22eb92eab1948a14a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
    }
}