diff --git a/include/osgDB/ReaderWriter b/include/osgDB/ReaderWriter index 77c8b54e1..03e53591a 100644 --- a/include/osgDB/ReaderWriter +++ b/include/osgDB/ReaderWriter @@ -41,13 +41,87 @@ class OSGDB_EXPORT ReaderWriter : public osg::Referenced }; - virtual osg::Object* readObject(const std::string& /*fileName*/,const Options* =NULL) { return NULL; } - virtual osg::Image* readImage(const std::string& /*fileName*/,const Options* =NULL) { return NULL; } - virtual osg::Node* readNode(const std::string& /*fileName*/,const Options* =NULL) { return NULL; } + class ReadResult + { + public: - virtual bool writeObject(const osg::Object& /*obj*/,const std::string& /*fileName*/,const Options* =NULL) {return false; } - virtual bool writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) {return false; } - virtual bool writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) { return false; } + enum Status + { + FILE_NOT_HANDLED, + FILE_LOADED, + ERROR_IN_READING_FILE + }; + + ReadResult(Status status=FILE_NOT_HANDLED):_status(status) {} + ReadResult(const std::string& m):_status(ERROR_IN_READING_FILE),_message(m) {} + ReadResult(osg::Object* obj):_status(FILE_LOADED),_object(obj) {} + + ReadResult(const ReadResult& rr):_status(rr._status),_message(rr._message),_object(rr._object) {} + ReadResult& operator = (const ReadResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message;_object=rr._object; return *this; } + + osg::Object* getObject() { return _object.get(); } + osg::Image* getImage() { return dynamic_cast(_object.get()); } + osg::Node* getNode() { return dynamic_cast(_object.get()); } + + const bool validObject() { return _object.valid(); } + const bool validImage() { return getImage()!=0; } + const bool validNode() { return getNode()!=0; } + + osg::Object* takeObject() { osg::Object* obj = _object.get(); if (obj) { obj->ref(); _object=NULL; } return obj; } + osg::Image* takeImage() { osg::Image* image=dynamic_cast(_object.get()); if (image) { image->ref(); _object==NULL; } return image; } + osg::Node* takeNode() { osg::Node* node=dynamic_cast(_object.get()); if (node) { node->ref(); _object==NULL; } return node; } + + const std::string& message() const { return _message; } + + const Status status() const { return _status; } + const bool success() const { return _status==FILE_LOADED; } + const bool error() const { return _status==ERROR_IN_READING_FILE; } + const bool notHandled() const { return _status==FILE_NOT_HANDLED; } + + protected: + + Status _status; + std::string _message; + osg::ref_ptr _object; + }; + + class WriteResult + { + public: + + enum Status + { + FILE_NOT_HANDLED, + FILE_SAVED, + ERROR_IN_WRITING_FILE + }; + + WriteResult(Status status=FILE_NOT_HANDLED):_status(status) {} + WriteResult(const std::string& m):_status(ERROR_IN_WRITING_FILE),_message(m) {} + + WriteResult(const WriteResult& rr):_status(rr._status),_message(rr._message) {} + WriteResult& operator = (const WriteResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message; return *this; } + + const std::string& message() const { return _message; } + + const Status status() const { return _status; } + const bool success() const { return _status==FILE_SAVED; } + const bool error() const { return _status==ERROR_IN_WRITING_FILE; } + const bool notHandled() const { return _status==FILE_NOT_HANDLED; } + + protected: + + Status _status; + std::string _message; + }; + + virtual ReadResult readObject(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } + virtual ReadResult readImage(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } + virtual ReadResult readNode(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } + + virtual WriteResult writeObject(const osg::Object& /*obj*/,const std::string& /*fileName*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } + virtual WriteResult writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } + virtual WriteResult writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) { return WriteResult(WriteResult::FILE_NOT_HANDLED); } }; diff --git a/src/osgDB/Registry.cpp b/src/osgDB/Registry.cpp index 7d396b3c1..24f7f895a 100644 --- a/src/osgDB/Registry.cpp +++ b/src/osgDB/Registry.cpp @@ -560,6 +560,10 @@ Object* Registry::readObject(const std::string& fileName) // record the existing reader writer. std::set rwOriginal; + + // record the errors reported by readerwriters. + typedef std::vector Results; + Results results; // first attempt to load the file from existing ReaderWriter's for(ReaderWriterList::iterator itr=_rwList.begin(); @@ -567,8 +571,9 @@ Object* Registry::readObject(const std::string& fileName) ++itr) { rwOriginal.insert(itr->get()); - Object* obj = (*itr)->readObject(file,_options.get()); - if (obj) return obj; + ReaderWriter::ReadResult rr = (*itr)->readObject(file,_options.get()); + if (rr.validObject()) return rr.takeObject(); + else if (rr.error()) results.push_back(rr); } // now look for a plug-in to load the file. @@ -581,17 +586,27 @@ Object* Registry::readObject(const std::string& fileName) { if (rwOriginal.find(itr->get())==rwOriginal.end()) { - Object* obj = (*itr)->readObject(file,_options.get()); - if (obj) return obj; + ReaderWriter::ReadResult rr = (*itr)->readObject(file,_options.get()); + if (rr.validObject()) return rr.takeObject(); + else if (rr.error()) results.push_back(rr); } } } - else + + if (results.empty()) { notify(NOTICE)<<"Warning: Could not find plugin to read file with extension ." <message()< rwOriginal; + // record the errors reported by readerwriters. + typedef std::vector Results; + Results results; + // first attempt to load the file from existing ReaderWriter's for(ReaderWriterList::iterator itr=_rwList.begin(); itr!=_rwList.end(); ++itr) { rwOriginal.insert(itr->get()); - if ((*itr)->writeObject(obj,fileName,_options.get())) return true; + ReaderWriter::WriteResult rr = (*itr)->writeObject(obj,fileName,_options.get()); + if (rr.success()) return true; + else if (rr.error()) results.push_back(rr); } // now look for a plug-in to save the file. @@ -621,16 +642,27 @@ bool Registry::writeObject(const Object& obj,const std::string& fileName) { if (rwOriginal.find(itr->get())==rwOriginal.end()) { - if ((*itr)->writeObject(obj,fileName,_options.get())) return true; + ReaderWriter::WriteResult rr = (*itr)->writeObject(obj,fileName,_options.get()); + if (rr.success()) return true; + else if (rr.error()) results.push_back(rr); } } } - else + + if (results.empty()) { notify(NOTICE)<<"Warning: Could not find plugin to write file with extension ." <message()< rwOriginal; + // record the errors reported by readerwriters. + typedef std::vector Results; + Results results; + // first attempt to load the file from existing ReaderWriter's for(ReaderWriterList::iterator itr=_rwList.begin(); itr!=_rwList.end(); ++itr) { rwOriginal.insert(itr->get()); - Image* image = (*itr)->readImage(file,_options.get()); - if (image) return image; + ReaderWriter::ReadResult rr = (*itr)->readImage(file,_options.get()); + if (rr.validImage()) return rr.takeImage(); + else if (rr.error()) results.push_back(rr); } // now look for a plug-in to load the file. @@ -665,17 +702,27 @@ Image* Registry::readImage(const std::string& fileName) { if (rwOriginal.find(itr->get())==rwOriginal.end()) { - Image* image = (*itr)->readImage(file,_options.get()); - if (image) return image; + ReaderWriter::ReadResult rr = (*itr)->readImage(file,_options.get()); + if (rr.validImage()) return rr.takeImage(); + else if (rr.error()) results.push_back(rr); } } } - else + + if (results.empty()) { notify(NOTICE)<<"Warning: Could not find plugin to read file with extension ." <message()< rwOriginal; + // record the errors reported by readerwriters. + typedef std::vector Results; + Results results; + // first attempt to load the file from existing ReaderWriter's for(ReaderWriterList::iterator itr=_rwList.begin(); itr!=_rwList.end(); ++itr) { rwOriginal.insert(itr->get()); - if ((*itr)->writeImage(image,fileName,_options.get())) return true; + ReaderWriter::WriteResult rr = (*itr)->writeImage(image,fileName,_options.get()); + if (rr.success()) return true; + else if (rr.error()) results.push_back(rr); } // now look for a plug-in to save the file. @@ -705,16 +758,27 @@ bool Registry::writeImage(const Image& image,const std::string& fileName) { if (rwOriginal.find(itr->get())==rwOriginal.end()) { - if ((*itr)->writeImage(image,fileName,_options.get())) return true; + ReaderWriter::WriteResult rr = (*itr)->writeImage(image,fileName,_options.get()); + if (rr.success()) return true; + else if (rr.error()) results.push_back(rr); } } } - else + + if (results.empty()) { notify(NOTICE)<<"Warning: Could not find plugin to write file with extension ." <message()< rwOriginal; + // record the errors reported by readerwriters. + typedef std::vector Results; + Results results; + // first attempt to load the file from existing ReaderWriter's for(ReaderWriterList::iterator itr=_rwList.begin(); itr!=_rwList.end(); ++itr) { rwOriginal.insert(itr->get()); - Node* node = (*itr)->readNode(file,_options.get()); - if (node) return node; + ReaderWriter::ReadResult rr = (*itr)->readNode(file,_options.get()); + if (rr.validNode()) return rr.takeNode(); + else if (rr.error()) results.push_back(rr); } bool couldNotFindPlugin = false; @@ -752,8 +821,9 @@ Node* Registry::readNode(const std::string& fileName) { if (rwOriginal.find(itr->get())==rwOriginal.end()) { - Node* node = (*itr)->readNode(file,_options.get()); - if (node) return node; + ReaderWriter::ReadResult rr = (*itr)->readNode(file,_options.get()); + if (rr.validNode()) return rr.takeNode(); + else if (rr.error()) results.push_back(rr); } } } @@ -762,22 +832,38 @@ Node* Registry::readNode(const std::string& fileName) couldNotFindPlugin = true; } - if (_createNodeFromImage) - { - ref_ptr image = readImage(fileName); - if (image.valid()) - { - return createGeodeForImage(image.get()); - } - } +// +// if (_createNodeFromImage) +// { +// ref_ptr image = readImage(fileName); +// if (image.valid()) +// { +// return createGeodeForImage(image.get()); +// } +// } +// +// if (couldNotFindPlugin) +// { +// notify(NOTICE)<<"Warning: Could not find plugin to read file with extension ." +// <message()< rwOriginal; + // record the errors reported by readerwriters. + typedef std::vector Results; + Results results; + // first attempt to write the file from existing ReaderWriter's for(ReaderWriterList::iterator itr=_rwList.begin(); itr!=_rwList.end(); ++itr) { rwOriginal.insert(itr->get()); - if ((*itr)->writeNode(node,fileName,_options.get())) return true; + ReaderWriter::WriteResult rr = (*itr)->writeNode(node,fileName,_options.get()); + if (rr.success()) return true; + else if (rr.error()) results.push_back(rr); } // now look for a plug-in to save the file. @@ -807,16 +899,27 @@ bool Registry::writeNode(const Node& node,const std::string& fileName) { if (rwOriginal.find(itr->get())==rwOriginal.end()) { - if ((*itr)->writeNode(node,fileName,_options.get())) return true; + ReaderWriter::WriteResult rr = (*itr)->writeNode(node,fileName,_options.get()); + if (rr.success()) return true; + else if (rr.error()) results.push_back(rr); } } } - else + + if (results.empty()) { notify(NOTICE)<<"Warning: Could not find plugin to write file with extension ." <message()< FaceList; typedef std::map GeoStateMap; @@ -59,11 +59,11 @@ ReaderWriter3DS::ReaderWriter3DS() } -osg::Node* ReaderWriter3DS::readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) +osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) { Lib3dsFile *f = lib3ds_open(fileName.c_str()); - if (f==NULL) return NULL; + if (f==NULL) return ReadResult::FILE_NOT_HANDLED; _directory = osgDB::getFilePath(fileName); diff --git a/src/osgPlugins/lwo/ReaderWriterLWO.cpp b/src/osgPlugins/lwo/ReaderWriterLWO.cpp index 8398f3df9..a5f8bb024 100644 --- a/src/osgPlugins/lwo/ReaderWriterLWO.cpp +++ b/src/osgPlugins/lwo/ReaderWriterLWO.cpp @@ -43,7 +43,7 @@ public: return (extension == "lwo" || extension == "lw" || extension == "geo"); } - virtual osg::Node* readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*); + virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*); protected: }; @@ -54,11 +54,11 @@ osgDB::RegisterReaderWriterProxy g_lwoReaderWriterProxy; // read file and convert to OSG. -osg::Node* ReaderWriterLWO::readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) +osgDB::ReaderWriter::ReadResult ReaderWriterLWO::readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) { lwObject* lw = lw_object_read(fileName.c_str()); if (!lw) - return NULL; + return ReadResult::FILE_NOT_HANDLED; osg::notify(osg::INFO) << "faces " << lw->face_cnt << endl; osg::notify(osg::INFO) << "materials " << lw->material_cnt << endl; diff --git a/src/osgPlugins/obj/ReaderWriterOBJ.cpp b/src/osgPlugins/obj/ReaderWriterOBJ.cpp index 8aa194703..8aadde108 100644 --- a/src/osgPlugins/obj/ReaderWriterOBJ.cpp +++ b/src/osgPlugins/obj/ReaderWriterOBJ.cpp @@ -48,7 +48,7 @@ public: return (extension == "obj"); } - virtual osg::Node* readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*); + virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*); protected: osg::Drawable* makeDrawable(GLMmodel* obj, GLMgroup* grp, osg::StateSet**); @@ -60,11 +60,11 @@ osgDB::RegisterReaderWriterProxy g_objReaderWriterProxy; // read file and convert to OSG. -osg::Node* ReaderWriterOBJ::readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) +osgDB::ReaderWriter::ReadResult ReaderWriterOBJ::readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) { GLMmodel* obj = glmReadOBJ((char*) fileName.c_str()); if (!obj) - return NULL; + return ReadResult::FILE_NOT_HANDLED; std::string directory = osgDB::getFilePath(fileName); diff --git a/src/osgPlugins/osg/ReaderWriterOSG.cpp b/src/osgPlugins/osg/ReaderWriterOSG.cpp index c64431093..e0c0baac3 100644 --- a/src/osgPlugins/osg/ReaderWriterOSG.cpp +++ b/src/osgPlugins/osg/ReaderWriterOSG.cpp @@ -19,12 +19,12 @@ class OSGReaderWriter : public ReaderWriter return equalCaseInsensitive(extension,"osg"); } - virtual Object* readObject(const std::string& fileName, const osgDB::ReaderWriter::Options* opt) { return readNode(fileName,opt); } + virtual ReadResult readObject(const std::string& fileName, const Options* opt) { return readNode(fileName,opt); } - virtual Node* readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual ReadResult readNode(const std::string& fileName, const Options*) { std::string ext = getFileExtension(fileName); - if (!acceptsExtension(ext)) return NULL; + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; ifstream fin(fileName.c_str()); if (fin) @@ -60,7 +60,7 @@ class OSGReaderWriter : public ReaderWriter } // group->getNumChildren()==0 else { - return 0L; + return ReadResult("No data loaded from "+fileName); } } @@ -70,7 +70,7 @@ class OSGReaderWriter : public ReaderWriter } } - virtual bool writeObject(const Object& obj,const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual WriteResult writeObject(const Object& obj,const std::string& fileName, const osgDB::ReaderWriter::Options*) { Output fout; fout.open(fileName.c_str()); @@ -78,12 +78,12 @@ class OSGReaderWriter : public ReaderWriter { fout.writeObject(obj); fout.close(); - return true; + return WriteResult::FILE_SAVED; } - return false; + return WriteResult("Unable to open file for output"); } - virtual bool writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options*) { Output fout; fout.open(fileName.c_str()); @@ -91,9 +91,9 @@ class OSGReaderWriter : public ReaderWriter { fout.writeObject(node); fout.close(); - return true; + return WriteResult::FILE_SAVED; } - return false; + return WriteResult("Unable to open file for output"); } }; diff --git a/src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp b/src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp index e0f449e1e..1752a5c49 100644 --- a/src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp +++ b/src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp @@ -30,10 +30,10 @@ class sgReaderWriterOSGTGZ : public osgDB::ReaderWriter return osgDB::equalCaseInsensitive(extension,"osgtgz"); } - virtual Node* readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) { std::string ext = osgDB::getFileExtension(fileName); - if (!acceptsExtension(ext)) return NULL; + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; osg::notify(osg::INFO)<<"sgReaderWriterOSGTGZ::readNode( "<getNumChildren() == 0 ) { grp->unref(); - return NULL; + return ReadResult::FILE_NOT_HANDLED; } return grp; diff --git a/src/osgPlugins/pfb/ReaderWriterPFB.cpp b/src/osgPlugins/pfb/ReaderWriterPFB.cpp index 7edddf0cc..6ef8c88bd 100644 --- a/src/osgPlugins/pfb/ReaderWriterPFB.cpp +++ b/src/osgPlugins/pfb/ReaderWriterPFB.cpp @@ -95,7 +95,7 @@ class ReaderWriterPFB : public osgDB::ReaderWriter false; } - virtual osg::Image* readImage(const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual ReadResult readImage(const std::string& fileName, const osgDB::ReaderWriter::Options*) { osg::notify(osg::INFO)<< "ReaderWriterPFB::readImage( "<sizeX; diff --git a/src/osgPlugins/tga/ReaderWriterTGA.cpp b/src/osgPlugins/tga/ReaderWriterTGA.cpp index 6b5c67a6f..019a8b8d6 100644 --- a/src/osgPlugins/tga/ReaderWriterTGA.cpp +++ b/src/osgPlugins/tga/ReaderWriterTGA.cpp @@ -469,7 +469,7 @@ class ReaderWriterTGA : public osgDB::ReaderWriter virtual const char* className() { return "TGA Image Reader"; } virtual bool acceptsExtension(const std::string& extension) { return extension=="tga"; } - virtual osg::Image* readImage(const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual ReadResult readImage(const std::string& fileName, const osgDB::ReaderWriter::Options*) { unsigned char *imageData = NULL; @@ -479,7 +479,7 @@ class ReaderWriterTGA : public osgDB::ReaderWriter imageData = simage_tga_load(fileName.c_str(),&width_ret,&height_ret,&numComponents_ret); - if (imageData==NULL) return NULL; + if (imageData==NULL) return ReadResult::FILE_NOT_HANDLED; int s = width_ret; int t = height_ret; diff --git a/src/osgPlugins/tgz/ReaderWriterTGZ.cpp b/src/osgPlugins/tgz/ReaderWriterTGZ.cpp index ba12b147a..13a60eefa 100644 --- a/src/osgPlugins/tgz/ReaderWriterTGZ.cpp +++ b/src/osgPlugins/tgz/ReaderWriterTGZ.cpp @@ -31,10 +31,10 @@ class ReaderWriterTGZ : public osgDB::ReaderWriter return osgDB::equalCaseInsensitive(extension,"tgz"); } - virtual Node* readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) { std::string ext = osgDB::getLowerCaseFileExtension(fileName); - if (!acceptsExtension(ext)) return NULL; + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; osg::notify(osg::INFO)<< "ReaderWriterTGZ::readNode( "<getNumChildren() == 0 ) { grp->unref(); - return NULL; + return ReadResult::FILE_NOT_HANDLED; } return grp; diff --git a/src/osgPlugins/tiff/ReaderWriterTIFF.cpp b/src/osgPlugins/tiff/ReaderWriterTIFF.cpp index 5ee9e5d14..4972bed39 100644 --- a/src/osgPlugins/tiff/ReaderWriterTIFF.cpp +++ b/src/osgPlugins/tiff/ReaderWriterTIFF.cpp @@ -405,7 +405,7 @@ class ReaderWriterTIFF : public osgDB::ReaderWriter virtual const char* className() { return "TIFF Image Reader"; } virtual bool acceptsExtension(const std::string& extension) { return extension=="tiff"; } - virtual osg::Image* readImage(const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual ReadResult readImage(const std::string& fileName, const osgDB::ReaderWriter::Options*) { unsigned char *imageData = NULL; @@ -415,7 +415,7 @@ class ReaderWriterTIFF : public osgDB::ReaderWriter imageData = simage_tiff_load(fileName.c_str(),&width_ret,&height_ret,&numComponents_ret); - if (imageData==NULL) return NULL; + if (imageData==NULL) return ReadResult::FILE_NOT_HANDLED; int s = width_ret; int t = height_ret; diff --git a/src/osgPlugins/zip/ReaderWriterZIP.cpp b/src/osgPlugins/zip/ReaderWriterZIP.cpp index 9daf1c38f..a5b7bb446 100644 --- a/src/osgPlugins/zip/ReaderWriterZIP.cpp +++ b/src/osgPlugins/zip/ReaderWriterZIP.cpp @@ -29,11 +29,11 @@ class ReaderWriterZIP : public osgDB::ReaderWriter return osgDB::equalCaseInsensitive(extension,"zip"); } - virtual osg::Node* readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) { std::string ext = osgDB::getLowerCaseFileExtension(fileName); - if (!acceptsExtension(ext)) return NULL; + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; osg::notify(osg::INFO)<<"ReaderWriterZIP::readNode( "<getNumChildren() == 0 ) { grp->unref(); - return NULL; + return ReadResult::FILE_NOT_HANDLED; } return grp;