InputStream: add ReadFull method
Convenient method that behave differently than Read, and that will be used by tag scanners. This method will return in case of error, if the whole data is read or is EOF is reached.
This commit is contained in:
parent
90e7ace980
commit
6e2d7445c5
@ -132,10 +132,39 @@ InputStream::LockRead(void *ptr, size_t _size, Error &error)
|
||||
return Read(ptr, _size, error);
|
||||
}
|
||||
|
||||
bool
|
||||
InputStream::ReadFull(void *_ptr, size_t _size, Error &error)
|
||||
{
|
||||
uint8_t *ptr = (uint8_t *)_ptr;
|
||||
|
||||
size_t nbytes_total = 0;
|
||||
while (_size > 0) {
|
||||
size_t nbytes = Read(ptr + nbytes_total, _size, error);
|
||||
if (nbytes == 0)
|
||||
return false;
|
||||
|
||||
nbytes_total += nbytes;
|
||||
_size -= nbytes;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
InputStream::LockReadFull(void *ptr, size_t _size, Error &error)
|
||||
{
|
||||
#if !CLANG_CHECK_VERSION(3,6)
|
||||
/* disabled on clang due to -Wtautological-pointer-compare */
|
||||
assert(ptr != nullptr);
|
||||
#endif
|
||||
assert(_size > 0);
|
||||
|
||||
const ScopeLock protect(mutex);
|
||||
return ReadFull(ptr, _size, error);
|
||||
}
|
||||
|
||||
bool
|
||||
InputStream::LockIsEOF()
|
||||
{
|
||||
const ScopeLock protect(mutex);
|
||||
return IsEOF();
|
||||
}
|
||||
|
||||
|
@ -363,6 +363,25 @@ public:
|
||||
*/
|
||||
gcc_nonnull_all
|
||||
size_t LockRead(void *ptr, size_t size, Error &error);
|
||||
|
||||
/**
|
||||
* Reads the whole data from the stream into the caller-supplied buffer.
|
||||
*
|
||||
* The caller must lock the mutex.
|
||||
*
|
||||
* @param ptr the buffer to read into
|
||||
* @param size the number of bytes to read
|
||||
* @return true if the whole data was read, false otherwise.
|
||||
*/
|
||||
gcc_nonnull_all
|
||||
bool ReadFull(void *ptr, size_t size, Error &error);
|
||||
|
||||
/**
|
||||
* Wrapper for ReadFull() which locks and unlocks the mutex;
|
||||
* the caller must not be holding it already.
|
||||
*/
|
||||
gcc_nonnull_all
|
||||
bool LockReadFull(void *ptr, size_t size, Error &error);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user