Commit c382e9d0 c382e9d0b7b893cb5eb7d6a91bdbd21c1987f934 by Sergey Poznyakoff

Stream improvements.

* include/mailutils/stream.h (mu_stream_printf): Mark as printf-like.
* include/mailutils/sys/stream.h (_MU_STR_EVENT_CLOSE): New event.
(_mu_stream) <event_cb_data>: New member.
* libmailutils/stream.c (mu_stream_seek): Avoid unnecessary seeks
in mu_buffer_none mode.  Complements 906499db.
(mu_stream_close): Mark _MU_STR_EVENT_CLOSE event.
1 parent 30a7e0be
......@@ -140,7 +140,8 @@ int mu_stream_set_flags (mu_stream_t stream, int fl);
int mu_stream_clr_flags (mu_stream_t stream, int fl);
int mu_stream_vprintf (mu_stream_t str, const char *fmt, va_list ap);
int mu_stream_printf (mu_stream_t stream, const char *fmt, ...);
int mu_stream_printf (mu_stream_t stream, const char *fmt, ...)
MU_PRINTFLIKE(2,3);
int mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size,
mu_off_t *pcsz);
......
......@@ -28,6 +28,7 @@
#define _MU_STR_EVENT_CLRFLAG 1
#define _MU_STR_EVENT_FILLBUF 2
#define _MU_STR_EVENT_FLUSHBUF 3
#define _MU_STR_EVENT_CLOSE 4
#define _MU_STR_EVMASK(n) (1<<(n))
......@@ -63,6 +64,7 @@ struct _mu_stream
void (*event_cb) (struct _mu_stream *, int code, unsigned long, void *);
int event_mask;
void *event_cb_data;
const char *(*error_string) (struct _mu_stream *, int);
......
......@@ -375,10 +375,11 @@ mu_stream_seek (mu_stream_t stream, mu_off_t offset, int whence,
return mu_stream_seterr (stream, EINVAL, 1);
}
if ((stream->buftype == mu_buffer_none && offset != stream->offset)
|| stream->level == 0
if (stream->buftype == mu_buffer_none ?
(offset != stream->offset)
: (stream->level == 0
|| offset < stream->offset
|| offset > stream->offset + stream->level)
|| offset > stream->offset + stream->level))
{
if ((rc = _stream_flush_buffer (stream, 1)))
return rc;
......@@ -945,10 +946,12 @@ mu_stream_close (mu_stream_t stream)
if (!stream)
return EINVAL;
mu_stream_flush (stream);
/* Do close the stream only if it is not used by anyone else */
if (stream->ref_count > 1)
return 0;
_stream_event (stream, _MU_STR_EVENT_CLOSE, 0, NULL);
if (stream->close)
rc = stream->close (stream);
return rc;
......