1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/html/htmlrc.cpp,v 1.2 1999/05/17 02:52:22 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1997 by Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
htmlrc.cpp - resource cache
Function
Notes
Modified
10/25/97 MJRoberts - Creation
*/
#ifndef HTMLRC_H
#include "htmlrc.h"
#endif
#ifndef HTMLRF_H
#include "htmlrf.h"
#endif
#ifndef HTMLSYS_H
#include "htmlsys.h"
#endif
#ifndef HTMLHASH_H
#include "htmlhash.h"
#endif
#ifndef HTMLSND_H
#include "htmlsnd.h"
#endif
/* ------------------------------------------------------------------------ */
/*
* Resource cache object
*/
/*
* Delete an resource cache object. Note that we're only deleted by the
* remove_ref() method.
*/
CHtmlResCacheObject::~CHtmlResCacheObject()
{
/* if I'm in a cache, remove myself from the cache */
if (cache_ != 0)
cache_->remove(this);
/* we own the resource, so delete it */
if (resource_ != 0)
delete resource_;
}
/*
* Downcast routines
*/
/* get the resource as an image */
CHtmlSysImage *CHtmlResCacheObject::get_image()
{
return (resource_ != 0 ? resource_->cast_image() : 0);
}
/* get the resource as a sound */
CHtmlSysSound *CHtmlResCacheObject::get_sound()
{
return (resource_ != 0 ? resource_->cast_sound() : 0);
}
/* ------------------------------------------------------------------------ */
/*
* Hash entry subclass for resource cache objects
*/
class CHtmlHashEntryRes: public CHtmlHashEntryCS
{
public:
CHtmlHashEntryRes(CHtmlResCacheObject *obj)
: CHtmlHashEntryCS(obj->get_url(), get_strlen(obj->get_url()), FALSE)
{
cache_obj_ = obj;
}
~CHtmlHashEntryRes()
{
/* delete our associated cache object */
// $$$ delete cache_obj_;
}
/* cache object */
CHtmlResCacheObject *cache_obj_;
};
/* ------------------------------------------------------------------------ */
/*
* Resource cache
*/
CHtmlResCache::CHtmlResCache()
{
/* create my hash table with a case-sensitive hash function */
hashtable_ = new CHtmlHashTable(512, new CHtmlHashFuncCS);
}
CHtmlResCache::~CHtmlResCache()
{
/* delete the hash table */
delete hashtable_;
}
/*
* Find an existing resource in the cache
*/
CHtmlResCacheObject *CHtmlResCache::find(const CHtmlUrl *url)
{
CHtmlHashEntryRes *entry;
/* if the url is empty, there's no object */
if (url->get_url() == 0)
return 0;
/* look in the hash table for the object */
entry = (CHtmlHashEntryRes *)
hashtable_->find(url->get_url(), get_strlen(url->get_url()));
/* if we found a hash table entry, return the cache object */
return (entry == 0 ? 0 : entry->cache_obj_);
}
/*
* Find an existing resource, or create a new one if there isn't one in
* the cache matching the given URL.
*/
CHtmlResCacheObject *CHtmlResCache::find_or_create(
CHtmlSysWin *win, CHtmlResFinder *res_finder, const CHtmlUrl *url)
{
CHtmlResCacheObject *entry;
CHtmlSysResource *resource;
htmlres_loader_func_t loader_func;
unsigned long seekpos;
unsigned long filesize;
CStringBuf fname;
int cacheable;
/* search for an existing entry, and return it if we can find it */
if ((entry = find(url)) != 0)
return entry;
/* presume we're not going to be able to load the resource */
resource = 0;
/*
* We didn't find one - load the resource from the URL. First
* determine the resource type, and see if we can find the resource
* in the .GAM file.
*/
if (CHtmlResType::get_res_mapping(url->get_url(), &loader_func)
!= HTML_res_type_unknown)
{
/* find the resource in the .GAM file or externally */
res_finder->get_file_info(&fname, url->get_url(),
get_strlen(url->get_url()),
&seekpos, &filesize);
/* load it */
resource = (*loader_func)(url, fname.get(), seekpos, filesize, win);
}
/* note if the entry is cacheable */
cacheable = (resource == 0 || resource->is_cacheable());
/* create a new cache object for the resource */
entry = new CHtmlResCacheObject(url, resource, cacheable ? this : 0);
/* if the resource is cacheable, add it to our cache */
if (cacheable)
add(entry);
/* return the new entry */
return entry;
}
/*
* Add an object to the cache.
*/
void CHtmlResCache::add(CHtmlResCacheObject *obj)
{
CHtmlHashEntryRes *entry;
/* create an entry for the object */
entry = new CHtmlHashEntryRes(obj);
/* add it to the hash table */
hashtable_->add(entry);
}
/*
* Remove an object from the cache.
*/
void CHtmlResCache::remove(CHtmlResCacheObject *obj)
{
CHtmlHashEntry *entry;
/* find the hash table entry for the object */
entry = hashtable_->find(obj->get_url(), get_strlen(obj->get_url()));
/*
* If we found it, remove it from the hash table, and delete the
* hash entry object, since its only purpose is to fill the slot in
* the hash table.
*/
if (entry != 0)
{
hashtable_->remove(entry);
delete entry;
}
}
|