home *** CD-ROM | disk | FTP | other *** search
- /* -*- Emacs Mode: C++ -*- */
-
- /* link.c - Link Class implementation
-
- Copyright (C) 1989, Integrity Software
- Author: Isaac J. Salzman (salzman@rand.org)
-
- This software may be freely used/modified/distributed
- as desired so long as this copyright notice remains
- in tact.
-
- */
-
- #ifndef lint
- static char *RcsId = "$Header: /tmp_mnt/amnt/lh/salzman/src/class/RCS/link.c,v 1.1 89/09/17 15:01:28 salzman Exp Locker: salzman $";
- #endif
-
- /*
- * $Log: link.c,v $
- * Revision 1.1 89/09/17 15:01:28 salzman
- * Initial revision
- *
- */
-
- #include <stdio.h>
-
- #include "link.h"
-
- /* just set all ptr's to 0 ... */
-
- DEF_INIT(Link)()
- {
- nextp = (Link *)0;
- prevp = (Link *)0;
- tailp = (Link *)0;
-
- RETURN_THIS;
- }
-
- /* DANGER DANGER -- calling a destructor recursively! this starts from the
- head of the list and deletes all links. the tail link needs to be
- deleted first, and the work backwards - so we'll use recursion
- */
-
- DEF_DEST(Link)(void)
- {
- if (nextp)
- {
- #ifdef DEBUG
- printf("destroying next object. this is ...\n");
- showval();
- #endif
- DestroyObject(nextp);
- }
- #ifdef DEBUG
- else
- {
- printf("destroy: this is last object...\n");
- showval();
- }
- #endif
- }
-
- /* return a pointer to the next link */
-
- Link *Link::next(void)
- {
- return nextp;
- }
-
- /* return a pointer to the previous link */
-
- Link *Link::prev(void)
- {
- return prevp;
- }
-
- /* append to tail of list */
-
- int Link::append(class Link *l)
- {
- if (! l)
- {
- fprintf(stderr,"error - can't append nil link into list\n");
- return ERROR;
- }
-
- if (! nextp) /* this is only link in list */
- {
- nextp = l;
- tailp = l;
- l->prevp = this;
- }
- else
- {
- l->prevp = tailp;
- tailp->nextp = l; /* this was of course nil, should assert this */
- tailp = l;
- }
- return OK;
- }
-
-
- /* instert a new link into list at some position determined by
- the results of the compare method. this is an exercize
- for the reader (i.e. i'm lazy and haven't done it yet! :-)
- */
-
- int Link::insert(class Link *l)
- {
- return OK;
- }
-
- /* insert a new link at the head of the list */
-
- int Link::prepend(class Link *l)
- {
- if (! l)
- {
- fprintf(stderr,"error - can't prepend nil link into list\n");
- return ERROR;
- }
-
- if (!nextp) /* new head link */
- {
- nextp = l;
- l->prevp = this;
- tailp = l;
- }
- else /* push some ptr's around... */
- {
- l->nextp = nextp;
- nextp->prevp = l;
- nextp = l;
- l->tailp = l->nextp;
- }
-
- return OK;
- }
-
- /* forward traverse list and call showval method for each
- link. -- head of list is really the first nextp....
- */
-
- void Link::showlist(void)
- {
- register class Link *lp;
- register int i;
-
- for(i=0,lp=nextp; lp; lp = lp->nextp, i++)
- {
- printf("link %05d: ", i);
- lp->showval();
- }
- }
-
- /* same as above in reverse order */
-
- void Link::rshowlist(void)
- {
- register class Link *lp;
- register int i;
-
- for(i=0,lp=tailp; lp && lp->prevp ; lp = lp->prevp, i++)
- {
- printf("link %05d: ", i);
- lp->showval();
- }
- }
-
- /* this be virtual -- redefined for derived classes */
-
- void Link::showval(void)
- {
- printf("[showval: no value in Link base class]\n");
- }
-
- /* returns 0 if equal, < 0 if item less than this, > 0 if item > this */
-
- int Link::compare(char *item)
- {
- printf("[no compare method defined for Link base class]\n");
- return -1;
- }
-
- /* find an item given some string to look it up by. just calls the compare
- method -- returns ptr to the link, 0 if not found. linear search to
- end of list -- yes, inefficient.
- */
-
- Link *Link::find(char * item)
- {
- register Link *lp;
-
- for (lp=nextp; lp ; lp = lp->nextp)
- {
- if (lp->compare(item) == 0)
- {
- return lp;
- }
- }
-
- return (Link *) 0;
- }
-