istream.c revision c06f4017027263cf3a08becc551f5126409e2a83
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2002-2003 Timo Sirainen */
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen _io_stream_ref(&stream->real_stream->iostream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = (*stream)->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen _io_stream_unref(&(*stream)->real_stream->iostream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen _io_stream_close(&stream->real_stream->iostream);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_set_max_buffer_size(struct istream *stream, size_t max_size)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen _io_stream_set_max_buffer_size(&stream->real_stream->iostream,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_skip(struct istream *stream, uoff_t count)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* within buffer */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* have to seek forward */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen _stream->seek(_stream, stream->v_offset + count, FALSE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_seek(struct istream *stream, uoff_t v_offset)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_skip(stream, v_offset - stream->v_offset);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid i_stream_seek_mark(struct istream *stream, uoff_t v_offset)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenconst struct stat *i_stream_stat(struct istream *stream, bool exact)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenbool i_stream_have_bytes_left(struct istream *stream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return !stream->eof || _stream->skip != _stream->pos;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic char *i_stream_next_line_finish(struct _istream *stream, size_t i)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* modify the buffer directly */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = (char *)stream->w_buffer + stream->skip;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* use a temporary string to return it */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen stream->line_str = str_new(default_pool, 256);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen str_append_n(stream->line_str, stream->buffer + stream->skip,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenchar *i_stream_next_line(struct istream *stream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_error("i_stream_next_line() called for unmodifiable stream");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* @UNSAFE */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = _stream->skip; i < _stream->pos; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret_buf = i_stream_next_line_finish(_stream, i);
d22301419109ed4a38351715e6760011421dadecTimo Sirainenchar *i_stream_read_next_line(struct istream *stream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenconst unsigned char *i_stream_get_data(struct istream *stream, size_t *size_r)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenunsigned char *i_stream_get_modifiable_data(struct istream *stream,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct _istream *_stream = stream->real_stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (_stream->skip >= _stream->pos || _stream->w_buffer == NULL) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint i_stream_read_data(struct istream *stream, const unsigned char **data_r,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* we need more data */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } while (ret > 0);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct istream *_i_stream_create(struct _istream *_stream, pool_t pool, int fd,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen memset(&_stream->statbuf, 0, sizeof(_stream->statbuf));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* gcc istream.c -o teststream liblib.a -Wall -DHAVE_CONFIG_H -DSTREAM_TEST -g */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void check_buffer(const unsigned char *data, size_t size, size_t offset)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < size; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const unsigned char *data;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen fd1 = open("teststream.1", O_RDWR | O_CREAT | O_TRUNC, 0600);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen fd2 = open("teststream.2", O_RDWR | O_CREAT | O_TRUNC, 0600);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen /* write initial data */
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen for (i = 0; i < sizeof(buf); i++)
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen /* test reading */
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen input = i_stream_create_file(fd1, default_pool, 512, FALSE);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(i_stream_get_size(input) == sizeof(buf));
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(i_stream_read_data(input, &data, &size, 512) == -2);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen /* test moving data */
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen output1 = o_stream_create_file(fd1, default_pool, 512, FALSE);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen output2 = o_stream_create_file(fd2, default_pool, 512, FALSE);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_stream_seek(input, 1); size = sizeof(buf)-1;
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(o_stream_send_istream(output2, input) == size);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(read(fd2, buf, sizeof(buf)) == size);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(o_stream_send_istream(output1, input) == sizeof(buf));
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen /* test moving with limits */
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen l_input = i_stream_create_limit(default_pool, input,
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(o_stream_send_istream(output1, l_input) == 512);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_stream_set_max_buffer_size(input, sizeof(buf));
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(i_stream_read_data(input, &data, &size, sizeof(buf)-1) > 0);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen /* reading within limits */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(i_stream_read_data(l_input, &data, &size, 511) > 0);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen i_assert(i_stream_read_data(l_input, &data, &size, 512) == -2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(i_stream_read_data(l_input, &data, &size, 0) > 0);