26 using std::streamsize;
37 _filename = _file->get_filename();
44 if (_vfile ==
nullptr) {
48 _timestamp = _vfile->get_timestamp();
49 _in = _vfile->open_read_file(
true);
50 _owns_in = (_in !=
nullptr);
51 return _owns_in && !_in->fail();
68 if (!filename.empty()) {
93 _read_first_datagram =
false;
105 nassertr(!_read_first_datagram,
false);
106 nassertr(_in !=
nullptr,
false);
108 char *buffer = (
char *)alloca(num_bytes);
109 nassertr(buffer !=
nullptr,
false);
111 _in->read(buffer, num_bytes);
112 if (_in->fail() || _in->eof()) {
116 header = std::string(buffer, num_bytes);
127 nassertr(_in !=
nullptr,
false);
128 _read_first_datagram =
true;
133 if (_in->fail() || _in->eof()) {
137 if (num_bytes_32 == 0) {
144 size_t num_bytes = (size_t)num_bytes_32;
145 if (num_bytes_32 == (uint32_t)-1) {
149 if (_in->fail() || _in->eof()) {
154 num_bytes = (size_t)num_bytes_64;
157 if (num_bytes_64 != (uint64_t)num_bytes) {
168 size_t bytes_read = 0;
169 while (bytes_read < num_bytes) {
170 size_t bytes_left = num_bytes - bytes_read;
176 bytes_left = std::min(bytes_left, (
size_t)4*1024*1024);
178 PTA_uchar buffer = data.modify_array();
179 buffer.resize(buffer.size() + bytes_left);
180 unsigned char *ptr = &buffer.p()[bytes_read];
182 _in->read((
char *)ptr, (streamsize)bytes_left);
183 if (_in->fail() || _in->eof()) {
188 bytes_read += bytes_left;
207 nassertr(_in !=
nullptr,
false);
208 _read_first_datagram =
true;
213 if (_in->fail() || _in->eof()) {
217 streamsize num_bytes = (streamsize)num_bytes_32;
218 if (num_bytes_32 == (uint32_t)-1) {
225 if (_file !=
nullptr) {
226 info =
SubfileInfo(_file, _in->tellg(), num_bytes);
227 _in->seekg(num_bytes, std::ios::cur);
234 Filename filename = tfile->get_filename();
238 <<
"Couldn't write to " << tfile->get_filename() <<
"\n";
242 if (util_cat.is_debug()) {
244 <<
"Copying " << num_bytes <<
" bytes to " << tfile->get_filename() <<
"\n";
247 streamsize num_remaining = num_bytes;
248 static const size_t buffer_size = 4096;
249 char buffer[buffer_size];
251 _in->read(buffer, std::min((streamsize)buffer_size, num_remaining));
252 streamsize count = _in->gcount();
254 out.write(buffer, count);
257 <<
"Couldn't write " << num_bytes <<
" bytes to " 258 << tfile->get_filename() <<
"\n";
261 num_remaining -= count;
262 if (num_remaining == 0) {
265 _in->read(buffer, std::min((streamsize)buffer_size, num_remaining));
266 count = _in->gcount();
269 if (num_remaining != 0) {
271 <<
"Truncated data stream.\n";
285 return _in !=
nullptr ? _in->eof() :
true;
293 if (_in ==
nullptr) {
349 if (_in ==
nullptr) {
bool open_write(std::ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
Keeps a reference-counted pointer to a file on disk.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS's file system.
void set_binary()
Indicates that the filename represents a binary file.
uint64_t get_uint64()
Extracts an unsigned 64-bit integer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
The abstract base class for a file or directory within the VirtualFileSystem.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void close_read_file(std::istream *stream)
Closes a file opened by a previous call to open_read_file().
This is a special kind of FileReference class that automatically deletes the file in question when it...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The name of a file, such as a texture file or an Egg file.
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PointerTo< VirtualFile > get_file(const Filename &filename, bool status_only=false) const
Looks up the file by the indicated name in the file system.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class records a particular byte sub-range within an existing file on disk.
static Filename temporary(const std::string &dirname, const std::string &prefix, const std::string &suffix=std::string(), Type type=T_general)
Generates a temporary filename within the indicated directory, using the indicated prefix...
A class to read sequential binary data directly from an istream.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.