Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/Cache.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003   
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Cache.cpp 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.3 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <base/Cache>
00026 #include <base/VFile>
00027 #include <base/VDirectory>
00028 #include <base/CacheDirectory>
00029 #include <base/MD5>
00030 
00031 using base::Cache;
00032 using base::ResourceCache;
00033 using base::VFile;
00034 using base::VDirectory;
00035 using base::CacheDirectory;
00036 
00037 
00038 Cache::Cache(ref<VFileSystem> fs, const PathName& resourceDirectory, const PathName& cacheDirectory)
00039   : ResourceCache(fs, resourceDirectory, cacheDirectory)
00040 {
00041 }
00042 
00043 
00044 Cache::~Cache()
00045 {
00046 }
00047 
00048 
00049 ref<VFile> Cache::getFile(const PathName& name)  throw (path_not_found, io_error)
00050 {
00051   return filesystem->getFile( name );
00052 }
00053 
00054 
00055 ref<VFile> Cache::findFile(const PathName& name, const PathName& additionalPath) throw (path_not_found, io_error)
00056 {
00057   const PathName& pn(name);
00058   ref<VFile> file;
00059   if (pn.isRelative()) {
00060     // try resource directory first, if not found, try the current directory, then additionalPath
00061     if (filesystem->exists( _resourceDirectory+pn ))
00062       file = filesystem->getFile( _resourceDirectory+pn );
00063     if (!additionalPath.empty())
00064       if (filesystem->exists(additionalPath+pn))
00065         file = filesystem->getFile(additionalPath+pn);
00066     if (!file)
00067       file = filesystem->getFile(pn);
00068   }
00069   else 
00070     file = filesystem->getFile(pn); 
00071   
00072   return file;
00073 }
00074 
00075 
00076 bool Cache::isCached(const PathName& name) throw (path_not_found, io_error)
00077 {
00078   return !getCache(name)->empty(); // not wonderfully efficient, but it'll do
00079 }
00080 
00081 ref<VDirectory> Cache::getCache(const PathName& name) throw (path_not_found, io_error)
00082 {
00083   // Construct CacheDirectory for this file resource.
00084   //  It consists of a directory containing a file for each cache file associated
00085   //  with the real file (cache files can contain anything the caching clients wishs to
00086   //  associate with a file).
00087   //  The implementation is to store files in the cache directory that have a prefix
00088   //   that is the MD5 hash of the file resource, and has a suffix that is used as
00089   //   the name visible through the CacheDirectory.
00090 
00091   ref<VFile> file = getFile(name);
00092 
00093   MD5 md5(file->istream());
00094   String prefix( md5.hex_digest());
00095   prefix = String("md5_")+prefix+"_";
00096 
00097   ref<VDirectory> cache = filesystem->getDirectory(_cacheDirectory);
00098   ref<CacheDirectory> cacheDir( NewObj CacheDirectory(cache,prefix) );
00099 
00100   return cacheDir;
00101 }
00102 
00103 

Generated on Thu Jul 29 15:56:04 2004 for OpenSim by doxygen 1.3.6