Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / data.c
1 /*
2  *  WINGs WMData function library
3  *
4  *  Copyright (c) 1999-2003 Dan Pascu
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <string.h>
22 #include "WUtil.h"
23
24 typedef struct W_Data {
25         unsigned length;        /* How many bytes we have */
26         unsigned capacity;      /* How many bytes it can hold */
27         unsigned growth;        /* How much to grow */
28         void *bytes;            /* Actual data */
29         unsigned retainCount;
30         WMFreeDataProc *destructor;
31         int format;             /* 0, 8, 16 or 32 */
32 } W_Data;
33
34 /* Creating and destroying data objects */
35
36 WMData *WMCreateDataWithCapacity(unsigned capacity)
37 {
38         WMData *aData;
39
40         aData = (WMData *) wmalloc(sizeof(WMData));
41
42         if (capacity > 0)
43                 aData->bytes = wmalloc(capacity);
44         else
45                 aData->bytes = NULL;
46
47         aData->capacity = capacity;
48         aData->growth = capacity / 2 > 0 ? capacity / 2 : 1;
49         aData->length = 0;
50         aData->retainCount = 1;
51         aData->format = 0;
52         aData->destructor = wfree;
53
54         return aData;
55 }
56
57 WMData *WMCreateDataWithLength(unsigned length)
58 {
59         WMData *aData;
60
61         aData = WMCreateDataWithCapacity(length);
62         if (length > 0) {
63                 memset(aData->bytes, 0, length);
64                 aData->length = length;
65         }
66
67         return aData;
68 }
69
70 WMData *WMCreateDataWithBytes(void *bytes, unsigned length)
71 {
72         WMData *aData;
73
74         aData = WMCreateDataWithCapacity(length);
75         aData->length = length;
76         memcpy(aData->bytes, bytes, length);
77
78         return aData;
79 }
80
81 WMData *WMCreateDataWithBytesNoCopy(void *bytes, unsigned length, WMFreeDataProc * destructor)
82 {
83         WMData *aData;
84
85         aData = (WMData *) wmalloc(sizeof(WMData));
86         aData->length = length;
87         aData->capacity = length;
88         aData->growth = length / 2 > 0 ? length / 2 : 1;
89         aData->bytes = bytes;
90         aData->retainCount = 1;
91         aData->format = 0;
92         aData->destructor = destructor;
93
94         return aData;
95 }
96
97 WMData *WMCreateDataWithData(WMData * aData)
98 {
99         WMData *newData;
100
101         if (aData->length > 0) {
102                 newData = WMCreateDataWithBytes(aData->bytes, aData->length);
103         } else {
104                 newData = WMCreateDataWithCapacity(0);
105         }
106         newData->format = aData->format;
107
108         return newData;
109 }
110
111 WMData *WMRetainData(WMData * aData)
112 {
113         aData->retainCount++;
114         return aData;
115 }
116
117 void WMReleaseData(WMData * aData)
118 {
119         aData->retainCount--;
120         if (aData->retainCount > 0)
121                 return;
122         if (aData->bytes != NULL && aData->destructor != NULL) {
123                 aData->destructor(aData->bytes);
124         }
125         wfree(aData);
126 }
127
128 /* Adjusting capacity */
129
130 void WMSetDataCapacity(WMData * aData, unsigned capacity)
131 {
132         if (aData->capacity != capacity) {
133                 aData->bytes = wrealloc(aData->bytes, capacity);
134                 aData->capacity = capacity;
135                 aData->growth = capacity / 2 > 0 ? capacity / 2 : 1;
136         }
137         if (aData->length > capacity) {
138                 aData->length = capacity;
139         }
140 }
141
142 void WMSetDataLength(WMData * aData, unsigned length)
143 {
144         if (length > aData->capacity) {
145                 WMSetDataCapacity(aData, length);
146         }
147         if (length > aData->length) {
148                 memset((unsigned char *)aData->bytes + aData->length, 0, length - aData->length);
149         }
150         aData->length = length;
151 }
152
153 void WMSetDataFormat(WMData * aData, unsigned format)
154 {
155         aData->format = format;
156 }
157
158 void WMIncreaseDataLengthBy(WMData * aData, unsigned extraLength)
159 {
160         WMSetDataLength(aData, aData->length + extraLength);
161 }
162
163 /* Accessing data */
164
165 const void *WMDataBytes(WMData * aData)
166 {
167         return aData->bytes;
168 }
169
170 void WMGetDataBytes(WMData * aData, void *buffer)
171 {
172         wassertr(aData->length > 0);
173
174         memcpy(buffer, aData->bytes, aData->length);
175 }
176
177 unsigned WMGetDataFormat(WMData * aData)
178 {
179         return aData->format;
180 }
181
182 void WMGetDataBytesWithLength(WMData * aData, void *buffer, unsigned length)
183 {
184         wassertr(aData->length > 0);
185         wassertr(length <= aData->length);
186
187         memcpy(buffer, aData->bytes, length);
188 }
189
190 void WMGetDataBytesWithRange(WMData * aData, void *buffer, WMRange aRange)
191 {
192         wassertr(aRange.position < aData->length);
193         wassertr(aRange.count <= aData->length - aRange.position);
194
195         memcpy(buffer, (unsigned char *)aData->bytes + aRange.position, aRange.count);
196 }
197
198 WMData *WMGetSubdataWithRange(WMData * aData, WMRange aRange)
199 {
200         void *buffer;
201         WMData *newData;
202
203         if (aRange.count <= 0)
204                 return WMCreateDataWithCapacity(0);
205
206         buffer = wmalloc(aRange.count);
207         WMGetDataBytesWithRange(aData, buffer, aRange);
208         newData = WMCreateDataWithBytesNoCopy(buffer, aRange.count, wfree);
209         newData->format = aData->format;
210
211         return newData;
212 }
213
214 /* Testing data */
215
216 Bool WMIsDataEqualToData(WMData * aData, WMData * anotherData)
217 {
218         if (aData->length != anotherData->length)
219                 return False;
220         else if (!aData->bytes && !anotherData->bytes)  /* both are empty */
221                 return True;
222         else if (!aData->bytes || !anotherData->bytes)  /* one of them is empty */
223                 return False;
224         return (memcmp(aData->bytes, anotherData->bytes, aData->length) == 0);
225 }
226
227 unsigned WMGetDataLength(WMData * aData)
228 {
229         return aData->length;
230 }
231
232 /* Adding data */
233 void WMAppendDataBytes(WMData * aData, void *bytes, unsigned length)
234 {
235         unsigned oldLength = aData->length;
236         unsigned newLength = oldLength + length;
237
238         if (newLength > aData->capacity) {
239                 unsigned nextCapacity = aData->capacity + aData->growth;
240                 unsigned nextGrowth = aData->capacity ? aData->capacity : 1;
241
242                 while (nextCapacity < newLength) {
243                         unsigned tmp = nextCapacity + nextGrowth;
244
245                         nextGrowth = nextCapacity;
246                         nextCapacity = tmp;
247                 }
248                 WMSetDataCapacity(aData, nextCapacity);
249                 aData->growth = nextGrowth;
250         }
251         memcpy((unsigned char *)aData->bytes + oldLength, bytes, length);
252         aData->length = newLength;
253 }
254
255 void WMAppendData(WMData * aData, WMData * anotherData)
256 {
257         if (anotherData->length > 0)
258                 WMAppendDataBytes(aData, anotherData->bytes, anotherData->length);
259 }
260
261 /* Modifying data */
262
263 void WMReplaceDataBytesInRange(WMData * aData, WMRange aRange, void *bytes)
264 {
265         wassertr(aRange.position < aData->length);
266         wassertr(aRange.count <= aData->length - aRange.position);
267
268         memcpy((unsigned char *)aData->bytes + aRange.position, bytes, aRange.count);
269 }
270
271 void WMResetDataBytesInRange(WMData * aData, WMRange aRange)
272 {
273         wassertr(aRange.position < aData->length);
274         wassertr(aRange.count <= aData->length - aRange.position);
275
276         memset((unsigned char *)aData->bytes + aRange.position, 0, aRange.count);
277 }
278
279 void WMSetData(WMData * aData, WMData * anotherData)
280 {
281         unsigned length = anotherData->length;
282
283         WMSetDataCapacity(aData, length);
284         if (length > 0)
285                 memcpy(aData->bytes, anotherData->bytes, length);
286         aData->length = length;
287 }
288
289 /* Storing data */