Annotation of libwww/Library/src/HTAnchor.c, revision 1.7
1.1 timbl 1: /* Hypertext "Anchor" Object HTAnchor.c
2: ** ==========================
3: **
4: ** An anchor represents a region of a hypertext document which is linked to
5: ** another anchor in the same or a different document.
6: **
7: ** History
8: **
9: ** Nov 1990 Written in Objective-C for the NeXT browser (TBL)
10: ** 24-Oct-1991 (JFG), written in C, browser-independant
11: ** 21-Nov-1991 (JFG), first complete version
12: **
13: ** (c) Copyright CERN 1991 - See Copyright.html
14: */
15:
16: #define HASH_SIZE 101 /* Arbitrary prime. Memory/speed tradeoff */
17:
18: #include <ctype.h>
19: #include "tcp.h"
1.7 ! luotonen 20: #include "HTFormat.h"
1.1 timbl 21: #include "HTAnchor.h"
1.6 timbl 22:
1.1 timbl 23: #include "HTUtils.h"
24: #include "HTParse.h"
1.6 timbl 25: #include "HTFWriter.h" /* for cache stuff */
1.1 timbl 26:
27: typedef struct _HyperDoc Hyperdoc;
1.5 duns 28: #ifdef VMS
1.1 timbl 29: struct _HyperDoc {
30: int junk; /* VMS cannot handle pointers to undefined structs */
31: };
32: #endif
33:
34: PRIVATE HTList **adult_table=0; /* Point to table of lists of all parents */
35:
36: /* Creation Methods
37: ** ================
38: **
39: ** Do not use "new" by itself outside this module. In order to enforce
40: ** consistency, we insist that you furnish more information about the
41: ** anchor you are creating : use newWithParent or newWithAddress.
42: */
43:
44: PRIVATE HTParentAnchor * HTParentAnchor_new
45: NOARGS
46: {
47: HTParentAnchor *newAnchor =
48: (HTParentAnchor *) calloc (1, sizeof (HTParentAnchor)); /* zero-filled */
49: newAnchor->parent = newAnchor;
50: return newAnchor;
51: }
52:
53: PRIVATE HTChildAnchor * HTChildAnchor_new
54: NOARGS
55: {
56: return (HTChildAnchor *) calloc (1, sizeof (HTChildAnchor)); /* zero-filled */
57: }
58:
59:
60: /* Case insensitive string comparison
61: ** ----------------------------------
62: ** On entry,
63: ** s Points to one string, null terminated
64: ** t points to the other.
65: ** On exit,
66: ** returns YES if the strings are equivalent ignoring case
67: ** NO if they differ in more than their case.
68: */
69:
70: PRIVATE BOOL equivalent
71: ARGS2 (CONST char *,s, CONST char *,t)
72: {
73: if (s && t) { /* Make sure they point to something */
74: for ( ; *s && *t ; s++, t++) {
75: if (TOUPPER(*s) != TOUPPER(*t))
76: return NO;
77: }
78: return TOUPPER(*s) == TOUPPER(*t);
79: } else
80: return s == t; /* Two NULLs are equivalent, aren't they ? */
81: }
82:
83:
84: /* Create new or find old sub-anchor
85: ** ---------------------------------
86: **
1.3 timbl 87: ** Me one is for a new anchor being edited into an existing
1.1 timbl 88: ** document. The parent anchor must already exist.
89: */
90:
1.2 timbl 91: PUBLIC HTChildAnchor * HTAnchor_findChild
1.1 timbl 92: ARGS2 (HTParentAnchor *,parent, CONST char *,tag)
93: {
94: HTChildAnchor *child;
95: HTList *kids;
96:
97: if (! parent) {
98: if (TRACE) printf ("HTAnchor_findChild called with NULL parent.\n");
99: return NULL;
100: }
1.7 ! luotonen 101: if ((kids = parent->children)) { /* parent has children : search them */
1.1 timbl 102: if (tag && *tag) { /* TBL */
1.7 ! luotonen 103: while ((child = HTList_nextObject (kids))) {
1.1 timbl 104: if (equivalent(child->tag, tag)) { /* Case sensitive 920226 */
1.4 timbl 105: if (TRACE) fprintf (stderr,
1.1 timbl 106: "Child anchor %p of parent %p with name `%s' already exists.\n",
1.4 timbl 107: (void*)child, (void*)parent, tag);
1.1 timbl 108: return child;
109: }
110: }
111: } /* end if tag is void */
112: } else /* parent doesn't have any children yet : create family */
113: parent->children = HTList_new ();
114:
115: child = HTChildAnchor_new ();
116: if (TRACE) fprintf(stderr, "new Anchor %p named `%s' is child of %p\n",
1.4 timbl 117: (void*)child, (int)tag ? tag : (CONST char *)"" , (void*)parent); /* int for apollo */
1.1 timbl 118: HTList_addObject (parent->children, child);
119: child->parent = parent;
120: StrAllocCopy(child->tag, tag);
121: return child;
122: }
123:
124:
125: /* Create or find a child anchor with a possible link
126: ** --------------------------------------------------
127: **
128: ** Create new anchor with a given parent and possibly
129: ** a name, and possibly a link to a _relatively_ named anchor.
130: ** (Code originally in ParseHTML.h)
131: */
132: PUBLIC HTChildAnchor * HTAnchor_findChildAndLink
133: ARGS4(
134: HTParentAnchor *,parent, /* May not be 0 */
135: CONST char *,tag, /* May be "" or 0 */
136: CONST char *,href, /* May be "" or 0 */
137: HTLinkType *,ltype /* May be 0 */
138: )
139: {
140: HTChildAnchor * child = HTAnchor_findChild(parent, tag);
141: if (href && *href) {
1.2 timbl 142: char * relative_to = HTAnchor_address((HTAnchor *) parent);
143: char * parsed_address = HTParse(href, relative_to, PARSE_ALL);
144: HTAnchor * dest = HTAnchor_findAddress(parsed_address);
1.1 timbl 145: HTAnchor_link((HTAnchor *) child, dest, ltype);
146: free(parsed_address);
1.2 timbl 147: free(relative_to);
1.1 timbl 148: }
149: return child;
150: }
151:
152:
153: /* Create new or find old named anchor
154: ** -----------------------------------
155: **
1.3 timbl 156: ** Me one is for a reference which is found in a document, and might
1.1 timbl 157: ** not be already loaded.
158: ** Note: You are not guaranteed a new anchor -- you might get an old one,
159: ** like with fonts.
160: */
161:
162: HTAnchor * HTAnchor_findAddress
163: ARGS1 (CONST char *,address)
164: {
165: char *tag = HTParse (address, "", PARSE_ANCHOR); /* Anchor tag specified ? */
166:
167: /* If the address represents a sub-anchor, we recursively load its parent,
168: then we create a child anchor within that document. */
169: if (*tag) {
170: char *docAddress = HTParse(address, "", PARSE_ACCESS | PARSE_HOST |
171: PARSE_PATH | PARSE_PUNCTUATION);
172: HTParentAnchor * foundParent =
173: (HTParentAnchor *) HTAnchor_findAddress (docAddress);
174: HTChildAnchor * foundAnchor = HTAnchor_findChild (foundParent, tag);
175: free (docAddress);
176: free (tag);
177: return (HTAnchor *) foundAnchor;
178: }
179:
1.3 timbl 180: else { /* If the address has no anchor tag,
181: check whether we have this node */
1.1 timbl 182: int hash;
183: CONST char *p;
184: HTList * adults;
185: HTList *grownups;
186: HTParentAnchor * foundAnchor;
187:
188: free (tag);
189:
190: /* Select list from hash table */
1.2 timbl 191: for(p=address, hash=0; *p; p++)
192: hash = (hash * 3 + (*(unsigned char*)p))
193: % HASH_SIZE;
1.1 timbl 194: if (!adult_table)
195: adult_table = (HTList**) calloc(HASH_SIZE, sizeof(HTList*));
196: if (!adult_table[hash]) adult_table[hash] = HTList_new();
197: adults = adult_table[hash];
198:
199: /* Search list for anchor */
200: grownups = adults;
1.7 ! luotonen 201: while ((foundAnchor = HTList_nextObject (grownups))) {
1.2 timbl 202: if (equivalent(foundAnchor->address, address)) {
1.1 timbl 203: if (TRACE) fprintf(stderr, "Anchor %p with address `%s' already exists.\n",
1.4 timbl 204: (void*) foundAnchor, address);
1.1 timbl 205: return (HTAnchor *) foundAnchor;
206: }
207: }
208:
209: /* Node not found : create new anchor */
210: foundAnchor = HTParentAnchor_new ();
211: if (TRACE) fprintf(stderr, "New anchor %p has hash %d and address `%s'\n",
1.4 timbl 212: (void*)foundAnchor, hash, address);
1.1 timbl 213: StrAllocCopy(foundAnchor->address, address);
214: HTList_addObject (adults, foundAnchor);
215: return (HTAnchor *) foundAnchor;
216: }
217: }
218:
219:
220: /* Delete an anchor and possibly related things (auto garbage collection)
221: ** --------------------------------------------
222: **
223: ** The anchor is only deleted if the corresponding document is not loaded.
224: ** All outgoing links from parent and children are deleted, and this anchor
225: ** is removed from the sources list of all its targets.
226: ** We also try to delete the targets whose documents are not loaded.
227: ** If this anchor's source list is empty, we delete it and its children.
228: */
229:
230: PRIVATE void deleteLinks
1.3 timbl 231: ARGS1 (HTAnchor *,me)
1.1 timbl 232: {
1.3 timbl 233: if (! me)
1.1 timbl 234: return;
235:
236: /* Recursively try to delete target anchors */
1.3 timbl 237: if (me->mainLink.dest) {
238: HTParentAnchor *parent = me->mainLink.dest->parent;
239: HTList_removeObject (parent->sources, me);
1.1 timbl 240: if (! parent->document) /* Test here to avoid calling overhead */
241: HTAnchor_delete (parent);
242: }
1.3 timbl 243: if (me->links) { /* Extra destinations */
1.1 timbl 244: HTLink *target;
1.7 ! luotonen 245: while ((target = HTList_removeLastObject (me->links))) {
1.1 timbl 246: HTParentAnchor *parent = target->dest->parent;
1.3 timbl 247: HTList_removeObject (parent->sources, me);
1.1 timbl 248: if (! parent->document) /* Test here to avoid calling overhead */
249: HTAnchor_delete (parent);
250: }
251: }
252: }
253:
254: PUBLIC BOOL HTAnchor_delete
1.3 timbl 255: ARGS1 (HTParentAnchor *,me)
1.1 timbl 256: {
257: HTChildAnchor *child;
258:
259: /* Don't delete if document is loaded */
1.3 timbl 260: if (me->document)
1.1 timbl 261: return NO;
262:
263: /* Recursively try to delete target anchors */
1.3 timbl 264: deleteLinks ((HTAnchor *) me);
1.1 timbl 265:
1.3 timbl 266: if (! HTList_isEmpty (me->sources)) { /* There are still incoming links */
1.1 timbl 267: /* Delete all outgoing links from children, if any */
1.3 timbl 268: HTList *kids = me->children;
1.7 ! luotonen 269: while ((child = HTList_nextObject (kids)))
1.1 timbl 270: deleteLinks ((HTAnchor *) child);
271: return NO; /* Parent not deleted */
272: }
273:
274: /* No more incoming links : kill everything */
275: /* First, recursively delete children */
1.7 ! luotonen 276: while ((child = HTList_removeLastObject (me->children))) {
1.1 timbl 277: deleteLinks ((HTAnchor *) child);
278: free (child->tag);
279: free (child);
280: }
281:
282: /* Now kill myself */
1.3 timbl 283: HTList_delete (me->children);
284: HTList_delete (me->sources);
285: free (me->address);
1.1 timbl 286: /* Devise a way to clean out the HTFormat if no longer needed (ref count?) */
1.3 timbl 287: free (me);
1.6 timbl 288: if (me->cacheItems) {
289: HTCacheClear(me->cacheItems);
290: }
1.1 timbl 291: return YES; /* Parent deleted */
292: }
293:
294:
295: /* Move an anchor to the head of the list of its siblings
296: ** ------------------------------------------------------
297: **
298: ** This is to ensure that an anchor which might have already existed
299: ** is put in the correct order as we load the document.
300: */
301:
302: void HTAnchor_makeLastChild
1.3 timbl 303: ARGS1(HTChildAnchor *,me)
1.1 timbl 304: {
1.3 timbl 305: if (me->parent != (HTParentAnchor *) me) { /* Make sure it's a child */
306: HTList * siblings = me->parent->children;
307: HTList_removeObject (siblings, me);
308: HTList_addObject (siblings, me);
1.1 timbl 309: }
310: }
311:
312: /* Data access functions
313: ** ---------------------
314: */
315:
316: PUBLIC HTParentAnchor * HTAnchor_parent
1.3 timbl 317: ARGS1 (HTAnchor *,me)
1.1 timbl 318: {
1.3 timbl 319: return me ? me->parent : NULL;
1.1 timbl 320: }
321:
322: void HTAnchor_setDocument
1.3 timbl 323: ARGS2 (HTParentAnchor *,me, HyperDoc *,doc)
1.1 timbl 324: {
1.3 timbl 325: if (me)
326: me->document = doc;
1.1 timbl 327: }
328:
329: HyperDoc * HTAnchor_document
1.3 timbl 330: ARGS1 (HTParentAnchor *,me)
1.1 timbl 331: {
1.3 timbl 332: return me ? me->document : NULL;
1.1 timbl 333: }
334:
335:
336: /* We don't want code to change an address after anchor creation... yet ?
337: void HTAnchor_setAddress
1.3 timbl 338: ARGS2 (HTAnchor *,me, char *,addr)
1.1 timbl 339: {
1.3 timbl 340: if (me)
341: StrAllocCopy (me->parent->address, addr);
1.1 timbl 342: }
343: */
344:
345: char * HTAnchor_address
1.3 timbl 346: ARGS1 (HTAnchor *,me)
1.1 timbl 347: {
348: char *addr = NULL;
1.3 timbl 349: if (me) {
350: if (((HTParentAnchor *) me == me->parent) ||
351: !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
352: StrAllocCopy (addr, me->parent->address);
1.1 timbl 353: }
354: else { /* it's a named child */
1.3 timbl 355: addr = malloc (2 + strlen (me->parent->address)
356: + strlen (((HTChildAnchor *) me)->tag));
1.1 timbl 357: if (addr == NULL) outofmem(__FILE__, "HTAnchor_address");
1.3 timbl 358: sprintf (addr, "%s#%s", me->parent->address,
359: ((HTChildAnchor *) me)->tag);
1.1 timbl 360: }
361: }
362: return addr;
363: }
364:
365:
366:
367: void HTAnchor_setFormat
1.3 timbl 368: ARGS2 (HTParentAnchor *,me, HTFormat ,form)
1.1 timbl 369: {
1.3 timbl 370: if (me)
371: me->format = form;
1.1 timbl 372: }
373:
1.2 timbl 374: HTFormat HTAnchor_format
1.3 timbl 375: ARGS1 (HTParentAnchor *,me)
1.1 timbl 376: {
1.3 timbl 377: return me ? me->format : NULL;
1.1 timbl 378: }
379:
380:
381:
382: void HTAnchor_setIndex
1.3 timbl 383: ARGS1 (HTParentAnchor *,me)
1.1 timbl 384: {
1.3 timbl 385: if (me)
386: me->isIndex = YES;
1.1 timbl 387: }
388:
389: BOOL HTAnchor_isIndex
1.3 timbl 390: ARGS1 (HTParentAnchor *,me)
1.1 timbl 391: {
1.3 timbl 392: return me ? me->isIndex : NO;
1.1 timbl 393: }
394:
395:
396:
397: BOOL HTAnchor_hasChildren
1.3 timbl 398: ARGS1 (HTParentAnchor *,me)
1.1 timbl 399: {
1.3 timbl 400: return me ? ! HTList_isEmpty(me->children) : NO;
1.1 timbl 401: }
402:
403: /* Title handling
404: */
405: CONST char * HTAnchor_title
1.3 timbl 406: ARGS1 (HTParentAnchor *,me)
1.1 timbl 407: {
1.3 timbl 408: return me ? me->title : 0;
1.1 timbl 409: }
410:
411: void HTAnchor_setTitle
1.3 timbl 412: ARGS2(HTParentAnchor *,me, CONST char *,title)
1.1 timbl 413: {
1.3 timbl 414: StrAllocCopy(me->title, title);
1.1 timbl 415: }
416:
417: void HTAnchor_appendTitle
1.3 timbl 418: ARGS2(HTParentAnchor *,me, CONST char *,title)
1.1 timbl 419: {
1.3 timbl 420: StrAllocCat(me->title, title);
1.1 timbl 421: }
422:
1.3 timbl 423: /* Link me Anchor to another given one
1.1 timbl 424: ** -------------------------------------
425: */
426:
427: BOOL HTAnchor_link
428: ARGS3(HTAnchor *,source, HTAnchor *,destination, HTLinkType *,type)
429: {
430: if (! (source && destination))
431: return NO; /* Can't link to/from non-existing anchor */
432: if (TRACE) printf ("Linking anchor %p to anchor %p\n", source, destination);
433: if (! source->mainLink.dest) {
434: source->mainLink.dest = destination;
435: source->mainLink.type = type;
436: } else {
437: HTLink * newLink = (HTLink *) malloc (sizeof (HTLink));
438: if (newLink == NULL) outofmem(__FILE__, "HTAnchor_link");
439: newLink->dest = destination;
440: newLink->type = type;
441: if (! source->links)
442: source->links = HTList_new ();
443: HTList_addObject (source->links, newLink);
444: }
445: if (!destination->parent->sources)
446: destination->parent->sources = HTList_new ();
447: HTList_addObject (destination->parent->sources, source);
448: return YES; /* Success */
449: }
450:
451:
452: /* Manipulation of links
453: ** ---------------------
454: */
455:
456: HTAnchor * HTAnchor_followMainLink
1.3 timbl 457: ARGS1 (HTAnchor *,me)
1.1 timbl 458: {
1.3 timbl 459: return me->mainLink.dest;
1.1 timbl 460: }
461:
462: HTAnchor * HTAnchor_followTypedLink
1.3 timbl 463: ARGS2 (HTAnchor *,me, HTLinkType *,type)
1.1 timbl 464: {
1.3 timbl 465: if (me->mainLink.type == type)
466: return me->mainLink.dest;
467: if (me->links) {
468: HTList *links = me->links;
1.1 timbl 469: HTLink *link;
1.7 ! luotonen 470: while ((link = HTList_nextObject (links)))
1.1 timbl 471: if (link->type == type)
472: return link->dest;
473: }
1.3 timbl 474: return NULL; /* No link of me type */
1.1 timbl 475: }
476:
1.2 timbl 477:
478: /* Make main link
479: */
1.1 timbl 480: BOOL HTAnchor_makeMainLink
1.3 timbl 481: ARGS2 (HTAnchor *,me, HTLink *,movingLink)
1.1 timbl 482: {
483: /* Check that everything's OK */
1.3 timbl 484: if (! (me && HTList_removeObject (me->links, movingLink)))
1.1 timbl 485: return NO; /* link not found or NULL anchor */
486: else {
487: /* First push current main link onto top of links list */
488: HTLink *newLink = (HTLink*) malloc (sizeof (HTLink));
489: if (newLink == NULL) outofmem(__FILE__, "HTAnchor_makeMainLink");
1.3 timbl 490: memcpy (newLink, & me->mainLink, sizeof (HTLink));
491: HTList_addObject (me->links, newLink);
1.1 timbl 492:
493: /* Now make movingLink the new main link, and free it */
1.3 timbl 494: memcpy (& me->mainLink, movingLink, sizeof (HTLink));
1.1 timbl 495: free (movingLink);
496: return YES;
497: }
1.2 timbl 498: }
499:
500:
501: /* Methods List
502: ** ------------
503: */
504:
1.3 timbl 505: PUBLIC HTList * HTAnchor_methods ARGS1(HTParentAnchor *, me)
1.2 timbl 506: {
1.3 timbl 507: if (!me->methods) {
508: me->methods = HTList_new();
1.2 timbl 509: }
1.3 timbl 510: return me->methods;
1.2 timbl 511: }
512:
513: /* Protocol
514: ** --------
515: */
516:
1.3 timbl 517: PUBLIC void * HTAnchor_protocol ARGS1(HTParentAnchor *, me)
1.2 timbl 518: {
1.3 timbl 519: return me->protocol;
1.2 timbl 520: }
521:
1.3 timbl 522: PUBLIC void HTAnchor_setProtocol ARGS2(HTParentAnchor *, me,
1.2 timbl 523: void*, protocol)
524: {
1.3 timbl 525: me->protocol = protocol;
1.2 timbl 526: }
527:
528: /* Physical Address
529: ** ----------------
530: */
531:
1.3 timbl 532: PUBLIC char * HTAnchor_physical ARGS1(HTParentAnchor *, me)
1.2 timbl 533: {
1.3 timbl 534: return me->physical;
1.2 timbl 535: }
536:
1.3 timbl 537: PUBLIC void HTAnchor_setPhysical ARGS2(HTParentAnchor *, me,
1.2 timbl 538: char *, physical)
539: {
1.3 timbl 540: StrAllocCopy(me->physical, physical);
1.1 timbl 541: }
Webmaster