diff --git a/src/SDL_dataqueue.c b/src/SDL_dataqueue.c index db1adaad8..84370fcbc 100644 --- a/src/SDL_dataqueue.c +++ b/src/SDL_dataqueue.c @@ -225,6 +225,31 @@ SDL_WriteToDataQueue(SDL_DataQueue *queue, const void *_data, const size_t _len) return 0; } +size_t +SDL_PeekIntoDataQueue(SDL_DataQueue *queue, void *_buf, const size_t _len) +{ + size_t len = _len; + Uint8 *buf = (Uint8 *) _buf; + Uint8 *ptr = buf; + SDL_DataQueuePacket *packet; + + if (!queue) { + return 0; + } + + for (packet = queue->head; len && packet; packet = packet->next) { + const size_t avail = packet->datalen - packet->startpos; + const size_t cpy = SDL_min(len, avail); + SDL_assert(queue->queued_bytes >= avail); + + SDL_memcpy(ptr, packet->data + packet->startpos, cpy); + ptr += cpy; + len -= cpy; + } + + return (size_t) (ptr - buf); +} + size_t SDL_ReadFromDataQueue(SDL_DataQueue *queue, void *_buf, const size_t _len) { diff --git a/src/SDL_dataqueue.h b/src/SDL_dataqueue.h index a5e3e1c60..8f135c0cf 100644 --- a/src/SDL_dataqueue.h +++ b/src/SDL_dataqueue.h @@ -31,6 +31,7 @@ void SDL_FreeDataQueue(SDL_DataQueue *queue); void SDL_ClearDataQueue(SDL_DataQueue *queue, const size_t slack); int SDL_WriteToDataQueue(SDL_DataQueue *queue, const void *data, const size_t len); size_t SDL_ReadFromDataQueue(SDL_DataQueue *queue, void *buf, const size_t len); +size_t SDL_PeekIntoDataQueue(SDL_DataQueue *queue, void *buf, const size_t len); size_t SDL_CountDataQueue(SDL_DataQueue *queue); /* this sets a section of the data queue aside (possibly allocating memory for it)