15 #include "subStreamBuf.h"
17 #include "memoryHook.h"
19 #ifndef HAVE_STREAMSIZE
21 typedef int streamsize;
24 static const size_t substream_buffer_size = 4096;
53 _buffer = (
char *)PANDA_MALLOC_ARRAY(substream_buffer_size * 2);
54 char *ebuf = _buffer + substream_buffer_size * 2;
55 char *mbuf = _buffer + substream_buffer_size;
56 setg(_buffer, mbuf, mbuf);
65 char *m = b + (t - b) / 2;
80 PANDA_FREE_ARRAY(_buffer);
118 pbump(pbase() - pptr());
119 gbump(egptr() - gptr());
128 seekoff(streamoff off, ios_seekdir dir, ios_openmode which) {
129 streampos result = -1;
134 if (which & ios::in) {
136 size_t n = egptr() - gptr();
139 nassertr(_gpos >= 0, EOF);
140 streampos cur_pos = _gpos;
141 streampos new_pos = cur_pos;
146 new_pos = (streampos)off + _start;
150 new_pos = (streampos)((streamoff)cur_pos + off);
154 if (_end == (streampos)0) {
160 new_pos = _end + off;
169 if (new_pos < _start) {
174 if (_end != (streampos)0 && new_pos > _end) {
180 nassertr(_gpos >= 0, EOF);
181 result = new_pos - _start;
184 if (which & ios::out) {
186 size_t n = pptr() - pbase();
187 streampos cur_pos = _ppos + (streamoff)n;
188 streampos new_pos = cur_pos;
193 new_pos = (streampos)off + _start;
197 new_pos = (streampos)((streamoff)cur_pos + off);
201 if (_end == (streampos)0) {
207 new_pos = _end + off;
216 if (new_pos < _start) {
221 if (_end != (streampos)0 && new_pos > _end) {
227 nassertr(_ppos >= 0, EOF);
228 result = new_pos - _start;
250 return seekoff(pos, ios::beg, which);
263 size_t n = pptr() - pbase();
265 if (_end != (streampos)0 && _ppos + (streampos)n > _end) {
267 n = (size_t)(_end - _ppos);
274 nassertr(_dest != NULL, EOF);
288 if (okflag && ch != EOF) {
289 if (pptr() != epptr()) {
313 size_t n = pptr() - pbase();
316 nassertr(_dest != NULL, EOF);
343 if (gptr() >= egptr()) {
347 size_t buffer_size = egptr() - eback();
348 gbump(-(
int)buffer_size);
350 streamsize num_bytes = buffer_size;
351 if (_end != (streampos)0 && _gpos + (streampos)num_bytes > _end) {
353 streamsize new_num_bytes = _end - _gpos;
354 if (new_num_bytes == 0) {
361 size_t delta = num_bytes - new_num_bytes;
363 num_bytes = new_num_bytes;
364 nassertr(egptr() - gptr() == num_bytes, EOF);
367 nassertr(_source != NULL, EOF);
368 streamsize read_count;
370 _source->
seek_read(_gpos, gptr(), num_bytes, read_count, eof);
373 if (read_count != num_bytes) {
375 if (read_count == 0) {
381 nassertr(read_count < num_bytes, EOF);
382 size_t delta = num_bytes - read_count;
383 memmove(gptr() + delta, gptr(), read_count);
388 return (
unsigned char)*gptr();
void seek_write(streamsize pos, const char *buffer, streamsize num_bytes, bool &fail)
Atomically seeks to a particular offset from the beginning of the file, and writes a number of bytes ...
void seek_eof_write(const char *buffer, streamsize num_bytes, bool &fail)
Atomically seeks to the end of the file, and writes a number of bytes to the stream.
void seek_read(streamsize pos, char *buffer, streamsize num_bytes, streamsize &read_bytes, bool &eof)
Atomically seeks to a particular offset from the beginning of the file, and reads a number of bytes f...
streamsize seek_gpos_eof()
Atomically seeks to EOF and returns the gpos there; that is, returns the file size.
virtual streampos seekpos(streampos pos, ios_openmode which)
A variant on seekoff() to implement seeking within a stream.
This class provides a locking wrapper around an arbitrary istream pointer.
streamsize seek_ppos_eof()
Atomically seeks to EOF and returns the ppos there; that is, returns the file size.
This class provides a locking wrapper around an arbitrary ostream pointer.
virtual streampos seekoff(streamoff off, ios_seekdir dir, ios_openmode which)
Implements seeking within the stream.