File: data.c

package info (click to toggle)
wmaker 0.96.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,332 kB
  • sloc: ansic: 99,482; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (289 lines) | stat: -rw-r--r-- 6,895 bytes parent folder | download | duplicates (6)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 *  WINGs WMData function library
 *
 *  Copyright (c) 1999-2003 Dan Pascu
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */

#include <string.h>
#include "WUtil.h"

typedef struct W_Data {
	unsigned length;	/* How many bytes we have */
	unsigned capacity;	/* How many bytes it can hold */
	unsigned growth;	/* How much to grow */
	void *bytes;		/* Actual data */
	unsigned retainCount;
	WMFreeDataProc *destructor;
	int format;		/* 0, 8, 16 or 32 */
} W_Data;

/* Creating and destroying data objects */

WMData *WMCreateDataWithCapacity(unsigned capacity)
{
	WMData *aData;

	aData = (WMData *) wmalloc(sizeof(WMData));

	if (capacity > 0)
		aData->bytes = wmalloc(capacity);
	else
		aData->bytes = NULL;

	aData->capacity = capacity;
	aData->growth = capacity / 2 > 0 ? capacity / 2 : 1;
	aData->length = 0;
	aData->retainCount = 1;
	aData->format = 0;
	aData->destructor = wfree;

	return aData;
}

WMData *WMCreateDataWithLength(unsigned length)
{
	WMData *aData;

	aData = WMCreateDataWithCapacity(length);
	if (length > 0) {
		aData->length = length;
	}

	return aData;
}

WMData *WMCreateDataWithBytes(const void *bytes, unsigned length)
{
	WMData *aData;

	aData = WMCreateDataWithCapacity(length);
	aData->length = length;
	memcpy(aData->bytes, bytes, length);

	return aData;
}

WMData *WMCreateDataWithBytesNoCopy(void *bytes, unsigned length, WMFreeDataProc * destructor)
{
	WMData *aData;

	aData = (WMData *) wmalloc(sizeof(WMData));
	aData->length = length;
	aData->capacity = length;
	aData->growth = length / 2 > 0 ? length / 2 : 1;
	aData->bytes = bytes;
	aData->retainCount = 1;
	aData->format = 0;
	aData->destructor = destructor;

	return aData;
}

WMData *WMCreateDataWithData(WMData * aData)
{
	WMData *newData;

	if (aData->length > 0) {
		newData = WMCreateDataWithBytes(aData->bytes, aData->length);
	} else {
		newData = WMCreateDataWithCapacity(0);
	}
	newData->format = aData->format;

	return newData;
}

WMData *WMRetainData(WMData * aData)
{
	aData->retainCount++;
	return aData;
}

void WMReleaseData(WMData * aData)
{
	aData->retainCount--;
	if (aData->retainCount > 0)
		return;
	if (aData->bytes != NULL && aData->destructor != NULL) {
		aData->destructor(aData->bytes);
	}
	wfree(aData);
}

/* Adjusting capacity */

void WMSetDataCapacity(WMData * aData, unsigned capacity)
{
	if (aData->capacity != capacity) {
		aData->bytes = wrealloc(aData->bytes, capacity);
		aData->capacity = capacity;
		aData->growth = capacity / 2 > 0 ? capacity / 2 : 1;
	}
	if (aData->length > capacity) {
		aData->length = capacity;
	}
}

void WMSetDataLength(WMData * aData, unsigned length)
{
	if (length > aData->capacity) {
		WMSetDataCapacity(aData, length);
	}
	if (length > aData->length) {
		memset((unsigned char *)aData->bytes + aData->length, 0, length - aData->length);
	}
	aData->length = length;
}

void WMSetDataFormat(WMData * aData, unsigned format)
{
	aData->format = format;
}

void WMIncreaseDataLengthBy(WMData * aData, unsigned extraLength)
{
	WMSetDataLength(aData, aData->length + extraLength);
}

/* Accessing data */

const void *WMDataBytes(WMData * aData)
{
	return aData->bytes;
}

void WMGetDataBytes(WMData * aData, void *buffer)
{
	wassertr(aData->length > 0);

	memcpy(buffer, aData->bytes, aData->length);
}

unsigned WMGetDataFormat(WMData * aData)
{
	return aData->format;
}

void WMGetDataBytesWithLength(WMData * aData, void *buffer, unsigned length)
{
	wassertr(aData->length > 0);
	wassertr(length <= aData->length);

	memcpy(buffer, aData->bytes, length);
}

void WMGetDataBytesWithRange(WMData * aData, void *buffer, WMRange aRange)
{
	wassertr(aRange.position < aData->length);
	wassertr(aRange.count <= aData->length - aRange.position);

	memcpy(buffer, (unsigned char *)aData->bytes + aRange.position, aRange.count);
}

WMData *WMGetSubdataWithRange(WMData * aData, WMRange aRange)
{
	void *buffer;
	WMData *newData;

	if (aRange.count <= 0)
		return WMCreateDataWithCapacity(0);

	buffer = wmalloc(aRange.count);
	WMGetDataBytesWithRange(aData, buffer, aRange);
	newData = WMCreateDataWithBytesNoCopy(buffer, aRange.count, wfree);
	newData->format = aData->format;

	return newData;
}

/* Testing data */

Bool WMIsDataEqualToData(WMData * aData, WMData * anotherData)
{
	if (aData->length != anotherData->length)
		return False;
	else if (!aData->bytes && !anotherData->bytes)	/* both are empty */
		return True;
	else if (!aData->bytes || !anotherData->bytes)	/* one of them is empty */
		return False;
	return (memcmp(aData->bytes, anotherData->bytes, aData->length) == 0);
}

unsigned WMGetDataLength(WMData * aData)
{
	return aData->length;
}

/* Adding data */
void WMAppendDataBytes(WMData * aData, const void *bytes, unsigned length)
{
	unsigned oldLength = aData->length;
	unsigned newLength = oldLength + length;

	if (newLength > aData->capacity) {
		unsigned nextCapacity = aData->capacity + aData->growth;
		unsigned nextGrowth = aData->capacity ? aData->capacity : 1;

		while (nextCapacity < newLength) {
			unsigned tmp = nextCapacity + nextGrowth;

			nextGrowth = nextCapacity;
			nextCapacity = tmp;
		}
		WMSetDataCapacity(aData, nextCapacity);
		aData->growth = nextGrowth;
	}
	memcpy((unsigned char *)aData->bytes + oldLength, bytes, length);
	aData->length = newLength;
}

void WMAppendData(WMData * aData, WMData * anotherData)
{
	if (anotherData->length > 0)
		WMAppendDataBytes(aData, anotherData->bytes, anotherData->length);
}

/* Modifying data */

void WMReplaceDataBytesInRange(WMData * aData, WMRange aRange, const void *bytes)
{
	wassertr(aRange.position < aData->length);
	wassertr(aRange.count <= aData->length - aRange.position);

	memcpy((unsigned char *)aData->bytes + aRange.position, bytes, aRange.count);
}

void WMResetDataBytesInRange(WMData * aData, WMRange aRange)
{
	wassertr(aRange.position < aData->length);
	wassertr(aRange.count <= aData->length - aRange.position);

	memset((unsigned char *)aData->bytes + aRange.position, 0, aRange.count);
}

void WMSetData(WMData * aData, WMData * anotherData)
{
	unsigned length = anotherData->length;

	WMSetDataCapacity(aData, length);
	if (length > 0)
		memcpy(aData->bytes, anotherData->bytes, length);
	aData->length = length;
}

/* Storing data */