diff --git a/panda/src/audio/Sources.pp b/panda/src/audio/Sources.pp index 438eea505d..c19b08a207 100644 --- a/panda/src/audio/Sources.pp +++ b/panda/src/audio/Sources.pp @@ -24,7 +24,7 @@ audio_trait.h audio_mikmod_traits.h \ audio_win_traits.I audio_win_traits.h audio_null_traits.I \ audio_null_traits.h audio_linux_traits.I audio_linux_traits.h \ - audio_music.I config_audio.h audio_manager.I audio_sound.h audio_sound.I + config_audio.h audio_manager.I audio_sound.h audio_sound.I #define IGATESCAN audio.h diff --git a/panda/src/express/datagramGenerator.h b/panda/src/express/datagramGenerator.h index a770e899fe..ecac48fd9e 100644 --- a/panda/src/express/datagramGenerator.h +++ b/panda/src/express/datagramGenerator.h @@ -20,7 +20,7 @@ public: INLINE DatagramGenerator(); virtual ~DatagramGenerator(); - virtual bool get_datagram(Datagram& dataBlock) = 0; + virtual bool get_datagram(Datagram &data) = 0; virtual bool is_eof() = 0; virtual bool is_error() = 0; }; diff --git a/panda/src/express/datagramInputFile.I b/panda/src/express/datagramInputFile.I new file mode 100644 index 0000000000..d1f59e947d --- /dev/null +++ b/panda/src/express/datagramInputFile.I @@ -0,0 +1,42 @@ +// Filename: datagramInputFile.I +// Created by: drose (30Oct00) +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: DatagramInputFile::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE DatagramInputFile:: +DatagramInputFile() { + _error = true; + _read_first_datagram = false; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramInputFile::open +// Access: Public +// Description: Opens the indicated filename for reading. Returns +// true if successful, false on failure. +//////////////////////////////////////////////////////////////////// +INLINE bool DatagramInputFile:: +open(Filename filename) { + // DatagramInputFiles are always binary. + _read_first_datagram = false; + _error = false; + filename.set_binary(); + return filename.open_read(_in); +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramInputFile::close +// Access: Public +// Description: Closes the file. This is also implicitly done when +// the DatagramInputFile destructs. +//////////////////////////////////////////////////////////////////// +INLINE void DatagramInputFile:: +close() { + _in.close(); +} diff --git a/panda/src/express/datagramInputFile.cxx b/panda/src/express/datagramInputFile.cxx new file mode 100644 index 0000000000..7d9930b7b8 --- /dev/null +++ b/panda/src/express/datagramInputFile.cxx @@ -0,0 +1,84 @@ +// Filename: datagramInputFile.h +// Created by: drose (30Oct00) +// +//////////////////////////////////////////////////////////////////// + +#include "datagramInputFile.h" +#include "numeric_types.h" +#include "datagramIterator.h" + +//////////////////////////////////////////////////////////////////// +// Function: DatagramInputFile::read_header +// Access: Public +// Description: Reads a sequence of bytes from the beginning of the +// datagram file. This may be called any number of +// times after the file has been opened and before the +// first datagram is written. It may not be called once +// the first datagram is written. +//////////////////////////////////////////////////////////////////// +bool DatagramInputFile:: +read_header(string &header, size_t num_bytes) { + nassertr(!_read_first_datagram, false); + + char *buffer = (char *)alloca(num_bytes); + nassertr(buffer != (char *)NULL, false); + + _in.read(buffer, num_bytes); + if (_in.fail() || _in.eof()) { + return false; + } + + header = string(buffer, num_bytes); + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramInputFile::get_datagram +// Access: Public, Virtual +// Description: Reads the next datagram from the file. Returns true +// on success, false if there is an error or end of +// file. +//////////////////////////////////////////////////////////////////// +bool DatagramInputFile:: +get_datagram(Datagram &data) { + _read_first_datagram = true; + + // First, get the size of the upcoming datagram. We do this with + // the help of a second datagram. + char sizebuf[sizeof(PN_uint32)]; + _in.read(sizebuf, sizeof(PN_uint32)); + if (_in.fail() || _in.eof()) { + return false; + } + + Datagram size(sizebuf, sizeof(PN_uint32)); + DatagramIterator di(size); + PN_uint32 num_bytes = di.get_uint32(); + + // Now, read the datagram itself. + char *buffer = (char *)alloca(num_bytes); + nassertr(buffer != (char *)NULL, false); + + _in.read(buffer, num_bytes); + if (_in.fail() || _in.eof()) { + _error = true; + return false; + } + + data = Datagram(buffer, num_bytes); + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramInputFile::is_error +// Access: Public, Virtual +// Description: Returns true if the file has reached an error +// condition. +//////////////////////////////////////////////////////////////////// +bool DatagramInputFile:: +is_error() { + if (_in.fail()) { + _error = true; + } + return _error; +} diff --git a/panda/src/express/datagramInputFile.h b/panda/src/express/datagramInputFile.h new file mode 100644 index 0000000000..a3a659203f --- /dev/null +++ b/panda/src/express/datagramInputFile.h @@ -0,0 +1,41 @@ +// Filename: datagramInputFile.h +// Created by: drose (30Oct00) +// +//////////////////////////////////////////////////////////////////// + +#ifndef DATAGRAMINPUTFILE_H +#define DATAGRAMINPUTFILE_H + +#include + +#include "datagramGenerator.h" +#include "filename.h" + +//////////////////////////////////////////////////////////////////// +// Class : DatagramInputFile +// Description : This class can be used to read a binary file that +// consists of an arbitrary header followed by a number +// of datagrams. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDAEXPRESS DatagramInputFile : public DatagramGenerator { +public: + INLINE DatagramInputFile(); + + INLINE bool open(Filename filename); + + bool read_header(string &header, size_t num_bytes); + virtual bool get_datagram(Datagram &data); + virtual bool is_eof(); + virtual bool is_error(); + + INLINE void close(); + +private: + bool _read_first_datagram; + bool _error; + ifstream _in; +}; + +#include "datagramInputFile.I" + +#endif diff --git a/panda/src/express/datagramOutputFile.I b/panda/src/express/datagramOutputFile.I index ee9621ee69..bf4a867a6f 100644 --- a/panda/src/express/datagramOutputFile.I +++ b/panda/src/express/datagramOutputFile.I @@ -30,20 +30,6 @@ open(Filename filename) { return filename.open_write(_out); } -//////////////////////////////////////////////////////////////////// -// Function: DatagramOutputFile::is_error -// Access: Public -// Description: Returns true if the file has reached an error -// condition. -//////////////////////////////////////////////////////////////////// -INLINE bool DatagramOutputFile:: -is_error() { - if (_out.fail()) { - _error = true; - } - return _error; -} - //////////////////////////////////////////////////////////////////// // Function: DatagramOutputFile::close // Access: Public diff --git a/panda/src/express/datagramOutputFile.cxx b/panda/src/express/datagramOutputFile.cxx new file mode 100644 index 0000000000..2559d63aaf --- /dev/null +++ b/panda/src/express/datagramOutputFile.cxx @@ -0,0 +1,59 @@ +// Filename: datagramOutputFile.h +// Created by: drose (30Oct00) +// +//////////////////////////////////////////////////////////////////// + +#include "datagramOutputFile.h" + +//////////////////////////////////////////////////////////////////// +// Function: DatagramOutputFile::write_header +// Access: Public +// Description: Writes a sequence of bytes to the beginning of the +// datagram file. This may be called any number of +// times after the file has been opened and before the +// first datagram is written. It may not be called once +// the first datagram is written. +//////////////////////////////////////////////////////////////////// +bool DatagramOutputFile:: +write_header(const string &header) { + nassertr(!_wrote_first_datagram, false); + + _out.write((void *)header.data(), header.size()); + return !_out.fail(); +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramOutputFile::put_datagram +// Access: Public, Virtual +// Description: Writes the given datagram to the file. Returns true +// on success, false if there is an error. +//////////////////////////////////////////////////////////////////// +bool DatagramOutputFile:: +put_datagram(const Datagram &data) { + _wrote_first_datagram = true; + + // First, write the size of the upcoming datagram. We do this with + // the help of a second datagram. + Datagram size; + size.add_uint32(data.get_length()); + _out.write(size.get_data(), size.get_length()); + + // Now, write the datagram itself. + _out.write(data.get_data(), data.get_length()); + + return !_out.fail(); +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramOutputFile::is_error +// Access: Public, Virtual +// Description: Returns true if the file has reached an error +// condition. +//////////////////////////////////////////////////////////////////// +bool DatagramOutputFile:: +is_error() { + if (_out.fail()) { + _error = true; + } + return _error; +} diff --git a/panda/src/express/datagramOutputFile.h b/panda/src/express/datagramOutputFile.h new file mode 100644 index 0000000000..0ea37b34d2 --- /dev/null +++ b/panda/src/express/datagramOutputFile.h @@ -0,0 +1,40 @@ +// Filename: datagramOutputFile.h +// Created by: drose (30Oct00) +// +//////////////////////////////////////////////////////////////////// + +#ifndef DATAGRAMOUTPUTFILE_H +#define DATAGRAMOUTPUTFILE_H + +#include + +#include "datagramSink.h" +#include "filename.h" + +//////////////////////////////////////////////////////////////////// +// Class : DatagramOutputFile +// Description : This class can be used to write a binary file that +// consists of an arbitrary header followed by a number +// of datagrams. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDAEXPRESS DatagramOutputFile : public DatagramSink { +public: + INLINE DatagramOutputFile(); + + INLINE bool open(Filename filename); + + bool write_header(const string &header); + virtual bool put_datagram(const Datagram &data); + virtual bool is_error(); + + INLINE void close(); + +private: + bool _wrote_first_datagram; + bool _error; + ofstream _out; +}; + +#include "datagramOutputFile.I" + +#endif diff --git a/panda/src/express/datagramSink.h b/panda/src/express/datagramSink.h index 5f4d821899..6ea3b9929f 100644 --- a/panda/src/express/datagramSink.h +++ b/panda/src/express/datagramSink.h @@ -20,7 +20,8 @@ public: INLINE DatagramSink(void); virtual ~DatagramSink(void); - virtual bool put_datagram(const Datagram& dataBlock) = 0; + virtual bool put_datagram(const Datagram &data) = 0; + virtual bool is_error() = 0; }; #include "datagramSink.I" diff --git a/panda/src/loader/bamFile.cxx b/panda/src/loader/bamFile.cxx index 5f725ff298..10309cdbc4 100644 --- a/panda/src/loader/bamFile.cxx +++ b/panda/src/loader/bamFile.cxx @@ -64,7 +64,12 @@ open_read(const Filename &filename, bool report_errors) { return false; } - string head = _din.read_header(_bam_header.size()); + string head; + if (!_din.read_header(head, _bam_header.size())) { + loader_cat.error() << bam_filename << " is not a valid BAM file.\n"; + return false; + } + if (head != _bam_header) { loader_cat.error() << bam_filename << " is not a valid BAM file.\n"; return false;