Panda3D
|
00001 // Filename: datagramIterator.cxx 00002 // Created by: jns (07Feb00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 #include "datagramIterator.h" 00017 #include "pnotify.h" 00018 00019 //////////////////////////////////////////////////////////////////// 00020 // Function: DatagramIterator::get_string 00021 // Access: Public 00022 // Description: Extracts a variable-length string. 00023 //////////////////////////////////////////////////////////////////// 00024 string DatagramIterator:: 00025 get_string() { 00026 // First, get the length of the string 00027 PN_uint16 s_len = get_uint16(); 00028 00029 nassertr(_datagram != (const Datagram *)NULL, ""); 00030 nassertr(_current_index + s_len <= _datagram->get_length(), ""); 00031 00032 const char *ptr = (const char *)_datagram->get_data(); 00033 int last_index = _current_index; 00034 00035 _current_index += s_len; 00036 00037 return string(ptr + last_index, s_len); 00038 } 00039 00040 //////////////////////////////////////////////////////////////////// 00041 // Function: DatagramIterator::get_string32 00042 // Access: Public 00043 // Description: Extracts a variable-length string with a 32-bit 00044 // length field. 00045 //////////////////////////////////////////////////////////////////// 00046 string DatagramIterator:: 00047 get_string32() { 00048 // First, get the length of the string 00049 PN_uint32 s_len = get_uint32(); 00050 00051 nassertr(_datagram != (const Datagram *)NULL, ""); 00052 nassertr(_current_index + s_len <= _datagram->get_length(), ""); 00053 00054 const char *ptr = (const char *)_datagram->get_data(); 00055 int last_index = _current_index; 00056 00057 _current_index += s_len; 00058 00059 return string(ptr + last_index, s_len); 00060 } 00061 00062 //////////////////////////////////////////////////////////////////// 00063 // Function: DatagramIterator::get_z_string 00064 // Access: Public 00065 // Description: Extracts a variable-length string, as a 00066 // NULL-terminated string. 00067 //////////////////////////////////////////////////////////////////// 00068 string DatagramIterator:: 00069 get_z_string() { 00070 nassertr(_datagram != (const Datagram *)NULL, ""); 00071 00072 // First, determine the length of the string. 00073 const char *ptr = (const char *)_datagram->get_data(); 00074 size_t length = _datagram->get_length(); 00075 size_t p = _current_index; 00076 while (p < length && ptr[p] != '\0') { 00077 ++p; 00078 } 00079 nassertr(p < length, ""); // no NULL character? 00080 00081 int last_index = _current_index; 00082 _current_index = p + 1; 00083 00084 return string(ptr + last_index, p - last_index); 00085 } 00086 00087 //////////////////////////////////////////////////////////////////// 00088 // Function: DatagramIterator::get_fixed_string 00089 // Access: Public 00090 // Description: Extracts a fixed-length string. However, if a zero 00091 // byte occurs within the string, it marks the end of 00092 // the string. 00093 //////////////////////////////////////////////////////////////////// 00094 string DatagramIterator:: 00095 get_fixed_string(size_t size) { 00096 nassertr(_datagram != (const Datagram *)NULL, ""); 00097 nassertr(_current_index + size <= _datagram->get_length(), ""); 00098 00099 const char *ptr = (const char *)_datagram->get_data(); 00100 string s(ptr + _current_index, size); 00101 00102 _current_index += size; 00103 00104 size_t zero_byte = s.find('\0'); 00105 return s.substr(0, zero_byte); 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: DatagramIterator::get_wstring 00110 // Access: Public 00111 // Description: Extracts a variable-length wstring (with a 32-bit 00112 // length field). 00113 //////////////////////////////////////////////////////////////////// 00114 wstring DatagramIterator:: 00115 get_wstring() { 00116 // First, get the length of the string 00117 PN_uint32 s_len = get_uint32(); 00118 00119 nassertr(_datagram != (const Datagram *)NULL, wstring()); 00120 nassertr(_current_index + s_len * 2 <= _datagram->get_length(), wstring()); 00121 00122 wstring result; 00123 result.reserve(s_len); 00124 while (s_len > 0) { 00125 result += wchar_t(get_uint16()); 00126 --s_len; 00127 } 00128 00129 return result; 00130 } 00131 00132 //////////////////////////////////////////////////////////////////// 00133 // Function: DatagramIterator::extract_bytes 00134 // Access: Public 00135 // Description: Extracts the indicated number of bytes in the 00136 // datagram and returns them as a string. 00137 //////////////////////////////////////////////////////////////////// 00138 string DatagramIterator:: 00139 extract_bytes(size_t size) { 00140 nassertr((int)size >= 0, ""); 00141 nassertr(_datagram != (const Datagram *)NULL, ""); 00142 nassertr(_current_index + size <= _datagram->get_length(), ""); 00143 00144 const char *ptr = (const char *)_datagram->get_data(); 00145 int last_index = _current_index; 00146 00147 _current_index += size; 00148 00149 return string(ptr + last_index, size); 00150 } 00151 00152 //////////////////////////////////////////////////////////////////// 00153 // Function : output 00154 // Access : Public 00155 // Description : Write a string representation of this instance to 00156 // <out>. 00157 //////////////////////////////////////////////////////////////////// 00158 void DatagramIterator:: 00159 output(ostream &out) const { 00160 #ifndef NDEBUG //[ 00161 out<<""<<"DatagramIterator"; 00162 #endif //] NDEBUG 00163 } 00164 00165 //////////////////////////////////////////////////////////////////// 00166 // Function : write 00167 // Access : Public 00168 // Description : Write a string representation of this instance to 00169 // <out>. 00170 //////////////////////////////////////////////////////////////////// 00171 void DatagramIterator:: 00172 write(ostream &out, unsigned int indent) const { 00173 #ifndef NDEBUG //[ 00174 out.width(indent); out<<""<<"DatagramIterator:\n"; 00175 out.width(indent+2); out<<""<<"_current_index "<<_current_index; 00176 if (_datagram) { 00177 out<<""<<" (of "<<(get_datagram().get_length())<<")"; 00178 out<<""<<" / 0x"<<(void*)_current_index<<" (of 0x" 00179 <<(void*)(get_datagram().get_length())<<")\n"; 00180 get_datagram().write(out, indent+2); 00181 } else { 00182 out<<""<<" (_datagram is null)\n"; 00183 } 00184 #endif //] NDEBUG 00185 } 00186 00187