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