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

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.41      frystyk    14: **      3-May-1995 (HF), Added a lot of methods and other stuff made an object
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.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,
                     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.37      frystyk    81:            TTYPrint(TDEST, "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.37      frystyk    91:                        TTYPrint(TDEST,
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.37      frystyk   107:        TTYPrint(TDEST,"Find Child.. New Anchor %p named `%s' is child of %p\n",
1.17      frystyk   108:                (void *) child, tag ? tag : (CONST char *) "", (void *)parent);
                    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.35      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;
                    137:        CONST char *p;
                    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.37      frystyk   161:                    TTYPrint(TDEST, "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.37      frystyk   172:        if (ANCH_TRACE) TTYPrint(TDEST, "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,
                    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.37      frystyk   280:        TTYPrint(TDEST, "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: 
                    346:        /* Now make movingLink the new main link, and 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.37      frystyk   396:        TTYPrint(TDEST, "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.37      frystyk   432:        TTYPrint(TDEST, "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.37      frystyk   467:        TTYPrint(TDEST, "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);
        !           556:     HT_FREE(me->derived_from);
        !           557:     HT_FREE(me->version);
1.19      frystyk   558:     if (me->extra_headers) {
                    559:        HTList *cur = me->extra_headers;
                    560:        char *pres;
                    561:        while ((pres = (char *) HTList_nextObject(cur)))
1.43    ! frystyk   562:            HT_FREE(pres);
1.19      frystyk   563:        HTList_delete(me->extra_headers);
                    564:     }
1.43    ! frystyk   565:     HT_FREE(me);
1.19      frystyk   566:     return doc;
                    567: }
                    568: 
                    569: 
1.41      frystyk   570: /*     Delete a parent anchor and all its children. If a hyperdoc object
                    571: **     is found hanging off the parent anchor then this is returned
1.19      frystyk   572: */
1.41      frystyk   573: PRIVATE void * delete_family (HTAnchor * me)
1.19      frystyk   574: {
                    575:     HTParentAnchor *parent = me->parent;
                    576:     if (ANCH_TRACE)
1.37      frystyk   577:        TTYPrint(TDEST, "AnchorDelete Remove parent %p and children\n", parent);
1.19      frystyk   578:     if (!me) {
                    579:        if (ANCH_TRACE)
1.37      frystyk   580:            TTYPrint(TDEST, "AnchorDelete No anchor found\n");
1.19      frystyk   581:        return NULL;
                    582:     }
                    583: 
                    584:     /* Delete children */
                    585:     if (parent->children) {
                    586:        HTChildAnchor *child;
                    587:        while ((child = (HTChildAnchor *)
                    588:                HTList_removeLastObject(parent->children))) {
1.43    ! frystyk   589:            HT_FREE(child->tag);
1.26      frystyk   590:            if (child->links) {
                    591:                HTList *cur = child->links;
                    592:                HTLink *pres;
                    593:                while ((pres = (HTLink *) HTList_nextObject(cur)))
1.43    ! frystyk   594:                    HT_FREE(pres);
1.26      frystyk   595:                HTList_delete(child->links);
                    596:            }
1.43    ! frystyk   597:            HT_FREE(child);
1.19      frystyk   598:        }
                    599:     }
                    600:     return delete_parent(parent);
                    601: }
                    602: 
                    603: 
                    604: /*     DELETE ALL ANCHORS
                    605: **     ------------------
                    606: **     Deletes all anchors and return a list of all the HyperDocs found.
                    607: **     It is for the application to delete any HyperDocs.
1.39      frystyk   608: **     If NULL then no hyperdocs are returned
1.19      frystyk   609: **     Return YES if OK, else NO
                    610: */
1.35      frystyk   611: PUBLIC BOOL HTAnchor_deleteAll (HTList * documents)
1.19      frystyk   612: {
                    613:     int cnt;
                    614:     HTList *cur;
1.39      frystyk   615:     if (!adult_table)
1.19      frystyk   616:        return NO;
                    617:     for (cnt=0; cnt<HASH_SIZE; cnt++) {
                    618:        if ((cur = adult_table[cnt])) { 
                    619:            HTParentAnchor *pres;
                    620:            while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL){
1.41      frystyk   621:                void * doc = delete_family((HTAnchor *) pres);
                    622:                if (doc && documents) HTList_addObject(documents, doc);
1.19      frystyk   623:            }
                    624:        }
                    625:        HTList_delete(adult_table[cnt]);
                    626:     }
1.43    ! frystyk   627:     HT_FREE(adult_table);
1.19      frystyk   628:     return YES;
                    629: }
                    630: 
                    631: 
1.35      frystyk   632: PRIVATE void deleteLinks (HTAnchor * me)
1.1       timbl     633: {
1.3       timbl     634:   if (! me)
1.1       timbl     635:     return;
                    636: 
                    637:   /* Recursively try to delete target anchors */
1.3       timbl     638:   if (me->mainLink.dest) {
                    639:     HTParentAnchor *parent = me->mainLink.dest->parent;
                    640:     HTList_removeObject (parent->sources, me);
1.1       timbl     641:     if (! parent->document)  /* Test here to avoid calling overhead */
                    642:       HTAnchor_delete (parent);
                    643:   }
1.3       timbl     644:   if (me->links) {  /* Extra destinations */
1.1       timbl     645:     HTLink *target;
1.12      frystyk   646:     while ((target = (HTLink *) HTList_removeLastObject (me->links))) {
1.1       timbl     647:       HTParentAnchor *parent = target->dest->parent;
1.3       timbl     648:       HTList_removeObject (parent->sources, me);
1.1       timbl     649:       if (! parent->document)  /* Test here to avoid calling overhead */
                    650:        HTAnchor_delete (parent);
                    651:     }
                    652:   }
                    653: }
                    654: 
1.35      frystyk   655: PUBLIC BOOL HTAnchor_delete (HTParentAnchor * me)
1.1       timbl     656: {
                    657:   HTChildAnchor *child;
                    658: 
                    659:   /* Don't delete if document is loaded */
1.3       timbl     660:   if (me->document)
1.1       timbl     661:     return NO;
                    662: 
                    663:   /* Recursively try to delete target anchors */
1.3       timbl     664:   deleteLinks ((HTAnchor *) me);
1.1       timbl     665: 
1.3       timbl     666:   if (! HTList_isEmpty (me->sources)) {  /* There are still incoming links */
1.1       timbl     667:     /* Delete all outgoing links from children, if any */
1.3       timbl     668:     HTList *kids = me->children;
1.12      frystyk   669:     while ((child = (HTChildAnchor *) HTList_nextObject (kids)))
1.1       timbl     670:       deleteLinks ((HTAnchor *) child);
                    671:     return NO;  /* Parent not deleted */
                    672:   }
                    673: 
                    674:   /* No more incoming links : kill everything */
                    675:   /* First, recursively delete children */
1.12      frystyk   676:   while ((child = (HTChildAnchor *) HTList_removeLastObject (me->children))) {
1.1       timbl     677:     deleteLinks ((HTAnchor *) child);
1.43    ! frystyk   678:     HT_FREE(child->tag);
        !           679:     HT_FREE(child);
1.1       timbl     680:   }
                    681: 
                    682:   /* Now kill myself */
1.19      frystyk   683:   delete_parent(me);
1.1       timbl     684:   return YES;  /* Parent deleted */
                    685: }
                    686: 
1.17      frystyk   687: /* ------------------------------------------------------------------------- */
                    688: /*                             Data Access Methods                          */
                    689: /* ------------------------------------------------------------------------- */
1.1       timbl     690: 
1.35      frystyk   691: PUBLIC HTParentAnchor * HTAnchor_parent  (HTAnchor * me)
1.1       timbl     692: {
1.17      frystyk   693:     return me ? me->parent : NULL;
1.1       timbl     694: }
                    695: 
1.41      frystyk   696: PUBLIC void HTAnchor_setDocument  (HTParentAnchor * me, void * doc)
1.1       timbl     697: {
1.41      frystyk   698:     if (me) me->document = doc;
1.1       timbl     699: }
                    700: 
1.41      frystyk   701: PUBLIC void * HTAnchor_document  (HTParentAnchor * me)
1.1       timbl     702: {
1.17      frystyk   703:     return me ? me->document : NULL;
1.1       timbl     704: }
                    705: 
1.35      frystyk   706: PUBLIC char * HTAnchor_address  (HTAnchor * me)
1.1       timbl     707: {
1.17      frystyk   708:     char *addr = NULL;
                    709:     if (me) {
                    710:        if (((HTParentAnchor *) me == me->parent) ||
                    711:            !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
                    712:            StrAllocCopy (addr, me->parent->address);
                    713:        }
                    714:        else {                  /* it's a named child */
1.43    ! frystyk   715:            if ((addr = (char  *) HT_MALLOC(2 + strlen (me->parent->address) + strlen (((HTChildAnchor *) me)->tag))) == NULL)
        !           716:                HT_OUTOFMEM("HTAnchor_address");
1.17      frystyk   717:            sprintf (addr, "%s#%s", me->parent->address,
                    718:                     ((HTChildAnchor *) me)->tag);
                    719:        }
1.1       timbl     720:     }
1.17      frystyk   721:     return addr;
1.1       timbl     722: }
                    723: 
1.35      frystyk   724: PUBLIC BOOL HTAnchor_hasChildren  (HTParentAnchor * me)
1.17      frystyk   725: {
                    726:     return me ? ! HTList_isEmpty(me->children) : NO;
                    727: }
1.1       timbl     728: 
1.35      frystyk   729: PUBLIC void HTAnchor_clearIndex  (HTParentAnchor * me)
1.17      frystyk   730: {
1.36      frystyk   731:     if (me) me->isIndex = NO;
1.17      frystyk   732: }
1.1       timbl     733: 
1.35      frystyk   734: PUBLIC void HTAnchor_setIndex  (HTParentAnchor * me)
1.1       timbl     735: {
1.36      frystyk   736:   if (me) me->isIndex = YES;
1.17      frystyk   737: }
                    738: 
1.35      frystyk   739: PUBLIC BOOL HTAnchor_isIndex  (HTParentAnchor * me)
1.17      frystyk   740: {
                    741:     return me ? me->isIndex : NO;
1.9       frystyk   742: }
1.1       timbl     743: 
1.17      frystyk   744: /*     Physical Address
                    745: **     ----------------
                    746: */
                    747: 
1.35      frystyk   748: PUBLIC char * HTAnchor_physical (HTParentAnchor * me)
1.1       timbl     749: {
1.36      frystyk   750:     return me ? me->physical : NULL;
1.1       timbl     751: }
                    752: 
1.35      frystyk   753: PUBLIC void HTAnchor_setPhysical (HTParentAnchor * me,
                    754:        char *  physical)
1.1       timbl     755: {
1.17      frystyk   756:     if (!me || !physical) {
                    757:        if (ANCH_TRACE)
1.37      frystyk   758:            TTYPrint(TDEST, "HTAnchor.... setPhysical, called with null argument\n");
1.17      frystyk   759:        return;
                    760:     }
                    761:     StrAllocCopy(me->physical, physical);
1.27      frystyk   762: }
                    763: 
                    764: /*     Cache Information
                    765: **     -----------------
                    766: */
1.35      frystyk   767: PUBLIC BOOL HTAnchor_cacheHit (HTParentAnchor * me)
1.27      frystyk   768: {
1.36      frystyk   769:     return me ? me->cacheHit : NO;
1.27      frystyk   770: }
                    771: 
1.35      frystyk   772: PUBLIC void HTAnchor_setCacheHit (HTParentAnchor * me, BOOL cacheHit)
1.27      frystyk   773: {
1.36      frystyk   774:     if (me) me->cacheHit = cacheHit;
1.1       timbl     775: }
                    776: 
1.17      frystyk   777: /* ------------------------------------------------------------------------- */
                    778: /*                           Entity Header Information                      */
                    779: /* ------------------------------------------------------------------------- */
                    780: 
                    781: /*
                    782: **     Media Types (Content-Type)
                    783: */
1.35      frystyk   784: PUBLIC HTFormat HTAnchor_format (HTParentAnchor * me)
1.17      frystyk   785: {
                    786:     return me ? me->content_type : NULL;
                    787: }
1.1       timbl     788: 
1.35      frystyk   789: PUBLIC void HTAnchor_setFormat (HTParentAnchor * me, HTFormat form)
1.1       timbl     790: {
1.17      frystyk   791:     if (me) me->content_type = form;
1.1       timbl     792: }
                    793: 
1.17      frystyk   794: /*
                    795: **     Charset parameter to Content-Type
1.1       timbl     796: */
1.35      frystyk   797: PUBLIC HTCharset HTAnchor_charset (HTParentAnchor * me)
1.1       timbl     798: {
1.17      frystyk   799:     return me ? me->charset : NULL;
1.1       timbl     800: }
                    801: 
1.35      frystyk   802: PUBLIC void HTAnchor_setCharset (HTParentAnchor * me, HTCharset charset)
1.1       timbl     803: {
1.17      frystyk   804:     if (me) me->charset = charset;
1.1       timbl     805: }
                    806: 
1.17      frystyk   807: /*
1.20      frystyk   808: **     Level parameter to Content-Type
                    809: */
1.35      frystyk   810: PUBLIC HTLevel HTAnchor_level (HTParentAnchor * me)
1.20      frystyk   811: {
                    812:     return me ? me->level : NULL;
                    813: }
                    814: 
1.35      frystyk   815: PUBLIC void HTAnchor_setLevel (HTParentAnchor * me, HTLevel level)
1.20      frystyk   816: {
                    817:     if (me) me->level = level;
                    818: }
                    819: 
                    820: /*
1.17      frystyk   821: **     Content Encoding
                    822: */
1.35      frystyk   823: PUBLIC HTEncoding HTAnchor_encoding (HTParentAnchor * me)
1.1       timbl     824: {
1.17      frystyk   825:     return me ? me->content_encoding : NULL;
1.1       timbl     826: }
                    827: 
1.35      frystyk   828: PUBLIC void HTAnchor_setEncoding (HTParentAnchor * me, HTEncoding encoding)
1.17      frystyk   829: {
                    830:     if (me) me->content_encoding = encoding;
                    831: }
                    832: 
                    833: /*
1.21      frystyk   834: **     Content Language
                    835: **     @@@ SHOULD BE A LIST @@@
                    836: */
1.35      frystyk   837: PUBLIC HTLanguage HTAnchor_language (HTParentAnchor * me)
1.21      frystyk   838: {
                    839:     return me ? me->content_language : NULL;
                    840: }
                    841: 
1.35      frystyk   842: PUBLIC void HTAnchor_setLanguage (HTParentAnchor * me, HTLanguage language)
1.21      frystyk   843: {
                    844:     if (me) me->content_language = language;
                    845: }
                    846: 
                    847: /*
1.17      frystyk   848: **     Content Transfer Encoding
1.1       timbl     849: */
1.35      frystyk   850: PUBLIC HTCte HTAnchor_cte (HTParentAnchor * me)
1.17      frystyk   851: {
                    852:     return me ? me->cte : NULL;
                    853: }
1.1       timbl     854: 
1.35      frystyk   855: PUBLIC void HTAnchor_setCte (HTParentAnchor * me, HTCte cte)
1.17      frystyk   856: {
                    857:     if (me) me->cte = cte;
                    858: }
                    859: 
                    860: /*
                    861: **     Content Length
                    862: */
1.35      frystyk   863: PUBLIC long int HTAnchor_length (HTParentAnchor * me)
1.1       timbl     864: {
1.17      frystyk   865:     return me ? me->content_length : -1;
1.1       timbl     866: }
                    867: 
1.35      frystyk   868: PUBLIC void HTAnchor_setLength (HTParentAnchor * me, long int length)
1.17      frystyk   869: {
                    870:     if (me) me->content_length = length;
                    871: }
1.1       timbl     872: 
1.17      frystyk   873: /*
                    874: **     Allowed methods (Allow)
1.1       timbl     875: */
1.35      frystyk   876: PUBLIC int HTAnchor_methods (HTParentAnchor * me)
1.17      frystyk   877: {
1.32      frystyk   878:     return me ? me->methods : METHOD_INVALID;
1.17      frystyk   879: }
1.1       timbl     880: 
1.35      frystyk   881: PUBLIC void HTAnchor_setMethods (HTParentAnchor * me, int methodset)
1.1       timbl     882: {
1.17      frystyk   883:     if (me) me->methods = methodset;
1.1       timbl     884: }
                    885: 
1.35      frystyk   886: PUBLIC void HTAnchor_appendMethods (HTParentAnchor * me, int methodset)
1.1       timbl     887: {
1.32      frystyk   888:     if (me) me->methods |= methodset;
1.1       timbl     889: }
                    890: 
1.17      frystyk   891: /*
                    892: **     Title
1.2       timbl     893: */
1.35      frystyk   894: PUBLIC CONST char * HTAnchor_title  (HTParentAnchor * me)
1.1       timbl     895: {
1.17      frystyk   896:     return me ? me->title : NULL;
                    897: }
1.1       timbl     898: 
1.35      frystyk   899: PUBLIC void HTAnchor_setTitle (HTParentAnchor * me, CONST char * title)
1.17      frystyk   900: {
                    901:     if (me && title) StrAllocCopy(me->title, title);
1.2       timbl     902: }
                    903: 
1.35      frystyk   904: PUBLIC void HTAnchor_appendTitle (HTParentAnchor * me, CONST char * title)
1.17      frystyk   905: {
                    906:     if (me && title) StrAllocCat(me->title, title);
                    907: }
1.2       timbl     908: 
1.17      frystyk   909: /*
                    910: **     Version
1.2       timbl     911: */
1.41      frystyk   912: PUBLIC char * HTAnchor_version (HTParentAnchor * me)
1.17      frystyk   913: {
                    914:     return me ? me->version : NULL;
                    915: }
1.2       timbl     916: 
1.35      frystyk   917: PUBLIC void HTAnchor_setVersion (HTParentAnchor * me, CONST char * version)
1.2       timbl     918: {
1.17      frystyk   919:     if (me && version) StrAllocCopy(me->version, version);
1.2       timbl     920: }
                    921: 
1.17      frystyk   922: /*
                    923: **     Derived from
1.2       timbl     924: */
1.41      frystyk   925: PUBLIC char * HTAnchor_derived (HTParentAnchor * me)
1.17      frystyk   926: {
                    927:     return me ? me->derived_from : NULL;
                    928: }
                    929: 
1.35      frystyk   930: PUBLIC void HTAnchor_setDerived (HTParentAnchor * me, CONST char *derived_from)
1.17      frystyk   931: {
                    932:     if (me && derived_from) StrAllocCopy(me->derived_from, derived_from);
                    933: }
1.2       timbl     934: 
1.17      frystyk   935: /*
1.28      frystyk   936: **     Date
                    937: */
1.41      frystyk   938: PUBLIC time_t HTAnchor_date (HTParentAnchor * me)
                    939: {
                    940:     return me ? me->date : -1;
                    941: }
                    942: 
                    943: PUBLIC void HTAnchor_setDate (HTParentAnchor * me, CONST time_t date)
1.28      frystyk   944: {
1.41      frystyk   945:     if (me) me->date = date;
1.28      frystyk   946: }
                    947: 
                    948: /*
                    949: **     Expires
                    950: */
1.41      frystyk   951: PUBLIC time_t HTAnchor_expires (HTParentAnchor * me)
                    952: {
                    953:     return me ? me->expires : -1;
                    954: }
                    955: 
                    956: PUBLIC void HTAnchor_setExpires (HTParentAnchor * me, CONST time_t expires)
1.28      frystyk   957: {
1.41      frystyk   958:     if (me) me->expires = expires;
1.28      frystyk   959: }
                    960: 
                    961: /*
                    962: **     Last Modified
                    963: */
1.41      frystyk   964: PUBLIC time_t HTAnchor_lastModified (HTParentAnchor * me)
                    965: {
                    966:     return me ? me->last_modified : -1;
                    967: }
                    968: 
                    969: PUBLIC void HTAnchor_setLastModified (HTParentAnchor * me, CONST time_t lm)
1.28      frystyk   970: {
1.41      frystyk   971:     if (me) me->last_modified = lm;
1.28      frystyk   972: }
                    973: 
                    974: /*
1.17      frystyk   975: **     Extra Header List of unknown headers
                    976: */
1.35      frystyk   977: PUBLIC HTList * HTAnchor_Extra  (HTParentAnchor * me)
1.2       timbl     978: {
1.17      frystyk   979:     return me ? me->extra_headers : NULL;
1.2       timbl     980: }
                    981: 
1.35      frystyk   982: PUBLIC void HTAnchor_addExtra (HTParentAnchor * me, CONST char * header)
1.2       timbl     983: {
1.17      frystyk   984:     if (me) {
1.18      frystyk   985:        char *newhead = NULL;
                    986:        StrAllocCopy(newhead, header);
1.17      frystyk   987:        if (!me->extra_headers)
                    988:            me->extra_headers = HTList_new();
1.18      frystyk   989:        HTList_addObject(me->extra_headers, (void *) newhead);
1.17      frystyk   990:     }
1.2       timbl     991: }
                    992: 
1.23      frystyk   993: /*
                    994: **     Has header been parsed?
1.2       timbl     995: */
1.35      frystyk   996: PUBLIC BOOL HTAnchor_headerParsed (HTParentAnchor * me)
1.2       timbl     997: {
1.17      frystyk   998:     return (me ? me->header_parsed : NO);
1.23      frystyk   999: }
                   1000: 
1.35      frystyk  1001: PUBLIC void HTAnchor_setHeaderParsed (HTParentAnchor * me)
1.23      frystyk  1002: {
                   1003:     if (me) me->header_parsed = YES;
1.2       timbl    1004: }
                   1005: 
1.17      frystyk  1006: /*     Clear Header Information
                   1007: **     ------------------------
                   1008: */
1.35      frystyk  1009: PUBLIC void HTAnchor_clearHeader (HTParentAnchor * me)
1.2       timbl    1010: {
1.17      frystyk  1011:     me->methods = METHOD_INVALID;
                   1012:     me->content_encoding = NULL;
1.21      frystyk  1013: #ifdef NEW_CODE
                   1014:     /* WAIT UNTIL WE HANDLE LANGUAGE AS A LIST */
1.17      frystyk  1015:     if (me->content_language) {
                   1016:        HTList_delete(me->content_language);
                   1017:        me->content_language = HTList_new();
1.9       frystyk  1018:     }
1.21      frystyk  1019: #else
                   1020:     me->content_language = NULL;
                   1021: #endif
1.17      frystyk  1022:     me->content_length = -1;                                     /* Invalid */
                   1023:     me->cte = NULL;
                   1024:     me->content_type = WWW_UNKNOWN;
                   1025:     me->charset = NULL;
1.20      frystyk  1026:     me->level = NULL;
1.17      frystyk  1027:     
1.28      frystyk  1028:     me->date = (time_t) -1;
                   1029:     me->expires = (time_t) -1;
                   1030:     me->last_modified = (time_t) -1;
1.17      frystyk  1031:     
1.43    ! frystyk  1032:     HT_FREE(me->derived_from);
        !          1033:     HT_FREE(me->version);
1.18      frystyk  1034: 
                   1035:     if (me->extra_headers) {
                   1036:        HTList *cur = me->extra_headers;
                   1037:        char *pres;
                   1038:        while ((pres = (char *) HTList_nextObject(cur)))
1.43    ! frystyk  1039:            HT_FREE(pres);
1.18      frystyk  1040:        HTList_delete(me->extra_headers);
                   1041:        me->extra_headers = NULL;
                   1042:     }
1.17      frystyk  1043:     me->header_parsed = NO;                                  /* All cleared */
1.1       timbl    1044: }

Webmaster