Commit 00668ac3 00668ac3d890df067217835b0ebb127b60902099 by Sergey Poznyakoff

(mu_envelope_get_sender)

(mu_envelope_sget_sender, mu_envelope_aget_sender)
(mu_envelope_get_date, mu_envelope_sget_date)
(mu_envelope_aget_date): New functions
(mu_envelope_sender, mu_envelope_date): Rewrite as macros
1 parent 2c0e6104
......@@ -27,15 +27,26 @@ extern "C" { /*}*/
int mu_envelope_create (mu_envelope_t *, void *);
void mu_envelope_destroy (mu_envelope_t *, void *);
void * mu_envelope_get_owner (mu_envelope_t);
void *mu_envelope_get_owner (mu_envelope_t);
int mu_envelope_get_sender (mu_envelope_t, char *, size_t, size_t *);
int mu_envelope_sget_sender (mu_envelope_t, const char **);
int mu_envelope_aget_sender (mu_envelope_t, char **);
int mu_envelope_get_date (mu_envelope_t, char *, size_t, size_t *);
int mu_envelope_sget_date (mu_envelope_t, const char **);
int mu_envelope_aget_date (mu_envelope_t, char **);
#define mu_envelope_sender mu_envelope_get_sender
#define mu_envelope_date mu_envelope_get_date
int mu_envelope_set_sender (mu_envelope_t,
int (*_sender) (mu_envelope_t, char *, size_t, size_t*),
void *);
int mu_envelope_sender (mu_envelope_t, char *, size_t, size_t *);
int (*_sender) (mu_envelope_t,
char *, size_t, size_t*),
void *);
int mu_envelope_set_date (mu_envelope_t,
int (*_date) (mu_envelope_t, char *, size_t , size_t *),
void *);
int mu_envelope_date (mu_envelope_t, char *, size_t, size_t *);
int (*_date) (mu_envelope_t, char *, size_t ,
size_t *),
void *);
/* mu_strftime format for envelope dates */
#define MU_ENVELOPE_DATE_FORMAT "%a %b %d %H:%M:%S %Y"
......
......@@ -21,6 +21,7 @@
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/errno.h>
#include <envelope0.h>
......@@ -48,6 +49,8 @@ mu_envelope_destroy (mu_envelope_t *penvelope, void *owner)
{
if (envelope->_destroy)
envelope->_destroy (envelope);
free (envelope->date);
free (envelope->sender);
free (envelope);
}
*penvelope = NULL;
......@@ -62,54 +65,115 @@ mu_envelope_get_owner (mu_envelope_t envelope)
int
mu_envelope_set_sender (mu_envelope_t envelope,
int (*_sender) (mu_envelope_t, char *, size_t, size_t*),
void *owner)
int (*_sender) (mu_envelope_t, char *, size_t,
size_t*),
void *owner)
{
if (envelope == NULL)
return EINVAL;
if (envelope->owner != owner)
return EACCES;
envelope->_sender = _sender;
return 0;
}
int
mu_envelope_sender (mu_envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
{
if (envelope == NULL)
return EINVAL;
if (envelope->_sender)
return envelope->_sender (envelope, buf, len, pnwrite);
if (buf && len)
*buf = '\0';
if (pnwrite)
*pnwrite = 0;
envelope->_get_sender = _sender;
return 0;
}
int
mu_envelope_set_date (mu_envelope_t envelope,
int (*_date) (mu_envelope_t, char *, size_t , size_t *),
void *owner)
int (*_date) (mu_envelope_t, char *, size_t , size_t *),
void *owner)
{
if (envelope == NULL)
return EINVAL;
if (envelope->owner != owner)
return EACCES;
envelope->_date = _date;
envelope->_get_date = _date;
return 0;
}
int
mu_envelope_date (mu_envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
{
if (envelope == NULL)
return EINVAL;
if (envelope->_date)
return envelope->_date (envelope, buf, len, pnwrite);
if (buf && len)
*buf = '\0';
if (pnwrite)
*pnwrite = 0;
return 0;
/* General accessors: */
#define AC2(a,b) a ## b
#define AC4(a,b,c,d) a ## b ## c ## d
#define ACCESSOR(action,field) AC4(mu_envelope_,action,_,field)
#define DECL_SGET(field) \
int \
ACCESSOR(sget,field) (mu_envelope_t env, char const **sptr) \
{ \
if (env == NULL) \
return EINVAL; \
if (!env->field) \
{ \
if (env->AC2(_get_,field)) \
{ \
size_t n; \
char *buf; \
int status; \
\
status = env->AC2(_get_,field) (env, NULL, 0, &n); \
if (status) \
return status; \
\
buf = malloc (n + 1); \
if (!buf) \
return ENOMEM; \
\
status = env->AC2(_get_,field) (env, buf, n + 1, NULL); \
if (status) \
return status; \
\
env->field = buf; \
} \
else \
return MU_ERR_NOENT; \
} \
*sptr = env->field; \
return 0; \
}
#define DECL_GET(field) \
int \
ACCESSOR(get,field) (mu_envelope_t env, char *buf, size_t len, size_t *n) \
{ \
size_t i; \
const char *str; \
int status = ACCESSOR(sget, field) (env, &str); \
\
if (status) \
return status; \
\
i = mu_cpystr (buf, str, len); \
if (n) \
*n = i; \
return 0; \
}
#define DECL_AGET(field) \
int \
ACCESSOR(aget, field) (mu_envelope_t env, char **buf) \
{ \
const char *str; \
int status = ACCESSOR(sget, field) (env, &str); \
\
if (status) \
return status; \
\
if (str) \
{ \
*buf = strdup (str); \
if (!*buf) \
status = ENOMEM; \
} \
else \
*buf = NULL; \
return status; \
}
#define DECL_ACCESSORS(field) \
DECL_SGET(field) \
DECL_GET(field) \
DECL_AGET(field)
DECL_ACCESSORS(sender)
DECL_ACCESSORS(date)
......