Annotation of libwww/Library/src/HTAnchor.c, revision 1.47
1.14 frystyk 1: /* HTAnchor.c
2: ** HYPERTEXT "ANCHOR" OBJECT
1.1 timbl 3: **
1.22 frystyk 4: ** (c) COPYRIGHT MIT 1995.
1.14 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
1.47 ! frystyk 6: ** @(#) $Id: Date Author State $
1.14 frystyk 7: **
8: ** An anchor represents a region of a hypertext document which is
9: ** linked to another anchor in the same or a different document.
1.1 timbl 10: **
11: ** History
12: ** Nov 1990 Written in Objective-C for the NeXT browser (TBL)
13: ** 24-Oct-1991 (JFG), written in C, browser-independant
14: ** 21-Nov-1991 (JFG), first complete version
1.41 frystyk 15: ** 3-May-1995 (HF), Added a lot of methods and other stuff made an object
1.1 timbl 16: */
17:
1.16 frystyk 18: /* Library include files */
1.46 frystyk 19: #include "sysdep.h"
1.16 frystyk 20: #include "HTUtils.h"
21: #include "HTString.h"
1.7 luotonen 22: #include "HTFormat.h"
1.1 timbl 23: #include "HTParse.h"
1.24 frystyk 24: #include "HTMethod.h"
1.41 frystyk 25: #include "HTAncMan.h" /* Implemented here */
1.11 frystyk 26:
27: #define HASH_SIZE 101 /* Arbitrary prime. Memory/speed tradeoff */
1.1 timbl 28:
29: PRIVATE HTList **adult_table=0; /* Point to table of lists of all parents */
30:
1.17 frystyk 31: /* ------------------------------------------------------------------------- */
32: /* Creation Methods */
33: /* ------------------------------------------------------------------------- */
34:
35: /*
1.1 timbl 36: ** Do not use "new" by itself outside this module. In order to enforce
37: ** consistency, we insist that you furnish more information about the
38: ** anchor you are creating : use newWithParent or newWithAddress.
39: */
1.35 frystyk 40: PRIVATE HTParentAnchor * HTParentAnchor_new (void)
1.1 timbl 41: {
1.43 frystyk 42: HTParentAnchor *newAnchor;
43: if ((newAnchor = (HTParentAnchor *) HT_CALLOC(1, sizeof (HTParentAnchor))) == NULL)
44: HT_OUTOFMEM("HTParentAnchor_new");
1.16 frystyk 45: newAnchor->parent = newAnchor;
1.17 frystyk 46: newAnchor->content_type = WWW_UNKNOWN;
1.24 frystyk 47: newAnchor->mainLink.method = METHOD_INVALID;
1.41 frystyk 48: newAnchor->content_length = -1; /* howcome 6 dec 95 */
1.28 frystyk 49: newAnchor->date = (time_t) -1;
50: newAnchor->expires = (time_t) -1;
51: newAnchor->last_modified = (time_t) -1;
1.16 frystyk 52: return newAnchor;
1.1 timbl 53: }
54:
1.16 frystyk 55:
1.35 frystyk 56: PRIVATE HTChildAnchor * HTChildAnchor_new (void)
1.1 timbl 57: {
1.43 frystyk 58: HTChildAnchor *child;
59: if ((child = (HTChildAnchor *) HT_CALLOC(1, sizeof(HTChildAnchor))) == NULL)
60: HT_OUTOFMEM("HTChildAnchor_new");
1.33 frystyk 61: return child;
1.1 timbl 62: }
63:
64:
1.17 frystyk 65: /* Create new or find old child anchor
66: ** -----------------------------------
1.1 timbl 67: **
1.3 timbl 68: ** Me one is for a new anchor being edited into an existing
1.17 frystyk 69: ** document. The parent anchor must already exist. All
70: ** children without tags (no NAME attribut) points to the same NULL
71: ** child.
1.1 timbl 72: */
1.35 frystyk 73: PUBLIC HTChildAnchor * HTAnchor_findChild (HTParentAnchor * parent,
1.46 frystyk 74: const char * tag)
1.1 timbl 75: {
1.17 frystyk 76: HTChildAnchor *child;
77: HTList *kids;
78:
79: if (!parent) {
80: if (ANCH_TRACE)
1.44 eric 81: HTTrace("Find Child.. called with NULL parent.\n");
1.17 frystyk 82: return NULL;
83: }
1.1 timbl 84:
1.17 frystyk 85: /* First search list of children to see if tag is already there */
86: if ((kids = parent->children)) {
87: if (tag && *tag) { /* TBL */
88: while ((child = (HTChildAnchor *) HTList_nextObject(kids))) {
1.33 frystyk 89: if (child->tag && !strcmp(child->tag, tag)) {
1.17 frystyk 90: if (ANCH_TRACE)
1.44 eric 91: HTTrace(
1.17 frystyk 92: "Find Child.. %p of parent %p with name `%s' already exists.\n",
93: (void *) child, (void *) parent, tag);
94: return child;
95: }
1.1 timbl 96: }
97: }
1.17 frystyk 98: } else /* Create new list of children */
99: parent->children = HTList_new ();
100:
101: /* Did't find it so we need to create a new one */
102: child = HTChildAnchor_new();
103: HTList_addObject(parent->children, child);
104: child->parent = parent;
105: StrAllocCopy(child->tag, tag);
106: if (ANCH_TRACE)
1.44 eric 107: HTTrace("Find Child.. New Anchor %p named `%s' is child of %p\n",
1.46 frystyk 108: (void *) child, tag ? tag : (const char *) "", (void *)parent);
1.17 frystyk 109: return child;
1.1 timbl 110: }
111:
112:
113: /* Create new or find old named anchor
114: ** -----------------------------------
115: **
1.3 timbl 116: ** Me one is for a reference which is found in a document, and might
1.1 timbl 117: ** not be already loaded.
118: ** Note: You are not guaranteed a new anchor -- you might get an old one,
119: ** like with fonts.
120: */
1.46 frystyk 121: PUBLIC HTAnchor * HTAnchor_findAddress (const char * address)
1.1 timbl 122: {
1.16 frystyk 123: char *tag = HTParse (address, "", PARSE_ANCHOR); /* Any tags? */
1.1 timbl 124:
1.16 frystyk 125: /* If the address represents a sub-anchor, we recursively load its parent,
126: then we create a child anchor within that document. */
127: if (*tag) {
1.34 frystyk 128: char *addr = HTParse(address, "", PARSE_ACCESS | PARSE_HOST |
129: PARSE_PATH | PARSE_PUNCTUATION);
130: HTParentAnchor * parent = (HTParentAnchor*) HTAnchor_findAddress(addr);
131: HTChildAnchor * child = HTAnchor_findChild(parent, tag);
1.43 frystyk 132: HT_FREE(addr);
133: HT_FREE(tag);
1.34 frystyk 134: return (HTAnchor *) child;
1.16 frystyk 135: } else { /* Else check whether we have this node */
136: int hash;
1.46 frystyk 137: const char *p;
1.16 frystyk 138: HTList * adults;
139: HTList *grownups;
140: HTParentAnchor * foundAnchor;
1.34 frystyk 141: char *newaddr = NULL;
142: StrAllocCopy(newaddr, address); /* Get our own copy */
1.43 frystyk 143: HT_FREE(tag);
1.38 frystyk 144: newaddr = HTSimplify(&newaddr);
1.34 frystyk 145:
1.16 frystyk 146: /* Select list from hash table */
147: for(p=newaddr, hash=0; *p; p++)
148: hash = (int) ((hash * 3 + (*(unsigned char*)p)) % HASH_SIZE);
1.33 frystyk 149: if (!adult_table) {
1.43 frystyk 150: if ((adult_table = (HTList* *) HT_CALLOC(HASH_SIZE, sizeof(HTList*))) == NULL)
151: HT_OUTOFMEM("HTAnchor_findAddress");
1.33 frystyk 152: }
1.16 frystyk 153: if (!adult_table[hash]) adult_table[hash] = HTList_new();
154: adults = adult_table[hash];
155:
156: /* Search list for anchor */
157: grownups = adults;
158: while ((foundAnchor = (HTParentAnchor *) HTList_nextObject(grownups))){
1.33 frystyk 159: if (!strcmp(foundAnchor->address, newaddr)) {
1.16 frystyk 160: if (ANCH_TRACE)
1.44 eric 161: HTTrace("Find Parent. %p with address `%s' already exists.\n",
1.16 frystyk 162: (void*) foundAnchor, newaddr);
1.43 frystyk 163: HT_FREE(newaddr); /* We already have it */
1.16 frystyk 164: return (HTAnchor *) foundAnchor;
165: }
166: }
167:
168: /* Node not found : create new anchor. */
169: foundAnchor = HTParentAnchor_new();
170: foundAnchor->address = newaddr; /* Remember our copy */
171: HTList_addObject (adults, foundAnchor);
1.44 eric 172: if (ANCH_TRACE) HTTrace("Find Parent. %p with hash %d and address `%s' created\n", (void*)foundAnchor, hash, newaddr);
1.1 timbl 173: return (HTAnchor *) foundAnchor;
174: }
175: }
176:
1.17 frystyk 177: /* Create or find a child anchor with a possible link
178: ** --------------------------------------------------
179: **
180: ** Create new anchor with a given parent and possibly
181: ** a name, and possibly a link to a _relatively_ named anchor.
1.34 frystyk 182: ** All parameters EXCEPT parent can be NULL
1.17 frystyk 183: */
1.34 frystyk 184: PUBLIC HTChildAnchor * HTAnchor_findChildAndLink (HTParentAnchor * parent,
1.46 frystyk 185: const char * tag,
186: const char * href,
1.41 frystyk 187: HTLinkType ltype)
1.17 frystyk 188: {
1.34 frystyk 189: HTChildAnchor * child = HTAnchor_findChild(parent, tag);
190: if (href && *href) {
191: char * relative_to = HTAnchor_address((HTAnchor *) parent);
192: char * parsed_address = HTParse(href, relative_to, PARSE_ALL);
193: HTAnchor * dest = HTAnchor_findAddress(parsed_address);
194: HTAnchor_link((HTAnchor *) child, dest, ltype, METHOD_INVALID);
1.43 frystyk 195: HT_FREE(parsed_address);
196: HT_FREE(relative_to);
1.34 frystyk 197: }
198: return child;
1.17 frystyk 199: }
200:
1.41 frystyk 201: /* ------------------------------------------------------------------------- */
202: /* Link Methods */
203: /* ------------------------------------------------------------------------- */
204:
205: /*
206: ** Link destinations
207: */
208: PUBLIC BOOL HTLink_setDestination (HTLink * link, HTAnchor * dest)
209: {
210: if (link) {
211: link->dest = dest;
212: return YES;
213: }
214: return NO;
215: }
216:
217: PUBLIC HTAnchor * HTLink_destination (HTLink * link)
218: {
219: return link ? link->dest : NULL;
220: }
221:
222: PUBLIC BOOL HTLink_setType (HTLink * link, HTLinkType type)
223: {
224: if (link) {
225: link->type = type;
226: return YES;
227: }
228: return NO;
229: }
230:
231: PUBLIC HTLinkType HTLink_type (HTLink * link)
232: {
233: return link ? link->type : NULL;
234: }
235:
236: /*
237: ** When a link has been used for posting an object from a source to a
238: ** destination link, the result of the operation is stored as part of the
239: ** link information.
240: */
241: PUBLIC BOOL HTLink_setResult (HTLink * link, HTLinkResult result)
242: {
243: if (link) {
244: link->result = result;
245: return YES;
246: }
247: return NO;
248: }
249:
250: PUBLIC HTLinkResult HTLink_result (HTLink * link)
251: {
252: return link ? link->result : HT_LINK_INVALID;
253: }
254:
255: PUBLIC BOOL HTLink_setMethod (HTLink * link, HTMethod method)
256: {
257: if (link) {
258: link->method = method;
259: return YES;
260: }
261: return NO;
262: }
263:
264: PUBLIC HTMethod HTLink_method (HTLink * link)
265: {
266: return link ? link->method : METHOD_INVALID;
267: }
268:
1.17 frystyk 269: /* Link me Anchor to another given one
270: ** -------------------------------------
271: */
1.35 frystyk 272: PUBLIC BOOL HTAnchor_link (HTAnchor * source,
273: HTAnchor * destination,
1.41 frystyk 274: HTLinkType type,
1.35 frystyk 275: HTMethod method)
1.17 frystyk 276: {
277: if (!(source && destination))
278: return NO; /* Can't link to/from non-existing anchor */
279: if (ANCH_TRACE)
1.44 eric 280: HTTrace("Link Anchors anchor %p to anchor %p\n",
1.17 frystyk 281: (void *) source, (void *) destination);
282: if (!source->mainLink.dest) {
283: source->mainLink.dest = destination;
284: source->mainLink.type = type;
1.24 frystyk 285: source->mainLink.method = method;
1.17 frystyk 286: } else {
1.43 frystyk 287: HTLink * newLink;
288: if ((newLink = (HTLink *) HT_CALLOC(1, sizeof (HTLink))) == NULL)
289: HT_OUTOFMEM("HTAnchor_link");
1.17 frystyk 290: newLink->dest = destination;
291: newLink->type = type;
1.24 frystyk 292: newLink->method = method;
293: if (!source->links)
294: source->links = HTList_new();
1.17 frystyk 295: HTList_addObject (source->links, newLink);
296: }
297: if (!destination->parent->sources)
298: destination->parent->sources = HTList_new ();
299: HTList_addObject (destination->parent->sources, source);
300: return YES;
301: }
302:
1.24 frystyk 303: /*
1.28 frystyk 304: ** Find the anchor object between a destination and a source ancher.
305: ** Return link object if any, else NULL
306: */
1.41 frystyk 307: PUBLIC HTLink * HTAnchor_findLink (HTAnchor * src, HTAnchor * dest)
1.28 frystyk 308: {
309: if (src && dest) {
310: if (src->mainLink.dest == dest)
311: return &(src->mainLink);
312: if (src->links) {
313: HTList *cur = src->links;
314: HTLink *pres;
315: while ((pres = (HTLink *) HTList_nextObject(cur)) != NULL) {
316: if (pres->dest == dest)
317: return pres;
318: }
319: }
320: }
321: return NULL;
322: }
323:
1.41 frystyk 324: /*
325: ** Upgrade the link to the main destination and and downgrade the
326: ** current main link to the list
327: */
328: PUBLIC HTLink * HTAnchor_mainLink (HTAnchor * me)
329: {
330: return me ? &(me->mainLink) : NULL;
331: }
1.28 frystyk 332:
1.41 frystyk 333: PUBLIC BOOL HTAnchor_setMainLink (HTAnchor * me, HTLink * movingLink)
1.28 frystyk 334: {
1.41 frystyk 335: if (!(me && me->links && movingLink &&
336: HTList_removeObject(me->links, movingLink)))
337: return NO;
338: else {
339: /* First push current main link onto top of links list */
1.43 frystyk 340: HTLink *newLink;
341: if ((newLink = (HTLink *) HT_MALLOC(sizeof (HTLink))) == NULL)
342: HT_OUTOFMEM("HTAnchor_makeMainLink");
1.42 frystyk 343: memcpy ((void *) newLink, & me->mainLink, sizeof (HTLink));
1.41 frystyk 344: HTList_addObject (me->links, newLink);
345:
1.46 frystyk 346: /* Now make movingLink the new main link, and HT_FREE it */
1.42 frystyk 347: memcpy ((void *) &me->mainLink, movingLink, sizeof (HTLink));
1.43 frystyk 348: HT_FREE(movingLink);
1.28 frystyk 349: return YES;
350: }
351: }
352:
353: /*
1.41 frystyk 354: ** Handling sub links
1.24 frystyk 355: */
1.41 frystyk 356: PUBLIC HTList * HTAnchor_subLinks (HTAnchor * anchor)
1.24 frystyk 357: {
1.41 frystyk 358: return anchor ? anchor->links : NULL;
1.24 frystyk 359: }
360:
1.41 frystyk 361: PUBLIC BOOL HTAnchor_setSubLinks (HTAnchor * anchor, HTList * list)
362: {
363: if (anchor) {
364: anchor->links = list;
365: return YES;
366: }
367: return NO;
368: }
1.24 frystyk 369:
370: /*
371: ** Returns the methods registered for the main destination of this
372: ** anchor
1.17 frystyk 373: */
1.35 frystyk 374: PUBLIC HTMethod HTAnchor_mainLinkMethod (HTAnchor * me)
1.24 frystyk 375: {
376: return me ? me->mainLink.method : METHOD_INVALID;
377: }
378:
1.41 frystyk 379: /*
380: ** Returns the main destination of this anchor
381: */
382: PUBLIC HTAnchor * HTAnchor_followMainLink (HTAnchor * me)
383: {
384: return me ? me->mainLink.dest : NULL;
385: }
1.17 frystyk 386:
1.24 frystyk 387: /*
388: ** Moves all link information from one anchor to another.
389: ** This is used in redirection etc.
390: ** Returns YES if OK, else NO
391: */
1.35 frystyk 392: PUBLIC BOOL HTAnchor_moveAllLinks (HTAnchor * src, HTAnchor * dest)
1.17 frystyk 393: {
1.24 frystyk 394: if (!src || !dest) return NO;
395: if (ANCH_TRACE)
1.44 eric 396: HTTrace("Move Links.. from anchor %p to anchor %p\n",
1.24 frystyk 397: (void *) src, (void *) dest);
398:
399: /* Move main link information */
400: dest->mainLink.dest = src->mainLink.dest;
401: dest->mainLink.type = src->mainLink.type;
402: dest->mainLink.method = src->mainLink.method;
1.25 frystyk 403: dest->mainLink.result = src->mainLink.result;
1.24 frystyk 404:
405: src->mainLink.dest = NULL;
406: src->mainLink.type = NULL;
1.25 frystyk 407: src->mainLink.method = METHOD_INVALID;
1.28 frystyk 408: src->mainLink.result = HT_LINK_INVALID;
1.24 frystyk 409:
410: /* Move link information for other links */
1.26 frystyk 411: if (dest->links) {
412: HTList *cur = dest->links;
413: HTLink *pres;
414: while ((pres = (HTLink *) HTList_nextObject(cur)))
1.43 frystyk 415: HT_FREE(pres);
1.24 frystyk 416: HTList_delete(dest->links);
1.26 frystyk 417: }
1.24 frystyk 418: dest->links = src->links;
419: src->links = NULL;
420: return YES;
1.17 frystyk 421: }
422:
1.24 frystyk 423:
424: /*
1.28 frystyk 425: ** Removes all link information from one anchor to another.
426: ** Returns YES if OK, else NO
427: */
1.35 frystyk 428: PUBLIC BOOL HTAnchor_removeLink (HTAnchor * src, HTAnchor * dest)
1.28 frystyk 429: {
430: if (!src || !dest) return NO;
431: if (ANCH_TRACE)
1.44 eric 432: HTTrace("Remove Link. from anchor %p to anchor %p\n",
1.28 frystyk 433: (void *) src, (void *) dest);
434:
435: /* Remove if dest is the main link */
436: if (src->mainLink.dest == dest) {
437: src->mainLink.dest = NULL;
438: src->mainLink.type = NULL;
439: src->mainLink.method = METHOD_INVALID;
440: src->mainLink.result = HT_LINK_INVALID;
441: return YES;
442: }
443:
444: /* Remove link information for other links */
445: if (dest->links) {
446: HTList *cur = dest->links;
447: HTLink *pres;
448: while ((pres = (HTLink *) HTList_nextObject(cur))) {
449: if (pres->dest == dest) {
450: HTList_removeObject(dest->links, pres);
1.43 frystyk 451: HT_FREE(pres);
1.28 frystyk 452: return YES;
453: }
454: }
455: }
456: return NO;
457: }
458:
459: /*
460: ** Removes all link information
461: ** Returns YES if OK, else NO
462: */
1.35 frystyk 463: PUBLIC BOOL HTAnchor_removeAllLinks (HTAnchor * me)
1.28 frystyk 464: {
465: if (!me) return NO;
466: if (ANCH_TRACE)
1.44 eric 467: HTTrace("Remove Link. from anchor %p\n", (void *) me);
1.28 frystyk 468:
469: /* Remove if dest is the main link */
470: me->mainLink.dest = NULL;
471: me->mainLink.type = NULL;
472: me->mainLink.method = METHOD_INVALID;
473: me->mainLink.result = HT_LINK_INVALID;
474:
475: /* Remove link information for other links */
476: if (me->links) {
477: HTList *cur = me->links;
478: HTLink *pres;
479: while ((pres = (HTLink *) HTList_nextObject(cur)))
1.43 frystyk 480: HT_FREE(pres);
1.28 frystyk 481: HTList_delete(me->links);
482: me->links = NULL;
483: }
484: return YES;
485: }
486:
487: /*
1.24 frystyk 488: ** Returns a link with a given link type or NULL if nothing found
489: */
1.41 frystyk 490: PUBLIC HTAnchor * HTAnchor_followTypedLink (HTAnchor * me, HTLinkType type)
1.17 frystyk 491: {
1.24 frystyk 492: if (me->mainLink.type == type)
493: return me->mainLink.dest;
494: if (me->links) {
495: HTList *links = me->links;
496: HTLink *link;
497: while ((link = (HTLink *) HTList_nextObject (links)))
498: if (link->type == type)
499: return link->dest;
500: }
501: return NULL; /* No link of me type */
1.17 frystyk 502: }
503:
504:
505: /* Move an anchor to the head of the list of its siblings
506: ** ------------------------------------------------------
507: **
508: ** This is to ensure that an anchor which might have already existed
509: ** is put in the correct order as we load the document.
510: */
1.35 frystyk 511: PUBLIC void HTAnchor_makeLastChild (HTChildAnchor * me)
1.17 frystyk 512: {
1.35 frystyk 513: if (me->parent != (HTParentAnchor *) me) { /* Make sure it's a child */
514: HTList * siblings = me->parent->children;
515: HTList_removeObject (siblings, me);
516: HTList_addObject (siblings, me);
517: }
1.17 frystyk 518: }
519:
520: /* ------------------------------------------------------------------------- */
521: /* Deletion Methods */
522: /* ------------------------------------------------------------------------- */
1.1 timbl 523:
524: /* Delete an anchor and possibly related things (auto garbage collection)
525: ** --------------------------------------------
526: **
527: ** The anchor is only deleted if the corresponding document is not loaded.
1.10 frystyk 528: ** All outgoing links from parent and children are deleted, and this
529: ** anchor is removed from the sources list of all its targets.
1.1 timbl 530: ** We also try to delete the targets whose documents are not loaded.
531: ** If this anchor's source list is empty, we delete it and its children.
532: */
533:
1.41 frystyk 534: /* Deletes all the memory allocated in a parent anchor and returns any
535: ** hyperdoc object hanging of this anchor
1.19 frystyk 536: */
1.41 frystyk 537: PRIVATE void * delete_parent (HTParentAnchor * me)
1.19 frystyk 538: {
1.41 frystyk 539: void * doc = me->document;
1.19 frystyk 540:
541: /* Remove link and address information */
1.26 frystyk 542: if (me->links) {
543: HTList *cur = me->links;
544: HTLink *pres;
545: while ((pres = (HTLink *) HTList_nextObject(cur)))
1.43 frystyk 546: HT_FREE(pres);
1.26 frystyk 547: HTList_delete(me->links);
548: }
1.19 frystyk 549: HTList_delete (me->children);
550: HTList_delete (me->sources);
1.43 frystyk 551: HT_FREE(me->physical);
552: HT_FREE(me->address);
1.19 frystyk 553:
554: /* Then remove entity header information (metainformation) */
1.43 frystyk 555: HT_FREE(me->title);
1.47 ! frystyk 556: if (me->content_encoding) HTList_delete(me->content_encoding);
! 557: if (me->content_language) HTList_delete(me->content_language);
1.43 frystyk 558: HT_FREE(me->derived_from);
559: HT_FREE(me->version);
1.19 frystyk 560: if (me->extra_headers) {
561: HTList *cur = me->extra_headers;
562: char *pres;
563: while ((pres = (char *) HTList_nextObject(cur)))
1.43 frystyk 564: HT_FREE(pres);
1.19 frystyk 565: HTList_delete(me->extra_headers);
566: }
1.43 frystyk 567: HT_FREE(me);
1.19 frystyk 568: return doc;
569: }
570:
571:
1.41 frystyk 572: /* Delete a parent anchor and all its children. If a hyperdoc object
573: ** is found hanging off the parent anchor then this is returned
1.19 frystyk 574: */
1.41 frystyk 575: PRIVATE void * delete_family (HTAnchor * me)
1.19 frystyk 576: {
577: HTParentAnchor *parent = me->parent;
578: if (ANCH_TRACE)
1.44 eric 579: HTTrace("AnchorDelete Remove parent %p and children\n", parent);
1.19 frystyk 580: if (!me) {
581: if (ANCH_TRACE)
1.44 eric 582: HTTrace("AnchorDelete No anchor found\n");
1.19 frystyk 583: return NULL;
584: }
585:
586: /* Delete children */
587: if (parent->children) {
588: HTChildAnchor *child;
589: while ((child = (HTChildAnchor *)
590: HTList_removeLastObject(parent->children))) {
1.43 frystyk 591: HT_FREE(child->tag);
1.26 frystyk 592: if (child->links) {
593: HTList *cur = child->links;
594: HTLink *pres;
595: while ((pres = (HTLink *) HTList_nextObject(cur)))
1.43 frystyk 596: HT_FREE(pres);
1.26 frystyk 597: HTList_delete(child->links);
598: }
1.43 frystyk 599: HT_FREE(child);
1.19 frystyk 600: }
601: }
602: return delete_parent(parent);
603: }
604:
605:
606: /* DELETE ALL ANCHORS
607: ** ------------------
608: ** Deletes all anchors and return a list of all the HyperDocs found.
609: ** It is for the application to delete any HyperDocs.
1.39 frystyk 610: ** If NULL then no hyperdocs are returned
1.19 frystyk 611: ** Return YES if OK, else NO
612: */
1.35 frystyk 613: PUBLIC BOOL HTAnchor_deleteAll (HTList * documents)
1.19 frystyk 614: {
615: int cnt;
616: HTList *cur;
1.39 frystyk 617: if (!adult_table)
1.19 frystyk 618: return NO;
619: for (cnt=0; cnt<HASH_SIZE; cnt++) {
620: if ((cur = adult_table[cnt])) {
621: HTParentAnchor *pres;
622: while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL){
1.41 frystyk 623: void * doc = delete_family((HTAnchor *) pres);
624: if (doc && documents) HTList_addObject(documents, doc);
1.19 frystyk 625: }
626: }
627: HTList_delete(adult_table[cnt]);
628: }
1.43 frystyk 629: HT_FREE(adult_table);
1.19 frystyk 630: return YES;
631: }
632:
633:
1.35 frystyk 634: PRIVATE void deleteLinks (HTAnchor * me)
1.1 timbl 635: {
1.3 timbl 636: if (! me)
1.1 timbl 637: return;
638:
639: /* Recursively try to delete target anchors */
1.3 timbl 640: if (me->mainLink.dest) {
641: HTParentAnchor *parent = me->mainLink.dest->parent;
642: HTList_removeObject (parent->sources, me);
1.1 timbl 643: if (! parent->document) /* Test here to avoid calling overhead */
644: HTAnchor_delete (parent);
645: }
1.3 timbl 646: if (me->links) { /* Extra destinations */
1.1 timbl 647: HTLink *target;
1.12 frystyk 648: while ((target = (HTLink *) HTList_removeLastObject (me->links))) {
1.1 timbl 649: HTParentAnchor *parent = target->dest->parent;
1.3 timbl 650: HTList_removeObject (parent->sources, me);
1.1 timbl 651: if (! parent->document) /* Test here to avoid calling overhead */
652: HTAnchor_delete (parent);
653: }
654: }
655: }
656:
1.35 frystyk 657: PUBLIC BOOL HTAnchor_delete (HTParentAnchor * me)
1.1 timbl 658: {
659: HTChildAnchor *child;
660:
661: /* Don't delete if document is loaded */
1.3 timbl 662: if (me->document)
1.1 timbl 663: return NO;
664:
665: /* Recursively try to delete target anchors */
1.3 timbl 666: deleteLinks ((HTAnchor *) me);
1.1 timbl 667:
1.3 timbl 668: if (! HTList_isEmpty (me->sources)) { /* There are still incoming links */
1.1 timbl 669: /* Delete all outgoing links from children, if any */
1.3 timbl 670: HTList *kids = me->children;
1.12 frystyk 671: while ((child = (HTChildAnchor *) HTList_nextObject (kids)))
1.1 timbl 672: deleteLinks ((HTAnchor *) child);
673: return NO; /* Parent not deleted */
674: }
675:
676: /* No more incoming links : kill everything */
677: /* First, recursively delete children */
1.12 frystyk 678: while ((child = (HTChildAnchor *) HTList_removeLastObject (me->children))) {
1.1 timbl 679: deleteLinks ((HTAnchor *) child);
1.43 frystyk 680: HT_FREE(child->tag);
681: HT_FREE(child);
1.1 timbl 682: }
683:
684: /* Now kill myself */
1.19 frystyk 685: delete_parent(me);
1.1 timbl 686: return YES; /* Parent deleted */
687: }
688:
1.17 frystyk 689: /* ------------------------------------------------------------------------- */
690: /* Data Access Methods */
691: /* ------------------------------------------------------------------------- */
1.1 timbl 692:
1.35 frystyk 693: PUBLIC HTParentAnchor * HTAnchor_parent (HTAnchor * me)
1.1 timbl 694: {
1.17 frystyk 695: return me ? me->parent : NULL;
1.1 timbl 696: }
697:
1.41 frystyk 698: PUBLIC void HTAnchor_setDocument (HTParentAnchor * me, void * doc)
1.1 timbl 699: {
1.41 frystyk 700: if (me) me->document = doc;
1.1 timbl 701: }
702:
1.41 frystyk 703: PUBLIC void * HTAnchor_document (HTParentAnchor * me)
1.1 timbl 704: {
1.17 frystyk 705: return me ? me->document : NULL;
1.1 timbl 706: }
707:
1.35 frystyk 708: PUBLIC char * HTAnchor_address (HTAnchor * me)
1.1 timbl 709: {
1.17 frystyk 710: char *addr = NULL;
711: if (me) {
712: if (((HTParentAnchor *) me == me->parent) ||
713: !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
714: StrAllocCopy (addr, me->parent->address);
715: }
716: else { /* it's a named child */
1.43 frystyk 717: if ((addr = (char *) HT_MALLOC(2 + strlen (me->parent->address) + strlen (((HTChildAnchor *) me)->tag))) == NULL)
718: HT_OUTOFMEM("HTAnchor_address");
1.17 frystyk 719: sprintf (addr, "%s#%s", me->parent->address,
720: ((HTChildAnchor *) me)->tag);
721: }
1.1 timbl 722: }
1.17 frystyk 723: return addr;
1.1 timbl 724: }
725:
1.35 frystyk 726: PUBLIC BOOL HTAnchor_hasChildren (HTParentAnchor * me)
1.17 frystyk 727: {
728: return me ? ! HTList_isEmpty(me->children) : NO;
729: }
1.1 timbl 730:
1.35 frystyk 731: PUBLIC void HTAnchor_clearIndex (HTParentAnchor * me)
1.17 frystyk 732: {
1.36 frystyk 733: if (me) me->isIndex = NO;
1.17 frystyk 734: }
1.1 timbl 735:
1.35 frystyk 736: PUBLIC void HTAnchor_setIndex (HTParentAnchor * me)
1.1 timbl 737: {
1.36 frystyk 738: if (me) me->isIndex = YES;
1.17 frystyk 739: }
740:
1.35 frystyk 741: PUBLIC BOOL HTAnchor_isIndex (HTParentAnchor * me)
1.17 frystyk 742: {
743: return me ? me->isIndex : NO;
1.9 frystyk 744: }
1.1 timbl 745:
1.17 frystyk 746: /* Physical Address
747: ** ----------------
748: */
749:
1.35 frystyk 750: PUBLIC char * HTAnchor_physical (HTParentAnchor * me)
1.1 timbl 751: {
1.36 frystyk 752: return me ? me->physical : NULL;
1.1 timbl 753: }
754:
1.35 frystyk 755: PUBLIC void HTAnchor_setPhysical (HTParentAnchor * me,
756: char * physical)
1.1 timbl 757: {
1.17 frystyk 758: if (!me || !physical) {
759: if (ANCH_TRACE)
1.44 eric 760: HTTrace("HTAnchor.... setPhysical, called with null argument\n");
1.17 frystyk 761: return;
762: }
763: StrAllocCopy(me->physical, physical);
1.27 frystyk 764: }
765:
766: /* Cache Information
767: ** -----------------
768: */
1.35 frystyk 769: PUBLIC BOOL HTAnchor_cacheHit (HTParentAnchor * me)
1.27 frystyk 770: {
1.36 frystyk 771: return me ? me->cacheHit : NO;
1.27 frystyk 772: }
773:
1.35 frystyk 774: PUBLIC void HTAnchor_setCacheHit (HTParentAnchor * me, BOOL cacheHit)
1.27 frystyk 775: {
1.36 frystyk 776: if (me) me->cacheHit = cacheHit;
1.1 timbl 777: }
778:
1.17 frystyk 779: /* ------------------------------------------------------------------------- */
780: /* Entity Header Information */
781: /* ------------------------------------------------------------------------- */
782:
783: /*
784: ** Media Types (Content-Type)
785: */
1.35 frystyk 786: PUBLIC HTFormat HTAnchor_format (HTParentAnchor * me)
1.17 frystyk 787: {
788: return me ? me->content_type : NULL;
789: }
1.1 timbl 790:
1.35 frystyk 791: PUBLIC void HTAnchor_setFormat (HTParentAnchor * me, HTFormat form)
1.1 timbl 792: {
1.17 frystyk 793: if (me) me->content_type = form;
1.1 timbl 794: }
795:
1.17 frystyk 796: /*
797: ** Charset parameter to Content-Type
1.1 timbl 798: */
1.35 frystyk 799: PUBLIC HTCharset HTAnchor_charset (HTParentAnchor * me)
1.1 timbl 800: {
1.17 frystyk 801: return me ? me->charset : NULL;
1.1 timbl 802: }
803:
1.35 frystyk 804: PUBLIC void HTAnchor_setCharset (HTParentAnchor * me, HTCharset charset)
1.1 timbl 805: {
1.17 frystyk 806: if (me) me->charset = charset;
1.1 timbl 807: }
808:
1.17 frystyk 809: /*
1.20 frystyk 810: ** Level parameter to Content-Type
811: */
1.35 frystyk 812: PUBLIC HTLevel HTAnchor_level (HTParentAnchor * me)
1.20 frystyk 813: {
814: return me ? me->level : NULL;
815: }
816:
1.35 frystyk 817: PUBLIC void HTAnchor_setLevel (HTParentAnchor * me, HTLevel level)
1.20 frystyk 818: {
819: if (me) me->level = level;
820: }
821:
822: /*
1.17 frystyk 823: ** Content Encoding
824: */
1.47 ! frystyk 825: PUBLIC HTList * HTAnchor_encoding (HTParentAnchor * me)
1.1 timbl 826: {
1.17 frystyk 827: return me ? me->content_encoding : NULL;
1.1 timbl 828: }
829:
1.47 ! frystyk 830: PUBLIC BOOL HTAnchor_addEncoding (HTParentAnchor * me, HTEncoding encoding)
1.17 frystyk 831: {
1.47 ! frystyk 832: if (me && encoding) {
! 833: if (!me->content_encoding) me->content_encoding = HTList_new();
! 834: return HTList_addObject(me->content_encoding, encoding);
! 835: }
! 836: return NO;
1.17 frystyk 837: }
838:
839: /*
1.21 frystyk 840: ** Content Language
841: */
1.47 ! frystyk 842: PUBLIC HTList * HTAnchor_language (HTParentAnchor * me)
1.21 frystyk 843: {
844: return me ? me->content_language : NULL;
845: }
846:
1.47 ! frystyk 847: PUBLIC BOOL HTAnchor_addLanguage (HTParentAnchor * me, HTLanguage language)
1.21 frystyk 848: {
1.47 ! frystyk 849: if (me && language) {
! 850: if (!me->content_language) me->content_language = HTList_new();
! 851: return HTList_addObject(me->content_language, language);
! 852: }
! 853: return NO;
1.21 frystyk 854: }
855:
856: /*
1.17 frystyk 857: ** Content Transfer Encoding
1.1 timbl 858: */
1.35 frystyk 859: PUBLIC HTCte HTAnchor_cte (HTParentAnchor * me)
1.17 frystyk 860: {
861: return me ? me->cte : NULL;
862: }
1.1 timbl 863:
1.35 frystyk 864: PUBLIC void HTAnchor_setCte (HTParentAnchor * me, HTCte cte)
1.17 frystyk 865: {
866: if (me) me->cte = cte;
867: }
868:
869: /*
870: ** Content Length
871: */
1.35 frystyk 872: PUBLIC long int HTAnchor_length (HTParentAnchor * me)
1.1 timbl 873: {
1.17 frystyk 874: return me ? me->content_length : -1;
1.1 timbl 875: }
876:
1.35 frystyk 877: PUBLIC void HTAnchor_setLength (HTParentAnchor * me, long int length)
1.17 frystyk 878: {
879: if (me) me->content_length = length;
880: }
1.1 timbl 881:
1.17 frystyk 882: /*
883: ** Allowed methods (Allow)
1.1 timbl 884: */
1.45 frystyk 885: PUBLIC HTMethod HTAnchor_methods (HTParentAnchor * me)
1.17 frystyk 886: {
1.32 frystyk 887: return me ? me->methods : METHOD_INVALID;
1.17 frystyk 888: }
1.1 timbl 889:
1.45 frystyk 890: PUBLIC void HTAnchor_setMethods (HTParentAnchor * me, HTMethod methodset)
1.1 timbl 891: {
1.17 frystyk 892: if (me) me->methods = methodset;
1.1 timbl 893: }
894:
1.45 frystyk 895: PUBLIC void HTAnchor_appendMethods (HTParentAnchor * me, HTMethod methodset)
1.1 timbl 896: {
1.32 frystyk 897: if (me) me->methods |= methodset;
1.1 timbl 898: }
899:
1.17 frystyk 900: /*
901: ** Title
1.2 timbl 902: */
1.46 frystyk 903: PUBLIC const char * HTAnchor_title (HTParentAnchor * me)
1.1 timbl 904: {
1.17 frystyk 905: return me ? me->title : NULL;
906: }
1.1 timbl 907:
1.46 frystyk 908: PUBLIC void HTAnchor_setTitle (HTParentAnchor * me, const char * title)
1.17 frystyk 909: {
910: if (me && title) StrAllocCopy(me->title, title);
1.2 timbl 911: }
912:
1.46 frystyk 913: PUBLIC void HTAnchor_appendTitle (HTParentAnchor * me, const char * title)
1.17 frystyk 914: {
915: if (me && title) StrAllocCat(me->title, title);
916: }
1.2 timbl 917:
1.17 frystyk 918: /*
919: ** Version
1.2 timbl 920: */
1.41 frystyk 921: PUBLIC char * HTAnchor_version (HTParentAnchor * me)
1.17 frystyk 922: {
923: return me ? me->version : NULL;
924: }
1.2 timbl 925:
1.46 frystyk 926: PUBLIC void HTAnchor_setVersion (HTParentAnchor * me, const char * version)
1.2 timbl 927: {
1.17 frystyk 928: if (me && version) StrAllocCopy(me->version, version);
1.2 timbl 929: }
930:
1.17 frystyk 931: /*
932: ** Derived from
1.2 timbl 933: */
1.41 frystyk 934: PUBLIC char * HTAnchor_derived (HTParentAnchor * me)
1.17 frystyk 935: {
936: return me ? me->derived_from : NULL;
937: }
938:
1.46 frystyk 939: PUBLIC void HTAnchor_setDerived (HTParentAnchor * me, const char *derived_from)
1.17 frystyk 940: {
941: if (me && derived_from) StrAllocCopy(me->derived_from, derived_from);
942: }
1.2 timbl 943:
1.17 frystyk 944: /*
1.28 frystyk 945: ** Date
946: */
1.41 frystyk 947: PUBLIC time_t HTAnchor_date (HTParentAnchor * me)
948: {
949: return me ? me->date : -1;
950: }
951:
1.46 frystyk 952: PUBLIC void HTAnchor_setDate (HTParentAnchor * me, const time_t date)
1.28 frystyk 953: {
1.41 frystyk 954: if (me) me->date = date;
1.28 frystyk 955: }
956:
957: /*
958: ** Expires
959: */
1.41 frystyk 960: PUBLIC time_t HTAnchor_expires (HTParentAnchor * me)
961: {
962: return me ? me->expires : -1;
963: }
964:
1.46 frystyk 965: PUBLIC void HTAnchor_setExpires (HTParentAnchor * me, const time_t expires)
1.28 frystyk 966: {
1.41 frystyk 967: if (me) me->expires = expires;
1.28 frystyk 968: }
969:
970: /*
971: ** Last Modified
972: */
1.41 frystyk 973: PUBLIC time_t HTAnchor_lastModified (HTParentAnchor * me)
974: {
975: return me ? me->last_modified : -1;
976: }
977:
1.46 frystyk 978: PUBLIC void HTAnchor_setLastModified (HTParentAnchor * me, const time_t lm)
1.28 frystyk 979: {
1.41 frystyk 980: if (me) me->last_modified = lm;
1.28 frystyk 981: }
982:
983: /*
1.17 frystyk 984: ** Extra Header List of unknown headers
985: */
1.35 frystyk 986: PUBLIC HTList * HTAnchor_Extra (HTParentAnchor * me)
1.2 timbl 987: {
1.17 frystyk 988: return me ? me->extra_headers : NULL;
1.2 timbl 989: }
990:
1.46 frystyk 991: PUBLIC void HTAnchor_addExtra (HTParentAnchor * me, const char * header)
1.2 timbl 992: {
1.17 frystyk 993: if (me) {
1.18 frystyk 994: char *newhead = NULL;
995: StrAllocCopy(newhead, header);
1.17 frystyk 996: if (!me->extra_headers)
997: me->extra_headers = HTList_new();
1.18 frystyk 998: HTList_addObject(me->extra_headers, (void *) newhead);
1.17 frystyk 999: }
1.2 timbl 1000: }
1001:
1.23 frystyk 1002: /*
1003: ** Has header been parsed?
1.2 timbl 1004: */
1.35 frystyk 1005: PUBLIC BOOL HTAnchor_headerParsed (HTParentAnchor * me)
1.2 timbl 1006: {
1.17 frystyk 1007: return (me ? me->header_parsed : NO);
1.23 frystyk 1008: }
1009:
1.35 frystyk 1010: PUBLIC void HTAnchor_setHeaderParsed (HTParentAnchor * me)
1.23 frystyk 1011: {
1012: if (me) me->header_parsed = YES;
1.2 timbl 1013: }
1014:
1.17 frystyk 1015: /* Clear Header Information
1016: ** ------------------------
1017: */
1.35 frystyk 1018: PUBLIC void HTAnchor_clearHeader (HTParentAnchor * me)
1.2 timbl 1019: {
1.17 frystyk 1020: me->methods = METHOD_INVALID;
1021: me->content_encoding = NULL;
1.21 frystyk 1022: #ifdef NEW_CODE
1023: /* WAIT UNTIL WE HANDLE LANGUAGE AS A LIST */
1.17 frystyk 1024: if (me->content_language) {
1025: HTList_delete(me->content_language);
1026: me->content_language = HTList_new();
1.9 frystyk 1027: }
1.21 frystyk 1028: #else
1029: me->content_language = NULL;
1030: #endif
1.17 frystyk 1031: me->content_length = -1; /* Invalid */
1032: me->cte = NULL;
1033: me->content_type = WWW_UNKNOWN;
1034: me->charset = NULL;
1.20 frystyk 1035: me->level = NULL;
1.17 frystyk 1036:
1.28 frystyk 1037: me->date = (time_t) -1;
1038: me->expires = (time_t) -1;
1039: me->last_modified = (time_t) -1;
1.17 frystyk 1040:
1.43 frystyk 1041: HT_FREE(me->derived_from);
1042: HT_FREE(me->version);
1.18 frystyk 1043:
1044: if (me->extra_headers) {
1045: HTList *cur = me->extra_headers;
1046: char *pres;
1047: while ((pres = (char *) HTList_nextObject(cur)))
1.43 frystyk 1048: HT_FREE(pres);
1.18 frystyk 1049: HTList_delete(me->extra_headers);
1050: me->extra_headers = NULL;
1051: }
1.17 frystyk 1052: me->header_parsed = NO; /* All cleared */
1.1 timbl 1053: }
Webmaster