istream-data.c revision 411d6baa37f31d90730e90c4a28c43e1974bbe58
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen/* Copyright (c) 2002-2003 Timo Sirainen */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "lib.h"
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen#include "istream-internal.h"
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenstatic void _close(struct _iostream *stream __attr_unused__)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
03f5c621d06d6b6d77a145196c9633a7aa64dc78Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
6449bd276af37b3e0b81a9c47ecd01f39a2cba53Timo Sirainenstatic void _destroy(struct _iostream *stream __attr_unused__)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
daf029d2a627daa39d05507140f385162828172eTimo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenstatic void _set_max_buffer_size(struct _iostream *stream __attr_unused__,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen size_t max_size __attr_unused__)
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen}
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenstatic ssize_t _read(struct _istream *stream __attr_unused__)
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen stream->istream.eof = TRUE;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return -1;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenstatic void _seek(struct _istream *stream, uoff_t v_offset)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen stream->skip = v_offset;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen stream->istream.v_offset = v_offset;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenstatic uoff_t _get_size(struct _istream *stream)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return stream->pos;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenstruct istream *i_stream_create_from_data(pool_t pool, const void *data,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen size_t size)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct _istream *stream;
f923659c0e5298263d80622c99f4dc4132b4675bTimo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream = p_new(pool, struct _istream, 1);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->buffer = data;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->pos = size;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
f923659c0e5298263d80622c99f4dc4132b4675bTimo Sirainen stream->iostream.close = _close;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->iostream.destroy = _destroy;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->iostream.set_max_buffer_size = _set_max_buffer_size;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen stream->read = _read;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->seek = _seek;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->get_size = _get_size;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->istream.seekable = TRUE;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return _i_stream_create(stream, pool, -1, 0);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen}
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen