Mercurial > hg > dmlib
annotate src/dmgrowbuf.c @ 1530:94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
reallocating the grow buffer in various enlargement functions.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 12 May 2018 03:10:47 +0300 |
parents | 15afe578f0ae |
children | 260bf529a8f2 |
rev | line source |
---|---|
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * DMLib |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * -- Growable buffer implementation |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * Programmed and designed by Matti 'ccr' Hamalainen |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * (C) Copyright 2018 Tecnic Software productions (TNSP) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 */ |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 #include "dmgrowbuf.h" |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
10 int dmGrowBufInit(DMGrowBuf *buf) |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
11 { |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
12 if (buf == NULL) |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
13 return DMERR_NULLPTR; |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
14 |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
15 memset(buf, 0, sizeof(DMGrowBuf)); |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
16 |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
17 return DMERR_OK; |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
18 } |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
19 |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
20 |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
21 int dmGrowBufAlloc(DMGrowBuf *buf, const size_t initial, const size_t mingrow) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 { |
1459 | 23 int res; |
24 | |
25 if ((res = dmGrowBufInit(buf)) != DMERR_OK) | |
26 return res; | |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 buf->len = 0; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 buf->size = initial; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 buf->mingrow = mingrow; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 buf->allocated = FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
33 // Allocate the data |
1508
97b8096f16dc
Initialize the memory in growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1484
diff
changeset
|
34 if ((buf->data = dmMalloc0(initial)) == NULL) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 return DMERR_MALLOC; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 return DMERR_OK; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
41 int dmGrowBufNew(DMGrowBuf **pbuf, const size_t initial, const size_t mingrow) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 int res; |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
44 if (pbuf == NULL) |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
45 return DMERR_NULLPTR; |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 if ((*pbuf = dmMalloc0(sizeof(DMGrowBuf))) == NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 return DMERR_MALLOC; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
50 if ((res = dmGrowBufAlloc(*pbuf, initial, mingrow)) != DMERR_OK) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 { |
1459 | 52 // The "allocated" flag has not yet been set |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 dmGrowBufFree(*pbuf); |
1459 | 54 |
55 // .. thus free the allocated struct here | |
56 dmFreeR(pbuf); | |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 return res; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 (*pbuf)->allocated = TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 return res; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 void dmGrowBufFree(DMGrowBuf *buf) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 if (buf != NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 dmFreeR(&buf->data); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 if (buf->allocated) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 dmFree(buf); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
76 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear) |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
77 { |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
78 if ((buf->data = dmRealloc(buf->data, nsize)) == NULL) |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
79 return FALSE; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
80 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
81 if (clear) |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
82 memset(buf->adata + buf->size, 0, nsize - buf->size); |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
83 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
84 buf->size = nsize; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
85 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
86 return TRUE; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
87 } |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
88 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
89 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
90 // |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
91 // Grow the buffer by "amount" bytes, but at least by buf->mingrow, |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
92 // if there is not enough space for at least that amount compared to |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
93 // current buffer "len". |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
94 // |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 { |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
97 if (buf->data == NULL || buf->offs + buf->len + amount >= buf->size) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 { |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
99 size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
100 if (!dmGrowBufRealloc(buf, buf->offs + buf->len + grow, TRUE)) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
108 // |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
109 // Grow the buffer if "nsize" is larger than the current buffer size. |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
110 // Buffer is enlarged to nsize + mingrow. |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
111 // |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
112 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize) |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
113 { |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
114 if (buf->data == NULL || buf->offs + nsize > buf->size) |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
115 { |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
116 if (!dmGrowBufRealloc(buf, buf->offs + nsize + buf->mingrow, TRUE)) |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
117 return FALSE; |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
118 } |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
119 |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
120 return TRUE; |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
121 } |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
122 |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
123 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 int dmGrowBufResize(DMGrowBuf *buf) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 if (buf == NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 return DMERR_NULLPTR; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 buf->size = buf->len; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 if (buf->len == 0) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 return DMERR_OK; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
133 if (!dmGrowBufRealloc(buf, buf->size, FALSE)) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 return DMERR_MALLOC; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 return DMERR_OK; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 BOOL dmGrowBufPutU8(DMGrowBuf *buf, const Uint8 value) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 if (!dmGrowBufGrow(buf, sizeof(Uint8))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 buf->data[buf->len++] = value; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 |
1477
e8fe529f4341
Rename one of the growbuf functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1459
diff
changeset
|
151 BOOL dmGrowBufPut(DMGrowBuf *buf, const void *str, const size_t len) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 if (str == NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 |
1484
a0166c4050be
Fix dmGrowBufPut() to not include extra byte.
Matti Hamalainen <ccr@tnsp.org>
parents:
1477
diff
changeset
|
156 if (!dmGrowBufGrow(buf, len)) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 |
1484
a0166c4050be
Fix dmGrowBufPut() to not include extra byte.
Matti Hamalainen <ccr@tnsp.org>
parents:
1477
diff
changeset
|
159 memcpy(buf->data + buf->len, str, len); |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 buf->len += len; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 BOOL dmGrowBufPutU16BE(DMGrowBuf *buf, const Uint16 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 if (!dmGrowBufGrow(buf, sizeof(Uint16))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 buf->data[buf->len++] = (val >> 8) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 buf->data[buf->len++] = val & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 BOOL dmGrowBufPutU16LE(DMGrowBuf *buf, const Uint16 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 if (!dmGrowBufGrow(buf, sizeof(Uint16))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 buf->data[buf->len++] = val & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 buf->data[buf->len++] = (val >> 8) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
189 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 BOOL dmGrowBufPutU32BE(DMGrowBuf *buf, const Uint32 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 if (!dmGrowBufGrow(buf, sizeof(Uint32))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 buf->data[buf->len++] = (val >> 24) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 buf->data[buf->len++] = (val >> 16) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 buf->data[buf->len++] = (val >> 8) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 buf->data[buf->len++] = val & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 BOOL dmGrowBufPutU32LE(DMGrowBuf *buf, const Uint32 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 if (!dmGrowBufGrow(buf, sizeof(Uint32))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 buf->data[buf->len++] = val & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
210 buf->data[buf->len++] = (val >> 8) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 buf->data[buf->len++] = (val >> 16) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 buf->data[buf->len++] = (val >> 24) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 } |