root/OpenSceneGraph/trunk/src/osgDB/FileCache.cpp @ 10174

Revision 10174, 3.1 kB (checked in by robert, 6 years ago)

Moved Registry::ReadFileCallback? + WriteFileCallback?, and osgDB::ReaderWriter::Options into their own separate Options file and into the osgDB namespace.

Introduced a new callback osgDB::FindFileCallback? that overrides the default behavior of findDataFile/findLibraryFile.

Introduced support for assigning ReaderWriter::Options directory to PagedLOD.

Introduced new osgDB::FileLocationCallback? for assistancing the DatabasePager? to know when a file is hosted on a local or remote file system.

Line 
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 <osgDB/FileCache>
15#include <osgDB/FileUtils>
16#include <osgDB/FileNameUtils>
17#include <osgDB/ReadFile>
18#include <osgDB/WriteFile>
19
20using namespace osgDB;
21
22FileCache::FileCache(const std::string& path):
23    _fileCachePath(path)
24{
25    osg::notify(osg::INFO)<<"Constructed FileCache : "<<path<<std::endl;
26}
27
28FileCache::~FileCache()
29{
30    osg::notify(osg::INFO)<<"Destructed FileCache "<<std::endl;
31}
32
33bool FileCache::isFileAppropriateForFileCache(const std::string& originalFileName) const
34{
35    return osgDB::containsServerAddress(originalFileName);
36}
37
38std::string FileCache::createCacheFileName(const std::string& originalFileName) const
39{
40    std::string cacheFileName = _fileCachePath + "/" +
41                                osgDB::getServerAddress(originalFileName) + "/" +
42                                osgDB::getServerFileName(originalFileName);
43                               
44    osg::notify(osg::INFO)<<"FileCache::createCacheFileName("<<originalFileName<<") = "<<cacheFileName<<std::endl;
45   
46    return cacheFileName;
47}
48
49bool FileCache::existsInCache(const std::string& originalFileName) const
50{
51    return osgDB::fileExists(createCacheFileName(originalFileName));
52}
53
54ReaderWriter::ReadResult FileCache::readNode(const std::string& originalFileName, const osgDB::Options* options, bool buildKdTreeIfRequired) const
55{
56    std::string cacheFileName = createCacheFileName(originalFileName);
57    if (!cacheFileName.empty() && osgDB::fileExists(cacheFileName))
58    {
59        osg::notify(osg::INFO)<<"FileCache::readNodeFromCache("<<originalFileName<<") as "<<cacheFileName<<std::endl;
60        return osgDB::Registry::instance()->readNode(cacheFileName, options, buildKdTreeIfRequired);
61    }
62    else
63    {
64        return 0;
65    }
66}
67
68ReaderWriter::WriteResult FileCache::writeNode(const osg::Node& node, const std::string& originalFileName, const osgDB::Options* options) const
69{
70    std::string cacheFileName = createCacheFileName(originalFileName);
71    if (!cacheFileName.empty())
72    {
73        std::string path = osgDB::getFilePath(cacheFileName);
74
75        if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path))
76        {
77            osg::notify(osg::NOTICE)<<"Could not create cache directory: "<<path<<std::endl;
78            return ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE;
79        }
80
81        osg::notify(osg::INFO)<<"FileCache::writeNodeToCache("<<originalFileName<<") as "<<cacheFileName<<std::endl;
82        return osgDB::Registry::instance()->writeNode(node, cacheFileName, options);
83    }
84    return ReaderWriter::WriteResult::FILE_NOT_HANDLED;
85}
Note: See TracBrowser for help on using the browser.