home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 2.ddi / OWLSRC.EXE / CSCAPE / SOURCE / JARRAY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  3.8 KB  |  166 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.1
  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. */
  31.  
  32. #include "oakhead.h"
  33. #include "jadecl.h"
  34. /* -------------------------------------------------------------------------- */
  35.  
  36. jarray ja_Open(size, eltsize)
  37.     unsigned int size;
  38.     SIZE_T eltsize;
  39. /*
  40.     Create a jarray with elements of size eltsize.
  41.     size is the starting size of the array.
  42.     if eltsize == 0, then it's an array of pointers.
  43. */
  44. {
  45.     jarray j;
  46.     
  47.     if ((j = (jarray) omalloc(OA_XARR, sizeof(struct jarray_struct))) == NULL) {
  48.         return(NULL);
  49.     }
  50.  
  51.     if ((j->array = ocalloc(OA_XARRA, size, (eltsize == 0) ? sizeof(VOID *) : eltsize)) == NULL) {
  52.         return(NULL);
  53.     }
  54.  
  55.     j->size = size;
  56.     j->eltsize = eltsize;
  57.     return(j);
  58. }
  59. /* -------------------------------------------------------------------------- */
  60.  
  61. void ja_Close(ja)
  62.     jarray ja;
  63. {
  64.     ofree(OA_XARRA, ja->array);
  65.     ofree(OA_XARR, (VOID *) ja);
  66. }
  67.  
  68. int ja_Put(ja, eltno, elt)
  69.     jarray ja;
  70.     unsigned int eltno;
  71.     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(ja, newsize)
  101.     jarray ja;
  102.     unsigned int newsize;
  103. /*
  104.     Increase the size of the jarray's array.
  105. */
  106. {
  107.     unsigned int size;
  108.     SIZE_T    eltsize;
  109.  
  110.     eltsize = (ja->eltsize == 0) ? sizeof(VOID *) : ja->eltsize;
  111.  
  112.     if (newsize < JA_GROW) {
  113.         size = newsize * 2;
  114.     }
  115.     else {
  116.         size = (newsize + JA_GROW < UINT_MAX) ? newsize + JA_GROW : UINT_MAX;
  117.     }
  118.  
  119.     if ((ja->array = orealloc(OA_XARRA, ja->array, size * eltsize)) == NULL) {
  120.         return(FALSE);
  121.     }
  122.  
  123.     /* clear out the extra area */
  124.     memset((VOID *) ((char *) ja->array + (ja->size * eltsize)), 0, 
  125.             (size - ja->size) * eltsize);
  126.  
  127.     ja->size = size;
  128.  
  129.     return(TRUE);
  130. }
  131.  
  132. /* -------------------------------------------------------------------------- */
  133.  
  134. VOID *ja_Get(ja, eltno, out)
  135.     jarray ja;
  136.     unsigned int eltno;
  137.     VOID *out;
  138. /*
  139.     Gets an element from a jarray.
  140.  
  141.     Copies it into out returns a pointer to the array slot.
  142.  
  143.     If eltno > size, clears out and returns NULL.
  144. */
  145. {
  146.     SIZE_T    eltsize;
  147.     VOID   *ret;    
  148.  
  149.     eltsize = (ja->eltsize == 0) ? sizeof(VOID *) : ja->eltsize;
  150.  
  151.     if (eltno >= ja->size) {
  152.         /* clear out 'out' */
  153.         memset(out, 0, eltsize);
  154.         return(NULL);
  155.     }
  156.     else {
  157.         /* copy the array elt into 'out' */
  158.         memmove(out, ret = (VOID *) ((char *) ja->array + (eltno * eltsize)), eltsize);
  159.     }
  160.  
  161.     return(ret);
  162. }
  163.  
  164. /* ------------------------------------------------------------------------- */
  165.  
  166.