Browse Source

add z_string

David Rose 24 years ago
parent
commit
3335dae1be

+ 17 - 0
panda/src/express/datagram.I

@@ -309,6 +309,23 @@ add_string(const string &str) {
   _message += str;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: Datagram::add_z_string
+//       Access: Public
+//  Description: Adds a variable-length string to the datagram, as a
+//               NULL-terminated string.
+////////////////////////////////////////////////////////////////////
+INLINE void Datagram::
+add_z_string(string str) {
+  // We must not have any nested null characters in the string.
+  size_t null_pos = str.find('\0');
+  // Add the string (sans the null character).
+  _message += str.substr(0, null_pos);
+
+  // And the null character.
+  _message += '\0';
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: Datagram::add_fixed_string
 //       Access: Public

+ 1 - 0
panda/src/express/datagram.h

@@ -83,6 +83,7 @@ PUBLISHED:
   INLINE void add_be_float64(PN_float64 value);
 
   INLINE void add_string(const string &str);
+  INLINE void add_z_string(string str);
   INLINE void add_fixed_string(const string &str, size_t size);
 
   INLINE void pad_bytes(size_t size);

+ 24 - 0
panda/src/express/datagramIterator.cxx

@@ -42,6 +42,30 @@ get_string() {
   return s;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: DatagramIterator::get_z_string
+//       Access: Public
+//  Description: Extracts a variable-length string, as a
+//               NULL-terminated string.
+////////////////////////////////////////////////////////////////////
+string DatagramIterator::
+get_z_string() {
+  nassertr(_datagram != (const Datagram *)NULL, "");
+
+  // First, determine the length of the string.
+  const string &message = _datagram->get_message();
+  size_t p = _current_index;
+  while (p < message.length() && message[p] != '\0') {
+  }
+  nassertr(p < message.length(), "");  // no NULL character?
+
+  string s =
+    _datagram->get_message().substr(_current_index, p - _current_index);
+  _current_index = p + 1;
+
+  return s;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: DatagramIterator::get_fixed_string
 //       Access: Public

+ 1 - 0
panda/src/express/datagramIterator.h

@@ -62,6 +62,7 @@ PUBLISHED:
   INLINE PN_float64 get_be_float64();
 
   string get_string();
+  string get_z_string();
   string get_fixed_string(size_t size);
 
   INLINE void skip_bytes(size_t size);