Advertisement
Guest User

list.c

a guest
Oct 4th, 2010
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. /*
  2.  *******************************************************************************
  3.  * list.c
  4.  * Creation date: 07.03.2007
  5.  * Author:        Firoball
  6.  *
  7.  *******************************************************************************
  8.  * $Date: 2010-08-05 00:23:10 +0200 (Do, 05 Aug 2010) $
  9.  * $Revision: 4 $
  10.  * $Author: Firo $
  11.  *
  12.  *******************************************************************************
  13.  * Description
  14.  *
  15.  * Script for list management
  16.  *
  17.  * Comments
  18.  *
  19.  * for short descriptions see comments in list.h
  20.  *
  21.  *******************************************************************************
  22.  */
  23.  
  24. /* ----- INCLUDES ----- */
  25.  
  26.  
  27. #include <acknex.h>
  28.  
  29. //#include "list.h"
  30.  
  31.  
  32. /* ----- GLOBALS ----- */
  33.  
  34.  
  35. /* ----- EXTERNAL FUNCTIONS ----- */
  36.  
  37.  
  38. void LIST_append(LIST* psHost, void* psNewItem)
  39. {
  40.     /* array limit reached - extend it */
  41.     if ( ((psHost->iCount) + 1) == (LIST_ARRAY_STEPS * (psHost->iRange)) )
  42.     {
  43.         LIST__extend(psHost);  
  44.     }
  45.  
  46.     /* add new item to list */
  47.     (psHost->ppList)[psHost->iCount] = psNewItem;
  48.     psHost->iCount ++;
  49. }
  50.  
  51. void* LIST_getItem(LIST* psHost, int iIndex)
  52. {
  53.     /* return NULL for invalid indices */
  54.     if ((iIndex >= psHost->iCount) || (iIndex < 0))
  55.         return (NULL);
  56.     else
  57.         return ((psHost->ppList)[iIndex]);
  58. }
  59.  
  60. int LIST_items(LIST* psHost)
  61. {
  62.     return (psHost->iCount);
  63. }
  64.  
  65. LIST* LIST_create()
  66. {
  67.     LIST* psTmpList;
  68.     psTmpList = (LIST*)malloc(sizeof(LIST));
  69.     LIST__init(psTmpList);
  70.     return (psTmpList);
  71. }
  72.  
  73. void LIST_remove(LIST* psHost)
  74. {
  75.     free(psHost);
  76.     psHost = NULL; 
  77. }
  78.  
  79. void LIST_removeAll(LIST* psHost)
  80. {
  81.     int i;
  82.  
  83.     /* cycle through pointer list */
  84.     for(i = 0; i < LIST_items(psHost); i++)
  85.         free(LIST_getItem(psHost, i));
  86.     free(psHost);
  87.     psHost = NULL; 
  88. }
  89.  
  90. void LIST_removeItem(LIST* psHost, int iIndex)
  91. {
  92.     /* ignore invalid indices */
  93.     if ((iIndex < psHost->iCount) && (iIndex >= 0))
  94.         (psHost->ppList)[iIndex] = NULL;
  95. }
  96.  
  97.  
  98. /* ----- INTERNAL FUNCTIONS ----- */
  99.  
  100.  
  101. void LIST__init(LIST *psHost)
  102. {
  103.     int i;
  104.     psHost->iCount = 0;
  105.     psHost->iRange = 1;
  106.     psHost->ppList = (void**)malloc(sizeof(void*) * (psHost->iRange) * LIST_ARRAY_STEPS);
  107.  
  108.     for (i = 0; i < LIST_ARRAY_STEPS * (psHost->iRange); i++)
  109.         (psHost->ppList)[i] = NULL;
  110. }
  111.  
  112.  
  113. void LIST__extend(LIST* psHost)
  114. {
  115.     int i;
  116.     void** ppNewItemList;
  117.    
  118.     #ifdef SYSMSG_ACTIVE
  119.     SYSMSG_print(SYSMSG_SHOW_DEBUG, "LIST__extend: List extended");
  120.     #endif
  121.    
  122.     /* increase range for list pointer array */
  123.     psHost->iRange++;
  124.     /* allocate new pointer list */
  125.     ppNewItemList = (void**)malloc(sizeof(void*) * (psHost->iRange) * LIST_ARRAY_STEPS);
  126.     /* copy old pointer list */
  127.     for (i = 0; i < psHost->iCount; i++)
  128.     {
  129.         ppNewItemList[i] = (psHost->ppList)[i];
  130.     }
  131.     /* remove old pointer list */
  132.     free(psHost->ppList);
  133.     /* point to new list */
  134.     psHost->ppList = ppNewItemList;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement