瀏覽代碼

more interfaces to get out the packed data

David Rose 21 年之前
父節點
當前提交
a5349528e9
共有 3 個文件被更改,包括 46 次插入10 次删除
  1. 3 1
      direct/src/dcparser/dcPackData.I
  2. 40 8
      direct/src/dcparser/dcPacker.I
  3. 3 1
      direct/src/dcparser/dcPacker.h

+ 3 - 1
direct/src/dcparser/dcPackData.I

@@ -160,7 +160,9 @@ get_data() const {
 //               non-NULL.  This may (or may not) return NULL if the
 //               non-NULL.  This may (or may not) return NULL if the
 //               buffer is empty.
 //               buffer is empty.
 //
 //
-//               This also empties the DCPackData structure.
+//               This also empties the DCPackData structure, and sets
+//               its length to zero (so you should call get_length()
+//               before calling this method).
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE char *DCPackData::
 INLINE char *DCPackData::
 take_data() {
 take_data() {

+ 40 - 8
direct/src/dcparser/dcPacker.I

@@ -617,6 +617,18 @@ get_num_unpacked_bytes() const {
   return _unpack_p;
   return _unpack_p;
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: DCPacker::get_length
+//       Access: Published
+//  Description: Returns the current length of the buffer.  This is
+//               the number of useful bytes stored in the buffer, not
+//               the amount of memory it takes up.
+////////////////////////////////////////////////////////////////////
+INLINE size_t DCPacker::
+get_length() const {
+  return _pack_data.get_length();
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: DCPacker::get_string
 //     Function: DCPacker::get_string
 //       Access: Published
 //       Access: Published
@@ -629,15 +641,14 @@ get_string() const {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: DCPacker::get_length
+//     Function: DCPacker::get_string
 //       Access: Published
 //       Access: Published
-//  Description: Returns the current length of the buffer.  This is
-//               the number of useful bytes stored in the buffer, not
-//               the amount of memory it takes up.
+//  Description: Copies the packed data into the indicated string.
+//               Also see get_data().
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-INLINE size_t DCPacker::
-get_length() const {
-  return _pack_data.get_length();
+INLINE void DCPacker::
+get_string(string &data) const {
+  data.assign(_pack_data.get_data(), _pack_data.get_length());
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -647,13 +658,34 @@ get_length() const {
 //               is not null-terminated, but see also get_string().
 //               is not null-terminated, but see also get_string().
 //
 //
 //               This may be used in conjunction with get_length() to
 //               This may be used in conjunction with get_length() to
-//               copy all of the bytes out of the buffer.
+//               copy all of the bytes out of the buffer.  Also see
+//               take_data() to get the packed data without a copy
+//               operation.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE const char *DCPacker::
 INLINE const char *DCPacker::
 get_data() const {
 get_data() const {
   return _pack_data.get_data();
   return _pack_data.get_data();
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: DCPacker::take_data
+//       Access: Public
+//  Description: Returns the pointer to the beginning of the data
+//               buffer, and transfers ownership of the buffer to the
+//               caller.  The caller is now responsible for ultimately
+//               freeing the returned pointer with delete[], if it is
+//               non-NULL.  This may (or may not) return NULL if the
+//               buffer is empty.
+//
+//               This also empties the DCPackData structure, and sets
+//               its length to zero (so you should call get_length()
+//               before calling this method).
+////////////////////////////////////////////////////////////////////
+INLINE char *DCPacker::
+take_data() {
+  return _pack_data.take_data();
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: DCPacker::append_data
 //     Function: DCPacker::append_data
 //       Access: Public
 //       Access: Public

+ 3 - 1
direct/src/dcparser/dcPacker.h

@@ -122,10 +122,12 @@ PUBLISHED:
   INLINE bool had_error() const;
   INLINE bool had_error() const;
   INLINE size_t get_num_unpacked_bytes() const;
   INLINE size_t get_num_unpacked_bytes() const;
 
 
-  INLINE string get_string() const;
   INLINE size_t get_length() const;
   INLINE size_t get_length() const;
+  INLINE string get_string() const;
 public:
 public:
+  INLINE void get_string(string &data) const;
   INLINE const char *get_data() const;
   INLINE const char *get_data() const;
+  INLINE char *take_data();
 
 
   INLINE void append_data(const char *buffer, size_t size);
   INLINE void append_data(const char *buffer, size_t size);
   INLINE char *get_write_pointer(size_t size);
   INLINE char *get_write_pointer(size_t size);