Panda3D
 All Classes Functions Variables Enumerations
streamReader.cxx
1 // Filename: streamReader.cxx
2 // Created by: drose (04Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "streamReader.h"
16 #include "memoryHook.h"
17 
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: StreamReader::get_string
21 // Access: Published
22 // Description: Extracts a variable-length string.
23 ////////////////////////////////////////////////////////////////////
24 string StreamReader::
26  nassertr(!_in->eof() && !_in->fail(), string());
27 
28  // First, get the length of the string
29  size_t size = get_uint16();
30 
31  char *buffer = (char *)alloca(size);
32  _in->read(buffer, size);
33  size_t read_bytes = _in->gcount();
34  return string(buffer, read_bytes);
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: StreamReader::get_string32
39 // Access: Published
40 // Description: Extracts a variable-length string with a 32-bit
41 // length field.
42 ////////////////////////////////////////////////////////////////////
43 string StreamReader::
45  nassertr(!_in->eof() && !_in->fail(), string());
46 
47  // First, get the length of the string
48  size_t size = get_uint32();
49 
50  char *buffer = (char *)PANDA_MALLOC_ARRAY(size);
51  _in->read(buffer, size);
52  size_t read_bytes = _in->gcount();
53  string result(buffer, read_bytes);
54  PANDA_FREE_ARRAY(buffer);
55  return result;
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: StreamReader::get_z_string
60 // Access: Published
61 // Description: Extracts a variable-length string, as a
62 // NULL-terminated string.
63 ////////////////////////////////////////////////////////////////////
64 string StreamReader::
66  nassertr(!_in->eof() && !_in->fail(), string());
67 
68  string result;
69  int ch = _in->get();
70  while (!_in->eof() && !_in->fail() && ch != '\0') {
71  result += ch;
72  ch = _in->get();
73  }
74 
75  return result;
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: StreamReader::get_fixed_string
80 // Access: Published
81 // Description: Extracts a fixed-length string. However, if a zero
82 // byte occurs within the string, it marks the end of
83 // the string.
84 ////////////////////////////////////////////////////////////////////
85 string StreamReader::
86 get_fixed_string(size_t size) {
87  nassertr(!_in->eof() && !_in->fail(), string());
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 // Function: StreamReader::skip_bytes
100 // Access: Published
101 // Description: Skips over the indicated number of bytes in the
102 // stream.
103 ////////////////////////////////////////////////////////////////////
104 void StreamReader::
105 skip_bytes(size_t size) {
106  nassertv(!_in->eof() && !_in->fail());
107  nassertv((int)size >= 0);
108 
109  while (size > 0) {
110  _in->get();
111  size--;
112  }
113 }
114 
115 ////////////////////////////////////////////////////////////////////
116 // Function: StreamReader::extract_bytes
117 // Access: Published
118 // Description: Extracts the indicated number of bytes in the
119 // stream and returns them as a string. Returns empty
120 // string at end-of-file.
121 ////////////////////////////////////////////////////////////////////
122 string StreamReader::
123 extract_bytes(size_t size) {
124  if (_in->eof() || _in->fail()) {
125  return string();
126  }
127 
128  char *buffer = (char *)alloca(size);
129  _in->read(buffer, size);
130  size_t read_bytes = _in->gcount();
131  return string(buffer, read_bytes);
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function: StreamReader::extract_bytes
136 // Access: Published
137 // Description: Extracts the indicated number of bytes in the
138 // stream into the given character buffer. Assumes
139 // that the buffer is big enough to hold the requested
140 // number of bytes. Returns the number of bytes
141 // that were successfully written.
142 ////////////////////////////////////////////////////////////////////
143 size_t StreamReader::
144 extract_bytes(unsigned char *into, size_t size) {
145  if (_in->eof() || _in->fail()) {
146  return 0;
147  }
148 
149  _in->read((char *)into, size);
150  return _in->gcount();
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: StreamReader::readline
155 // Access: Published
156 // Description: Assumes the stream represents a text file, and
157 // extracts one line up to and including the trailing
158 // newline character. Returns empty string when the end
159 // of file is reached.
160 //
161 // The interface here is intentionally designed to be
162 // similar to that for Python's File.readline()
163 // function.
164 ////////////////////////////////////////////////////////////////////
165 string StreamReader::
167  string line;
168  int ch = _in->get();
169  while (!_in->eof() && !_in->fail()) {
170  line += ch;
171  if (ch == '\n') {
172  // Here's the newline character.
173  return line;
174  }
175  ch = _in->get();
176  }
177 
178  return line;
179 }
180 
string get_string()
Extracts a variable-length string.
string extract_bytes(size_t size)
Extracts the indicated number of bytes in the stream and returns them as a string.
PN_uint32 get_uint32()
Extracts an unsigned 32-bit integer.
Definition: streamReader.I:183
string get_fixed_string(size_t size)
Extracts a fixed-length string.
string get_z_string()
Extracts a variable-length string, as a NULL-terminated string.
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
Definition: streamReader.I:169
string get_string32()
Extracts a variable-length string with a 32-bit length field.
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.