Commit 48786ae5 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont
Browse files

stream: provide a common implementation of stream_Tell()

parent 79b36fbb
......@@ -142,6 +142,13 @@ VLC_API ssize_t stream_Read(stream_t *, void *, size_t) VLC_USED;
*/
VLC_API ssize_t stream_Peek(stream_t *, const uint8_t **, size_t) VLC_USED;
/**
* Tells the current stream position.
*
* @return the byte offset from the beginning of the stream (cannot fail)
*/
VLC_API uint64_t stream_Tell(const stream_t *) VLC_USED;
VLC_API int stream_vaControl( stream_t *s, int i_query, va_list args );
VLC_API void stream_Delete( stream_t *s );
VLC_API int stream_Control( stream_t *s, int i_query, ... );
......@@ -149,18 +156,6 @@ VLC_API block_t * stream_Block( stream_t *s, size_t );
VLC_API char * stream_ReadLine( stream_t * );
VLC_API input_item_t *stream_ReadDir( stream_t * );
/**
* Get the current position in a stream
*/
static inline int64_t stream_Tell( stream_t *s )
{
uint64_t i_pos;
stream_Control( s, STREAM_GET_POSITION, &i_pos );
if( i_pos >> 62 )
return (int64_t)1 << 62;
return i_pos;
}
/**
* Get the size of the stream.
*/
......
......@@ -45,6 +45,7 @@ typedef struct stream_priv_t
{
stream_t stream;
block_t *peek;
uint64_t offset;
/* UTF-16 and UTF-32 file reading */
struct {
......@@ -75,6 +76,7 @@ stream_t *stream_CommonNew(vlc_object_t *parent)
s->pf_destroy = NULL;
s->p_input = NULL;
priv->peek = NULL;
priv->offset = 0;
/* UTF16 and UTF32 text file conversion */
priv->text.conv = (vlc_iconv_t)(-1);
......@@ -313,6 +315,7 @@ error:
static ssize_t stream_ReadRaw(stream_t *s, void *buf, size_t len)
{
stream_priv_t *priv = (stream_priv_t *)s;
size_t copy = 0;
ssize_t ret = 0;
......@@ -333,6 +336,7 @@ static ssize_t stream_ReadRaw(stream_t *s, void *buf, size_t len)
buf = (unsigned char *)buf + ret;
len -= ret;
copy += ret;
priv->offset += ret;
}
return (copy > 0) ? (ssize_t)copy : ret;
......@@ -430,6 +434,20 @@ ssize_t stream_Peek(stream_t *s, const uint8_t **restrict bufp, size_t len)
return len;
}
uint64_t stream_Tell(const stream_t *s)
{
const stream_priv_t *priv = (const stream_priv_t *)s;
uint64_t pos = priv->offset;
if (priv->peek != NULL)
{
assert(pos >= priv->peek->i_buffer);
pos -= priv->peek->i_buffer;
}
return pos;
}
static int stream_ControlInternal(stream_t *s, int cmd, ...)
{
va_list ap;
......
......@@ -403,6 +403,7 @@ stream_MemoryNew
stream_Peek
stream_Read
stream_ReadLine
stream_Tell
stream_UrlNew
stream_vaControl
stream_ReadDir
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment