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 
30  char *buffer = (char *)alloca(size);
31  _in->read(buffer, size);
32  size_t read_bytes = _in->gcount();
33  return string(buffer, read_bytes);
34 }
35 
36 /**
37  * Extracts a variable-length string with a 32-bit length field.
38  */
39 string StreamReader::
41  nassertr(!_in->eof() && !_in->fail(), string());
42 
43  // First, get the length of the string
44  size_t size = get_uint32();
45 
46  char *buffer = (char *)PANDA_MALLOC_ARRAY(size);
47  _in->read(buffer, size);
48  size_t read_bytes = _in->gcount();
49  string result(buffer, read_bytes);
50  PANDA_FREE_ARRAY(buffer);
51  return result;
52 }
53 
54 /**
55  * Extracts a variable-length string, as a NULL-terminated string.
56  */
57 string StreamReader::
59  nassertr(!_in->eof() && !_in->fail(), string());
60 
61  string result;
62  int ch = _in->get();
63  while (!_in->eof() && !_in->fail() && ch != '\0') {
64  result += (char)ch;
65  ch = _in->get();
66  }
67 
68  return result;
69 }
70 
71 /**
72  * Extracts a fixed-length string. However, if a zero byte occurs within the
73  * string, it marks the end of the string.
74  */
75 string StreamReader::
76 get_fixed_string(size_t size) {
77  nassertr(!_in->eof() && !_in->fail(), string());
78 
79  char *buffer = (char *)alloca(size);
80  _in->read(buffer, size);
81  size_t read_bytes = _in->gcount();
82  string result(buffer, read_bytes);
83 
84  size_t zero_byte = result.find('\0');
85  return result.substr(0, zero_byte);
86 }
87 
88 /**
89  * Skips over the indicated number of bytes in the stream.
90  */
91 void StreamReader::
92 skip_bytes(size_t size) {
93  nassertv(!_in->eof() && !_in->fail());
94  nassertv((int)size >= 0);
95 
96  while (size > 0) {
97  _in->get();
98  size--;
99  }
100 }
101 
102 /**
103  * Extracts the indicated number of bytes in the stream into the given
104  * character buffer. Assumes that the buffer is big enough to hold the
105  * requested number of bytes. Returns the number of bytes that were
106  * successfully written.
107  */
108 size_t StreamReader::
109 extract_bytes(unsigned char *into, size_t size) {
110  if (_in->eof() || _in->fail()) {
111  return 0;
112  }
113 
114  _in->read((char *)into, size);
115  return _in->gcount();
116 }
117 
118 /**
119  * Extracts the indicated number of bytes in the stream and returns them as a
120  * string. Returns empty string at end-of-file.
121  */
122 vector_uchar StreamReader::
123 extract_bytes(size_t size) {
124  vector_uchar buffer;
125  if (_in->eof() || _in->fail()) {
126  return buffer;
127  }
128 
129  buffer.resize(size);
130  _in->read((char *)&buffer[0], size);
131  size_t read_bytes = _in->gcount();
132  buffer.resize(read_bytes);
133  return buffer;
134 }
135 
136 /**
137  * Assumes the stream represents a text file, and extracts one line up to and
138  * including the trailing newline character. Returns empty string when the
139  * end of file is reached.
140  *
141  * The interface here is intentionally designed to be similar to that for
142  * Python's File.readline() function.
143  */
144 string StreamReader::
146  string line;
147  int ch = _in->get();
148  while (!_in->eof() && !_in->fail()) {
149  line += (char)ch;
150  if (ch == '\n') {
151  // Here's the newline character.
152  return line;
153  }
154  ch = _in->get();
155  }
156 
157  return line;
158 }
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.