home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / JARRAY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-25  |  3.8 KB  |  161 lines

  1. /*
  2.     jarray.c  4/30/86
  3.  
  4.     % jarray routines
  5.  
  6.     'smart'  array functions  (replaces x,l, and iarrays)
  7.  
  8.     OWL 1.2
  9.     Copyright (c) 1986-1989 by Oakland Group, Inc.
  10.     ALL RIGHTS RESERVED.
  11.  
  12.     Revision History:
  13.     -----------------
  14.      4/30/86 jmd/sng    created.
  15.      5/05/86 sng    added errno handling, renamed xa_put1 to _xa_put
  16.      5/22/86 sng    now clears out extra allocated space to NULLs.
  17.      6/14/86 sng    fixed boundary case bug in _xa_put, added non-define
  18.                     xa_get (untested!).
  19.      7/08/86 sng    fixed noninitialization of xa_make.
  20.      6/01/88 jmd      changed growth strategy
  21.      6/01/88 jmd      changed some names
  22.      6/24/88 jmd      moved to oaklib
  23.      8/17/88 jmd      removed  eNOMEM
  24.      11/8/88 ted    fixed bug in _xa_put: if ('elt' < XA_GROW) {
  25.                     also changed : size = (eltno < UINT_MAX - (XA_GROW + 1)) ?
  26.                     to:               size = (eltno + XA_GROW < UINT_MAX) ?
  27.  
  28.      4/14/89 jmd    combined into one new super thingy
  29.  
  30.      3/28/90 jmd    ansi-fied
  31.      4/13/90 jmd    added olimits.h
  32.      9/24/90 jmd    added test for bad array in ja_Close
  33. */
  34.  
  35. #include "oakhead.h"
  36. #include "olimits.h"
  37.  
  38. /* -------------------------------------------------------------------------- */
  39.  
  40. jarray ja_Open(unsigned int size, SIZE_T eltsize)
  41. /*
  42.     Create a jarray with elements of size eltsize.
  43.     size is the starting size of the array.
  44.     if eltsize == 0, then it's an array of pointers.
  45. */
  46. {
  47.     jarray j;
  48.     
  49.     if ((j = (jarray) omalloc(OA_XARR, sizeof(struct jarray_struct))) == NULL) {
  50.         return(NULL);
  51.     }
  52.  
  53.     if ((j->array = ocalloc(OA_XARRA, size, (eltsize == 0) ? sizeof(VOID *) : eltsize)) == NULL) {
  54.         return(NULL);
  55.     }
  56.  
  57.     j->size = size;
  58.     j->eltsize = eltsize;
  59.     return(j);
  60. }
  61. /* -------------------------------------------------------------------------- */
  62.  
  63. void ja_Close(jarray ja)
  64. {
  65.     if (ja->array != NULL) {
  66.         ofree(OA_XARRA, ja->array);
  67.     }
  68.     ofree(OA_XARR, (VOID *) ja);
  69. }
  70.  
  71. int ja_Put(jarray ja, unsigned int eltno, VOID *elt)
  72. /*
  73.     Puts an element into a jarray.
  74.  
  75.     If necessary, it increases the size of the jarray to hold the new element.
  76.     The array doubles in size until JA_GROW then adds JA_GROW at a time.
  77. */
  78. {
  79.     SIZE_T    eltsize;
  80.  
  81.     eltsize = (ja->eltsize == 0) ? sizeof(VOID *) : ja->eltsize;
  82.  
  83.     if (eltno >= ja->size) {
  84.         if (!ja_Resize(ja, eltno)) {
  85.             return(FALSE);
  86.         }
  87.     }
  88.  
  89.     /* put the new elt into the array (test if elts are pointers) */
  90.     if (ja->eltsize == 0) {
  91.         ((VOID **)ja->array)[(eltno)] = elt;
  92.     }
  93.     else {
  94.         memmove((VOID *) ((char *) ja->array + (eltno * eltsize)), elt, eltsize);
  95.     }
  96.  
  97.     return(TRUE);
  98. }
  99.  
  100. boolean ja_Resize(jarray ja, unsigned int newsize)
  101. /*
  102.     Increase the size of the jarray's array.
  103. */
  104. {
  105.     unsigned int size;
  106.     SIZE_T    eltsize;
  107.  
  108.     eltsize = (ja->eltsize == 0) ? sizeof(VOID *) : ja->eltsize;
  109.  
  110.     if (newsize < JA_GROW) {
  111.         size = newsize * 2;
  112.     }
  113.     else {
  114.         size = (newsize + JA_GROW < UINT_MAX) ? newsize + JA_GROW : UINT_MAX;
  115.     }
  116.  
  117.     if ((ja->array = orealloc(OA_XARRA, ja->array, size * eltsize)) == NULL) {
  118.         return(FALSE);
  119.     }
  120.  
  121.     /* clear out the extra area */
  122.     memset((VOID *) ((char *) ja->array + (ja->size * eltsize)), 0, 
  123.             (size - ja->size) * eltsize);
  124.  
  125.     ja->size = size;
  126.  
  127.     return(TRUE);
  128. }
  129.  
  130. /* -------------------------------------------------------------------------- */
  131.  
  132. VOID *ja_Get(jarray ja, unsigned int eltno, VOID *out)
  133. /*
  134.     Gets an element from a jarray.
  135.  
  136.     Copies it into out returns a pointer to the array slot.
  137.  
  138.     If eltno > size, clears out and returns NULL.
  139. */
  140. {
  141.     SIZE_T    eltsize;
  142.     VOID   *ret;    
  143.  
  144.     eltsize = (ja->eltsize == 0) ? sizeof(VOID *) : ja->eltsize;
  145.  
  146.     if (eltno >= ja->size) {
  147.         /* clear out 'out' */
  148.         memset(out, 0, eltsize);
  149.         return(NULL);
  150.     }
  151.     else {
  152.         /* copy the array elt into 'out' */
  153.         memmove(out, ret = (VOID *) ((char *) ja->array + (eltno * eltsize)), eltsize);
  154.     }
  155.  
  156.     return(ret);
  157. }
  158.  
  159. /* ------------------------------------------------------------------------- */
  160.  
  161.