Mercurial > hg > dmlib
annotate src/dmgrowbuf.c @ 1699:f71cd6691e05
Implement dmGrowBufGetU8().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 05 Jun 2018 15:02:56 +0300 |
parents | 1036b0dcccb5 |
children | a2e65aa47554 |
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 |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
9 //#define DM_GROWBUF_DEBUG 1 |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
10 |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
11 #ifdef DM_GROWBUF_DEBUG |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
12 # define DM_DBG(...) do { fprintf(stderr, __VA_ARGS__); } while (0) |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
13 #else |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
14 # define DM_DBG(...) do { } while (0) |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
15 #endif |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
16 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 |
1694
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
18 int dmGrowBufInit(DMGrowBuf *buf) |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
19 { |
1694
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
20 DM_DBG("dmGrowBufInit(%p)\n", buf); |
1687 | 21 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
22 if (buf == NULL) |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
23 return DMERR_NULLPTR; |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
24 |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
25 memset(buf, 0, sizeof(DMGrowBuf)); |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
26 |
1694
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
27 return DMERR_OK; |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
28 } |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
29 |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
30 |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
31 int dmGrowBufAlloc(DMGrowBuf *buf, const size_t initial, const size_t mingrow) |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
32 { |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
33 int res; |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
34 |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
35 DM_DBG("dmGrowBufAlloc(%p, %" DM_PRIu_SIZE_T ", %" DM_PRIu_SIZE_T ")\n", |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
36 buf, initial, mingrow); |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
37 |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
38 if ((res = dmGrowBufInit(buf)) != DMERR_OK) |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
39 return res; |
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
40 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 buf->len = 0; |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
42 buf->offs = 0; |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 buf->size = initial; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 buf->mingrow = mingrow; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 buf->allocated = FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
47 // Allocate the data |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
48 if ((buf->data = dmMalloc0(initial)) == NULL) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 return DMERR_MALLOC; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 return DMERR_OK; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
55 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
|
56 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 int res; |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
58 if (pbuf == NULL) |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
59 return DMERR_NULLPTR; |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 |
1687 | 61 DM_DBG("dmGrowBufNew(%p, %" DM_PRIu_SIZE_T ", %" DM_PRIu_SIZE_T ")\n", |
62 pbuf, initial, mingrow); | |
63 | |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 if ((*pbuf = dmMalloc0(sizeof(DMGrowBuf))) == NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 return DMERR_MALLOC; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
67 if ((res = dmGrowBufAlloc(*pbuf, initial, mingrow)) != DMERR_OK) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 { |
1459 | 69 // The "allocated" flag has not yet been set |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 dmGrowBufFree(*pbuf); |
1459 | 71 |
72 // .. thus free the allocated struct here | |
73 dmFreeR(pbuf); | |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 return res; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 (*pbuf)->allocated = TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 return res; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 void dmGrowBufFree(DMGrowBuf *buf) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 { |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
84 DM_DBG("dmGrowBufFree(%p)\n", buf); |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 if (buf != NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 { |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
87 DM_DBG( |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
88 " buf->data = %p\n" |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
89 " buf->allocated = %s\n", |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
90 buf->data, buf->allocated ? "YES" : "NO"); |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
91 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
92 dmFreeR(&buf->data); |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
93 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 if (buf->allocated) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 dmFree(buf); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
100 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
|
101 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
102 DM_DBG("dmGrowBufRealloc(%p):\n" |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
103 " size=%" DM_PRIu_SIZE_T ", nsize=" DM_PRIu_SIZE_T |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
104 ", nstack=%d, offs=%" DM_PRIu_SIZE_T "\n", |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
105 buf, buf->size, nsize, buf->nstack, buf->offs); |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
106 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
107 // Can't be smaller than current size! |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
108 if (nsize < buf->size) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
109 return FALSE; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
110 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
111 if ((buf->data = dmRealloc(buf->data, nsize)) == NULL) |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
112 return FALSE; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
113 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
114 // For buffers growing backwards, we must move the |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
115 // current data to the end of the buffer .. |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
116 size_t clrsize = nsize - buf->size; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
117 if (buf->backwards) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
118 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
119 memmove(buf->data + clrsize, buf->data, clrsize); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
120 buf->offs += clrsize; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
121 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
122 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
123 // Check if we need to clear the newly allocated area? |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
124 if (clear) |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
125 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
126 if (buf->backwards) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
127 memset(buf->data, 0, clrsize); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
128 else |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
129 memset(buf->data + buf->size, 0, clrsize); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
130 } |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
131 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
132 buf->size = nsize; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
133 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
134 return TRUE; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
135 } |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
136 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
137 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
138 // |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
139 // 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
|
140 // 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
|
141 // current buffer "len". |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
142 // |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
145 size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
146 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
147 if (buf->data == NULL || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
148 (buf->backwards && amount >= buf->offs) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
149 (!buf->backwards && buf->offs + amount >= buf->size)) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
151 if (!dmGrowBufRealloc(buf, buf->size + grow, TRUE)) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
155 buf->len += amount; |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
160 // |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
161 // 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
|
162 // Buffer is enlarged to nsize + mingrow. |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
163 // |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
164 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize) |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
165 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
166 if (buf->data == NULL || nsize > buf->size) |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
167 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
168 if (!dmGrowBufRealloc(buf, nsize + buf->mingrow, TRUE)) |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
169 return FALSE; |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
170 } |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
171 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
172 buf->len = nsize; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
173 return TRUE; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
174 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
175 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
176 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
177 static void dmGrowBufUpdate(DMGrowBuf *buf) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
178 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
179 if (buf->offs < buf->min_offs) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
180 buf->min_offs = buf->offs; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
181 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
182 if (buf->offs > buf->max_offs) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
183 buf->max_offs = buf->offs; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
184 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
185 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
186 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
187 BOOL dmGrowBufPut(DMGrowBuf *buf, const Uint8 *data, const size_t len) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
188 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
189 if (data == NULL) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
190 return FALSE; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
191 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
192 if (!dmGrowBufGrow(buf, len)) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
193 return FALSE; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
194 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
195 if (buf->backwards) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
196 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
197 if (buf->literal) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
198 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
199 buf->offs -= len; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
200 memcpy(buf->data + buf->offs, data, len); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
201 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
202 else |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
203 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
204 for (size_t n = 0; n < len; n++) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
205 buf->data[buf->offs--] = data[n]; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
206 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
207 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
208 else |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
209 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
210 memcpy(buf->data + buf->offs, data, len); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
211 buf->offs += len; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
212 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
213 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
214 dmGrowBufUpdate(buf); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
215 |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
216 return TRUE; |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
217 } |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
218 |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
219 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 BOOL dmGrowBufPutU8(DMGrowBuf *buf, const Uint8 value) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 if (!dmGrowBufGrow(buf, sizeof(Uint8))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
225 buf->data[buf->offs] = value; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
226 if (buf->backwards) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
227 buf->offs--; |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
228 else |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
229 buf->offs++; |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
231 dmGrowBufUpdate(buf); |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 BOOL dmGrowBufPutU16BE(DMGrowBuf *buf, const Uint16 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
239 if (buf->literal && buf->backwards) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
240 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
241 return |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
242 !dmGrowBufPutU8(buf, val & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
243 !dmGrowBufPutU8(buf, (val >> 8) & 0xff); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
244 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
245 else |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
246 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
247 return |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
248 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
249 !dmGrowBufPutU8(buf, val & 0xff); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
250 } |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 BOOL dmGrowBufPutU16LE(DMGrowBuf *buf, const Uint16 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
256 if (buf->literal && buf->backwards) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
257 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
258 return |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
259 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
260 !dmGrowBufPutU8(buf, val & 0xff); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
261 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
262 else |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
263 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
264 return |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
265 !dmGrowBufPutU8(buf, val & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
266 !dmGrowBufPutU8(buf, (val >> 8) & 0xff); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
267 } |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
271 BOOL dmGrowBufPutU32BE(DMGrowBuf *buf, const Uint32 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
272 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
273 if (buf->literal && buf->backwards) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
274 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
275 return |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
276 !dmGrowBufPutU8(buf, (val >> 24) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
277 !dmGrowBufPutU8(buf, (val >> 16) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
278 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
279 !dmGrowBufPutU8(buf, val & 0xff); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
280 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
281 else |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
282 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
283 return |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
284 !dmGrowBufPutU8(buf, val & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
285 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
286 !dmGrowBufPutU8(buf, (val >> 16) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
287 !dmGrowBufPutU8(buf, (val >> 24) & 0xff); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
288 } |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
289 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
291 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
292 BOOL dmGrowBufPutU32LE(DMGrowBuf *buf, const Uint32 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
293 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
294 if (buf->literal && buf->backwards) |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
295 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
296 return |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
297 !dmGrowBufPutU8(buf, val & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
298 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
299 !dmGrowBufPutU8(buf, (val >> 16) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
300 !dmGrowBufPutU8(buf, (val >> 24) & 0xff); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
301 } |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
302 else |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
303 { |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
304 return |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
305 !dmGrowBufPutU8(buf, (val >> 24) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
306 !dmGrowBufPutU8(buf, (val >> 16) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
307 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) || |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
308 !dmGrowBufPutU8(buf, val & 0xff); |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
309 } |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
310 } |
1699
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
311 |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
312 |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
313 BOOL dmGrowBufGetU8(DMGrowBuf *buf, Uint8 *value) |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
314 { |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
315 if (buf->backwards && buf->offs > 0) |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
316 { |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
317 *value = buf->data[buf->offs]; |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
318 buf->offs--; |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
319 return TRUE; |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
320 } |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
321 else |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
322 if (!buf->backwards && buf->offs < buf->len) |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
323 { |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
324 *value = buf->data[buf->offs]; |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
325 buf->offs++; |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
326 return TRUE; |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
327 } |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
328 |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
329 return FALSE; |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
330 } |