Commit de5a84ae de5a84ae4a3a5c8f8ffa096dcd0cb40ddec9f217 by Alain Magloire

attribute.c attribute.h attribute0.h header.c header.h

 	header0.h io.c io.h io0.h mailbox.c mailbox0.h mbx_unix.c
 	message.c message.h message0.h rfc822.c

message_t is a pure interface.
1 parent c77752c0
......@@ -22,7 +22,7 @@
#include <errno.h>
int
attribute_init (attribute_t *pattr)
attribute_init (attribute_t *pattr, void *owner)
{
attribute_t attr;
if (pattr == NULL)
......@@ -30,19 +30,24 @@ attribute_init (attribute_t *pattr)
attr = calloc (1, sizeof(*attr));
if (attr == NULL)
return ENOMEM;
attr->owner = owner;
*pattr = attr;
return 0;
}
void
attribute_destroy (attribute_t *pattr)
attribute_destroy (attribute_t *pattr, void *owner)
{
if (pattr && *pattr)
{
attribute_t attr = *pattr;
/* no owner we really can free it */
if (! attr->message)
free (*pattr);
attr->ref_count--;
if ((attr->owner && attr->owner == owner) ||
(attr->owner == NULL && attr->ref_count <= 0))
{
free (attr);
}
/* loose the link */
*pattr = NULL;
}
......@@ -249,20 +254,11 @@ attribute_copy (attribute_t dest, attribute_t src)
}
int
attribute_set_owner (attribute_t attr, message_t *msg)
{
if (attr == NULL)
return EINVAL;
attr->message = msg;
return 0;
}
int
attribute_get_owner (attribute_t attr, message_t *msg)
attribute_get_owner (attribute_t attr, void **powner)
{
if (attr == NULL)
return EINVAL;
if (msg)
*msg = attr->message;
if (powner)
*powner = attr->owner;
return 0;
}
......
......@@ -33,8 +33,8 @@ extern "C" {
struct _attribute;
typedef struct _attribute * attribute_t;
extern int attribute_init __P ((attribute_t *));
extern void attribute_destroy __P ((attribute_t *));
extern int attribute_init __P ((attribute_t *, void *owner));
extern void attribute_destroy __P ((attribute_t *, void *owner));
extern int attribute_is_seen __P ((attribute_t));
extern int attribute_is_answered __P ((attribute_t));
......
......@@ -46,13 +46,13 @@ extern "C" {
struct _attribute
{
size_t flag;
void *message;
void *owner;
int ref_count;
};
/* not user visible ?? */
extern int attribute_copy __P ((attribute_t dst, attribute_t src));
extern int attribute_set_owner __P ((attribute_t attr, message_t *msg));
extern int attribute_get_owner __P ((attribute_t attr, message_t *msg));
extern int attribute_get_owner __P ((attribute_t attr, void **owner));
#ifdef __cplusplus
}
......
......@@ -26,18 +26,19 @@
#include <errno.h>
int
header_init (header_t *ph, const char *blurb, size_t len, int flag)
header_init (header_t *ph, const char *blurb, size_t len,
int flag, void *owner)
{
if (flag != MU_HEADER_RFC822)
return ENOTSUP;
return rfc822_init (ph, blurb, len);
return rfc822_init (ph, blurb, len, owner);
}
void
header_destroy (header_t *ph)
header_destroy (header_t *ph, void *owner)
{
if (ph && *ph)
(*ph)->_destroy (ph);
(*ph)->_destroy (ph, owner);
}
/* stub functions */
......
......@@ -69,8 +69,8 @@ struct _header;
typedef struct _header * header_t;
extern int header_init __P ((header_t *, const char *blurb,
size_t ln, int flag));
extern void header_destroy __P ((header_t *));
size_t ln, int flag, void *owner));
extern void header_destroy __P ((header_t *, void *owner));
extern int header_set_value __P ((header_t, const char *fn,
const char *fv, size_t n, int replace));
......
......@@ -48,15 +48,17 @@ struct _header
size_t num;
/* Data */
void *data;
/* owner ? */
void *message;
/* streams */
istream_t is;
ostream_t os;
/* owner ? */
void *owner;
int ref_count;
/* Functions */
int (*_init) __P ((header_t *, const char *, size_t));
void (*_destroy) __P ((header_t *));
int (*_init) __P ((header_t *, const char *, size_t, void *owner));
void (*_destroy) __P ((header_t *, void *owner));
int (*_set_value) __P ((header_t, const char *fn, const char *fv,
size_t n, int replace));
int (*_get_value) __P ((header_t, const char *fn, char *fv,
......@@ -72,8 +74,9 @@ struct _header
};
/* rfc822 */
extern int rfc822_init __P ((header_t *ph, const char *blurb, size_t len));
extern void rfc822_destroy __P ((header_t *ph));
extern int rfc822_init __P ((header_t *ph, const char *blurb,
size_t len, void *owner));
extern void rfc822_destroy __P ((header_t *ph, void *owner));
#ifdef _cpluscplus
}
......
......@@ -22,7 +22,9 @@
#include <stdio.h>
int
istream_init (istream_t *pis)
istream_init (istream_t *pis, int (*_read)
__P ((istream_t, char *, size_t, off_t, ssize_t *)),
void *owner)
{
istream_t is;
if (pis == NULL)
......@@ -30,12 +32,16 @@ istream_init (istream_t *pis)
is = calloc (1, sizeof (*is));
if (is == NULL)
return ENOMEM;
is->owner = owner;
is->_read = _read;
*pis = is;
return 0;
}
int
ostream_init (ostream_t *pos)
ostream_init (ostream_t *pos, int (*_write)
__P ((ostream_t, const char *, size_t, off_t, ssize_t *)),
void *owner)
{
ostream_t os;
if (pos == NULL)
......@@ -43,60 +49,54 @@ ostream_init (ostream_t *pos)
os = calloc (1, sizeof (*os));
if (os == NULL)
return ENOMEM;
os->owner = owner;
os->_write = _write;
*pos = os;
return 0;
}
void
istream_destroy (istream_t *pis)
istream_destroy (istream_t *pis, void *owner)
{
free (pis);
if (pis && *pis)
{
istream_t is = *pis;
is->ref_count--;
if ((is->owner && is->owner == owner) ||
(is->owner == NULL && is->ref_count <= 0))
free (pis);
*pis = NULL;
}
}
void
ostream_destroy (ostream_t *pos)
ostream_destroy (ostream_t *pos, void *owner)
{
free (pos);
if (pos && (*pos))
{
ostream_t os = *pos;
os->ref_count--;
if ((os->owner && os->owner == owner) ||
(os->owner == NULL && os->ref_count <= 0))
free (*pos);
*pos = NULL;
}
}
int
istream_set_read (istream_t is, ssize_t (*read)
__P ((istream_t, char *, size_t, off_t)))
istream_read (istream_t is, char *buf, size_t count,
off_t offset, ssize_t *pnread)
{
if (is == NULL)
if (is == NULL || is->_read == NULL)
return EINVAL;
is->_read = read;
return 0;
return is->_read (is, buf, count, offset, pnread);
}
int
ostream_set_write (ostream_t os, ssize_t (*write)
__P ((ostream_t, const char *, size_t, off_t)))
{
if (os == NULL)
return EINVAL;
os->_write = write;
return 0;
}
ssize_t
istream_read (istream_t is, char *buf, size_t count, off_t offset)
{
if (is == NULL || is->_read == NULL)
{
errno = EINVAL;
return -1;
}
return is->_read (is, buf, count, offset);
}
ssize_t
ostream_write (ostream_t os, const char *buf, size_t count, off_t offset)
ostream_write (ostream_t os, const char *buf, size_t count,
off_t offset, ssize_t *pnwrite)
{
if (os == NULL || os->_write == NULL)
{
errno = EINVAL;
return -1;
}
return os->_write (os, buf, count, offset);
return EINVAL;
return os->_write (os, buf, count, offset, pnwrite);
}
......
......@@ -33,13 +33,14 @@ extern "C" {
#endif /*__P */
struct _istream;
typedef struct _istream * istream_t;
typedef struct _istream *istream_t;
struct _ostream;
typedef struct _ostream * ostream_t;
typedef struct _ostream *ostream_t;
extern ssize_t istream_read __P ((istream_t, char *, size_t, off_t));
extern int istream_read __P ((istream_t, char *, size_t, off_t, ssize_t *));
extern ssize_t ostream_write __P ((ostream_t, const char *, size_t, off_t));
extern int ostream_write __P ((ostream_t, const char *, size_t,
off_t, ssize_t *));
#ifdef __cplusplus
}
......
......@@ -36,20 +36,28 @@ struct _istream
{
/* owner of the stream can not be a specific type */
void *owner;
ssize_t (*_read) __P ((istream_t, char *, size_t, off_t));
int ref_count;
int (*_read) __P ((istream_t, char *, size_t, off_t, ssize_t *));
};
struct _ostream
{
/* owner of the stream can not be a specific type */
void *owner;
ssize_t (*_write) __P ((ostream_t, const char *, size_t, off_t));
int ref_count;
int (*_write) __P ((ostream_t, const char *, size_t, off_t, ssize_t *));
};
extern int istream_init __P ((istream_t *));
extern void istream_destroy __P ((istream_t *));
extern int ostream_init __P ((ostream_t *));
extern void ostream_destroy __P ((ostream_t *));
extern int istream_init __P ((istream_t *,
int (*_read) __P ((istream_t, char *,
size_t, off_t, ssize_t *)),
void *owner));
extern void istream_destroy __P ((istream_t *, void *owner));
extern int ostream_init __P ((ostream_t *,
int (*_write) __P ((ostream_t, const char *,
size_t, off_t, ssize_t *)),
void *owner));
extern void ostream_destroy __P ((ostream_t *, void *owner));
#ifdef __cplusplus
}
......
......@@ -112,17 +112,9 @@ mailbox_append_message (mailbox_t mbox, message_t msg)
int
mailbox_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
{
int status;
if (mbox == NULL || pmsg == NULL)
if (mbox == NULL || mbox->_get_message == NULL)
return EINVAL;
if (mailbox_is_deleted (mbox, msgno))
return EINVAL;
status = message_init (pmsg);
if (status != 0)
return status;
(*pmsg)->mailbox = mbox;
(*pmsg)->num = msgno;
return 0;
return mbox->_get_message (mbox, msgno, pmsg);
}
int
......@@ -194,24 +186,6 @@ mailbox_get_size (mailbox_t mbox, size_t msgno, size_t *h, size_t *b)
return mbox->_get_size (mbox, msgno, h, b);
}
ssize_t
mailbox_get_header (mailbox_t mbox, size_t msgno, char *h,
size_t len, off_t off, int *err)
{
if (mbox == NULL || mbox->_get_header == NULL)
return ENOSYS;
return mbox->_get_header (mbox, msgno, h, len, off, err);
}
ssize_t
mailbox_get_body (mailbox_t mbox, size_t msgno, char *b,
size_t len, off_t off, int *err)
{
if (mbox == NULL || mbox->_get_body == NULL)
return ENOSYS;
return mbox->_get_body (mbox, msgno, b, len, off, err);
}
/* locking */
int
mailbox_set_locker (mailbox_t mbox, locker_t locker)
......@@ -251,18 +225,3 @@ mailbox_get_auth (mailbox_t mbox, auth_t *pauth)
return 0;
}
int
mailbox_get_attribute (mailbox_t mbox, size_t msgno, attribute_t *pattr)
{
if (mbox == NULL || mbox->_get_attribute == NULL)
return ENOSYS;
return mbox->_get_attribute (mbox, msgno, pattr);
}
int
mailbox_set_attribute (mailbox_t mbox, size_t msgno, attribute_t attr)
{
if (mbox == NULL || mbox->_set_attribute == NULL)
return ENOSYS;
return mbox->_set_attribute (mbox, msgno, attr);
}
......
......@@ -38,17 +38,10 @@ extern "C" {
struct _mailbox
{
/* Data */
char *name;
FILE *file;
message_t *messages;
size_t messages_num;
off_t size;
auth_t auth;
locker_t locker;
url_t url;
int (*_progress) __P ((int, void *arg));
void *progress_arg;
......@@ -80,15 +73,6 @@ struct _mailbox
int (*_num_deleted) __P ((mailbox_t, size_t *));
int (*_get_size) __P ((mailbox_t, size_t msgno,
size_t *h, size_t *b));
ssize_t (*_get_header) __P ((mailbox_t, size_t msgno, char *h,
size_t len, off_t off, int *err));
ssize_t (*_get_body) __P ((mailbox_t, size_t msgno, char *b,
size_t len, off_t off, int *err));
int (*_get_attribute) __P ((mailbox_t mbox, size_t msgno,
attribute_t *attr));
int (*_set_attribute) __P ((mailbox_t mbox, size_t msgno,
attribute_t attr));
};
/* private */
......
......@@ -40,23 +40,23 @@ extern "C" {
struct _message;
typedef struct _message *message_t;
extern int message_init __P ((message_t *));
extern void message_destroy __P ((message_t *));
extern int message_init __P ((message_t *, void *owner));
extern void message_destroy __P ((message_t *, void *owner));
extern int message_get_header __P ((message_t, header_t *));
extern int message_set_header __P ((message_t, header_t));
extern int message_set_header __P ((message_t, header_t, void *owner));
extern int message_get_istream __P ((message_t, istream_t *));
extern int message_set_istream __P ((message_t, istream_t *));
extern int message_set_istream __P ((message_t, istream_t, void *owner));
extern int message_get_ostream __P ((message_t, ostream_t *));
extern int message_set_ostream __P ((message_t, ostream_t *));
extern int message_set_ostream __P ((message_t, ostream_t, void *owner));
extern int message_is_multipart __P ((message_t));
extern int message_get_size __P ((message_t, size_t *));
extern int message_get_attribute __P ((message_t, attribute_t *));
extern int message_set_attribute __P ((message_t, attribute_t));
extern int message_set_attribute __P ((message_t, attribute_t, void *owner));
extern int message_clone __P ((message_t, message_t *));
......
......@@ -42,27 +42,27 @@ typedef struct _body * body_t;
/* forward declaration */
struct _message
{
/* whos is the owner, only mailbox can own messages */
mailbox_t mailbox;
header_t header;
istream_t is;
ostream_t os;
body_t body;
size_t num;
attribute_t attribute;
char *header_buffer;
off_t header_offset;
size_t num;
/* who is the owner */
void *owner;
int ref_count;
int (*_get_header) __P ((message_t msg, header_t *hdr));
int (*_set_header) __P ((message_t msg, header_t hdr));
int (*_set_header) __P ((message_t msg, header_t hdr, void *owner));
int (*_get_attribute) __P ((message_t msg, attribute_t *attr));
int (*_set_attribute) __P ((message_t msg, attribute_t attr));
int (*_set_attribute) __P ((message_t msg, attribute_t attr, void *owner));
int (*_get_istream) __P ((message_t msg, istream_t *));
int (*_set_istream) __P ((message_t msg, istream_t));
int (*_set_istream) __P ((message_t msg, istream_t, void *owner));
int (*_get_ostream) __P ((message_t msg, ostream_t *));
int (*_set_ostream) __P ((message_t msg, ostream_t));
int (*_set_ostream) __P ((message_t msg, ostream_t, void *owner));
int (*_size) __P ((message_t msg, size_t *size));
......
......@@ -37,7 +37,8 @@ static int rfc822_entry_value (header_t h, size_t num, char *buf,
size_t buflen, size_t *total);
static int rfc822_get_istream (header_t h, istream_t *pis);
static int rfc822_get_ostream (header_t h, ostream_t *pos);
static ssize_t rfc822_read (istream_t is, char *buf, size_t buflen, off_t off);
static int rfc822_read (istream_t is, char *buf, size_t buflen,
off_t off, ssize_t *pnread);
struct _rfc822
{
......@@ -51,13 +52,14 @@ typedef struct _rfc822 * rfc822_t;
int
rfc822_init (header_t *ph, const char *blurb, size_t len)
rfc822_init (header_t *ph, const char *blurb, size_t len, void *owner)
{
header_t h;
int status;
h = calloc (1, sizeof (*h));
if (h == NULL)
return ENOMEM;
h->owner = owner;
h->_init = rfc822_init;
h->_destroy = rfc822_destroy;
h->_parse = rfc822_parse;
......@@ -77,37 +79,32 @@ rfc822_init (header_t *ph, const char *blurb, size_t len)
}
void
rfc822_destroy (header_t *ph)
rfc822_destroy (header_t *ph, void *owner)
{
if (ph && *ph)
{
header_t h = *ph;
/* own by a message */
if (h->message)
{
*ph = NULL;
return;
}
/* is the istream own by us */
if (h->is && h->is->owner == h)
{
h->is->owner = NULL;
istream_destroy (&(h->is));
}
/* is the ostream own by us */
if (h->os && h->os->owner == h)
{
h->os->owner = NULL;
ostream_destroy (&(h->os));
}
if (h->data)
/* if destroy is call always decremente */
h->ref_count--;
/* can we destroy ? */
if ((h->owner && h->owner == owner) ||
(h->owner == NULL && h->ref_count <= 0))
{
rfc822_t rfc = (rfc822_t)h->data;
free (rfc->hdr);
free (rfc->blurb);
free (rfc);
/* io */
istream_destroy (&(h->is), owner);
ostream_destroy (&(h->os), owner);
if (h->data)
{
rfc822_t rfc = (rfc822_t)h->data;
free (rfc->hdr);
free (rfc->blurb);
free (rfc);
}
free (h);
}
free (h);
*ph = NULL;
}
}
......@@ -342,8 +339,9 @@ rfc822_entry_value (header_t h, size_t num, char *buf,
return 0;
}
static ssize_t
rfc822_read (istream_t is, char *buf, size_t buflen, off_t off)
static int
rfc822_read (istream_t is, char *buf, size_t buflen,
off_t off, ssize_t *pnread)
{
header_t h;
rfc822_t rfc = NULL;
......@@ -351,10 +349,7 @@ rfc822_read (istream_t is, char *buf, size_t buflen, off_t off)
if (is == NULL || (h = (header_t)is->owner) == NULL ||
(rfc = (rfc822_t)h->data) == NULL)
{
errno = EINVAL;
return -1;
}
return EINVAL;
len = rfc->blurb_len - off;
if ((rfc->blurb_len - off) > 0)
......@@ -368,7 +363,9 @@ rfc822_read (istream_t is, char *buf, size_t buflen, off_t off)
else
len = 0;
return len;
if (pnread)
*pnread = len;
return 0;
}
int
......@@ -381,7 +378,7 @@ rfc822_get_istream (header_t h, istream_t *pis)
if (h->is)
*pis = h->is;
err = istream_init (&(h->is));
err = istream_init (&(h->is), rfc822_read, h->owner);
if (err != 0)
return err;
/* tell the world this is ours */
......