forked from rhdunn/rsynth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
darray.c
138 lines (124 loc) · 2.8 KB
/
darray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Copyright (c) 1994,2001-2003 Nick Ing-Simmons. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
#include "config.h"
/* $Id: //depot/rsynth/darray.c#12 $
*/
char *darray_id = "$Id: //depot/rsynth/darray.c#12 $";
#include "useconfig.h"
#include "darray.h"
void
darray_free(darray_t * a)
{
if (a->data) {
free(a->data);
a->data = NULL;
}
a->items = a->alloc = 0;
}
void *
Darray_find(darray_t * a, unsigned int n)
{
if (n >= a->alloc || n >= a->items) {
unsigned osize = a->items * a->esize;
unsigned nsize;
if (!a->esize)
abort();
if (n >= a->alloc) {
unsigned add = (a->get) ? a->get : 1;
char *ndata = (char *) malloc(nsize = (n + add) * a->esize);
if (ndata) {
if (osize)
memcpy(ndata, a->data, osize);
if (a->data)
free(a->data);
a->data = ndata;
a->alloc = n + add;
}
else
return NULL;
}
else
nsize = (n + 1) * a->esize;
if (n >= a->items) {
memset(a->data + osize, 0, nsize - osize);
a->items = n + 1;
}
}
return (void *) (a->data + n * a->esize);
}
int
darray_delete(darray_t * a, unsigned int n)
{
char *p = (char *) darray_find(a, n);
if (p) {
if (a->items) {
a->items--;
while (n++ < a->items) {
memcpy(p, p + a->esize, a->esize);
p += a->esize;
}
memset(p, 0, a->esize);
return 1;
}
else
abort();
}
else
return 0;
}
int
darray_append(darray_ptr p, int ch)
{
char *s = (char *) darray_find(p, p->items);
*s = ch;
return ch;
}
void
darray_cat(void *arg, char *s)
{
darray_ptr p = (darray_ptr) arg;
char ch;
while ((ch = *s++))
darray_append(p, ch);
}
int
darray_fget(FILE * f, darray_ptr p)
{
int ch;
while ((ch = fgetc(f)) != EOF) {
darray_append(p, ch);
if (ch == '\n')
break;
}
darray_append(p, '\0');
return p->items - 1;
}
#if !defined(__GNUC__)
float
darray_float(darray_ptr f0, float f)
{
float *fp = (float *) darray_find(f0, f0->items);
*fp = f;
return f;
}
short
darray_short(darray_ptr f0, short f)
{
short *fp = (short *) darray_find(f0, f0->items);
*fp = f;
return f;
}
#endif