home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Select Multiple Files 0.98 / ArrayListLib.c next >
Encoding:
Text File  |  1994-12-01  |  5.4 KB  |  216 lines  |  [TEXT/KAHL]

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