home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libpics / htlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.4 KB  |  215 lines

  1.  
  2. /*  W3 Copyright statement 
  3. Copyright 1995 by: Massachusetts Institute of Technology (MIT), INRIA</H2>
  4.  
  5. This W3C software is being provided by the copyright holders under the
  6. following license. By obtaining, using and/or copying this software,
  7. you agree that you have read, understood, and will comply with the
  8. following terms and conditions: 
  9.  
  10. Permission to use, copy, modify, and distribute this software and its
  11. documentation for any purpose and without fee or royalty is hereby
  12. granted, provided that the full text of this NOTICE appears on
  13. <EM>ALL</EM> copies of the software and documentation or portions
  14. thereof, including modifications, that you make. 
  15.  
  16. <B>THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
  17. REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF EXAMPLE,
  18. BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
  19. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
  20. THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
  21. THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
  22. COPYRIGHT HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE
  23. OR DOCUMENTATION.
  24.  
  25. The name and trademarks of copyright holders may NOT be used
  26. in advertising or publicity pertaining to the software without
  27. specific, written prior permission.  Title to copyright in this
  28. software and any associated documentation will at all times remain
  29. with copyright holders. 
  30. */
  31. /*                                       HTList.c
  32. **    MANAGEMENT OF LINKED LISTS
  33. **
  34. **    (c) COPYRIGHT MIT 1995.
  35. **    Please first read the full copyright statement in the file COPYRIGH.
  36. **    @(#) $Id: htlist.c,v 3.1 1998/03/28 03:32:07 ltabb Exp $
  37. **
  38. **    A list is represented as a sequence of linked nodes of type HTList.
  39. **    The first node is a header which contains no object.
  40. **    New nodes are inserted between the header and the rest of the list.
  41. */
  42.  
  43. /* Library include files */
  44. /* --- BEGIN added by mharmsen@netscape.com on 7/9/97 --- */
  45. #include "xp.h"
  46. /* --- END added by mharmsen@netscape.com on 7/9/97 --- */
  47. /* #include "sysdep.h"  jhines -- 7/9/97 */
  48. #include "htutils.h"
  49. #include "htlist.h"
  50.  
  51. PUBLIC HTList * HTList_new (void)
  52. {
  53.     HTList *newList;
  54.     if ((newList = (HTList  *) HT_CALLOC(1, sizeof (HTList))) == NULL)
  55.         HT_OUTOFMEM("HTList_new");
  56.     newList->object = NULL;
  57.     newList->next = NULL;
  58.     return newList;
  59. }
  60.  
  61. PUBLIC BOOL HTList_delete (HTList * me)
  62. {
  63.     if (me) {
  64.     HTList *current;
  65.     while ((current = me) != NULL) {
  66.         me = me->next;
  67.         HT_FREE(current);
  68.     }
  69.     return YES;
  70.     }
  71.     return NO;
  72. }
  73.  
  74. PUBLIC BOOL HTList_addObject (HTList * me, void * newObject)
  75. {
  76.     if (me) {
  77.     HTList *newNode;
  78.     if ((newNode = (HTList  *) HT_CALLOC(1, sizeof(HTList))) == NULL)
  79.         HT_OUTOFMEM("HTList_addObject");
  80.     newNode->object = newObject;
  81.     newNode->next = me->next;
  82.     me->next = newNode;
  83.     return YES;
  84.     } else {
  85.     if (WWWTRACE)
  86.         HTTrace(
  87.             "HTList...... Can not add object %p to nonexisting list\n",
  88.             newObject);
  89.     }
  90.     return NO;
  91. }
  92.  
  93. PUBLIC BOOL HTList_appendObject (HTList * me, void * newObject)
  94. {
  95.     if (me) {
  96.     while (me->next) me = me->next;
  97.     return HTList_addObject(me, newObject);
  98.     }
  99.     return NO;
  100. }
  101.  
  102. PUBLIC BOOL HTList_removeObject (HTList *  me, void *  oldObject)
  103. {
  104.     if (me) {
  105.     HTList *previous;
  106.     while (me->next) {
  107.         previous = me;
  108.         me = me->next;
  109.         if (me->object == oldObject) {
  110.         previous->next = me->next;
  111.         HT_FREE(me);
  112.         return YES;    /* Success */
  113.         }
  114.     }
  115.     }
  116.     return NO;            /* object not found or NULL list */
  117. }
  118.  
  119. PUBLIC void * HTList_removeLastObject  (HTList * me)
  120. {
  121.     if (me && me->next) {
  122.     HTList *lastNode = me->next;
  123.     void * lastObject = lastNode->object;
  124.     me->next = lastNode->next;
  125.     HT_FREE(lastNode);
  126.     return lastObject;
  127.     } else            /* Empty list */
  128.     return NULL;
  129. }
  130.  
  131. PUBLIC void * HTList_removeFirstObject  (HTList * me)
  132. {
  133.     if (me && me->next) {
  134.     HTList * prevNode;
  135.     void *firstObject;
  136.     while (me->next) {
  137.         prevNode = me;
  138.         me = me->next;
  139.     }
  140.     firstObject = me->object;
  141.     prevNode->next = NULL;
  142.     HT_FREE(me);
  143.     return firstObject;
  144.     } else            /* Empty list */
  145.     return NULL;
  146. }
  147.  
  148. PUBLIC void * HTList_firstObject  (HTList * me)
  149. {
  150.     if (me && me->next) {
  151.     HTList * prevNode;
  152.     while (me->next) {
  153.         prevNode = me;
  154.         me = me->next;
  155.     }
  156.     return me->object;
  157.     } else            /* Empty list */
  158.     return NULL;
  159. }
  160.  
  161. PUBLIC int HTList_count  (HTList * me)
  162. {
  163.     int count = 0;
  164.     if (me)
  165.     while ((me = me->next) != NULL)
  166.         count++;
  167.     return count;
  168. }
  169.  
  170. PUBLIC int HTList_indexOf (HTList * me, void * object)
  171. {
  172.     if (me) {
  173.     int position = 0;
  174.     while ((me = me->next) != NULL) {
  175.         if (me->object == object)
  176.         return position;
  177.         position++;
  178.     }
  179.     }
  180.     return -1;
  181. }
  182.  
  183. PUBLIC void * HTList_objectAt  (HTList * me, int position)
  184. {
  185.     if (position < 0)
  186.     return NULL;
  187.     if (me) {
  188.     while ((me = me->next) != NULL) {
  189.         if (position == 0)
  190.         return me->object;
  191.         position--;
  192.     }
  193.     }
  194.     return NULL;        /* Reached the end of the list */
  195. }
  196.  
  197. PRIVATE void * HTList_removeObjectAt  (HTList * me, int position)
  198. {
  199.     if (position < 0)
  200.     return NULL;
  201.     if (me) {
  202.     HTList * prevNode;
  203.     prevNode = me;
  204.     while ((me = me->next) != NULL) {
  205.         if (position == 0) {
  206.         prevNode->next = me->next;
  207.         return me->object;
  208.         }
  209.         prevNode = me;
  210.         position--;
  211.     }
  212.     }
  213.     return NULL;  /* Reached the end of the list */
  214. }
  215.