home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / tcopuls_.sit / link.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-17  |  3.7 KB  |  205 lines  |  [TEXT/MPS ]

  1. /* -*- Emacs Mode: C++ -*- */
  2.  
  3. /*    link.c - Link Class implementation
  4.  
  5.     Copyright (C) 1989, Integrity Software
  6.     Author: Isaac J. Salzman (salzman@rand.org)
  7.  
  8.     This software may be freely used/modified/distributed
  9.     as desired so long as this copyright notice remains
  10.     in tact.
  11.  
  12. */
  13.  
  14. #ifndef lint
  15. 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 $";
  16. #endif
  17.  
  18. /*
  19.  * $Log:    link.c,v $
  20.  * Revision 1.1  89/09/17  15:01:28  salzman
  21.  * Initial revision
  22.  * 
  23.  */
  24.  
  25. #include <stdio.h>
  26.  
  27. #include "link.h"
  28.  
  29. /* just set all ptr's to 0 ... */
  30.  
  31. DEF_INIT(Link)()
  32. {
  33.     nextp = (Link *)0;
  34.     prevp = (Link *)0;
  35.     tailp = (Link *)0;
  36.     
  37.     RETURN_THIS;
  38. }
  39.  
  40. /* DANGER DANGER -- calling a destructor recursively! this starts from the
  41.    head of the list and deletes all links. the tail link needs to be
  42.    deleted first, and the work backwards - so we'll use recursion
  43. */
  44.  
  45. DEF_DEST(Link)(void)
  46. {
  47.     if (nextp)
  48.       {
  49. #ifdef DEBUG
  50.           printf("destroying next object. this is ...\n");
  51.          showval();
  52. #endif
  53.           DestroyObject(nextp);
  54.       }
  55. #ifdef DEBUG
  56.     else
  57.       {
  58.           printf("destroy: this is last object...\n");
  59.           showval();
  60.       }
  61. #endif
  62. }
  63.  
  64. /* return a pointer to the next link */
  65.  
  66. Link *Link::next(void)
  67. {
  68.     return nextp;
  69. }
  70.  
  71. /*  return a pointer to the previous link */
  72.  
  73. Link *Link::prev(void)
  74. {
  75.     return prevp;
  76. }
  77.  
  78. /* append to tail of list */
  79.  
  80. int Link::append(class Link *l)
  81. {
  82.     if (! l)
  83.       {
  84.         fprintf(stderr,"error - can't append nil link into list\n");
  85.         return ERROR;
  86.       }
  87.     
  88.     if (! nextp)    /* this is only link in list */
  89.       {
  90.           nextp = l;
  91.         tailp = l;
  92.         l->prevp = this;
  93.        }
  94.      else
  95.        {
  96.            l->prevp = tailp;
  97.            tailp->nextp = l; /* this was of course nil, should assert this */
  98.            tailp = l;
  99.        }
  100.     return OK;
  101. }
  102.  
  103.  
  104. /* instert a new link into list at some position determined by
  105.    the results of the compare method. this is an exercize
  106.    for the reader (i.e. i'm lazy and haven't done it yet! :-)
  107. */
  108.  
  109. int Link::insert(class Link *l)
  110. {
  111.     return OK;
  112. }
  113.  
  114. /* insert a new link at the head of the list */
  115.  
  116. int Link::prepend(class Link *l)
  117. {
  118.     if (! l)
  119.       {
  120.         fprintf(stderr,"error - can't prepend nil link into list\n");
  121.         return ERROR;
  122.       }
  123.  
  124.     if (!nextp)        /* new head link */
  125.     {
  126.         nextp = l;
  127.         l->prevp = this;
  128.         tailp = l;
  129.     }
  130.     else            /* push some ptr's around... */
  131.     {
  132.         l->nextp = nextp;
  133.         nextp->prevp = l;
  134.         nextp = l;
  135.         l->tailp = l->nextp;
  136.     }
  137.     
  138.     return OK;
  139. }
  140.  
  141. /* forward traverse list and call showval method for each
  142.    link. -- head of list is really the first nextp....
  143. */
  144.  
  145. void Link::showlist(void)
  146. {
  147.     register class Link *lp;
  148.     register int i;
  149.     
  150.     for(i=0,lp=nextp; lp; lp = lp->nextp, i++)
  151.     {
  152.         printf("link %05d: ", i);
  153.         lp->showval();
  154.     }
  155. }
  156.  
  157. /* same as above in reverse order  */
  158.  
  159. void Link::rshowlist(void)
  160. {
  161.     register class Link *lp;
  162.     register int i;
  163.     
  164.     for(i=0,lp=tailp; lp && lp->prevp ; lp = lp->prevp, i++)
  165.     {
  166.         printf("link %05d: ", i);
  167.         lp->showval();
  168.     }
  169. }
  170.  
  171. /* this be virtual -- redefined for derived classes */
  172.  
  173. void Link::showval(void)
  174. {
  175.     printf("[showval: no value in Link base class]\n");
  176. }
  177.  
  178. /* returns 0 if equal, < 0 if item less than this, > 0 if item > this */
  179.  
  180. int Link::compare(char *item)
  181. {
  182.     printf("[no compare method defined for Link base class]\n");
  183.     return -1;
  184. }
  185.  
  186. /* find an item given some string to look it up by. just calls the compare
  187.    method -- returns ptr to the link, 0 if not found. linear search to
  188.    end of list -- yes, inefficient.
  189. */
  190.  
  191. Link *Link::find(char * item)
  192. {
  193.     register Link *lp;
  194.     
  195.     for (lp=nextp; lp ; lp = lp->nextp)
  196.       {
  197.       if (lp->compare(item) == 0)
  198.         {
  199.             return lp;
  200.         }
  201.       }
  202.  
  203.     return (Link *) 0;
  204. }
  205.