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

Revision 10171, 3.0 kB (checked in by robert, 5 years ago)

Refactored the Registry::ReadFileCallback?, WriteFileCallback? and ReaderWriter::Options to they are now defined in their own header and in the osgDB namespace.

Introduced a new FindFileCallback? to Registry to compliement the existing ReadFileCallback? and WriteFileCallback?.

Added support for assign Find, Read and WriteFileCallbacks? to osdDB::Options to enable plugins/applications to override the callbacks just for that
read/write call and any nested file operations

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
33std::string FileCache::createCacheFileName(const std::string& originalFileName) const
34{
35    std::string cacheFileName = _fileCachePath + "/" +
36                                osgDB::getServerAddress(originalFileName) + "/" +
37                                osgDB::getServerFileName(originalFileName);
38                               
39    osg::notify(osg::INFO)<<"FileCache::createCacheFileName("<<originalFileName<<") = "<<cacheFileName<<std::endl;
40   
41    return cacheFileName;
42}
43
44bool FileCache::existsInCache(const std::string& originalFileName) const
45{
46    return osgDB::fileExists(createCacheFileName(originalFileName));
47}
48
49ReaderWriter::ReadResult FileCache::readNode(const std::string& originalFileName, const osgDB::Options* options, bool buildKdTreeIfRequired) const
50{
51    std::string cacheFileName = createCacheFileName(originalFileName);
52    if (!cacheFileName.empty() && osgDB::fileExists(cacheFileName))
53    {
54        osg::notify(osg::INFO)<<"FileCache::readNodeFromCache("<<originalFileName<<") as "<<cacheFileName<<std::endl;
55        return osgDB::Registry::instance()->readNode(cacheFileName, options, buildKdTreeIfRequired);
56    }
57    else
58    {
59        return 0;
60    }
61}
62
63ReaderWriter::WriteResult FileCache::writeNode(const osg::Node& node, const std::string& originalFileName, const osgDB::Options* options) const
64{
65    std::string cacheFileName = createCacheFileName(originalFileName);
66    if (!cacheFileName.empty())
67    {
68        std::string path = osgDB::getFilePath(cacheFileName);
69
70        if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path))
71        {
72            osg::notify(osg::NOTICE)<<"Could not create cache directory: "<<path<<std::endl;
73            return ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE;
74        }
75
76        osg::notify(osg::INFO)<<"FileCache::writeNodeToCache("<<originalFileName<<") as "<<cacheFileName<<std::endl;
77        return osgDB::Registry::instance()->writeNode(node, cacheFileName, options);
78    }
79    return ReaderWriter::WriteResult::FILE_NOT_HANDLED;
80}
Note: See TracBrowser for help on using the browser.