Added following methods to osgDB::Archive in support of work by Fradley Anderegg on .zip archive support:

/** Get the file name which represents the archived file.*/
       virtual std::string getArchiveFileName() const = 0;

       /** return type of file. */
       virtual FileType getFileType(const std::string& filename) const = 0;

       /** return the contents of a directory.
       * returns an empty array on any error.*/
       virtual DirectoryContents getDirectoryContents(const std::string& dirName) const = 0;

Added implementations of these new methods into src/osgPlugins/osga/OSGA_Archive.h src/osgPlugins/osga/OSGA_Archive.cpp
This commit is contained in:
Robert Osfield 2011-04-29 16:34:26 +00:00
parent fb09d0e553
commit b4353c1a8e
3 changed files with 89 additions and 15 deletions

View File

@ -16,6 +16,7 @@
#include <osgDB/ReaderWriter>
#include <osgDB/Registry>
#include <osgDB/FileUtils>
#include <fstream>
#include <list>
@ -35,20 +36,31 @@ class OSGDB_EXPORT Archive : public ReaderWriter
virtual const char* className() const { return "Archive"; }
virtual bool acceptsExtension(const std::string& /*extension*/) const { return true; }
/** close the archive.*/
virtual void close() = 0;
/** return true if file exists in archive.*/
virtual bool fileExists(const std::string& filename) const = 0;
/** Get the file name which represents the archived file.*/
virtual std::string getArchiveFileName() const = 0;
/** Get the file name which represents the master file recorded in the Archive.*/
virtual std::string getMasterFileName() const = 0;
typedef std::vector<std::string> FileNameList;
/** return true if file exists in archive.*/
virtual bool fileExists(const std::string& filename) const = 0;
/** return type of file. */
virtual FileType getFileType(const std::string& filename) const = 0;
typedef osgDB::DirectoryContents FileNameList;
/** Get the full list of file names available in the archive.*/
virtual bool getFileNames(FileNameList& fileNameList) const = 0;
virtual bool getFileNames(FileNameList& fileNames) const = 0;
/** return the contents of a directory.
* returns an empty array on any error.*/
virtual DirectoryContents getDirectoryContents(const std::string& dirName) const = 0;
virtual ReadResult readObject(const std::string& /*fileName*/,const Options* =NULL) const = 0;
virtual ReadResult readImage(const std::string& /*fileName*/,const Options* =NULL) const = 0;
@ -59,8 +71,6 @@ class OSGDB_EXPORT Archive : public ReaderWriter
virtual WriteResult writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) const = 0;
virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,const std::string& /*fileName*/,const Options* =NULL) const = 0;
virtual WriteResult writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) const = 0;
};

View File

@ -330,6 +330,8 @@ bool OSGA_Archive::open(const std::string& filename, ArchiveStatus status, unsig
{
SERIALIZER();
_archiveFileName = filename;
if (status==READ)
{
_status = status;
@ -413,6 +415,8 @@ bool OSGA_Archive::open(std::istream& fin)
{
SERIALIZER();
_archiveFileName = "";
OSG_NOTICE<<"OSGA_Archive::open"<<std::endl;
static_cast<std::istream&>(_input).rdbuf(fin.rdbuf());
return _open(_input);
@ -499,6 +503,12 @@ std::string OSGA_Archive::getMasterFileName() const
return _masterFileName;
}
osgDB::FileType OSGA_Archive::getFileType(const std::string& filename) const
{
if (_indexMap.count(filename)!=0) return osgDB::REGULAR_FILE;
return osgDB::FILE_NOT_FOUND;
}
bool OSGA_Archive::getFileNames(FileNameList& fileNameList) const
{
SERIALIZER();
@ -514,6 +524,50 @@ bool OSGA_Archive::getFileNames(FileNameList& fileNameList) const
return !fileNameList.empty();
}
osgDB::DirectoryContents OSGA_Archive::getDirectoryContents(const std::string& dirName) const
{
osgDB::DirectoryContents files;
for(FileNamePositionMap::const_iterator itr=_indexMap.begin();
itr!=_indexMap.end();
++itr)
{
const std::string& filename = itr->first;
if (filename.size()>dirName.size())
{
// check for match of directory name while accounting for potential
// differences in types of slashes
unsigned int i=0;
for(; i<dirName.size(); ++i)
{
char f = filename[i];
char d = dirName[i];
if (f=='\\') f='/';
if (d=='\\') d='/';
if (f!=d) break;
}
if (i==dirName.size())
{
// directory name matched, but need to make sure a trailing filename exists
// and to skip over any immediate slashes
char f = filename[i];
// check for slash
if (f=='\\' || f=='/')
{
// found slash, now need to skip over it.
++i;
if (i<filename.size())
{
// still have charaters left after slash which respresent the filename
files.push_back(filename.substr(i));
}
}
}
}
}
return files;
}
void OSGA_Archive::writeIndexBlocks()
{

View File

@ -44,17 +44,26 @@ class OSGA_Archive : public osgDB::Archive
/** close the archive.*/
virtual void close();
/** return true if file exists in archive.*/
virtual bool fileExists(const std::string& filename) const;
/** Get the file name which represents the archived file.*/
virtual std::string getArchiveFileName() const { return _archiveFileName; }
/** Get the file name which represents the master file recorded in the Archive.*/
virtual std::string getMasterFileName() const;
typedef std::vector<std::string> FileNameList;
/** return true if file exists in archive.*/
virtual bool fileExists(const std::string& filename) const;
/** return type of file. */
virtual osgDB::FileType getFileType(const std::string& filename) const;
/** Get the full list of file names available in the archive.*/
virtual bool getFileNames(FileNameList& fileNameList) const;
/** return the contents of a directory.
* returns an empty array on any error.*/
virtual osgDB::DirectoryContents getDirectoryContents(const std::string& dirName) const;
/** Read an osg::Object of specified file name from the Archive.*/
virtual ReadResult readObject(const std::string& fileName,const Options* options=NULL) const;
@ -206,7 +215,8 @@ class OSGA_Archive : public osgDB::Archive
ArchiveStatus _status;
osgDB::ifstream _input;
osgDB::fstream _output;
std::string _archiveFileName;
std::string _masterFileName;
IndexBlockList _indexBlockList;
FileNamePositionMap _indexMap;