11#include " messageaddress.h"
22#include < cstddef>
3+ #include < cstring>
34#include < regex>
45#include < stdexcept>
56#include < string>
89using namespace jed_utils ;
910
1011MessageAddress::MessageAddress (const char *pEmailAddress, const char *pDisplayName)
11- : mEmailAddress(nullptr ), mDisplayName(nullptr ) {
12+ : mEmailAddress(nullptr ), mDisplayName(nullptr ), mDomainName( nullptr ) {
1213 std::string email_address { pEmailAddress };
1314 // Check if the email address is not empty or white spaces
1415 if (email_address.length () == 0 || StringUtils::trim (email_address).empty ()) {
@@ -28,30 +29,48 @@ MessageAddress::MessageAddress(const char *pEmailAddress, const char *pDisplayNa
2829 mDisplayName = new char [name_len+1 ];
2930 strncpy (mDisplayName , pDisplayName, name_len);
3031 mDisplayName [name_len] = ' \0 ' ;
32+
33+ std::string emailAddress (mEmailAddress );
34+ auto atPos = emailAddress.find (' @' );
35+ if (atPos == std::string::npos || atPos == emailAddress.size () - 1 ) {
36+ mDomainName = new char [1 ];
37+ mDomainName [0 ] = ' \0 ' ;
38+ } else {
39+ auto domainNamePart = emailAddress.substr (atPos + 1 );
40+ mDomainName = new char [domainNamePart.length () +1 ];
41+ strncpy (mDomainName , domainNamePart.c_str (), domainNamePart.length ());
42+ mDomainName [domainNamePart.length ()] = ' \0 ' ;
43+ }
3144}
3245
3346MessageAddress::~MessageAddress () {
3447 delete[] mEmailAddress ;
3548 delete[] mDisplayName ;
49+ delete[] mDomainName ;
3650}
3751
3852// Copy constructor
3953MessageAddress::MessageAddress (const MessageAddress& other)
4054 : mEmailAddress(new char [strlen(other.mEmailAddress ) + 1]),
41- mDisplayName(new char [strlen(other.mDisplayName ) + 1]) {
55+ mDisplayName(new char [strlen(other.mDisplayName ) + 1]),
56+ mDomainName(new char [strlen(other.mDomainName ) + 1]) {
4257 size_t email_len = strlen (other.mEmailAddress );
4358 strncpy (mEmailAddress , other.mEmailAddress , email_len);
4459 mEmailAddress [email_len] = ' \0 ' ;
4560 size_t name_len = strlen (other.mDisplayName );
4661 strncpy (mDisplayName , other.mDisplayName , name_len);
4762 mDisplayName [name_len] = ' \0 ' ;
63+ size_t domain_len = strlen (other.mDomainName );
64+ strncpy (mDomainName , other.mDomainName , domain_len);
65+ mDomainName [domain_len] = ' \0 ' ;
4866}
4967
5068// Assignment operator
5169MessageAddress& MessageAddress::operator =(const MessageAddress& other) {
5270 if (this != &other) {
5371 delete[] mEmailAddress ;
5472 delete[] mDisplayName ;
73+ delete[] mDomainName ;
5574 // mEmailAddress
5675 size_t email_len = strlen (other.mEmailAddress );
5776 mEmailAddress = new char [email_len + 1 ];
@@ -62,32 +81,42 @@ MessageAddress& MessageAddress::operator=(const MessageAddress& other) {
6281 mDisplayName = new char [name_len + 1 ];
6382 strncpy (mDisplayName , other.mDisplayName , name_len);
6483 mDisplayName [name_len] = ' \0 ' ;
84+ // mDomainName
85+ size_t domain_len = strlen (other.mDomainName );
86+ mDomainName = new char [domain_len + 1 ];
87+ strncpy (mDomainName , other.mDomainName , domain_len);
88+ mDomainName [domain_len] = ' \0 ' ;
6589 }
6690 return *this ;
6791}
6892
6993// Move constructor
7094MessageAddress::MessageAddress (MessageAddress&& other) noexcept
7195 : mEmailAddress(other.mEmailAddress ),
72- mDisplayName(other.mDisplayName ) {
96+ mDisplayName(other.mDisplayName ),
97+ mDomainName(other.mDomainName ) {
7398 // Release the data pointer from the source object so that the destructor
7499 // does not free the memory multiple times.
75100 other.mEmailAddress = nullptr ;
76101 other.mDisplayName = nullptr ;
102+ other.mDomainName = nullptr ;
77103}
78104
79105// Move assignement operator
80106MessageAddress& MessageAddress::operator =(MessageAddress&& other) noexcept {
81107 if (this != &other) {
82108 delete[] mEmailAddress ;
83109 delete[] mDisplayName ;
110+ delete[] mDomainName ;
84111 // Copy the data pointer and its length from the source object.
85112 mEmailAddress = other.mEmailAddress ;
86113 mDisplayName = other.mDisplayName ;
114+ mDomainName = other.mDomainName ;
87115 // Release the data pointer from the source object so that
88116 // the destructor does not free the memory multiple times.
89117 other.mEmailAddress = nullptr ;
90118 other.mDisplayName = nullptr ;
119+ other.mDomainName = nullptr ;
91120 }
92121 return *this ;
93122}
@@ -116,6 +145,10 @@ const char *MessageAddress::getDisplayName() const {
116145 return mDisplayName ;
117146}
118147
148+ const char *MessageAddress::getDomainName () const {
149+ return mDomainName ;
150+ }
151+
119152bool MessageAddress::isEmailAddressValid (const std::string &pEmailAddress) const {
120153 std::regex emailPattern (" ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+(\\ .[a-zA-Z0-9-]+)*$" );
121154 return regex_match (StringUtils::toLower (pEmailAddress), emailPattern);
0 commit comments