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