15 #include "datagramOutputFile.h"
16 #include "streamWriter.h"
31 _filename = _file->get_filename();
42 _out = _vfile->open_write_file(
true,
true);
43 _owns_out = (_out != (ostream *)NULL);
44 return _owns_out && !_out->fail();
64 if (!filename.empty()) {
84 _out = (ostream *)NULL;
90 _wrote_first_datagram =
false;
105 nassertr(_out != (ostream *)NULL,
false);
106 nassertr(!_wrote_first_datagram,
false);
108 _out->write(header.data(), header.size());
109 thread_consider_yield();
110 return !_out->fail();
121 nassertr(_out != (ostream *)NULL,
false);
122 _wrote_first_datagram =
true;
127 if (num_bytes == (PN_uint32)-1 || num_bytes != (PN_uint32)num_bytes) {
138 thread_consider_yield();
140 return !_out->fail();
157 nassertr(_out != (ostream *)NULL,
false);
158 _wrote_first_datagram =
true;
165 istream *in = vfile->open_read_file(
true);
170 streamsize size = vfile->get_file_size(in);
171 streamsize num_remaining = size;
174 if (num_remaining == (PN_uint32)-1 || num_remaining != (PN_uint32)num_remaining) {
183 static const size_t buffer_size = 4096;
184 char buffer[buffer_size];
186 streampos start = _out->tellp();
187 in->read(buffer, min((streamsize)buffer_size, num_remaining));
188 streamsize count = in->gcount();
190 _out->write(buffer, count);
192 vfile->close_read_file(in);
195 num_remaining -= count;
196 if (num_remaining == 0) {
199 in->read(buffer, min((streamsize)buffer_size, num_remaining));
200 count = in->gcount();
203 vfile->close_read_file(in);
205 if (num_remaining != 0) {
207 <<
"Truncated input stream.\n";
230 nassertr(_out != (ostream *)NULL,
false);
231 _wrote_first_datagram =
true;
238 streamsize num_remaining = source.
get_size();
241 if (num_remaining == (PN_uint32)-1 || num_remaining != (PN_uint32)num_remaining) {
250 static const size_t buffer_size = 4096;
251 char buffer[buffer_size];
253 streampos start = _out->tellp();
255 in.read(buffer, min((streamsize)buffer_size, num_remaining));
256 streamsize count = in.gcount();
258 _out->write(buffer, count);
262 num_remaining -= count;
263 if (num_remaining == 0) {
266 in.read(buffer, min((streamsize)buffer_size, num_remaining));
270 if (num_remaining != 0) {
272 <<
"Truncated input stream.\n";
288 if (_out == (ostream *)NULL) {
306 if (_out != (ostream *)NULL) {
350 if (_out == (ostream *)NULL) {
353 return _out->tellp();
A StreamWriter object is used to write sequential binary data directly to an ostream.
virtual streampos get_file_pos()
Returns the current file position within the data stream, if any, or 0 if the file position is not me...
streamsize get_size() const
Returns the number of consecutive bytes, beginning at get_start(), that correspond to this file data...
Keeps a reference-counted pointer to a file on disk.
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.
bool write_header(const string &header)
Writes a sequence of bytes to the beginning of the datagram file.
bool open_read(ifstream &stream) const
Opens the indicated ifstream for reading the file, if possible.
virtual const Filename & get_filename()
Returns the filename that provides the target for these datagrams, if any, or empty string if the dat...
virtual bool is_error()
Returns true if the file has reached an error condition.
streampos get_start() const
Returns the offset within the file at which this file data begins.
The abstract base class for a file or directory within the VirtualFileSystem.
size_t get_length() const
Returns the number of bytes in the datagram.
PointerTo< VirtualFile > create_file(const Filename &filename)
Attempts to create a file by the indicated name in the filesystem, if possible, and returns it...
virtual bool copy_datagram(SubfileInfo &result, const Filename &filename)
Copies the file data from the entire indicated file (via the vfs) as the next datagram.
bool open(const FileReference *file)
Opens the indicated filename for writing.
virtual bool put_datagram(const Datagram &data)
Writes the given datagram to the file.
The name of a file, such as a texture file or an Egg file.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
void add_uint32(PN_uint32 value)
Adds an unsigned 32-bit integer to the stream.
virtual void flush()
Ensures that all datagrams previously written will be visible in the output file. ...
PointerTo< VirtualFile > get_file(const Filename &filename, bool status_only=false) const
Looks up the file by the indicated name in the file system.
virtual const FileReference * get_file()
Returns the FileReference that provides the target for these datagrams, if any, or NULL if the datagr...
const Filename & get_filename() const
A shortcut to the filename.
This class records a particular byte sub-range within an existing file on disk.
const void * get_data() const
Returns a pointer to the beginning of the datagram's data.
static void close_write_file(ostream *stream)
Closes a file opened by a previous call to open_write_file().
void close()
Closes the file.
void add_uint64(PN_uint64 value)
Adds an unsigned 64-bit integer to the stream.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...