This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 1.0 patch 9: 3 portability problems
[perl5.git] / array.c
CommitLineData
8d063cd8
LW
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
16STR *
17afetch(ar,key)
18register ARRAY *ar;
19int key;
20{
21 if (key < 0 || key > ar->ary_max)
22 return Nullstr;
23 return ar->ary_array[key];
24}
25
26bool
27astore(ar,key,val)
28register ARRAY *ar;
29int key;
30STR *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
54bool
55adelete(ar,key)
56register ARRAY *ar;
57int 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
69ARRAY *
70anew()
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
81void
82afree(ar)
83register 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
95bool
96apush(ar,val)
97register ARRAY *ar;
98STR *val;
99{
100 return astore(ar,++(ar->ary_fill),val);
101}
102
103STR *
104apop(ar)
105register 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
116aunshift(ar,num)
117register ARRAY *ar;
118register 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
134STR *
135ashift(ar)
136register 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
149long
150alen(ar)
151register ARRAY *ar;
152{
153 return (long)ar->ary_fill;
154}
155
156void
157ajoin(ar,delim,str)
158register ARRAY *ar;
159char *delim;
160register 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}