istream.c revision abe8754852e70763e92f74caabbcc13d0917714c
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2002-2003 Timo Sirainen */
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen _io_stream_ref(&stream->real_stream->iostream);
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen _io_stream_unref(&stream->real_stream->iostream);
507ea0bc5b25efb4c96033a19dec66689a50ebd0Baofeng Wang struct _istream *_stream = stream->real_stream;
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen _io_stream_close(&stream->real_stream->iostream);
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainenvoid i_stream_set_max_buffer_size(struct istream *stream, size_t max_size)
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody _io_stream_set_max_buffer_size(&stream->real_stream->iostream,
b04e76cbc807707d299055be79500f8ff131da43Timo Sirainen struct _istream *_stream = stream->real_stream;
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainenvoid i_stream_skip(struct istream *stream, uoff_t count)
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainen struct _istream *_stream = stream->real_stream;
83172e28d4ac684dfed83f7c9db933493d7c5922Timo Sirainen /* within buffer */
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen /* have to seek forward */
f5c0d5cada4da23a167c38426d0c481a3e1d5583Timo Sirainen _stream->seek(_stream, stream->v_offset + count);
f5c0d5cada4da23a167c38426d0c481a3e1d5583Timo Sirainenvoid i_stream_seek(struct istream *stream, uoff_t v_offset)
f5c0d5cada4da23a167c38426d0c481a3e1d5583Timo Sirainen struct _istream *_stream = stream->real_stream;
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen i_stream_skip(stream, v_offset - stream->v_offset);
8b1a9a4d63b0abccdf7cb1acb8359d5396dd657bTimo Sirainenuoff_t i_stream_get_size(struct istream *stream)
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen struct _istream *_stream = stream->real_stream;
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainenchar *i_stream_next_line(struct istream *stream)
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen struct _istream *_stream = stream->real_stream;
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen i_error("i_stream_next_line() called for unmodifyable stream");
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen /* @UNSAFE */
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen for (i = _stream->skip; i < _stream->pos; i++) {
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainen ret_buf = (char *) _stream->w_buffer + _stream->skip;
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainenchar *i_stream_read_next_line(struct istream *stream)
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainenconst unsigned char *i_stream_get_data(struct istream *stream, size_t *size)
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen struct _istream *_stream = stream->real_stream;
b04e76cbc807707d299055be79500f8ff131da43Timo Sirainenunsigned char *i_stream_get_modifyable_data(struct istream *stream,
0c5854b6891c59c1c3f443569bc823d7db571582Teemu Huovila struct _istream *_stream = stream->real_stream;
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainen if (_stream->skip >= _stream->pos || _stream->w_buffer == NULL) {
83172e28d4ac684dfed83f7c9db933493d7c5922Timo Sirainenint i_stream_read_data(struct istream *stream, const unsigned char **data,
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen /* we need more data */
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila } while (ret > 0);
f5c0d5cada4da23a167c38426d0c481a3e1d5583Timo Sirainenstruct istream *_i_stream_create(struct _istream *_stream, pool_t pool, int fd,
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen/* gcc istream.c -o teststream liblib.a -Wall -DHAVE_CONFIG_H -DSTREAM_TEST -g */
568fec5b1e629f25d288b48007485b9aa4a018b1Timo Sirainenstatic void check_buffer(const unsigned char *data, size_t size, size_t offset)
b04e76cbc807707d299055be79500f8ff131da43Timo Sirainen for (i = 0; i < size; i++)
b6b06530d654f0436bfbaefc1e988d53fff0cbeeTimo Sirainen const unsigned char *data;
83172e28d4ac684dfed83f7c9db933493d7c5922Timo Sirainen fd1 = open("teststream.1", O_RDWR | O_CREAT | O_TRUNC, 0600);
83172e28d4ac684dfed83f7c9db933493d7c5922Timo Sirainen fd2 = open("teststream.2", O_RDWR | O_CREAT | O_TRUNC, 0600);
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen /* write initial data */
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen for (i = 0; i < sizeof(buf); i++)
62fc0b4f07eb6f18a3bff4b1fccb636e6fae3cf4Timo Sirainen /* test reading */
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen input = i_stream_create_file(fd1, default_pool, 512, FALSE);
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen i_assert(i_stream_get_size(input) == sizeof(buf));
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
c865b0e9c65fd77f7b2ab6f8616d3def5501ecb3Timo Sirainen i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(i_stream_read_data(input, &data, &size, 512) == -2);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody /* test moving data */
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila output1 = o_stream_create_file(fd1, default_pool, 512, FALSE);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila output2 = o_stream_create_file(fd2, default_pool, 512, FALSE);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_stream_seek(input, 1); size = sizeof(buf)-1;
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(o_stream_send_istream(output2, input) == size);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(read(fd2, buf, sizeof(buf)) == size);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(o_stream_send_istream(output1, input) == sizeof(buf));
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila /* test moving with limits */
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila l_input = i_stream_create_limit(default_pool, input,
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(o_stream_send_istream(output1, l_input) == 512);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_stream_set_max_buffer_size(input, sizeof(buf));
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(i_stream_read_data(input, &data, &size, sizeof(buf)-1) > 0);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila /* reading within limits */
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(i_stream_read_data(l_input, &data, &size, 511) > 0);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(i_stream_read_data(l_input, &data, &size, 512) == -2);
3a54211bd6c4dc3f8687c16020770551cf83a548Teemu Huovila i_assert(i_stream_read_data(l_input, &data, &size, 0) > 0);