Browse Source

Add version of extract_bytes that extracts into character buffer

rdb 12 years ago
parent
commit
266890c86a

+ 18 - 0
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

+ 1 - 0
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();
 

+ 22 - 1
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
 }
 
-

+ 1 - 0
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;