Re: [mpd-devel] [PATCH 1/6] InputStream: add ReadFull method
On 2015/06/05 16:27, Thomas Guillem wrote: > 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. Merged. Sorry for being so late. ___ mpd-devel mailing list mpd-devel@musicpd.org http://mailman.blarg.de/listinfo/mpd-devel
Re: [mpd-devel] [PATCH 1/6] InputStream: add ReadFull method
On Fri, May 29, 2015, at 22:30, Max Kellermann wrote: > On 2015/05/22 08:47, Thomas Guillem wrote: > > Maybe I should return 0 in case of error like the Read method. > > Or make it a bool return value: true on success (all was read) or > false on everything else. > > I mean, what's the point of this return value being a number of bytes? > The caller wants exactly this number of bytes and nothing else. I agree, I'll send new patches ___ mpd-devel mailing list mpd-devel@musicpd.org http://mailman.blarg.de/listinfo/mpd-devel
[mpd-devel] [PATCH 1/6] 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. --- src/input/InputStream.cxx | 31 ++- src/input/InputStream.hxx | 19 +++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/input/InputStream.cxx b/src/input/InputStream.cxx index 419c4f0..bf6fd19 100644 --- a/src/input/InputStream.cxx +++ b/src/input/InputStream.cxx @@ -133,9 +133,38 @@ InputStream::LockRead(void *ptr, size_t _size, Error &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(); } - diff --git a/src/input/InputStream.hxx b/src/input/InputStream.hxx index 84b4f3f..3733b20 100644 --- a/src/input/InputStream.hxx +++ b/src/input/InputStream.hxx @@ -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 -- 2.1.4 ___ mpd-devel mailing list mpd-devel@musicpd.org http://mailman.blarg.de/listinfo/mpd-devel
Re: [mpd-devel] [PATCH 1/6] InputStream: add ReadFull method
On 2015/05/22 08:47, Thomas Guillem wrote: > Maybe I should return 0 in case of error like the Read method. Or make it a bool return value: true on success (all was read) or false on everything else. I mean, what's the point of this return value being a number of bytes? The caller wants exactly this number of bytes and nothing else. ___ mpd-devel mailing list mpd-devel@musicpd.org http://mailman.blarg.de/listinfo/mpd-devel
Re: [mpd-devel] [PATCH 1/6] InputStream: add ReadFull method
On Thu, May 21, 2015, at 23:48, Max Kellermann wrote: > On 2015/05/21 23:43, Thomas Guillem wrote: > > 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. > > But how will the caller know that an error has occurred? Maybe I should return 0 in case of error like the Read method. ___ mpd-devel mailing list mpd-devel@musicpd.org http://mailman.blarg.de/listinfo/mpd-devel
Re: [mpd-devel] [PATCH 1/6] InputStream: add ReadFull method
On 2015/05/21 23:43, Thomas Guillem wrote: > 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. But how will the caller know that an error has occurred? ___ mpd-devel mailing list mpd-devel@musicpd.org http://mailman.blarg.de/listinfo/mpd-devel
[mpd-devel] [PATCH 1/6] 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. --- src/input/InputStream.cxx | 31 ++- src/input/InputStream.hxx | 21 + 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/input/InputStream.cxx b/src/input/InputStream.cxx index 419c4f0..e58e3ff 100644 --- a/src/input/InputStream.cxx +++ b/src/input/InputStream.cxx @@ -132,10 +132,39 @@ InputStream::LockRead(void *ptr, size_t _size, Error &error) return Read(ptr, _size, error); } +size_t +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 nbytes_total; + + nbytes_total += nbytes; + _size -= nbytes; + } + return nbytes_total; +} + +size_t +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(); } - diff --git a/src/input/InputStream.hxx b/src/input/InputStream.hxx index 84b4f3f..6044c3f 100644 --- a/src/input/InputStream.hxx +++ b/src/input/InputStream.hxx @@ -363,6 +363,27 @@ 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. +* Returns 0 on error +* +* The caller must lock the mutex. +* +* @param ptr the buffer to read into +* @param size the number of bytes to read +* @return the number of bytes read, if this number is less than size, +* then EOF is reached. +*/ + gcc_nonnull_all + size_t 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 + size_t LockReadFull(void *ptr, size_t size, Error &error); }; #endif -- 2.1.4 ___ mpd-devel mailing list mpd-devel@musicpd.org http://mailman.blarg.de/listinfo/mpd-devel