1: /* $Header: array.c,v 1.0 87/12/18 13:04:42 root Exp $
   2:  *
   3:  * $Log:	array.c,v $
   4:  * Revision 1.0  87/12/18  13:04:42  root
   5:  * Initial revision
   6:  *
   7:  */
   8: 
   9: #include <stdio.h>
  10: #include "EXTERN.h"
  11: #include "handy.h"
  12: #include "util.h"
  13: #include "search.h"
  14: #include "perl.h"
  15: 
  16: STR *
  17: afetch(ar,key)
  18: register ARRAY *ar;
  19: int key;
  20: {
  21:     if (key < 0 || key > ar->ary_max)
  22:     return Nullstr;
  23:     return ar->ary_array[key];
  24: }
  25: 
  26: bool
  27: astore(ar,key,val)
  28: register ARRAY *ar;
  29: int key;
  30: STR *val;
  31: {
  32:     bool retval;
  33: 
  34:     if (key < 0)
  35:     return FALSE;
  36:     if (key > ar->ary_max) {
  37:     int newmax = key + ar->ary_max / 5;
  38: 
  39:     ar->ary_array = (STR**)saferealloc((char*)ar->ary_array,
  40:         (newmax+1) * sizeof(STR*));
  41:     bzero((char*)&ar->ary_array[ar->ary_max+1],
  42:         (newmax - ar->ary_max) * sizeof(STR*));
  43:     ar->ary_max = newmax;
  44:     }
  45:     if (key > ar->ary_fill)
  46:     ar->ary_fill = key;
  47:     retval = (ar->ary_array[key] != Nullstr);
  48:     if (retval)
  49:     str_free(ar->ary_array[key]);
  50:     ar->ary_array[key] = val;
  51:     return retval;
  52: }
  53: 
  54: bool
  55: adelete(ar,key)
  56: register ARRAY *ar;
  57: int key;
  58: {
  59:     if (key < 0 || key > ar->ary_max)
  60:     return FALSE;
  61:     if (ar->ary_array[key]) {
  62:     str_free(ar->ary_array[key]);
  63:     ar->ary_array[key] = Nullstr;
  64:     return TRUE;
  65:     }
  66:     return FALSE;
  67: }
  68: 
  69: ARRAY *
  70: anew()
  71: {
  72:     register ARRAY *ar = (ARRAY*)safemalloc(sizeof(ARRAY));
  73: 
  74:     ar->ary_array = (STR**) safemalloc(5 * sizeof(STR*));
  75:     ar->ary_fill = -1;
  76:     ar->ary_max = 4;
  77:     bzero((char*)ar->ary_array, 5 * sizeof(STR*));
  78:     return ar;
  79: }
  80: 
  81: void
  82: afree(ar)
  83: register ARRAY *ar;
  84: {
  85:     register int key;
  86: 
  87:     if (!ar)
  88:     return;
  89:     for (key = 0; key <= ar->ary_fill; key++)
  90:     str_free(ar->ary_array[key]);
  91:     safefree((char*)ar->ary_array);
  92:     safefree((char*)ar);
  93: }
  94: 
  95: bool
  96: apush(ar,val)
  97: register ARRAY *ar;
  98: STR *val;
  99: {
 100:     return astore(ar,++(ar->ary_fill),val);
 101: }
 102: 
 103: STR *
 104: apop(ar)
 105: register ARRAY *ar;
 106: {
 107:     STR *retval;
 108: 
 109:     if (ar->ary_fill < 0)
 110:     return Nullstr;
 111:     retval = ar->ary_array[ar->ary_fill];
 112:     ar->ary_array[ar->ary_fill--] = Nullstr;
 113:     return retval;
 114: }
 115: 
 116: aunshift(ar,num)
 117: register ARRAY *ar;
 118: register int num;
 119: {
 120:     register int i;
 121:     register STR **sstr,**dstr;
 122: 
 123:     if (num <= 0)
 124:     return;
 125:     astore(ar,ar->ary_fill+num,(STR*)0);    /* maybe extend array */
 126:     sstr = ar->ary_array + ar->ary_fill;
 127:     dstr = sstr + num;
 128:     for (i = ar->ary_fill; i >= 0; i--) {
 129:     *dstr-- = *sstr--;
 130:     }
 131:     bzero((char*)(ar->ary_array), num * sizeof(STR*));
 132: }
 133: 
 134: STR *
 135: ashift(ar)
 136: register ARRAY *ar;
 137: {
 138:     STR *retval;
 139: 
 140:     if (ar->ary_fill < 0)
 141:     return Nullstr;
 142:     retval = ar->ary_array[0];
 143:     bcopy((char*)(ar->ary_array+1),(char*)ar->ary_array,
 144:       ar->ary_fill * sizeof(STR*));
 145:     ar->ary_array[ar->ary_fill--] = Nullstr;
 146:     return retval;
 147: }
 148: 
 149: long
 150: alen(ar)
 151: register ARRAY *ar;
 152: {
 153:     return (long)ar->ary_fill;
 154: }
 155: 
 156: void
 157: ajoin(ar,delim,str)
 158: register ARRAY *ar;
 159: char *delim;
 160: register STR *str;
 161: {
 162:     register int i;
 163:     register int len;
 164:     register int dlen;
 165: 
 166:     if (ar->ary_fill < 0) {
 167:     str_set(str,"");
 168:     STABSET(str);
 169:     return;
 170:     }
 171:     dlen = strlen(delim);
 172:     len = ar->ary_fill * dlen;      /* account for delimiters */
 173:     for (i = ar->ary_fill; i >= 0; i--)
 174:     len += str_len(ar->ary_array[i]);
 175:     str_grow(str,len);          /* preallocate for efficiency */
 176:     str_sset(str,ar->ary_array[0]);
 177:     for (i = 1; i <= ar->ary_fill; i++) {
 178:     str_ncat(str,delim,dlen);
 179:     str_scat(str,ar->ary_array[i]);
 180:     }
 181:     STABSET(str);
 182: }

Defined functions

adelete defined in line 54; used 1 times
ajoin defined in line 156; used 1 times
astore defined in line 26; used 9 times
aunshift defined in line 116; used 2 times
Last modified: 1988-02-03
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3537
Valid CSS Valid XHTML 1.0 Strict