From 6d0b7f4344d9ccd17f7a34dc0d7d238c6438996a Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 26 Jan 2006 15:03:40 +0000 Subject: [PATCH] a bit more .pz support --- .../src/converter/somethingToEggConverter.cxx | 12 ++++++++ .../src/converter/somethingToEggConverter.h | 1 + pandatool/src/dxf/dxfFile.cxx | 30 +++++++++++++++---- pandatool/src/dxf/dxfFile.h | 4 +-- pandatool/src/dxfegg/dxfToEggConverter.cxx | 18 +++++++++-- pandatool/src/dxfegg/dxfToEggConverter.h | 1 + pandatool/src/dxfegg/dxfToEggLayer.cxx | 6 ++-- pandatool/src/flt/Sources.pp | 2 ++ pandatool/src/flt/fltHeader.cxx | 22 ++++++++++---- pandatool/src/fltegg/fltToEggConverter.cxx | 12 ++++++++ pandatool/src/fltegg/fltToEggConverter.h | 1 + pandatool/src/lwo/iffInputFile.cxx | 24 +++++++++------ pandatool/src/lwoegg/lwoToEggConverter.cxx | 12 ++++++++ pandatool/src/lwoegg/lwoToEggConverter.h | 1 + .../src/ptloader/loaderFileTypePandatool.cxx | 12 ++++++++ .../src/ptloader/loaderFileTypePandatool.h | 1 + pandatool/src/vrml/parse_vrml.cxx | 13 ++++---- pandatool/src/vrmlegg/vrmlToEggConverter.cxx | 12 ++++++++ pandatool/src/vrmlegg/vrmlToEggConverter.h | 1 + pandatool/src/xfile/Sources.pp | 2 ++ pandatool/src/xfile/xFile.cxx | 9 ++++++ .../src/xfileegg/xFileToEggConverter.cxx | 12 ++++++++ pandatool/src/xfileegg/xFileToEggConverter.h | 1 + 23 files changed, 177 insertions(+), 32 deletions(-) diff --git a/pandatool/src/converter/somethingToEggConverter.cxx b/pandatool/src/converter/somethingToEggConverter.cxx index afd4a19844..8dc443a272 100644 --- a/pandatool/src/converter/somethingToEggConverter.cxx +++ b/pandatool/src/converter/somethingToEggConverter.cxx @@ -93,6 +93,18 @@ get_additional_extensions() const { return string(); } +//////////////////////////////////////////////////////////////////// +// Function: SomethingToEggConverter::supports_compressed +// Access: Published, Virtual +// Description: Returns true if this file type can transparently load +// compressed files (with a .pz extension), false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool SomethingToEggConverter:: +supports_compressed() const { + return false; +} + //////////////////////////////////////////////////////////////////// // Function: SomethingToEggConverter::get_input_units // Access: Public, Virtual diff --git a/pandatool/src/converter/somethingToEggConverter.h b/pandatool/src/converter/somethingToEggConverter.h index a5de288488..270ddbae25 100644 --- a/pandatool/src/converter/somethingToEggConverter.h +++ b/pandatool/src/converter/somethingToEggConverter.h @@ -106,6 +106,7 @@ public: virtual string get_name() const=0; virtual string get_extension() const=0; virtual string get_additional_extensions() const; + virtual bool supports_compressed() const; virtual bool convert_file(const Filename &filename)=0; virtual DistanceUnit get_input_units(); diff --git a/pandatool/src/dxf/dxfFile.cxx b/pandatool/src/dxf/dxfFile.cxx index fb33eea9b3..b6e0322a93 100644 --- a/pandatool/src/dxf/dxfFile.cxx +++ b/pandatool/src/dxf/dxfFile.cxx @@ -18,6 +18,7 @@ #include "dxfFile.h" #include "string_utils.h" +#include "virtualFileSystem.h" DXFFile::Color DXFFile::_colors[DXF_num_colors] = { { 1, 1, 1 }, // Color 0 is not used. @@ -286,6 +287,8 @@ DXFFile::Color DXFFile::_colors[DXF_num_colors] = { //////////////////////////////////////////////////////////////////// DXFFile:: DXFFile() { + _in = NULL; + _owns_in = false; _layer = NULL; reset_entity(); _color_index = -1; @@ -298,6 +301,10 @@ DXFFile() { //////////////////////////////////////////////////////////////////// DXFFile:: ~DXFFile() { + if (_owns_in) { + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + vfs->close_read_file(_in); + } } @@ -310,19 +317,32 @@ DXFFile:: void DXFFile:: process(Filename filename) { filename.set_text(); - filename.open_read(_in_file); - process(_in_file); + + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + istream *in = vfs->open_read_file(filename, true); + if (in == (istream *)NULL) { + return; + } + process(in, true); } //////////////////////////////////////////////////////////////////// // Function: DXFFile::process // Access: Public -// Description: Reads the indicated stream as a DXF file. +// Description: Reads the indicated stream as a DXF file. If owns_in +// is true, then the istream will be deleted via +// vfs->close_read_file() when the DXFFile object +// destructs. //////////////////////////////////////////////////////////////////// void DXFFile:: -process(istream &in) { - _in = ∈ +process(istream *in, bool owns_in) { + if (_owns_in) { + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + vfs->close_read_file(_in); + } + _in = in; + _owns_in = owns_in; _state = ST_top; begin_file(); diff --git a/pandatool/src/dxf/dxfFile.h b/pandatool/src/dxf/dxfFile.h index e35e7fe532..49df66e84c 100644 --- a/pandatool/src/dxf/dxfFile.h +++ b/pandatool/src/dxf/dxfFile.h @@ -46,7 +46,7 @@ public: virtual ~DXFFile(); void process(Filename filename); - void process(istream &in); + void process(istream *in, bool owns_in); // These functions are called as the file is processed. These are // the main hooks for redefining how the class should dispense its @@ -151,7 +151,7 @@ protected: LMatrix4d _ocs2wcs; istream *_in; - ifstream _in_file; + bool _owns_in; int _code; string _string; diff --git a/pandatool/src/dxfegg/dxfToEggConverter.cxx b/pandatool/src/dxfegg/dxfToEggConverter.cxx index a97c7c0e18..c9bac453a8 100644 --- a/pandatool/src/dxfegg/dxfToEggConverter.cxx +++ b/pandatool/src/dxfegg/dxfToEggConverter.cxx @@ -82,6 +82,18 @@ get_extension() const { return "dxf"; } +//////////////////////////////////////////////////////////////////// +// Function: DXFToEggConverter::supports_compressed +// Access: Published, Virtual +// Description: Returns true if this file type can transparently load +// compressed files (with a .pz extension), false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool DXFToEggConverter:: +supports_compressed() const { + return true; +} + //////////////////////////////////////////////////////////////////// // Function: DXFToEggConverter::convert_file // Access: Public, Virtual @@ -144,10 +156,10 @@ done_entity() { // same). We'll add the vertices to our list of vertices and then // define the polygon. _verts.clear(); - _verts.push_back(DXFVertex(_p)); - _verts.push_back(DXFVertex(_q)); - _verts.push_back(DXFVertex(_r)); _verts.push_back(DXFVertex(_s)); + _verts.push_back(DXFVertex(_r)); + _verts.push_back(DXFVertex(_q)); + _verts.push_back(DXFVertex(_p)); nassertv(_layer!=NULL); ((DXFToEggLayer *)_layer)->add_polygon(this); diff --git a/pandatool/src/dxfegg/dxfToEggConverter.h b/pandatool/src/dxfegg/dxfToEggConverter.h index 3177f377dd..092e899d8e 100644 --- a/pandatool/src/dxfegg/dxfToEggConverter.h +++ b/pandatool/src/dxfegg/dxfToEggConverter.h @@ -39,6 +39,7 @@ public: virtual string get_name() const; virtual string get_extension() const; + virtual bool supports_compressed() const; virtual bool convert_file(const Filename &filename); diff --git a/pandatool/src/dxfegg/dxfToEggLayer.cxx b/pandatool/src/dxfegg/dxfToEggLayer.cxx index 863ca7c40a..2a750403f0 100644 --- a/pandatool/src/dxfegg/dxfToEggLayer.cxx +++ b/pandatool/src/dxfegg/dxfToEggLayer.cxx @@ -57,9 +57,9 @@ add_polygon(const DXFToEggConverter *entity) { const DXFFile::Color &color = entity->get_color(); poly->set_color(Colorf(color.r, color.g, color.b, 1.0)); - // A polyline's vertices are stored in the attached vector by dxf.C. - // They were defined in the DXF file using a series of "VERTEX" - // entries. + // A polyline's vertices are stored in the attached vector by + // dxf.cxx. They were defined in the DXF file using a series of + // "VERTEX" entries. // For a 3dface, the vertices are defined explicitly as part of the // entity; but in this case, they were added to the vector before diff --git a/pandatool/src/flt/Sources.pp b/pandatool/src/flt/Sources.pp index 5c7e6fd571..8b38053c46 100644 --- a/pandatool/src/flt/Sources.pp +++ b/pandatool/src/flt/Sources.pp @@ -1,3 +1,5 @@ +#define USE_PACKAGES zlib + #begin ss_lib_target #define TARGET flt #define LOCAL_LIBS converter pandatoolbase diff --git a/pandatool/src/flt/fltHeader.cxx b/pandatool/src/flt/fltHeader.cxx index 863452040c..0010b4aa19 100644 --- a/pandatool/src/flt/fltHeader.cxx +++ b/pandatool/src/flt/fltHeader.cxx @@ -21,8 +21,9 @@ #include "fltRecordWriter.h" #include "fltUnsupportedRecord.h" #include "config_flt.h" - +#include "zStream.h" #include "nearly_zero.h" +#include "virtualFileSystem.h" #include #include @@ -222,13 +223,15 @@ read_flt(Filename filename) { filename.set_binary(); _flt_filename = filename; - ifstream in; - if (!filename.open_read(in)) { + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + istream *in = vfs->open_read_file(filename, true); + if (in == (istream *)NULL) { assert(!flt_error_abort); return FE_could_not_open; } - - return read_flt(in); + FltError result = read_flt(*in); + vfs->close_read_file(in); + return result; } //////////////////////////////////////////////////////////////////// @@ -280,6 +283,15 @@ write_flt(Filename filename) { return FE_could_not_open; } +#ifdef HAVE_ZLIB + if (filename.get_extension() == "pz") { + // The filename ends in .pz, which means to automatically compress + // the flt file that we write. + OCompressStream compressor(&out, false); + return write_flt(compressor); + } +#endif // HAVE_ZLIB + return write_flt(out); } diff --git a/pandatool/src/fltegg/fltToEggConverter.cxx b/pandatool/src/fltegg/fltToEggConverter.cxx index d7f3c4f8be..02afed41e8 100644 --- a/pandatool/src/fltegg/fltToEggConverter.cxx +++ b/pandatool/src/fltegg/fltToEggConverter.cxx @@ -107,6 +107,18 @@ get_extension() const { return "flt"; } +//////////////////////////////////////////////////////////////////// +// Function: FltToEggConverter::supports_compressed +// Access: Published, Virtual +// Description: Returns true if this file type can transparently load +// compressed files (with a .pz extension), false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool FltToEggConverter:: +supports_compressed() const { + return true; +} + //////////////////////////////////////////////////////////////////// // Function: FltToEggConverter::convert_file // Access: Public, Virtual diff --git a/pandatool/src/fltegg/fltToEggConverter.h b/pandatool/src/fltegg/fltToEggConverter.h index e8d6d05beb..d756fd5ac5 100644 --- a/pandatool/src/fltegg/fltToEggConverter.h +++ b/pandatool/src/fltegg/fltToEggConverter.h @@ -63,6 +63,7 @@ public: virtual string get_name() const; virtual string get_extension() const; + virtual bool supports_compressed() const; virtual bool convert_file(const Filename &filename); virtual DistanceUnit get_input_units(); diff --git a/pandatool/src/lwo/iffInputFile.cxx b/pandatool/src/lwo/iffInputFile.cxx index bc092ff1aa..730544a179 100644 --- a/pandatool/src/lwo/iffInputFile.cxx +++ b/pandatool/src/lwo/iffInputFile.cxx @@ -18,9 +18,9 @@ #include "iffInputFile.h" #include "iffGenericChunk.h" - #include "datagram.h" #include "datagramIterator.h" +#include "virtualFileSystem.h" TypeHandle IffInputFile::_type_handle; @@ -46,7 +46,8 @@ IffInputFile() { IffInputFile:: ~IffInputFile() { if (_owns_istream) { - delete _input; + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + vfs->close_read_file(_input); } } @@ -59,13 +60,16 @@ IffInputFile:: bool IffInputFile:: open_read(Filename filename) { filename.set_binary(); - ifstream *in = new ifstream; - if (filename.open_read(*in)) { - set_input(in, true); - set_filename(filename); - return true; + + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + istream *in = vfs->open_read_file(filename, true); + if (in == (istream *)NULL) { + return false; } + set_input(in, true); + set_filename(filename); + return false; } @@ -74,12 +78,14 @@ open_read(Filename filename) { // Access: Public // Description: Sets up the input to use an arbitrary istream. If // owns_istream is true, the istream will be deleted -// when the IffInputFile destructs. +// (via vfs->close_read_file()) when the IffInputFile +// destructs. //////////////////////////////////////////////////////////////////// void IffInputFile:: set_input(istream *input, bool owns_istream) { if (_owns_istream) { - delete _input; + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + vfs->close_read_file(_input); } _input = input; _owns_istream = owns_istream; diff --git a/pandatool/src/lwoegg/lwoToEggConverter.cxx b/pandatool/src/lwoegg/lwoToEggConverter.cxx index 9f798388a4..e85e4aecdf 100644 --- a/pandatool/src/lwoegg/lwoToEggConverter.cxx +++ b/pandatool/src/lwoegg/lwoToEggConverter.cxx @@ -101,6 +101,18 @@ get_extension() const { return "lwo"; } +//////////////////////////////////////////////////////////////////// +// Function: LwoToEggConverter::supports_compressed +// Access: Published, Virtual +// Description: Returns true if this file type can transparently load +// compressed files (with a .pz extension), false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool LwoToEggConverter:: +supports_compressed() const { + return true; +} + //////////////////////////////////////////////////////////////////// // Function: LwoToEggConverter::convert_file // Access: Public, Virtual diff --git a/pandatool/src/lwoegg/lwoToEggConverter.h b/pandatool/src/lwoegg/lwoToEggConverter.h index 5a6517e76a..039746b6a2 100644 --- a/pandatool/src/lwoegg/lwoToEggConverter.h +++ b/pandatool/src/lwoegg/lwoToEggConverter.h @@ -55,6 +55,7 @@ public: virtual bool convert_file(const Filename &filename); bool convert_lwo(const LwoHeader *lwo_header); + virtual bool supports_compressed() const; CLwoLayer *get_layer(int number) const; CLwoClip *get_clip(int number) const; diff --git a/pandatool/src/ptloader/loaderFileTypePandatool.cxx b/pandatool/src/ptloader/loaderFileTypePandatool.cxx index b33dda3eab..d6946c64bd 100644 --- a/pandatool/src/ptloader/loaderFileTypePandatool.cxx +++ b/pandatool/src/ptloader/loaderFileTypePandatool.cxx @@ -79,6 +79,18 @@ get_additional_extensions() const { return _converter->get_additional_extensions(); } +//////////////////////////////////////////////////////////////////// +// Function: LoaderFileTypePandatool::supports_compressed +// Access: Published, Virtual +// Description: Returns true if this file type can transparently load +// compressed files (with a .pz extension), false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool LoaderFileTypePandatool:: +supports_compressed() const { + return _converter->supports_compressed(); +} + //////////////////////////////////////////////////////////////////// // Function: LoaderFileTypePandatool::resolve_filename // Access: Public, Virtual diff --git a/pandatool/src/ptloader/loaderFileTypePandatool.h b/pandatool/src/ptloader/loaderFileTypePandatool.h index 1b270f5131..d6a416ac9b 100644 --- a/pandatool/src/ptloader/loaderFileTypePandatool.h +++ b/pandatool/src/ptloader/loaderFileTypePandatool.h @@ -40,6 +40,7 @@ public: virtual string get_name() const; virtual string get_extension() const; virtual string get_additional_extensions() const; + virtual bool supports_compressed() const; virtual void resolve_filename(Filename &path) const; virtual PT(PandaNode) load_file(const Filename &path, const LoaderOptions &options) const; diff --git a/pandatool/src/vrml/parse_vrml.cxx b/pandatool/src/vrml/parse_vrml.cxx index 888fa4073e..9078f77fa2 100644 --- a/pandatool/src/vrml/parse_vrml.cxx +++ b/pandatool/src/vrml/parse_vrml.cxx @@ -33,6 +33,7 @@ #include "vrmlNode.h" #include "standard_nodes.h" #include "zStream.h" +#include "virtualFileSystem.h" extern int vrmlyyparse(); extern void vrmlyyResetLineNumber(); @@ -87,13 +88,15 @@ get_standard_nodes() { VrmlScene * parse_vrml(Filename filename) { filename.set_text(); - ifstream infile; - if (!filename.open_read(infile)) { - cerr << "Error, couldn't open " << filename << "\n"; + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + istream *in = vfs->open_read_file(filename, true); + if (in == (istream *)NULL) { + nout << "Cannot open " << filename << " for reading.\n"; return NULL; } - - return parse_vrml(infile, filename); + VrmlScene *result = parse_vrml(*in, filename); + vfs->close_read_file(in); + return result; } //////////////////////////////////////////////////////////////////// diff --git a/pandatool/src/vrmlegg/vrmlToEggConverter.cxx b/pandatool/src/vrmlegg/vrmlToEggConverter.cxx index f3c37580b6..08fa067a27 100644 --- a/pandatool/src/vrmlegg/vrmlToEggConverter.cxx +++ b/pandatool/src/vrmlegg/vrmlToEggConverter.cxx @@ -89,6 +89,18 @@ get_extension() const { return "wrl"; } +//////////////////////////////////////////////////////////////////// +// Function: VRMLToEggConverter::supports_compressed +// Access: Published, Virtual +// Description: Returns true if this file type can transparently load +// compressed files (with a .pz extension), false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool VRMLToEggConverter:: +supports_compressed() const { + return true; +} + //////////////////////////////////////////////////////////////////// // Function: VRMLToEggConverter::convert_file // Access: Public, Virtual diff --git a/pandatool/src/vrmlegg/vrmlToEggConverter.h b/pandatool/src/vrmlegg/vrmlToEggConverter.h index bea1ef724c..d0ae83be08 100644 --- a/pandatool/src/vrmlegg/vrmlToEggConverter.h +++ b/pandatool/src/vrmlegg/vrmlToEggConverter.h @@ -45,6 +45,7 @@ public: virtual string get_name() const; virtual string get_extension() const; + virtual bool supports_compressed() const; virtual bool convert_file(const Filename &filename); diff --git a/pandatool/src/xfile/Sources.pp b/pandatool/src/xfile/Sources.pp index 17bd2b1c66..760c3db647 100644 --- a/pandatool/src/xfile/Sources.pp +++ b/pandatool/src/xfile/Sources.pp @@ -1,6 +1,8 @@ #define YACC_PREFIX xyy #define LFLAGS -i +#define USE_PACKAGES zlib + #begin ss_lib_target #define TARGET xfile #define LOCAL_LIBS pandatoolbase diff --git a/pandatool/src/xfile/xFile.cxx b/pandatool/src/xfile/xFile.cxx index b7b28ac874..5632c4956d 100644 --- a/pandatool/src/xfile/xFile.cxx +++ b/pandatool/src/xfile/xFile.cxx @@ -161,6 +161,15 @@ write(Filename filename) const { return false; } +#ifdef HAVE_ZLIB + if (filename.get_extension() == "pz") { + // The filename ends in .pz, which means to automatically compress + // the X file that we write. + OCompressStream compressor(&out, false); + return write(compressor); + } +#endif // HAVE_ZLIB + return write(out); } diff --git a/pandatool/src/xfileegg/xFileToEggConverter.cxx b/pandatool/src/xfileegg/xFileToEggConverter.cxx index e58b0306d0..d0d461d8db 100644 --- a/pandatool/src/xfileegg/xFileToEggConverter.cxx +++ b/pandatool/src/xfileegg/xFileToEggConverter.cxx @@ -101,6 +101,18 @@ get_extension() const { return "x"; } +//////////////////////////////////////////////////////////////////// +// Function: XFileToEggConverter::supports_compressed +// Access: Published, Virtual +// Description: Returns true if this file type can transparently load +// compressed files (with a .pz extension), false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool XFileToEggConverter:: +supports_compressed() const { + return true; +} + //////////////////////////////////////////////////////////////////// // Function: XFileToEggConverter::convert_file // Access: Public, Virtual diff --git a/pandatool/src/xfileegg/xFileToEggConverter.h b/pandatool/src/xfileegg/xFileToEggConverter.h index 395cf290aa..bb22fc6884 100644 --- a/pandatool/src/xfileegg/xFileToEggConverter.h +++ b/pandatool/src/xfileegg/xFileToEggConverter.h @@ -53,6 +53,7 @@ public: virtual string get_name() const; virtual string get_extension() const; + virtual bool supports_compressed() const; virtual bool convert_file(const Filename &filename); void close();