Panda3D
streamReader.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 streamReader.cxx
10  * @author drose
11  * @date 2002-08-04
12  */
13 
14 #include "streamReader.h"
15 #include "memoryHook.h"
16 
17 using std::string;
18 
19 
20 /**
21  * Extracts a variable-length string.
22  */
23 string StreamReader::
25  nassertr(!_in->eof() && !_in->fail(), string());
26 
27  // First, get the length of the string
28  size_t size = get_uint16();
29  if (size == 0) {
30  return string();
31  }
32 
33  char *buffer = (char *)alloca(size);
34  _in->read(buffer, size);
35  size_t read_bytes = _in->gcount();
36  return string(buffer, read_bytes);
37 }
38 
39 /**
40  * Extracts a variable-length string with a 32-bit length field.
41  */
42 string StreamReader::
44  nassertr(!_in->eof() && !_in->fail(), string());
45 
46  // First, get the length of the string
47  size_t size = get_uint32();
48  if (size == 0) {
49  return string();
50  }
51 
52  char *buffer = (char *)PANDA_MALLOC_ARRAY(size);
53  _in->read(buffer, size);
54  size_t read_bytes = _in->gcount();
55  string result(buffer, read_bytes);
56  PANDA_FREE_ARRAY(buffer);
57  return result;
58 }
59 
60 /**
61  * Extracts a variable-length string, as a NULL-terminated string.
62  */
63 string StreamReader::
65  nassertr(!_in->eof() && !_in->fail(), string());
66 
67  string result;
68  int ch = _in->get();
69  while (!_in->fail() && ch != EOF && ch != '\0') {
70  result += (char)ch;
71  ch = _in->get();
72  }
73 
74  return result;
75 }
76 
77 /**
78  * Extracts a fixed-length string. However, if a zero byte occurs within the
79  * string, it marks the end of the string.
80  */
81 string StreamReader::
82 get_fixed_string(size_t size) {
83  nassertr(!_in->eof() && !_in->fail(), string());
84 
85  if (size == 0) {
86  return string();
87  }
88 
89  char *buffer = (char *)alloca(size);
90  _in->read(buffer, size);
91  size_t read_bytes = _in->gcount();
92  string result(buffer, read_bytes);
93 
94  size_t zero_byte = result.find('\0');
95  return result.substr(0, zero_byte);
96 }
97 
98 /**
99  * Skips over the indicated number of bytes in the stream.
100  */
101 void StreamReader::
102 skip_bytes(size_t size) {
103  nassertv(!_in->fail());
104  nassertv((int)size >= 0);
105  nassertv(size == 0 || !_in->eof());
106 
107  while (size > 0) {
108  _in->get();
109  size--;
110  }
111 }
112 
113 /**
114  * Extracts the indicated number of bytes in the stream into the given
115  * character buffer. Assumes that the buffer is big enough to hold the
116  * requested number of bytes. Returns the number of bytes that were
117  * successfully written.
118  */
119 size_t StreamReader::
120 extract_bytes(unsigned char *into, size_t size) {
121  if (_in->eof() || _in->fail()) {
122  return 0;
123  }
124 
125  _in->read((char *)into, size);
126  return _in->gcount();
127 }
128 
129 /**
130  * Extracts the indicated number of bytes in the stream and returns them as a
131  * string. Returns empty string at end-of-file.
132  */
133 vector_uchar StreamReader::
134 extract_bytes(size_t size) {
135  vector_uchar buffer;
136  if (_in->eof() || _in->fail()) {
137  return buffer;
138  }
139 
140  buffer.resize(size);
141  _in->read((char *)&buffer[0], size);
142  size_t read_bytes = _in->gcount();
143  buffer.resize(read_bytes);
144  return buffer;
145 }
146 
147 /**
148  * Assumes the stream represents a text file, and extracts one line up to and
149  * including the trailing newline character. Returns empty string when the
150  * end of file is reached.
151  *
152  * The interface here is intentionally designed to be similar to that for
153  * Python's File.readline() function.
154  */
155 string StreamReader::
157  string line;
158  int ch = _in->get();
159  while (ch != EOF && !_in->fail()) {
160  line += (char)ch;
161  if (ch == '\n' || _in->eof()) {
162  // Here's the newline character.
163  return line;
164  }
165  ch = _in->get();
166  }
167 
168  return line;
169 }
std::string get_string()
Extracts a variable-length string.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
Definition: streamReader.I:139
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
Definition: streamReader.I:151
size_t extract_bytes(unsigned char *into, size_t size)
Extracts the indicated number of bytes in the stream into the given character buffer.
std::string get_fixed_string(size_t size)
Extracts a fixed-length string.
std::string get_z_string()
Extracts a variable-length string, as a NULL-terminated string.
std::string get_string32()
Extracts a variable-length string with a 32-bit length field.
std::string readline()
Assumes the stream represents a text file, and extracts one line up to and including the trailing new...
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the stream.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.