Add version of extract_bytes that extracts into character buffer

This commit is contained in:
rdb 2013-08-23 15:03:01 +00:00
parent 0854cf5aa2
commit 266890c86a
4 changed files with 42 additions and 1 deletions

View File

@ -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

View File

@ -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();

View File

@ -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
}

View File

@ -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;