Panda3D
datagramIterator.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file datagramIterator.cxx
10  * @author jns
11  * @date 2000-02-07
12  */
13 
14 #include "datagramIterator.h"
15 #include "pnotify.h"
16 
17 using std::string;
18 using std::wstring;
19 
20 TypeHandle DatagramIterator::_type_handle;
21 
22 /**
23  * Extracts a variable-length string.
24  */
25 string DatagramIterator::
27  // First, get the length of the string
28  uint16_t s_len = get_uint16();
29 
30  nassertr(_datagram != nullptr, "");
31  nassertr(_current_index + s_len <= _datagram->get_length(), "");
32 
33  const char *ptr = (const char *)_datagram->get_data();
34  size_t last_index = _current_index;
35 
36  _current_index += s_len;
37 
38  return string(ptr + last_index, s_len);
39 }
40 
41 /**
42  * Extracts a variable-length string with a 32-bit length field.
43  */
44 string DatagramIterator::
46  // First, get the length of the string
47  uint32_t s_len = get_uint32();
48 
49  nassertr(_datagram != nullptr, "");
50  nassertr(_current_index + s_len <= _datagram->get_length(), "");
51 
52  const char *ptr = (const char *)_datagram->get_data();
53  size_t last_index = _current_index;
54 
55  _current_index += s_len;
56 
57  return string(ptr + last_index, s_len);
58 }
59 
60 /**
61  * Extracts a variable-length string, as a NULL-terminated string.
62  */
63 string DatagramIterator::
65  nassertr(_datagram != nullptr, "");
66 
67  // First, determine the length of the string.
68  const char *ptr = (const char *)_datagram->get_data();
69  size_t length = _datagram->get_length();
70  size_t p = _current_index;
71  while (p < length && ptr[p] != '\0') {
72  ++p;
73  }
74  nassertr(p < length, ""); // no NULL character?
75 
76  size_t last_index = _current_index;
77  _current_index = p + 1;
78 
79  return string(ptr + last_index, p - last_index);
80 }
81 
82 /**
83  * Extracts a fixed-length string. However, if a zero byte occurs within the
84  * string, it marks the end of the string.
85  */
86 string DatagramIterator::
87 get_fixed_string(size_t size) {
88  nassertr(_datagram != nullptr, "");
89  nassertr(_current_index + size <= _datagram->get_length(), "");
90 
91  const char *ptr = (const char *)_datagram->get_data();
92  string s(ptr + _current_index, size);
93 
94  _current_index += size;
95 
96  size_t zero_byte = s.find('\0');
97  return s.substr(0, zero_byte);
98 }
99 
100 /**
101  * Extracts a variable-length wstring (with a 32-bit length field).
102  */
103 wstring DatagramIterator::
105  // First, get the length of the string
106  uint32_t s_len = get_uint32();
107 
108  nassertr(_datagram != nullptr, wstring());
109  nassertr(_current_index + s_len * 2 <= _datagram->get_length(), wstring());
110 
111  wstring result;
112  result.reserve(s_len);
113  while (s_len > 0) {
114  result += wchar_t(get_uint16());
115  --s_len;
116  }
117 
118  return result;
119 }
120 
121 /**
122  * Extracts the indicated number of bytes in the datagram and returns them as
123  * a string.
124  */
125 vector_uchar DatagramIterator::
126 extract_bytes(size_t size) {
127  nassertr((int)size >= 0, vector_uchar());
128  nassertr(_datagram != nullptr, vector_uchar());
129  nassertr(_current_index + size <= _datagram->get_length(), vector_uchar());
130 
131  const unsigned char *ptr = (const unsigned char *)_datagram->get_data();
132  ptr += _current_index;
133 
134  _current_index += size;
135 
136  return vector_uchar(ptr, ptr + size);
137 }
138 
139 /**
140  * Extracts the indicated number of bytes in the datagram into the given
141  * character buffer. Assumes that the buffer is big enough to hold the
142  * requested number of bytes. Returns the number of bytes that were
143  * successfully written.
144  */
145 size_t DatagramIterator::
146 extract_bytes(unsigned char *into, size_t size) {
147  nassertr((int)size >= 0, 0);
148  nassertr(_datagram != nullptr, 0);
149  nassertr(_current_index + size <= _datagram->get_length(), 0);
150 
151  const char *ptr = (const char *)_datagram->get_data();
152  memcpy(into, ptr + _current_index, size);
153 
154  _current_index += size;
155  return size;
156 }
157 
158 /**
159  * Write a string representation of this instance to <out>.
160  */
162 output(std::ostream &out) const {
163  #ifndef NDEBUG //[
164  out<<""<<"DatagramIterator";
165  #endif //] NDEBUG
166 }
167 
168 /**
169  * Write a string representation of this instance to <out>.
170  */
172 write(std::ostream &out, unsigned int indent) const {
173  #ifndef NDEBUG //[
174  out.width(indent); out<<""<<"DatagramIterator:\n";
175  out.width(indent+2); out<<""<<"_current_index "<<_current_index;
176  if (_datagram) {
177  out<<""<<" (of "<<(get_datagram().get_length())<<")";
178  out<<""<<" / 0x"<<(void*)_current_index<<" (of 0x"
179  <<(void*)(get_datagram().get_length())<<")\n";
180  get_datagram().write(out, indent+2);
181  } else {
182  out<<""<<" (_datagram is null)\n";
183  }
184  #endif //] NDEBUG
185 }
const Datagram & get_datagram() const
Return the datagram of this iterator.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write(std::ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::string get_string()
Extracts a variable-length string.
vector_uchar extract_bytes(size_t size)
Extracts the indicated number of bytes in the datagram and returns them as a string.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
void write(std::ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
Definition: datagram.cxx:181
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
std::string get_z_string()
Extracts a variable-length string, as a NULL-terminated string.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
void output(std::ostream &out) const
Write a string representation of this instance to <out>.
std::wstring get_wstring()
Extracts a variable-length wstring (with a 32-bit length field).
std::string get_string32()
Extracts a variable-length string with a 32-bit length field.
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:335
const void * get_data() const
Returns a pointer to the beginning of the datagram's data.
Definition: datagram.I:327
std::string get_fixed_string(size_t size)
Extracts a fixed-length string.