Annotation of libwww/Library/src/HTAnchor.c, revision 1.26

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

Webmaster