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