Commit eb9e1ecf eb9e1ecf546af4d96de809e62081062e82e6d692 by Wojciech Polak

Improve libmu_cpp.

* libmu_cpp/address.cc, libmu_cpp/envelope.cc, libmu_cpp/header.cc,
libmu_cpp/url.cc: Use sget instead of aget.
* libmu_cpp/list.cc (mulist_to_stl): New function.
(List::to_stl): New method.
* include/mailutils/cpp/list.h: Likewise.
1 parent b4b7a4a6
......@@ -21,6 +21,7 @@
#ifndef _MUCPP_LIST_H
#define _MUCPP_LIST_H
#include <list>
#include <errno.h>
#include <mailutils/list.h>
#include <mailutils/cpp/error.h>
......@@ -32,6 +33,8 @@ typedef int (*mu_list_comparator_t) (const void*, const void*);
namespace mailutils
{
std::list<void*> mulist_to_stl (mu_list_t mu_list);
class Iterator;
class List
......@@ -71,6 +74,8 @@ class List
size_t count ();
size_t size ();
std::list<void*> to_stl ();
inline void* operator [] (size_t index) {
return this->get (index);
}
......
......@@ -95,8 +95,8 @@ Address :: get_count ()
std::string
Address :: get_email (size_t n)
{
char *buf = NULL;
int status = mu_address_aget_email (addr, n, &buf);
const char* buf = NULL;
int status = mu_address_sget_email (addr, n, &buf);
if (status == EINVAL)
throw Address::EInval ("Address::get_email", status);
else if (status == ENOENT)
......@@ -108,8 +108,8 @@ Address :: get_email (size_t n)
std::string
Address :: get_local_part (size_t n)
{
char *buf = NULL;
int status = mu_address_aget_local_part (addr, n, &buf);
const char* buf = NULL;
int status = mu_address_sget_local_part (addr, n, &buf);
if (status == EINVAL)
throw Address::EInval ("Address::get_local_part", status);
else if (status == ENOENT)
......@@ -121,8 +121,8 @@ Address :: get_local_part (size_t n)
std::string
Address :: get_domain (size_t n)
{
char *buf = NULL;
int status = mu_address_aget_domain (addr, n, &buf);
const char* buf = NULL;
int status = mu_address_sget_domain (addr, n, &buf);
if (status == EINVAL)
throw Address::EInval ("Address::get_domain", status);
else if (status == ENOENT)
......@@ -134,8 +134,8 @@ Address :: get_domain (size_t n)
std::string
Address :: get_personal (size_t n)
{
char *buf = NULL;
int status = mu_address_aget_personal (addr, n, &buf);
const char* buf = NULL;
int status = mu_address_sget_personal (addr, n, &buf);
if (status == EINVAL)
throw Address::EInval ("Address::get_personal", status);
else if (status == ENOENT)
......@@ -147,8 +147,8 @@ Address :: get_personal (size_t n)
std::string
Address :: get_comments (size_t n)
{
char *buf = NULL;
int status = mu_address_aget_comments (addr, n, &buf);
const char* buf = NULL;
int status = mu_address_sget_comments (addr, n, &buf);
if (status == EINVAL)
throw Address::EInval ("Address::get_comments", status);
else if (status == ENOENT)
......@@ -160,8 +160,8 @@ Address :: get_comments (size_t n)
std::string
Address :: get_route (size_t n)
{
char *buf = NULL;
int status = mu_address_aget_route (addr, n, &buf);
const char* buf = NULL;
int status = mu_address_sget_route (addr, n, &buf);
if (status == EINVAL)
throw Address::EInval ("Address::get_route", status);
else if (status == ENOENT)
......
......@@ -42,28 +42,20 @@ Envelope :: Envelope (const mu_envelope_t env)
std::string
Envelope :: get_sender ()
{
char* c_val = NULL;
int status = mu_envelope_aget_sender (env, &c_val);
const char* buf = NULL;
int status = mu_envelope_sget_sender (env, &buf);
if (status)
throw Exception ("Envelope::get_sender", status);
std::string val (c_val);
free (c_val);
return val;
return std::string (buf);
}
std::string
Envelope :: get_date ()
{
char* c_val;
int status = mu_envelope_aget_date (env, &c_val);
const char* buf = NULL;
int status = mu_envelope_sget_date (env, &buf);
if (status)
throw Exception ("Envelope::get_date", status);
std::string val (c_val);
free (c_val);
return val;
return std::string (buf);
}
......
......@@ -42,31 +42,27 @@ Header :: Header (const mu_header_t hdr)
std::string
Header :: get_value (const std::string& name)
{
char* c_val;
const char* buf = NULL;
int status = mu_header_aget_value (hdr, name.c_str (), &c_val);
int status = mu_header_sget_value (hdr, name.c_str (), &buf);
if (status)
throw Exception ("Header::get_value", status);
std::string val (c_val);
free (c_val);
return val;
return std::string (buf);
}
std::string
Header :: get_value (const std::string& name, const std::string& def)
{
char* c_val;
const char* buf = NULL;
int status = mu_header_aget_value (hdr, name.c_str (), &c_val);
int status = mu_header_sget_value (hdr, name.c_str (), &buf);
if (status == MU_ERR_NOENT)
return std::string (def);
else if (status)
throw Exception ("Header::get_value", status);
std::string val (c_val);
free (c_val);
return val;
return std::string (buf);
}
size_t
......
......@@ -22,6 +22,29 @@
using namespace mailutils;
std::list<void*>
mailutils :: mulist_to_stl (mu_list_t mu_list)
{
size_t list_count;
std::list<void *> list;
if (!mu_list)
return list;
int status = mu_list_count (mu_list, &list_count);
if (status)
return list;
for (int i = 0; i < list_count; i++)
{
void *item = NULL;
status = mu_list_get (mu_list, i, &item);
if (!status && item)
list.push_back (item);
}
return list;
}
//
// List
//
......@@ -199,3 +222,9 @@ List :: set_destroy_item (void (*mu_destroy_item) (void *item))
throw Exception ("List::set_destroy_item", status);
}
std::list<void*>
List :: to_stl ()
{
return mulist_to_stl (mu_list);
}
......
......@@ -74,8 +74,8 @@ Url :: get_port ()
std::string
Url :: get_scheme ()
{
char *buf = NULL;
int status = mu_url_aget_scheme (url, &buf);
const char* buf = NULL;
int status = mu_url_sget_scheme (url, &buf);
if (status == MU_ERR_NOENT)
return "";
else if (status)
......@@ -86,8 +86,8 @@ Url :: get_scheme ()
std::string
Url :: get_user ()
{
char *buf = NULL;
int status = mu_url_aget_user (url, &buf);
const char* buf = NULL;
int status = mu_url_sget_user (url, &buf);
if (status == MU_ERR_NOENT)
return "";
else if (status)
......@@ -98,8 +98,8 @@ Url :: get_user ()
std::string
Url :: get_passwd ()
{
char *buf = NULL;
int status = mu_url_aget_passwd (url, &buf);
const char* buf = NULL;
int status = mu_url_sget_passwd (url, &buf);
if (status == MU_ERR_NOENT)
return "";
else if (status)
......@@ -110,8 +110,8 @@ Url :: get_passwd ()
std::string
Url :: get_auth ()
{
char *buf = NULL;
int status = mu_url_aget_auth (url, &buf);
const char* buf = NULL;
int status = mu_url_sget_auth (url, &buf);
if (status == MU_ERR_NOENT)
return "";
else if (status)
......@@ -122,8 +122,8 @@ Url :: get_auth ()
std::string
Url :: get_host ()
{
char *buf = NULL;
int status = mu_url_aget_host (url, &buf);
const char* buf = NULL;
int status = mu_url_sget_host (url, &buf);
if (status == MU_ERR_NOENT)
return "";
else if (status)
......@@ -134,8 +134,8 @@ Url :: get_host ()
std::string
Url :: get_path ()
{
char *buf = NULL;
int status = mu_url_aget_path (url, &buf);
const char* buf = NULL;
int status = mu_url_sget_path (url, &buf);
if (status == MU_ERR_NOENT)
return "";
else if (status)
......