2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
#ifndef OSGDB_READERWRITER
|
|
|
|
#define OSGDB_READERWRITER 1
|
|
|
|
|
|
|
|
#include <osg/Referenced>
|
|
|
|
#include <osg/Image>
|
2003-10-29 19:11:17 +08:00
|
|
|
#include <osg/Shape>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/Node>
|
|
|
|
|
2003-02-10 21:58:39 +08:00
|
|
|
#include <osgDB/Export>
|
|
|
|
|
2004-11-22 22:10:12 +08:00
|
|
|
#include <deque>
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
namespace osgDB {
|
|
|
|
|
2004-11-08 05:17:31 +08:00
|
|
|
class Archive;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2004-11-22 22:10:12 +08:00
|
|
|
/** list of directories to search through which searching for files. */
|
|
|
|
typedef std::deque<std::string> FilePathList;
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
/** pure virtual base class for reading and writing of non native formats. */
|
2004-11-08 05:17:31 +08:00
|
|
|
class OSGDB_EXPORT ReaderWriter : public osg::Object
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
public:
|
2004-11-08 05:17:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
ReaderWriter() {}
|
2004-11-29 09:12:25 +08:00
|
|
|
ReaderWriter(const ReaderWriter& rw,const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):
|
|
|
|
osg::Object(rw,copyop) {}
|
2004-11-08 05:17:31 +08:00
|
|
|
|
|
|
|
virtual ~ReaderWriter();
|
|
|
|
|
|
|
|
META_Object(osgDB,ReaderWriter);
|
|
|
|
|
2004-11-23 23:29:52 +08:00
|
|
|
virtual bool acceptsExtension(const std::string& /*extension*/) const { return false; }
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2004-10-06 17:31:34 +08:00
|
|
|
/** Options base class used for passing options into plugins to control their operation.*/
|
2004-11-20 21:35:49 +08:00
|
|
|
class Options : public osg::Object
|
2001-10-15 01:54:25 +08:00
|
|
|
{
|
|
|
|
public:
|
2004-11-22 22:10:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
/// bit mask for setting up which object types get cached by readObject/Image/HeightField/Node(filename) calls
|
|
|
|
enum CacheHintOptions
|
|
|
|
{ /// do not cache objects of any type
|
|
|
|
CACHE_NONE = 0,
|
|
|
|
|
|
|
|
/// cache nodes loaded via readNode(filename)
|
|
|
|
CACHE_NODES = 1,
|
|
|
|
|
|
|
|
/// cache images loaded via readImage(filename)
|
|
|
|
CACHE_IMAGES = 2,
|
|
|
|
|
|
|
|
/// cache heightfield loaded via readHeightField(filename)
|
|
|
|
CACHE_HEIGHTFIELDS = 4,
|
|
|
|
|
|
|
|
/// cache heightfield loaded via readHeightField(filename)
|
|
|
|
CACHE_ARCHIVES = 8,
|
|
|
|
|
|
|
|
/// cache objects loaded via readObject(filename)
|
|
|
|
CACHE_OBJECTS = 16,
|
|
|
|
|
|
|
|
/// cache on all read*(filename) calls
|
|
|
|
CACHE_ALL = CACHE_NODES |
|
|
|
|
CACHE_IMAGES |
|
|
|
|
CACHE_HEIGHTFIELDS |
|
|
|
|
CACHE_ARCHIVES |
|
|
|
|
CACHE_OBJECTS
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-11-23 23:29:52 +08:00
|
|
|
Options():_objectCacheHint(CACHE_ARCHIVES) {}
|
2001-10-15 01:54:25 +08:00
|
|
|
Options(const std::string& str):_str(str) {}
|
|
|
|
|
2004-11-20 21:35:49 +08:00
|
|
|
Options(const Options& options,const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):
|
2004-11-29 09:12:25 +08:00
|
|
|
osg::Object(options,copyop),
|
2004-11-20 21:35:49 +08:00
|
|
|
_str(options._str),
|
2004-11-22 22:10:12 +08:00
|
|
|
_databasePaths(options._databasePaths),
|
2004-11-23 23:29:52 +08:00
|
|
|
_objectCacheHint(options._objectCacheHint) {}
|
2004-11-20 21:35:49 +08:00
|
|
|
|
|
|
|
META_Object(osgDB,Options);
|
|
|
|
|
2004-10-06 17:31:34 +08:00
|
|
|
/** Set the general Options string.*/
|
2001-10-15 01:54:25 +08:00
|
|
|
void setOptionString(const std::string& str) { _str = str; }
|
2004-10-06 17:31:34 +08:00
|
|
|
|
|
|
|
/** Get the general Options string.*/
|
2001-10-15 01:54:25 +08:00
|
|
|
const std::string& getOptionString() const { return _str; }
|
|
|
|
|
2004-10-06 17:31:34 +08:00
|
|
|
/** Set the database path to use a hint of where to look when loading models.*/
|
2004-11-22 22:10:12 +08:00
|
|
|
void setDatabasePath(const std::string& str) { _databasePaths.clear(); _databasePaths.push_back(str); }
|
2004-10-06 17:31:34 +08:00
|
|
|
|
|
|
|
/** Get the database path which is used a hint of where to look when loading models.*/
|
2004-11-22 22:10:12 +08:00
|
|
|
FilePathList& getDatabasePathList() { return _databasePaths; }
|
|
|
|
|
|
|
|
/** Get the const database path which is used a hint of where to look when loading models.*/
|
|
|
|
const FilePathList& getDatabasePathList() const { return _databasePaths; }
|
|
|
|
|
|
|
|
/** Set whether the Registry::ObjectCache should be used by default.*/
|
2004-11-23 23:29:52 +08:00
|
|
|
void setObjectCacheHint(CacheHintOptions useObjectCache) { _objectCacheHint = useObjectCache; }
|
2004-11-22 22:10:12 +08:00
|
|
|
|
|
|
|
/** Get whether the Registry::ObjectCache should be used by default.*/
|
2004-11-23 23:29:52 +08:00
|
|
|
CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; }
|
2004-11-22 22:10:12 +08:00
|
|
|
|
2004-10-06 17:31:34 +08:00
|
|
|
|
2001-10-15 01:54:25 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~Options() {}
|
|
|
|
|
2004-11-22 22:10:12 +08:00
|
|
|
std::string _str;
|
|
|
|
FilePathList _databasePaths;
|
2004-11-23 23:29:52 +08:00
|
|
|
CacheHintOptions _objectCacheHint;
|
2001-10-15 01:54:25 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-11-09 00:39:32 +08:00
|
|
|
class OSGDB_EXPORT ReadResult
|
2001-10-30 22:20:37 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
enum ReadStatus
|
2001-10-30 22:20:37 +08:00
|
|
|
{
|
|
|
|
FILE_NOT_HANDLED,
|
2003-12-08 19:24:43 +08:00
|
|
|
FILE_NOT_FOUND,
|
2001-10-30 22:20:37 +08:00
|
|
|
FILE_LOADED,
|
2004-05-07 21:43:41 +08:00
|
|
|
FILE_LOADED_FROM_CACHE,
|
2001-10-30 22:20:37 +08:00
|
|
|
ERROR_IN_READING_FILE
|
|
|
|
};
|
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
ReadResult(ReadStatus status=FILE_NOT_HANDLED):_status(status) {}
|
2001-10-30 22:20:37 +08:00
|
|
|
ReadResult(const std::string& m):_status(ERROR_IN_READING_FILE),_message(m) {}
|
2004-05-07 21:43:41 +08:00
|
|
|
ReadResult(osg::Object* obj, ReadStatus status=FILE_LOADED):_status(status),_object(obj) {}
|
2001-10-30 22:20:37 +08:00
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2004-11-08 05:17:31 +08:00
|
|
|
osg::Object* getObject();
|
|
|
|
osg::Image* getImage();
|
|
|
|
osg::HeightField* getHeightField();
|
|
|
|
osg::Node* getNode();
|
|
|
|
osgDB::Archive* getArchive();
|
2001-10-30 22:20:37 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
bool validObject() { return _object.valid(); }
|
|
|
|
bool validImage() { return getImage()!=0; }
|
2003-10-29 19:11:17 +08:00
|
|
|
bool validHeightField() { return getHeightField()!=0; }
|
2002-09-02 20:31:35 +08:00
|
|
|
bool validNode() { return getNode()!=0; }
|
2004-11-08 05:17:31 +08:00
|
|
|
bool validArchive() { return getArchive()!=0; }
|
2001-10-30 22:20:37 +08:00
|
|
|
|
2004-11-08 05:17:31 +08:00
|
|
|
osg::Object* takeObject();
|
|
|
|
osg::Image* takeImage();
|
|
|
|
osg::HeightField* takeHeightField();
|
|
|
|
osg::Node* takeNode();
|
|
|
|
osgDB::Archive* takeArchive();
|
2001-10-30 22:20:37 +08:00
|
|
|
|
|
|
|
const std::string& message() const { return _message; }
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
ReadStatus status() const { return _status; }
|
2004-05-07 21:43:41 +08:00
|
|
|
bool success() const { return _status==FILE_LOADED || _status==FILE_LOADED_FROM_CACHE ; }
|
|
|
|
bool loadedFromCache() const { return _status==FILE_LOADED_FROM_CACHE; }
|
2002-09-02 20:31:35 +08:00
|
|
|
bool error() const { return _status==ERROR_IN_READING_FILE; }
|
|
|
|
bool notHandled() const { return _status==FILE_NOT_HANDLED; }
|
2003-12-08 19:24:43 +08:00
|
|
|
bool notFound() const { return _status==FILE_NOT_FOUND; }
|
2001-10-30 22:20:37 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
ReadStatus _status;
|
2001-10-30 22:20:37 +08:00
|
|
|
std::string _message;
|
|
|
|
osg::ref_ptr<osg::Object> _object;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WriteResult
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
enum WriteStatus
|
2001-10-30 22:20:37 +08:00
|
|
|
{
|
|
|
|
FILE_NOT_HANDLED,
|
|
|
|
FILE_SAVED,
|
|
|
|
ERROR_IN_WRITING_FILE
|
|
|
|
};
|
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
WriteResult(WriteStatus status=FILE_NOT_HANDLED):_status(status) {}
|
2001-10-30 22:20:37 +08:00
|
|
|
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; }
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
WriteStatus status() const { return _status; }
|
|
|
|
bool success() const { return _status==FILE_SAVED; }
|
|
|
|
bool error() const { return _status==ERROR_IN_WRITING_FILE; }
|
|
|
|
bool notHandled() const { return _status==FILE_NOT_HANDLED; }
|
2001-10-30 22:20:37 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
WriteStatus _status;
|
2001-10-30 22:20:37 +08:00
|
|
|
std::string _message;
|
|
|
|
};
|
|
|
|
|
2004-11-08 05:17:31 +08:00
|
|
|
enum ArchiveStatus
|
|
|
|
{
|
|
|
|
READ,
|
|
|
|
WRITE,
|
|
|
|
CREATE
|
|
|
|
};
|
|
|
|
|
2004-11-14 00:21:48 +08:00
|
|
|
/** open an archive for reading, writing or or to create an empty archive for writing to.*/
|
2004-11-23 23:29:52 +08:00
|
|
|
virtual ReadResult openArchive(const std::string& /*fileName*/,ArchiveStatus, unsigned int =4096, const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
2004-11-08 05:17:31 +08:00
|
|
|
|
2004-11-14 00:21:48 +08:00
|
|
|
/** open an archive for reading.*/
|
2004-11-23 23:29:52 +08:00
|
|
|
virtual ReadResult openArchive(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
|
|
|
|
virtual ReadResult readObject(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual ReadResult readImage(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual ReadResult readHeightField(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual ReadResult readNode(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
|
|
|
|
virtual WriteResult writeObject(const osg::Object& /*obj*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual WriteResult writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual WriteResult writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
|
|
|
|
|
|
|
|
virtual ReadResult readObject(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual ReadResult readImage(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual ReadResult readHeightField(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual ReadResult readNode(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
|
|
|
|
|
|
|
|
virtual WriteResult writeObject(const osg::Object& /*obj*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual WriteResult writeImage(const osg::Image& /*image*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
|
|
|
|
virtual WriteResult writeNode(const osg::Node& /*node*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
|
2003-05-26 17:27:16 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
};
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2004-10-24 22:24:15 +08:00
|
|
|
#endif // OSGDB_READERWRITER
|