home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume21 / pan / part01 / pan3.0 / llm.c < prev   
Encoding:
C/C++ Source or Header  |  1993-11-08  |  8.0 KB  |  417 lines

  1. /*
  2. Post A Note V3.0
  3. Copyright (c) 1993, Jeffrey W. Bailey
  4. All rights reserved.
  5.  
  6. Permission is granted to distribute this program in exact, complete
  7. source form, which includes this copyright notice, as long as no fee
  8. other than media and distribution cost is charged.
  9.  
  10. This program may not be used in whole, or in part, in any other manner
  11. without prior written permission from the author.
  12.  
  13. This program may not be distributed in modified form without prior
  14. written permission from the author.  In other words, patches may be
  15. distributed, but modified source may not be distributed.
  16.  
  17. If there are any questions, comments or suggestions, the author may be
  18. contacted at:
  19.  
  20.     jeff@rd1.interlan.com
  21.  
  22.     or
  23.  
  24.     Jeffrey Bailey
  25.     Racal-Datacom, Inc.
  26.     Mail Stop E-110
  27.     1601 N. Harrison Parkway
  28.     Sunrise, FL  33323-2899
  29. */
  30.  
  31. #include <stdio.h>
  32. #include "pan.h"
  33.  
  34. LLM_init(root, size)
  35.     struct LLM_root *root;
  36.     int  size;
  37.     {
  38.     if(root == NULL)
  39.         {
  40.         return(NULL);
  41.         }
  42.     root->First   = NULL;
  43.     root->Last    = NULL;
  44.     root->Current = NULL;
  45.     root->Size    = size + sizeof(struct LLM_node);
  46.     root->Init    = 1;
  47.     return(1);
  48.     }
  49.  
  50. char *LLM_add(root)
  51.     struct LLM_root *root;
  52.     {
  53.     struct LLM_node *hold, *temp;
  54.  
  55.     if(root == NULL)
  56.         {
  57.         return(NULL);
  58.         }
  59.     if(root->Init != 1)
  60.         {
  61.         return(NULL);
  62.         }
  63.     hold = (struct LLM_node *) malloc(root->Size);
  64.     if(hold == NULL)
  65.         {
  66.         return(NULL);
  67.         }
  68.     if(root->First == NULL)
  69.         {
  70.         root->First = hold;
  71.         root->Last = hold;
  72.         root->Current = hold;
  73.         hold->Next = NULL;
  74.         hold->Prev = NULL;
  75.         }
  76.     else
  77.         {
  78.         temp = root->Last;
  79.         temp->Next = hold;
  80.         hold->Next = NULL;
  81.         hold->Prev = temp;
  82.         root->Last = hold;
  83.         }
  84.     return( (char *) (hold + 1));
  85.     }
  86.  
  87. char *LLM_first(root)
  88.     struct LLM_root *root;
  89.     {
  90.     if(root == NULL)
  91.         {
  92.         return(NULL);
  93.         }
  94.     if(root->Init != 1)
  95.         {
  96.         return(NULL);
  97.         }
  98.     if(root->First == NULL)
  99.         {
  100.         return(NULL);
  101.         }
  102.     root->Current = root->First;
  103.     return((char *) (root->Current + 1));
  104.     }
  105.  
  106. char *LLM_last(root)
  107.     struct LLM_root *root;
  108.     {
  109.     if(root == NULL)
  110.         {
  111.         return(NULL);
  112.         }
  113.     if(root->Init != 1)
  114.         {
  115.         return(NULL);
  116.         }
  117.     if(root->Last == NULL)
  118.         {
  119.         return(NULL);
  120.         }
  121.     root->Current = root->Last;
  122.     return((char *) (root->Current + 1));
  123.     }
  124.  
  125. char *LLM_next(root)
  126.     struct LLM_root *root;
  127.     {
  128.     if(root == NULL)
  129.         {
  130.         return(NULL);
  131.         }
  132.     if(root->Init != 1)
  133.         {
  134.         return(NULL);
  135.         }
  136.     if(root->First == NULL)
  137.         {
  138.         return(NULL);
  139.         }
  140.     if(root->Current == NULL)
  141.         {
  142.         return(NULL);
  143.         }
  144.     if(root->Current == root->Last)
  145.         {
  146.         return(NULL);
  147.         }
  148.     root->Current = root->Current->Next;
  149.     return((char *) (root->Current + 1));
  150.     }
  151.  
  152. char *LLM_previous(root)
  153.     struct LLM_root *root;
  154.     {
  155.     if(root == NULL)
  156.         {
  157.         return(NULL);
  158.         }
  159.     if(root->Init != 1)
  160.         {
  161.         return(NULL);
  162.         }
  163.     if(root->First == NULL)
  164.         {
  165.         return(NULL);
  166.         }
  167.     if(root->Current == NULL)
  168.         {
  169.         return(NULL);
  170.         }
  171.     if(root->Current == root->First)
  172.         {
  173.         return(NULL);
  174.         }
  175.     root->Current = root->Current->Prev;
  176.     return((char *) (root->Current + 1));
  177.     }
  178.  
  179. LLM_free(root)
  180.     struct LLM_root *root;
  181.     {
  182.     struct LLM_node *temp, *hold;
  183.  
  184.     if(root == NULL)
  185.         {
  186.         return(NULL);
  187.         }
  188.     if(root->Init != 1)
  189.         {
  190.         return(NULL);
  191.         }
  192.     temp = root->First;
  193.     while(temp != NULL)   /* Loop until we're at the end. */
  194.         {
  195.         hold = temp;
  196.         temp = temp->Next;
  197.         (void)free((char *)hold);       /* Free up our current node. */
  198.         }
  199.     root->First   = NULL;
  200.     root->Last    = NULL;
  201.     root->Current = NULL;
  202.     return(1);
  203.     }
  204.  
  205. LLM_delete(root, node)
  206.     struct LLM_root *root;
  207.     char *node;
  208.     {
  209.     struct LLM_node *temp;
  210.  
  211.     if(root == NULL)
  212.         {
  213.         return(NULL);
  214.         }
  215.     if(root->Init != 1)
  216.         {
  217.         return(NULL);
  218.         }
  219.     if(node == NULL)
  220.         {
  221.         return(NULL);
  222.         }
  223.     temp = (struct LLM_node *) node;
  224.     temp--;
  225.  
  226.     if(temp->Prev != NULL)
  227.         temp->Prev->Next = temp->Next;
  228.     else
  229.         root->First = temp->Next;
  230.  
  231.     if(temp->Next != NULL)
  232.         temp->Next->Prev = temp->Prev;
  233.     else
  234.         root->Last = temp->Prev;
  235.  
  236.     if(temp == root->Current) root->Current = root->First;
  237.  
  238.     (void)free((char *)temp);
  239.     return(1);
  240.     }
  241.  
  242. char *LLM_insert(root, node)
  243.     struct LLM_root *root;
  244.     char *node;
  245.     {
  246.     struct LLM_node *temp, *hold;
  247.  
  248.     if(root == NULL)
  249.         {
  250.         return(NULL);
  251.         }
  252.     if(root->Init != 1)
  253.         {
  254.         return(NULL);
  255.         }
  256.     if(node == NULL)
  257.         {
  258.         return(NULL);
  259.         }
  260.     temp = (struct LLM_node *) node;
  261.     temp--;
  262.     hold = (struct LLM_node *) malloc(root->Size);
  263.     if(hold == NULL)
  264.         {
  265.         return(NULL);
  266.         }
  267.     if(temp == root->First)
  268.         {
  269.         hold->Next = root->First;
  270.         hold->Next->Prev = hold;
  271.         root->First = hold;
  272.         hold->Prev = NULL;
  273.         return( (char *) (hold + 1));
  274.         }
  275.     hold->Next = temp;
  276.     hold->Prev = temp->Prev;
  277.     temp->Prev = hold;
  278.     hold->Prev->Next = hold;
  279.  
  280.     return( (char *) (hold + 1));
  281.     }
  282.  
  283. LLM_position(root, node)
  284.     struct LLM_root *root;
  285.     char *node;
  286.     {
  287.     struct LLM_node *temp;
  288.  
  289.     if(root == NULL)
  290.         {
  291.         return(NULL);
  292.         }
  293.     if(node == NULL)
  294.         {
  295.         return(NULL);
  296.         }
  297.     if(root->Init != 1)
  298.         {
  299.         return(NULL);
  300.         }
  301.     temp = (struct LLM_node *) node;
  302.     temp--;
  303.     root->Current = temp;
  304.     return(1);
  305.     }
  306.  
  307. char *LLM_current(root)
  308.     struct LLM_root *root;
  309.     {
  310.     if(root == NULL)
  311.         {
  312.         return(NULL);
  313.         }
  314.     if(root->Init != 1)
  315.         {
  316.         return(NULL);
  317.         }
  318.     if(root->Current == NULL)
  319.         {
  320.         return(NULL);
  321.         }
  322.     return((char *) (root->Current + 1));
  323.     }
  324.  
  325. LLM_unlink(root, node)
  326.     struct LLM_root *root;
  327.     char *node;
  328.     {
  329.     struct LLM_node *temp;
  330.  
  331.     if(root == NULL)
  332.         {
  333.         return(NULL);
  334.         }
  335.     if(root->Init != 1)
  336.         {
  337.         return(NULL);
  338.         }
  339.     if(node == NULL)
  340.         {
  341.         return(NULL);
  342.         }
  343.     temp = (struct LLM_node *) node;
  344.     temp--;
  345.  
  346.     if(temp->Prev != NULL)
  347.         temp->Prev->Next = temp->Next;
  348.     else
  349.         root->First = temp->Next;
  350.  
  351.     if(temp->Next != NULL)
  352.         temp->Next->Prev = temp->Prev;
  353.     else
  354.         root->Last = temp->Prev;
  355.  
  356.     if(temp == root->Current) root->Current = root->First;
  357.  
  358.     return(1);
  359.     }
  360.  
  361. LLM_link(root, node, newnode)
  362.     struct LLM_root *root;
  363.     char *node;
  364.     char *newnode;
  365.     {
  366.     struct LLM_node *temp, *hold;
  367.  
  368.     if(root == NULL)
  369.         {
  370.         return(NULL);
  371.         }
  372.     if(root->Init != 1)
  373.         {
  374.         return(NULL);
  375.         }
  376.     if(newnode == NULL)
  377.         {
  378.         return(NULL);
  379.         }
  380.     hold = (struct LLM_node *) newnode;
  381.     hold--;
  382.     if(node == NULL && root->First != NULL) /* link at end */
  383.         {
  384.         temp = root->Last;
  385.         temp->Next = hold;
  386.         hold->Next = NULL;
  387.         hold->Prev = temp;
  388.         root->Last = hold;
  389.         return(1);
  390.         }
  391.     if(node == NULL && root->First == NULL) /* first node */
  392.         {
  393.         root->First = hold;
  394.         root->Last = hold;
  395.         root->Current = hold;
  396.         hold->Next = NULL;
  397.         hold->Prev = NULL;
  398.         return(1);
  399.         }
  400.     temp = (struct LLM_node *) node;
  401.     temp--;
  402.     if(temp == root->First)
  403.         {
  404.         hold->Next = root->First;
  405.         hold->Next->Prev = hold;
  406.         root->First = hold;
  407.         hold->Prev = NULL;
  408.         return(1);
  409.         }
  410.     hold->Next = temp;
  411.     hold->Prev = temp->Prev;
  412.     temp->Prev = hold;
  413.     hold->Prev->Next = hold;
  414.  
  415.     return(1);
  416.     }
  417.