1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12 */
13
14 #include <osg/Notify>
15 #include <osg/Endian>
16
17 #include <osgDB/Registry>
18 #include <osgDB/FileNameUtils>
19 #include <osgDB/Archive>
20
21 #include <streambuf>
22
23 using namespace osgDB;
24
openArchive(const std::string & filename,ReaderWriter::ArchiveStatus status,unsigned int indexBlockSizeHint)25 osgDB::Archive* osgDB::openArchive(const std::string& filename, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint)
26 {
27 return openArchive(filename, status, indexBlockSizeHint, Registry::instance()->getOptions());
28 }
29
openArchive(const std::string & filename,ReaderWriter::ArchiveStatus status,unsigned int indexBlockSizeHint,Options * options)30 osgDB::Archive* osgDB::openArchive(const std::string& filename, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint,Options* options)
31 {
32 // ensure archive extension is in the registry list
33 std::string::size_type dot = filename.find_last_of('.');
34 if (dot != std::string::npos)
35 {
36 std::string ext = filename.substr(dot+1);
37 Registry::instance()->addArchiveExtension(ext);
38 }
39 ReaderWriter::ReadResult result = osgDB::Registry::instance()->openArchive(filename, status, indexBlockSizeHint, options);
40 return result.takeArchive();
41 }
42
Archive()43 Archive::Archive()
44 {
45 OSG_INFO<<"Archive::Archive() open"<<std::endl;
46 }
47
~Archive()48 Archive::~Archive()
49 {
50 OSG_INFO<<"Archive::~Archive() closed"<<std::endl;
51 }
52
cleanupFileString(std::string & strFileOrDir)53 void cleanupFileString(std::string& strFileOrDir)
54 {
55 if (strFileOrDir.empty())
56 {
57 return;
58 }
59
60 // convert all separators to unix-style for conformity
61 for (unsigned int i = 0; i < strFileOrDir.length(); ++i)
62 {
63 if (strFileOrDir[i] == '\\')
64 {
65 strFileOrDir[i] = '/';
66 }
67 }
68
69 // get rid of trailing separators
70 if (strFileOrDir[strFileOrDir.length()-1] == '/')
71 {
72 strFileOrDir = strFileOrDir.substr(0, strFileOrDir.length()-1);
73 }
74
75 //add a beginning separator
76 if(strFileOrDir[0] != '/')
77 {
78 strFileOrDir.insert(0, "/");
79 }
80 }
81
getDirectoryContents(const std::string & dirName) const82 osgDB::DirectoryContents osgDB::Archive::getDirectoryContents(const std::string& dirName) const
83 {
84 DirectoryContents filenames;
85 getFileNames(filenames);
86
87 std::string searchPath = dirName;
88 cleanupFileString(searchPath);
89
90 osgDB::DirectoryContents dirContents;
91
92 DirectoryContents::const_iterator iter = filenames.begin();
93 DirectoryContents::const_iterator iterEnd = filenames.end();
94
95 for(; iter != iterEnd; ++iter)
96 {
97 std::string currentFile = *iter;
98
99 cleanupFileString(currentFile);
100
101 if(currentFile.size() > searchPath.size())
102 {
103 size_t endSubElement = currentFile.find(searchPath);
104
105 //we match the whole string in the beginning of the path
106 if(endSubElement == 0)
107 {
108 std::string remainingFile = currentFile.substr(searchPath.size() + 1, std::string::npos);
109 size_t endFileToken = remainingFile.find_first_of('/');
110
111 if(endFileToken == std::string::npos)
112 {
113 dirContents.push_back(remainingFile);
114 }
115 }
116 }
117 }
118
119 return dirContents;
120
121
122 }
123