home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Tertiary Source / ArrayListLib.c next >
Encoding:
Text File  |  1995-11-01  |  5.4 KB  |  220 lines  |  [TEXT/CWIE]

  1. ///////
  2. ////// Array List Library • Original by Ari Halberstadt from "Winter Shell"
  3. ///// Modified to by independent of the other parts of Winter Shell by Eddy J. Gurney
  4. //// These functions provide a useful set of routines for adding, removing and 
  5. /// retrieving an arbitrary list of data
  6. //
  7.  
  8. #include "ArrayListLib.h"
  9.  
  10. // Remove error-handling
  11. #define ErrorHandler(argument)
  12.  
  13. /* true if a valid list */
  14. Boolean ArrayListValid(ArrayListHandle list)
  15. {
  16.     if ((list != nil) && (((long)list & 1) == 0) &&
  17.             (*list != nil) && (((long)*list & 1) == 0) &&
  18.             (GetHandleSize((Handle)list) >= sizeof(ArrayListType)) &&
  19.             (GetHandleSize((Handle)(**list).array) == (**list).nelem * (**list).elemsz))
  20.         return true;
  21.     else
  22.         return false;
  23. }
  24.  
  25. /* true if a valid index into the array */
  26. Boolean ArrayListValidIndex(ArrayListHandle list, short index)
  27. {
  28.     if (ArrayListValid(list))
  29.         return index >= 0 && index < (**list).nelem;
  30.     else
  31.         return false;
  32. }
  33.  
  34. /* create a new list */
  35. ArrayListHandle ArrayListBegin(short elemsz)
  36. {
  37.     ArrayListHandle    list = nil;
  38.     Handle                array = nil;
  39.     
  40.     list = (ArrayListHandle)NewHandleClear(sizeof(ArrayListType));
  41.     if (list == nil)
  42.         ErrorHandler("\pNo memory!");
  43.     else {    
  44.         array = NewHandle(0);
  45.         if (array == nil)
  46.             ErrorHandler("\pNo memory!");
  47.         else {
  48.             (**list).array = array;
  49.             (**list).elemsz = elemsz;
  50.         }
  51.     }
  52.     
  53.     if (!ArrayListValid(list))
  54.         ErrorHandler("\pArray List Error!");
  55.  
  56.     return list;
  57. }
  58.  
  59. /* dispose of the list */
  60. ArrayListHandle ArrayListEnd(ArrayListHandle list)
  61. {
  62.     if (list == nil || !ArrayListValid(list))
  63.         ErrorHandler("\pArray List Error!");
  64.  
  65.     if ((**list).array != nil)
  66.         DisposeHandle((**list).array);
  67.     DisposeHandle((Handle)list);
  68.     list = nil;
  69.     
  70.     return(NULL);
  71. }
  72.  
  73. /* return number of items in array */
  74. short ArrayListCount(ArrayListHandle list)
  75. {
  76.     if (!ArrayListValid(list))
  77.         ErrorHandler("\pArray List Error!");
  78.  
  79.     return (**list).nelem;
  80. }
  81.  
  82. /* return size of items in array */
  83. short ArrayListItemSize(ArrayListHandle list)
  84. {
  85.     if (!ArrayListValid(list))
  86.         ErrorHandler("\pArray List Error!");
  87.  
  88.     return (**list).elemsz;
  89. }
  90.  
  91. /* insert space for an item before the indexed item */
  92. void ArrayListInsert(ArrayListHandle list, short index)
  93. {
  94.     SignedByte    state;
  95.     
  96.     if (!ArrayListValid(list))
  97.         ErrorHandler("\pArray List Error!");
  98.  
  99.     if (index >= 0 && index <= (**list).nelem) {
  100.         state = HGetState((Handle)list);
  101.         HNoPurge((Handle)list);
  102.         if (MemError() != noErr)
  103.             ErrorHandler("\pNo memory!");
  104.  
  105.         SetHandleSize((**list).array, ((**list).nelem + 1) * (**list).elemsz);
  106.         if (MemError() != noErr)
  107.             ErrorHandler("\pNo memory!");
  108.             
  109.         HSetState((Handle)list, state);
  110.         if (MemError() != noErr)
  111.             ErrorHandler("\pNo memory!");
  112.     }
  113.     if (index < (**list).nelem) {
  114.         BlockMove(*(**list).array + index * (**list).elemsz,
  115.                       *(**list).array + (index + 1) * (**list).elemsz,
  116.                      ((**list).nelem - index) * (**list).elemsz);
  117.     }
  118.     (**list).nelem++;
  119.  
  120.     if (!ArrayListValidIndex(list, index))
  121.         ErrorHandler("\pArray List Error!");
  122. }
  123.  
  124. /* remove the indexed item */
  125. void ArrayListDelete(ArrayListHandle list, short index)
  126. {
  127.     SignedByte    state;
  128.  
  129.     if (!ArrayListValid(list) || !ArrayListValidIndex(list, index))
  130.         ErrorHandler("\pArray List Error!");
  131.  
  132.     if (index < (**list).nelem - 1) {
  133.         BlockMove(*(**list).array + (index + 1) * (**list).elemsz,
  134.                      *(**list).array + index * (**list).elemsz,
  135.                      ((**list).nelem - index - 1) * (**list).elemsz);
  136.     }
  137.  
  138.     state = HGetState((Handle)list);
  139.     HNoPurge((Handle)list);
  140.     if (MemError() != noErr)
  141.         ErrorHandler("\pNo memory!");
  142.  
  143.     SetHandleSize((**list).array, --(**list).nelem * (**list).elemsz);
  144.     if (MemError() != noErr)
  145.         ErrorHandler("\pNo memory!");
  146.         
  147.     HSetState((Handle)list, state);
  148.     if (MemError() != noErr)
  149.         ErrorHandler("\pNo memory!");
  150.     
  151.     if (!ArrayListValid(list))
  152.         ErrorHandler("\pArray List Error!");
  153. }
  154.  
  155. /* set the value of the indexed item */
  156. void ArrayListSet(ArrayListHandle list, short index, void *data)
  157. {
  158.     if (!ArrayListValid(list) || !ArrayListValidIndex(list, index))
  159.         ErrorHandler("\pArray List Error!");
  160.  
  161.     BlockMove(data, *(**list).array + index * (**list).elemsz, (**list).elemsz);
  162.  
  163.     if (!ArrayListValid(list))
  164.         ErrorHandler("\pArray List Error!");
  165. }
  166.  
  167. /* get the value of the indexed item */
  168. void ArrayListGet(ArrayListHandle list, short index, void *data)
  169. {
  170.     if (!ArrayListValid(list) || !ArrayListValidIndex(list, index))
  171.         ErrorHandler("\pArray List Error!");
  172.  
  173.     BlockMove(*(**list).array + index * (**list).elemsz, data, (**list).elemsz);
  174.  
  175.     if (!ArrayListValid(list))
  176.         ErrorHandler("\pArray List Error!");
  177. }
  178.  
  179. /* return the handle to the array */
  180. void *ArrayListGetHandle(ArrayListHandle list)
  181. {
  182.     if (!ArrayListValid(list))
  183.         ErrorHandler("\pArray List Error!");
  184.  
  185.     return (**list).array;
  186. }
  187.  
  188. /* detach the array from the list (call ArrayListGetHandle first) */
  189. void ArrayListDetachHandle(ArrayListHandle list)
  190. {
  191.     if (!ArrayListValid(list))
  192.         ErrorHandler("\pArray List Error!");
  193.  
  194.     (**list).array = NewHandle(0);
  195.     if ((**list).array == nil)
  196.         ErrorHandler("\pNo memory!");
  197.     (**list).nelem = 0;
  198.  
  199.     if (ArrayListCount(list) != 0)
  200.         ErrorHandler("\pArray List Error!");
  201. }
  202.  
  203. /* attach the handle to the array */
  204. void ArrayListAttachHandle(ArrayListHandle list, void *array)
  205. {
  206.     if (!ArrayListValid(list))
  207.         ErrorHandler("\pArray List Error!");
  208.  
  209.     if (array != nil && (long)array & 1 == 0) {
  210.         if ((**list).array != nil)
  211.             DisposeHandle((**list).array);
  212.         (**list).array = array;
  213.         (**list).nelem = GetHandleSize(array) / (**list).elemsz;
  214.     } else
  215.         ErrorHandler("\pArray List Error!");
  216.  
  217.     if (!ArrayListValid(list))
  218.         ErrorHandler("\pArray List Error!");
  219. }
  220.