00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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();
00079 }
00080
00081 ref<VDirectory> Cache::getCache(const PathName& name) throw (path_not_found, io_error)
00082 {
00083
00084
00085
00086
00087
00088
00089
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