mpd/src/io/PeekReader.cxx

44 lines
857 B
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
2014-08-11 20:57:14 +02:00
#include "PeekReader.hxx"
#include <algorithm>
#include <cassert>
2014-08-11 20:57:14 +02:00
#include <string.h>
const void *
PeekReader::Peek(size_t size)
2014-08-11 20:57:14 +02:00
{
assert(size > 0);
assert(size < sizeof(buffer));
assert(buffer_size == 0);
assert(buffer_position == 0);
do {
size_t nbytes = next.Read(buffer + buffer_size,
size - buffer_size);
2014-08-11 20:57:14 +02:00
if (nbytes == 0)
return nullptr;
buffer_size += nbytes;
} while (buffer_size < size);
return buffer;
}
size_t
PeekReader::Read(void *data, size_t size)
2014-08-11 20:57:14 +02:00
{
size_t buffer_remaining = buffer_size - buffer_position;
if (buffer_remaining > 0) {
size_t nbytes = std::min(buffer_remaining, size);
memcpy(data, buffer + buffer_position, nbytes);
buffer_position += nbytes;
return nbytes;
}
return next.Read(data, size);
2014-08-11 20:57:14 +02:00
}