annotate src/dmgrowbuf.c @ 1531:260bf529a8f2

Implement current len/offs push/pop for growbuf.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 12 May 2018 03:23:32 +0300
parents 94eb6a8a7d56
children 064fc2e3ee64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
23 int res;
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
24
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
25 if ((res = dmGrowBufInit(buf)) != DMERR_OK)
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
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;
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
29 buf->offs = 0;
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 buf->size = initial;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 buf->mingrow = mingrow;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 buf->allocated = FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
1509
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
34 // Allocate the data
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
35 if ((buf->adata = buf->data = dmMalloc0(initial)) == NULL)
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 return DMERR_MALLOC;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 return DMERR_OK;
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
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
1458
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
42 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
43 {
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 int res;
1458
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
45 if (pbuf == NULL)
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
46 return DMERR_NULLPTR;
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 if ((*pbuf = dmMalloc0(sizeof(DMGrowBuf))) == NULL)
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
1458
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
51 if ((res = dmGrowBufAlloc(*pbuf, initial, mingrow)) != DMERR_OK)
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 {
1459
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
53 // The "allocated" flag has not yet been set
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 dmGrowBufFree(*pbuf);
1459
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
55
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
56 // .. thus free the allocated struct here
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
57 dmFreeR(pbuf);
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 return res;
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
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 (*pbuf)->allocated = TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 return res;
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
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 void dmGrowBufFree(DMGrowBuf *buf)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 {
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 if (buf != NULL)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 {
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
70 dmFreeR(&buf->adata);
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
71 buf->data = NULL;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
72
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 if (buf->allocated)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 dmFree(buf);
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
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
79 void dmGrowBufPush(DMGrowBuf *buf)
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
80 {
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
81 if (buf != NULL && buf->adata != NULL)
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
82 {
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
83 buf->stack[buf->nstack].offs = buf->offs;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
84 buf->stack[buf->nstack].len = buf->len;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
85 buf->nstack++;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
86
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
87 buf->offs = buf->len;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
88 buf->data = buf->adata + buf->offs;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
89 buf->len = 0;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
90 }
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
91 }
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
92
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
93
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
94 void dmGrowBufPop(DMGrowBuf *buf)
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
95 {
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
96 if (buf != NULL && buf->adata != NULL && buf->nstack > 0)
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
97 {
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
98 buf->nstack--;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
99 buf->offs = buf->stack[buf->nstack].offs;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
100 buf->len += buf->stack[buf->nstack].len;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
101
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
102 buf->data = buf->adata + buf->offs;
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
103 }
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
104 }
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
105
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
106
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
107 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
108 {
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
109 if ((buf->adata = dmRealloc(buf->adata, nsize)) == NULL)
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
110 return FALSE;
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
111
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
112 if (clear)
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
113 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
114
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
115 buf->data = buf->adata + buf->offs;
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
116 buf->size = nsize;
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
117
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
118 return TRUE;
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
119 }
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
120
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
121
1509
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
122 //
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
123 // 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
124 // 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
125 // current buffer "len".
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
126 //
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 {
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
129 if (buf->adata == NULL || buf->offs + buf->len + amount >= buf->size)
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
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 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
132 if (!dmGrowBufRealloc(buf, buf->offs + buf->len + grow, TRUE))
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 return FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 }
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 TRUE;
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
1509
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
140 //
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
141 // 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
142 // Buffer is enlarged to nsize + mingrow.
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
143 //
1455
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
144 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize)
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
145 {
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
146 if (buf->adata == NULL || buf->offs + nsize > buf->size)
1455
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
147 {
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
148 if (!dmGrowBufRealloc(buf, buf->offs + nsize + buf->mingrow, TRUE))
1455
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
149 return FALSE;
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
150 }
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
151
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
152 return TRUE;
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
153 }
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
154
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
155
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 int dmGrowBufResize(DMGrowBuf *buf)
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 if (buf == NULL)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 return DMERR_NULLPTR;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 buf->size = buf->len;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 if (buf->len == 0)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 return DMERR_OK;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
165 if (!dmGrowBufRealloc(buf, buf->size, FALSE))
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 return DMERR_MALLOC;
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 return DMERR_OK;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 }
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
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 BOOL dmGrowBufPutU8(DMGrowBuf *buf, const Uint8 value)
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 if (!dmGrowBufGrow(buf, sizeof(Uint8)))
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 return FALSE;
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 buf->data[buf->len++] = value;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 return TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182
1477
e8fe529f4341 Rename one of the growbuf functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1459
diff changeset
183 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
184 {
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 if (str == NULL)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 return FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187
1484
a0166c4050be Fix dmGrowBufPut() to not include extra byte.
Matti Hamalainen <ccr@tnsp.org>
parents: 1477
diff changeset
188 if (!dmGrowBufGrow(buf, len))
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 return FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
1484
a0166c4050be Fix dmGrowBufPut() to not include extra byte.
Matti Hamalainen <ccr@tnsp.org>
parents: 1477
diff changeset
191 memcpy(buf->data + buf->len, str, len);
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 buf->len += len;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 return TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 BOOL dmGrowBufPutU16BE(DMGrowBuf *buf, const Uint16 val)
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 if (!dmGrowBufGrow(buf, sizeof(Uint16)))
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 return FALSE;
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 buf->data[buf->len++] = (val >> 8) & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 buf->data[buf->len++] = val & 0xff;
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 return TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 }
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
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 BOOL dmGrowBufPutU16LE(DMGrowBuf *buf, const Uint16 val)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 {
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 if (!dmGrowBufGrow(buf, sizeof(Uint16)))
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 return FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 buf->data[buf->len++] = val & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 buf->data[buf->len++] = (val >> 8) & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 return TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220
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 BOOL dmGrowBufPutU32BE(DMGrowBuf *buf, const Uint32 val)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 {
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 if (!dmGrowBufGrow(buf, sizeof(Uint32)))
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 return FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 buf->data[buf->len++] = (val >> 24) & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 buf->data[buf->len++] = (val >> 16) & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 buf->data[buf->len++] = (val >> 8) & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 buf->data[buf->len++] = val & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 return TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 }
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 BOOL dmGrowBufPutU32LE(DMGrowBuf *buf, const Uint32 val)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 {
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 if (!dmGrowBufGrow(buf, sizeof(Uint32)))
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 return FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 buf->data[buf->len++] = val & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 buf->data[buf->len++] = (val >> 8) & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 buf->data[buf->len++] = (val >> 16) & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 buf->data[buf->len++] = (val >> 24) & 0xff;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 return TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 }