home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d832 / term.lha / Term / term-3.1-Source.lha / termLists.c < prev    next >
C/C++ Source or Header  |  1993-02-18  |  6KB  |  318 lines

  1. /*
  2. **    termLists.c
  3. **
  4. **    Generic list management routines
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* ClearGenericList(struct GenericList *List):
  13.      *
  14.      *    Clear a generic list, free its contents.
  15.      */
  16.  
  17. VOID __regargs
  18. ClearGenericList(struct GenericList *List)
  19. {
  20.     struct Node *Node,*NextNode;
  21.  
  22.     ObtainSemaphore(&List -> ListSemaphore);
  23.  
  24.     Node = (struct Node *)List -> ListHeader . mlh_Head;
  25.  
  26.     while(NextNode = Node -> ln_Succ)
  27.     {
  28.         Remove(Node);
  29.  
  30.         FreeVec(Node);
  31.  
  32.         Node = NextNode;
  33.     }
  34.  
  35.     List -> ListCount = 0;
  36.  
  37.     ReleaseSemaphore(&List -> ListSemaphore);
  38. }
  39.  
  40.     /* DeleteGenericList(struct GenericList *List):
  41.      *
  42.      *    Free a generic list, including its contents.
  43.      */
  44.  
  45. VOID __regargs
  46. DeleteGenericList(struct GenericList *List)
  47. {
  48.     ClearGenericList(List);
  49.  
  50.     FreeVec(List);
  51. }
  52.  
  53.     /* CreateGenericList():
  54.      *
  55.      *    Create a generic list.
  56.      */
  57.  
  58. struct GenericList *
  59. CreateGenericList()
  60. {
  61.     struct GenericList *List;
  62.  
  63.     if(List = (struct GenericList *)AllocVec(sizeof(struct GenericList),MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC))
  64.     {
  65.         NewList((struct List *)&List -> ListHeader);
  66.  
  67.         InitSemaphore(&List -> ListSemaphore);
  68.  
  69.         List -> ListCount = 0;
  70.     }
  71.  
  72.     return(List);
  73. }
  74.  
  75.     /* FirstGenericListNode(struct GenericList *List):
  76.      *
  77.      *    Pick the first node in a generic list.
  78.      */
  79.  
  80. struct Node * __regargs
  81. FirstGenericListNode(struct GenericList *List)
  82. {
  83.     struct Node *Node;
  84.  
  85.     ObtainSemaphore(&List -> ListSemaphore);
  86.  
  87.     if(List -> ListHeader . mlh_Head -> mln_Succ)
  88.         Node = List -> ListNode = (struct Node *)List -> ListHeader . mlh_Head;
  89.     else
  90.         Node = NULL;
  91.  
  92.     ReleaseSemaphore(&List -> ListSemaphore);
  93.  
  94.     return(Node);
  95. }
  96.  
  97.     /* LastGenericListNode(struct GenericList *List):
  98.      *
  99.      *    Pick the last node in a generic list.
  100.      */
  101.  
  102. struct Node * __regargs
  103. LastGenericListNode(struct GenericList *List)
  104. {
  105.     struct Node *Node;
  106.  
  107.     ObtainSemaphore(&List -> ListSemaphore);
  108.  
  109.     if(List -> ListHeader . mlh_TailPred -> mln_Succ)
  110.         Node = List -> ListNode = (struct Node *)List -> ListHeader . mlh_TailPred;
  111.     else
  112.         Node = NULL;
  113.  
  114.     ReleaseSemaphore(&List -> ListSemaphore);
  115.  
  116.     return(Node);
  117. }
  118.  
  119.     /* NextGenericListNode(struct GenericList *List):
  120.      *
  121.      *    Pick the next successive node in a generic list.
  122.      */
  123.  
  124. struct Node * __regargs
  125. NextGenericListNode(struct GenericList *List)
  126. {
  127.     struct Node *Node;
  128.  
  129.     ObtainSemaphore(&List -> ListSemaphore);
  130.  
  131.     if(List -> ListNode)
  132.     {
  133.         if(List -> ListNode -> ln_Succ -> ln_Succ)
  134.             Node = List -> ListNode = List -> ListNode -> ln_Succ;
  135.         else
  136.             Node = NULL;
  137.  
  138.         ReleaseSemaphore(&List -> ListSemaphore);
  139.     }
  140.     else
  141.     {
  142.         ReleaseSemaphore(&List -> ListSemaphore);
  143.  
  144.         Node = FirstGenericListNode(List);
  145.     }
  146.  
  147.     return(Node);
  148. }
  149.  
  150.     /* PrevGenericListNode(struct GenericList *List):
  151.      *
  152.      *    Pick the preceding node in a generic list.
  153.      */
  154.  
  155. struct Node * __regargs
  156. PrevGenericListNode(struct GenericList *List)
  157. {
  158.     struct Node *Node;
  159.  
  160.     ObtainSemaphore(&List -> ListSemaphore);
  161.  
  162.     if(List -> ListNode)
  163.     {
  164.         if(List -> ListNode -> ln_Pred -> ln_Pred)
  165.             Node = List -> ListNode = List -> ListNode -> ln_Pred;
  166.         else
  167.             Node = NULL;
  168.  
  169.         ReleaseSemaphore(&List -> ListSemaphore);
  170.     }
  171.     else
  172.     {
  173.         ReleaseSemaphore(&List -> ListSemaphore);
  174.  
  175.         Node = LastGenericListNode(List);
  176.     }
  177.  
  178.     return(Node);
  179. }
  180.  
  181.     /* DeleteGenericListNode(struct GenericList *List,struct Node *Node):
  182.      *
  183.      *    Delete a single node from a generic list.
  184.      */
  185.  
  186. VOID __regargs
  187. DeleteGenericListNode(struct GenericList *List,struct Node *Node)
  188. {
  189.     ObtainSemaphore(&List -> ListSemaphore);
  190.  
  191.     if(!Node)
  192.         Node = List -> ListNode;
  193.  
  194.     if(Node)
  195.     {
  196.         if(Node == List -> ListNode)
  197.         {
  198.             if(Node -> ln_Succ -> ln_Succ)
  199.                 List -> ListNode = Node -> ln_Succ;
  200.             else
  201.             {
  202.                 if(Node -> ln_Pred -> ln_Pred)
  203.                     List -> ListNode = Node -> ln_Pred;
  204.                 else
  205.                     List -> ListNode = NULL;
  206.             }
  207.         }
  208.  
  209.         Remove(Node);
  210.  
  211.         FreeVec(Node);
  212.  
  213.         List -> ListCount--;
  214.     }
  215.  
  216.     ReleaseSemaphore(&List -> ListSemaphore);
  217. }
  218.  
  219.     /* CreateGenericListNode(LONG Size,STRPTR Name):
  220.      *
  221.      *    Create a new generic list node.
  222.      */
  223.  
  224. struct Node * __regargs
  225. CreateGenericListNode(LONG Size,STRPTR Name)
  226. {
  227.     struct Node    *Node;
  228.     LONG         Head;
  229.  
  230.     if(Size < sizeof(struct Node))
  231.         Head = Size = sizeof(struct Node);
  232.     else
  233.         Head = Size;
  234.  
  235.     if(Name)
  236.         Size += strlen(Name) + 1;
  237.  
  238.     if(Node = (struct Node *)AllocVec(Size,MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC))
  239.     {
  240.         if(Name)
  241.         {
  242.             Node -> ln_Name = ((char *)Node) + Head;
  243.  
  244.             strcpy(Node -> ln_Name,Name);
  245.         }
  246.         else
  247.             Node -> ln_Name = NULL;
  248.     }
  249.  
  250.     return(Node);
  251. }
  252.  
  253.     /* AddGenericListNode(struct GenericList *List,struct Node *Node,BYTE Mode):
  254.      *
  255.      *    Add a node to a generic list.
  256.      */
  257.  
  258. VOID __regargs
  259. AddGenericListNode(struct GenericList *List,struct Node *Node,BYTE Mode)
  260. {
  261.     ObtainSemaphore(&List -> ListSemaphore);
  262.  
  263.     switch(Mode)
  264.     {
  265.         case ADD_GLIST_BOTTOM:
  266.  
  267.             AddTail((struct List *)&List -> ListHeader,Node);
  268.             break;
  269.  
  270.         case ADD_GLIST_TOP:
  271.  
  272.             AddHead((struct List *)&List -> ListHeader,Node);
  273.             break;
  274.  
  275.         case ADD_GLIST_BEHIND:
  276.  
  277.             if(List -> ListNode)
  278.                 Insert((struct List *)&List -> ListHeader,Node,List -> ListNode);
  279.             else
  280.                 AddTail((struct List *)&List -> ListHeader,Node);
  281.  
  282.             break;
  283.  
  284.         case ADD_GLIST_BEFORE:
  285.  
  286.             if(List -> ListNode && List -> ListNode != (struct Node *)List -> ListHeader . mlh_Head)
  287.                 Insert((struct List *)&List -> ListHeader,Node,List -> ListNode -> ln_Pred);
  288.             else
  289.                 AddHead((struct List *)&List -> ListHeader,Node);
  290.  
  291.             break;
  292.     }
  293.  
  294.     List -> ListCount++;
  295.  
  296.     ReleaseSemaphore(&List -> ListSemaphore);
  297. }
  298.  
  299.     /* GenericListCount(struct GenericList *List):
  300.      *
  301.      *    Return the number of generic list entries.
  302.      */
  303.  
  304. LONG __regargs
  305. GenericListCount(struct GenericList *List)
  306. {
  307.     LONG Count;
  308.  
  309.     ObtainSemaphore(&List -> ListSemaphore);
  310.  
  311.     Count = List -> ListCount;
  312.  
  313.     ReleaseSemaphore(&List -> ListSemaphore);
  314.  
  315.     return(Count);
  316.  
  317. }
  318.