From 3335dae1be1efd7903a06a1fa30483b1882daca9 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 22 Jun 2001 01:55:37 +0000 Subject: [PATCH] add z_string --- panda/src/express/datagram.I | 17 +++++++++++++++++ panda/src/express/datagram.h | 1 + panda/src/express/datagramIterator.cxx | 24 ++++++++++++++++++++++++ panda/src/express/datagramIterator.h | 1 + 4 files changed, 43 insertions(+) diff --git a/panda/src/express/datagram.I b/panda/src/express/datagram.I index 20c618ebb0..591c9d5800 100644 --- a/panda/src/express/datagram.I +++ b/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 diff --git a/panda/src/express/datagram.h b/panda/src/express/datagram.h index a82f5036f1..21b968e613 100644 --- a/panda/src/express/datagram.h +++ b/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); diff --git a/panda/src/express/datagramIterator.cxx b/panda/src/express/datagramIterator.cxx index 8a94a85ec8..d7131151f6 100644 --- a/panda/src/express/datagramIterator.cxx +++ b/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 diff --git a/panda/src/express/datagramIterator.h b/panda/src/express/datagramIterator.h index 7a2a973e70..eaab323b23 100644 --- a/panda/src/express/datagramIterator.h +++ b/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);