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

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

Webmaster