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