Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / data.c
Commit [+]AuthorDateLineData
9c59becb dan1999-12-14 01:07:24 +00001/*
2 * WINGs WMData function library
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1999-2003 Dan Pascu
6830b057 dan2004-10-12 21:28:27 +00005 *
9c59becb dan1999-12-14 01:07:24 +00006 * 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
9c59becb dan1999-12-14 01:07:24 +000021#include <string.h>
22#include "WUtil.h"
23
9c59becb dan1999-12-14 01:07:24 +000024typedef struct W_Data {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020025 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 */
9c59becb dan1999-12-14 01:07:24 +000032} W_Data;
33
9c59becb dan1999-12-14 01:07:24 +000034/* Creating and destroying data objects */
35
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020036WMData *WMCreateDataWithCapacity(unsigned capacity)
9c59becb dan1999-12-14 01:07:24 +000037{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020038 WMData *aData;
9c59becb dan1999-12-14 01:07:24 +000039
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020040 aData = (WMData *) wmalloc(sizeof(WMData));
9c59becb dan1999-12-14 01:07:24 +000041
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020042 if (capacity > 0)
43 aData->bytes = wmalloc(capacity);
44 else
45 aData->bytes = NULL;
b8ca9e2b dan2000-04-09 02:32:17 +000046
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020047 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;
9c59becb dan1999-12-14 01:07:24 +000053
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020054 return aData;
9c59becb dan1999-12-14 01:07:24 +000055}
56
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020057WMData *WMCreateDataWithLength(unsigned length)
9c59becb dan1999-12-14 01:07:24 +000058{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020059 WMData *aData;
9c59becb dan1999-12-14 01:07:24 +000060
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020061 aData = WMCreateDataWithCapacity(length);
62 if (length > 0) {
63 memset(aData->bytes, 0, length);
64 aData->length = length;
65 }
9c59becb dan1999-12-14 01:07:24 +000066
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020067 return aData;
9c59becb dan1999-12-14 01:07:24 +000068}
69
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020070WMData *WMCreateDataWithBytes(void *bytes, unsigned length)
9c59becb dan1999-12-14 01:07:24 +000071{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020072 WMData *aData;
9c59becb dan1999-12-14 01:07:24 +000073
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020074 aData = WMCreateDataWithCapacity(length);
75 aData->length = length;
76 memcpy(aData->bytes, bytes, length);
9c59becb dan1999-12-14 01:07:24 +000077
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020078 return aData;
9c59becb dan1999-12-14 01:07:24 +000079}
80
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020081WMData *WMCreateDataWithBytesNoCopy(void *bytes, unsigned length, WMFreeDataProc * destructor)
60a247f2 kojima2000-04-03 03:10:20 +000082{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020083 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;
9c59becb dan1999-12-14 01:07:24 +000095}
96
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020097WMData *WMCreateDataWithData(WMData * aData)
9c59becb dan1999-12-14 01:07:24 +000098{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020099 WMData *newData;
b8ca9e2b dan2000-04-09 02:32:17 +0000100
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200101 if (aData->length > 0) {
102 newData = WMCreateDataWithBytes(aData->bytes, aData->length);
103 } else {
104 newData = WMCreateDataWithCapacity(0);
105 }
106 newData->format = aData->format;
60a247f2 kojima2000-04-03 03:10:20 +0000107
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200108 return newData;
9c59becb dan1999-12-14 01:07:24 +0000109}
110
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200111WMData *WMRetainData(WMData * aData)
9c59becb dan1999-12-14 01:07:24 +0000112{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200113 aData->retainCount++;
114 return aData;
9c59becb dan1999-12-14 01:07:24 +0000115}
116
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200117void WMReleaseData(WMData * aData)
9c59becb dan1999-12-14 01:07:24 +0000118{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200119 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);
9c59becb dan1999-12-14 01:07:24 +0000126}
127
9c59becb dan1999-12-14 01:07:24 +0000128/* Adjusting capacity */
129
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200130void WMSetDataCapacity(WMData * aData, unsigned capacity)
9c59becb dan1999-12-14 01:07:24 +0000131{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200132 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 }
9c59becb dan1999-12-14 01:07:24 +0000140}
141
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200142void WMSetDataLength(WMData * aData, unsigned length)
9c59becb dan1999-12-14 01:07:24 +0000143{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200144 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;
9c59becb dan1999-12-14 01:07:24 +0000151}
152
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200153void WMSetDataFormat(WMData * aData, unsigned format)
60a247f2 kojima2000-04-03 03:10:20 +0000154{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200155 aData->format = format;
60a247f2 kojima2000-04-03 03:10:20 +0000156}
157
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200158void WMIncreaseDataLengthBy(WMData * aData, unsigned extraLength)
9c59becb dan1999-12-14 01:07:24 +0000159{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200160 WMSetDataLength(aData, aData->length + extraLength);
9c59becb dan1999-12-14 01:07:24 +0000161}
162
9c59becb dan1999-12-14 01:07:24 +0000163/* Accessing data */
164
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200165const void *WMDataBytes(WMData * aData)
9c59becb dan1999-12-14 01:07:24 +0000166{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200167 return aData->bytes;
9c59becb dan1999-12-14 01:07:24 +0000168}
169
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200170void WMGetDataBytes(WMData * aData, void *buffer)
9c59becb dan1999-12-14 01:07:24 +0000171{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200172 wassertr(aData->length > 0);
9c59becb dan1999-12-14 01:07:24 +0000173
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200174 memcpy(buffer, aData->bytes, aData->length);
9c59becb dan1999-12-14 01:07:24 +0000175}
176
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200177unsigned WMGetDataFormat(WMData * aData)
60a247f2 kojima2000-04-03 03:10:20 +0000178{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200179 return aData->format;
60a247f2 kojima2000-04-03 03:10:20 +0000180}
181
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200182void WMGetDataBytesWithLength(WMData * aData, void *buffer, unsigned length)
9c59becb dan1999-12-14 01:07:24 +0000183{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200184 wassertr(aData->length > 0);
185 wassertr(length <= aData->length);
9c59becb dan1999-12-14 01:07:24 +0000186
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200187 memcpy(buffer, aData->bytes, length);
9c59becb dan1999-12-14 01:07:24 +0000188}
189
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200190void WMGetDataBytesWithRange(WMData * aData, void *buffer, WMRange aRange)
9c59becb dan1999-12-14 01:07:24 +0000191{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200192 wassertr(aRange.position < aData->length);
193 wassertr(aRange.count <= aData->length - aRange.position);
9c59becb dan1999-12-14 01:07:24 +0000194
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200195 memcpy(buffer, (unsigned char *)aData->bytes + aRange.position, aRange.count);
9c59becb dan1999-12-14 01:07:24 +0000196}
197
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200198WMData *WMGetSubdataWithRange(WMData * aData, WMRange aRange)
9c59becb dan1999-12-14 01:07:24 +0000199{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200200 void *buffer;
201 WMData *newData;
9c59becb dan1999-12-14 01:07:24 +0000202
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200203 if (aRange.count <= 0)
204 return WMCreateDataWithCapacity(0);
9c59becb dan1999-12-14 01:07:24 +0000205
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200206 buffer = wmalloc(aRange.count);
207 WMGetDataBytesWithRange(aData, buffer, aRange);
208 newData = WMCreateDataWithBytesNoCopy(buffer, aRange.count, wfree);
209 newData->format = aData->format;
b2478b63 dan2000-10-01 23:26:03 +0000210
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200211 return newData;
9c59becb dan1999-12-14 01:07:24 +0000212}
213
9c59becb dan1999-12-14 01:07:24 +0000214/* Testing data */
215
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200216Bool WMIsDataEqualToData(WMData * aData, WMData * anotherData)
9c59becb dan1999-12-14 01:07:24 +0000217{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200218 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);
9c59becb dan1999-12-14 01:07:24 +0000225}
226
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200227unsigned WMGetDataLength(WMData * aData)
9c59becb dan1999-12-14 01:07:24 +0000228{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200229 return aData->length;
9c59becb dan1999-12-14 01:07:24 +0000230}
231
9c59becb dan1999-12-14 01:07:24 +0000232/* Adding data */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200233void WMAppendDataBytes(WMData * aData, void *bytes, unsigned length)
9c59becb dan1999-12-14 01:07:24 +0000234{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200235 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;
9c59becb dan1999-12-14 01:07:24 +0000253}
254
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200255void WMAppendData(WMData * aData, WMData * anotherData)
9c59becb dan1999-12-14 01:07:24 +0000256{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200257 if (anotherData->length > 0)
258 WMAppendDataBytes(aData, anotherData->bytes, anotherData->length);
9c59becb dan1999-12-14 01:07:24 +0000259}
260
9c59becb dan1999-12-14 01:07:24 +0000261/* Modifying data */
262
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200263void WMReplaceDataBytesInRange(WMData * aData, WMRange aRange, void *bytes)
9c59becb dan1999-12-14 01:07:24 +0000264{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200265 wassertr(aRange.position < aData->length);
266 wassertr(aRange.count <= aData->length - aRange.position);
9c59becb dan1999-12-14 01:07:24 +0000267
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200268 memcpy((unsigned char *)aData->bytes + aRange.position, bytes, aRange.count);
9c59becb dan1999-12-14 01:07:24 +0000269}
270
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200271void WMResetDataBytesInRange(WMData * aData, WMRange aRange)
9c59becb dan1999-12-14 01:07:24 +0000272{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200273 wassertr(aRange.position < aData->length);
274 wassertr(aRange.count <= aData->length - aRange.position);
9c59becb dan1999-12-14 01:07:24 +0000275
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200276 memset((unsigned char *)aData->bytes + aRange.position, 0, aRange.count);
9c59becb dan1999-12-14 01:07:24 +0000277}
278
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200279void WMSetData(WMData * aData, WMData * anotherData)
9c59becb dan1999-12-14 01:07:24 +0000280{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200281 unsigned length = anotherData->length;
9c59becb dan1999-12-14 01:07:24 +0000282
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200283 WMSetDataCapacity(aData, length);
284 if (length > 0)
285 memcpy(aData->bytes, anotherData->bytes, length);
286 aData->length = length;
9c59becb dan1999-12-14 01:07:24 +0000287}
288
9c59becb dan1999-12-14 01:07:24 +0000289/* Storing data */