Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/express/datagramIterator.cxx

Go to the documentation of this file.
00001 // Filename: datagramIterator.cxx
00002 // Created by:  jns (07Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 
00020 #include "datagramIterator.h"
00021 #include "notify.h"
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: DatagramIterator::get_string
00025 //       Access: Public
00026 //  Description: Extracts a variable-length string.
00027 ////////////////////////////////////////////////////////////////////
00028 string DatagramIterator::
00029 get_string() {
00030   // First, get the length of the string
00031   PN_uint16 s_len = get_uint16();
00032 
00033   nassertr(_datagram != (const Datagram *)NULL &&
00034            _current_index + s_len <= _datagram->get_length(), "");
00035 
00036   const char *ptr = (const char *)_datagram->get_data();
00037   int last_index = _current_index;
00038 
00039   _current_index += s_len;
00040 
00041   return string(ptr + last_index, s_len);
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: DatagramIterator::get_z_string
00046 //       Access: Public
00047 //  Description: Extracts a variable-length string, as a
00048 //               NULL-terminated string.
00049 ////////////////////////////////////////////////////////////////////
00050 string DatagramIterator::
00051 get_z_string() {
00052   nassertr(_datagram != (const Datagram *)NULL, "");
00053 
00054   // First, determine the length of the string.
00055   const char *ptr = (const char *)_datagram->get_data();
00056   size_t length = _datagram->get_length();
00057   size_t p = _current_index;
00058   while (p < length && ptr[p] != '\0') {
00059   }
00060   nassertr(p < length, "");  // no NULL character?
00061 
00062   int last_index = _current_index;
00063   _current_index = p + 1;
00064 
00065   return string(ptr + last_index, p - last_index);
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: DatagramIterator::get_fixed_string
00070 //       Access: Public
00071 //  Description: Extracts a fixed-length string.  However, if a zero
00072 //               byte occurs within the string, it marks the end of
00073 //               the string.
00074 ////////////////////////////////////////////////////////////////////
00075 string DatagramIterator::
00076 get_fixed_string(size_t size) {
00077   nassertr(_datagram != (const Datagram *)NULL &&
00078            _current_index + size <= _datagram->get_length(), "");
00079 
00080   const char *ptr = (const char *)_datagram->get_data();
00081   string s(ptr + _current_index, size);
00082 
00083   _current_index += size;
00084 
00085   size_t zero_byte = s.find('\0');
00086   return s.substr(0, zero_byte);
00087 }
00088 
00089 ////////////////////////////////////////////////////////////////////
00090 //     Function: DatagramIterator::extract_bytes
00091 //       Access: Public
00092 //  Description: Extracts the indicated number of bytes in the
00093 //               datagram and returns them as a string.
00094 ////////////////////////////////////////////////////////////////////
00095 string DatagramIterator::
00096 extract_bytes(size_t size) {
00097   nassertr((int)size >= 0, "");
00098   nassertr(_datagram != (const Datagram *)NULL &&
00099            _current_index + size <= _datagram->get_length(), "");
00100 
00101   const char *ptr = (const char *)_datagram->get_data();
00102   int last_index = _current_index;
00103 
00104   _current_index += size;
00105 
00106   return string(ptr + last_index, size);
00107 }
00108 

Generated on Fri May 2 00:38:22 2003 for Panda by doxygen1.3