istream.c revision 51e1a1c280ccb461a15827f7987d09cb9708b6e3
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2002-2008 Dovecot authors, see the included COPYING file */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen io_stream_ref(&stream->real_stream->iostream);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct istream_private *_stream = (*stream)->real_stream;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen io_stream_unref(&(*stream)->real_stream->iostream);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenvoid i_stream_set_destroy_callback(struct istream *stream,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct iostream_private *iostream = &stream->real_stream->iostream;
c57776c06ec99ba9b0dafdbf9475ea72ea8ca134Timo Sirainen struct istream_private *_stream = stream->real_stream;
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen io_stream_close(&stream->real_stream->iostream);
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainenvoid i_stream_set_max_buffer_size(struct istream *stream, size_t max_size)
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen io_stream_set_max_buffer_size(&stream->real_stream->iostream, max_size);
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainenvoid i_stream_set_return_partial_line(struct istream *stream, bool set)
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainen struct istream_private *_stream = stream->real_stream;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen /* error handling should be easier if we now just
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen assume the stream is now at EOF */
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainenvoid i_stream_skip(struct istream *stream, uoff_t count)
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen struct istream_private *_stream = stream->real_stream;
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen /* within buffer */
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen /* have to seek forward */
0cc875d28852d15005cff2a77252e7836c862a01Timo Sirainen _stream->seek(_stream, stream->v_offset + count, FALSE);
0cc875d28852d15005cff2a77252e7836c862a01Timo Sirainenstatic bool i_stream_can_optimize_seek(struct istream *stream)
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen /* use the fast route only if the parent stream is at the
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen expected offset */
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen expected_offset = stream->real_stream->parent_start_offset +
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen if (stream->real_stream->parent->v_offset != expected_offset)
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen return i_stream_can_optimize_seek(stream->real_stream->parent);
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainenvoid i_stream_seek(struct istream *stream, uoff_t v_offset)
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen struct istream_private *_stream = stream->real_stream;
966cb0c1aa58578339cea6f79b4a423a851ab074Timo Sirainen i_stream_skip(stream, v_offset - stream->v_offset);
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainenvoid i_stream_seek_mark(struct istream *stream, uoff_t v_offset)
966cb0c1aa58578339cea6f79b4a423a851ab074Timo Sirainen struct istream_private *_stream = stream->real_stream;
21c317a20c4c3784b54fb3e90ee3751870afdcc3Timo Sirainen struct istream_private *_stream = stream->real_stream;
4ac2e38bdb450d13b62be41638b12df9e0658009Stephan Boschconst struct stat *i_stream_stat(struct istream *stream, bool exact)
4ac2e38bdb450d13b62be41638b12df9e0658009Stephan Bosch struct istream_private *_stream = stream->real_stream;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenbool i_stream_have_bytes_left(const struct istream *stream)
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen const struct istream_private *_stream = stream->real_stream;
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen return !stream->eof || _stream->skip != _stream->pos;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic char *i_stream_next_line_finish(struct istream_private *stream, size_t i)
e9e2d23e1ea5a149a7d8828d2a45b9f2313c3785Timo Sirainen /* modify the buffer directly */
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen ret = (char *)stream->w_buffer + stream->skip;
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen /* use a temporary string to return it */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen stream->line_str = str_new(default_pool, 256);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen str_append_n(stream->line_str, stream->buffer + stream->skip,
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainenstatic char *i_stream_last_line(struct istream_private *_stream)
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen if (_stream->istream.eof && _stream->skip != _stream->pos &&
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen /* the last line is missing LF and we want to return it. */
e9e2d23e1ea5a149a7d8828d2a45b9f2313c3785Timo Sirainen return i_stream_next_line_finish(_stream, _stream->pos);
f97cf1c086715db87094bc3d0a4fefdd80bd869cTimo Sirainenchar *i_stream_next_line(struct istream *stream)
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen struct istream_private *_stream = stream->real_stream;
size_t i;
return NULL;
return NULL;
return ret_buf;
char *line;
return line;
*size_r = 0;
return NULL;
*size_r = 0;
return NULL;
if (ret > 0)
} while (ret > 0);
if (ret == 0) {
if (read_more) {
return FALSE;
return TRUE;
static const struct stat *
struct istream *
#ifdef STREAM_TEST
/* gcc istream.c -o teststream liblib.a -Wall -DHAVE_CONFIG_H -DSTREAM_TEST -g */
#include <fcntl.h>
#include <unistd.h>
#include "ostream.h"
size_t i;
for (i = 0; i < size; i++)
int main(void)
const unsigned char *data;
lib_init();
if (fd1 < 0)
if (fd2 < 0)
for (i = 0; i < sizeof(buf); i++)