Skip to content

Commit

Permalink
email_lib: add MAIL::to_str
Browse files Browse the repository at this point in the history
  • Loading branch information
fcneuf committed Aug 30, 2024
1 parent a07be3c commit 8358916
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/gromox/mail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct GX_EXPORT MAIL {
bool serialize(STREAM *) const;
bool emit(MIME::write_func, void *) const;
gromox::errno_t to_fd(int) const;
gromox::errno_t to_str(std::string &) const;
ssize_t get_length() const;
MIME *add_head();
MIME *get_head();
Expand Down
16 changes: 16 additions & 0 deletions lib/email/mail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ errno_t MAIL::to_fd(int fd) const
return 0;
}

errno_t MAIL::to_str(std::string &out) const try
{
STREAM st;
if (!serialize(&st))
return ENOMEM;
char *data;
unsigned int size = STREAM_BLOCK_SIZE;
while ((data = static_cast<char *>(st.get_read_buf(&size))) != nullptr) {
out.append(data, size);
size = STREAM_BLOCK_SIZE;
}
return 0;
} catch (const std::bad_alloc &) {
return ENOMEM;
}

/*
* calculate the mail object length in bytes
* @param
Expand Down

0 comments on commit 8358916

Please sign in to comment.