diff --git a/dtool/src/prc/streamReader.cxx b/dtool/src/prc/streamReader.cxx index 4cb7ad58f2..d55e1ffca5 100644 --- a/dtool/src/prc/streamReader.cxx +++ b/dtool/src/prc/streamReader.cxx @@ -130,6 +130,24 @@ extract_bytes(size_t size) { return string(buffer, read_bytes); } +//////////////////////////////////////////////////////////////////// +// Function: StreamReader::extract_bytes +// Access: Published +// Description: Extracts the indicated number of bytes in the +// stream into the given character buffer. Assumes +// that the buffer is big enough to hold the requested +// number of bytes. Returns the number of bytes +// that were successfully written. +//////////////////////////////////////////////////////////////////// +size_t StreamReader:: +extract_bytes(char *into, size_t size) { + if (_in->eof() || _in->fail()) { + return 0; + } + + _in->read(into, size); + return _in->gcount(); +} //////////////////////////////////////////////////////////////////// // Function: StreamReader::readline diff --git a/dtool/src/prc/streamReader.h b/dtool/src/prc/streamReader.h index fc3bad6afe..45211df410 100644 --- a/dtool/src/prc/streamReader.h +++ b/dtool/src/prc/streamReader.h @@ -67,6 +67,7 @@ PUBLISHED: BLOCKING void skip_bytes(size_t size); BLOCKING string extract_bytes(size_t size); + BLOCKING size_t extract_bytes(char *into, size_t size); BLOCKING string readline(); diff --git a/panda/src/express/datagramIterator.cxx b/panda/src/express/datagramIterator.cxx index 635bdc6605..0c72f558fc 100644 --- a/panda/src/express/datagramIterator.cxx +++ b/panda/src/express/datagramIterator.cxx @@ -149,6 +149,28 @@ extract_bytes(size_t size) { return string(ptr + last_index, size); } +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::extract_bytes +// Access: Published +// Description: Extracts the indicated number of bytes in the +// datagram into the given character buffer. Assumes +// that the buffer is big enough to hold the requested +// number of bytes. Returns the number of bytes +// that were successfully written. +//////////////////////////////////////////////////////////////////// +size_t DatagramIterator:: +extract_bytes(char *into, size_t size) { + nassertr((int)size >= 0, 0); + nassertr(_datagram != (const Datagram *)NULL, 0); + nassertr(_current_index + size <= _datagram->get_length(), 0); + + const char *ptr = (const char *)_datagram->get_data(); + memcpy(into, ptr + _current_index, size); + + _current_index += size; + return size; +} + //////////////////////////////////////////////////////////////////// // Function : output // Access : Public @@ -184,4 +206,3 @@ write(ostream &out, unsigned int indent) const { #endif //] NDEBUG } - diff --git a/panda/src/express/datagramIterator.h b/panda/src/express/datagramIterator.h index 9b2c51139e..1d9668b9c8 100644 --- a/panda/src/express/datagramIterator.h +++ b/panda/src/express/datagramIterator.h @@ -68,6 +68,7 @@ PUBLISHED: INLINE void skip_bytes(size_t size); string extract_bytes(size_t size); + size_t extract_bytes(char *into, size_t size); INLINE string get_remaining_bytes() const; INLINE int get_remaining_size() const;