1
/* Chunk handling: Flexible arrays
2
** ===============================
12
** Initialize a chunk with a certain allocation unit
14
PUBLIC void HTChunkInit ARGS2 (HTChunk *,ch, int,grow)
22
/* Create a chunk with a certain allocation unit
25
PUBLIC HTChunk * HTChunkCreate ARGS1 (int,grow)
27
HTChunk * ch = typecalloc(HTChunk);
29
outofmem(__FILE__, "creation of chunk");
31
HTChunkInit (ch, grow);
35
PUBLIC HTChunk * HTChunkCreateMayFail ARGS2 (int,grow, int,failok)
37
HTChunk * ch = typecalloc(HTChunk);
40
outofmem(__FILE__, "creation of chunk");
45
HTChunkInit (ch, grow);
50
/* Create a chunk with a certain allocation unit and ensured size
53
PUBLIC HTChunk * HTChunkCreate2 ARGS2 (int,grow, size_t, needed)
55
HTChunk * ch = typecalloc(HTChunk);
57
outofmem(__FILE__, "HTChunkCreate2");
59
HTChunkInit (ch, grow);
61
ch->allocated = needed-1 - ((needed-1) % ch->growby)
62
+ ch->growby; /* Round up */
63
CTRACE((tfp, "HTChunkCreate2: requested %d, allocate %d\n",
64
(int) needed, ch->allocated));
65
ch->data = typecallocn(char, ch->allocated);
67
outofmem(__FILE__, "HTChunkCreate2 data");
73
/* Clear a chunk of all data
74
** --------------------------
76
PUBLIC void HTChunkClear ARGS1 (HTChunk *,ch)
87
PUBLIC void HTChunkFree ARGS1 (HTChunk *,ch)
97
PUBLIC BOOL HTChunkRealloc ARGS2 (HTChunk *,ch, int,growby)
100
ch->allocated = ch->allocated + growby;
102
data = ch->data ? (char *)realloc(ch->data, ch->allocated)
103
: typecallocn(char, ch->allocated);
106
} else if (ch->failok) {
107
HTChunkClear(ch); /* allocation failed, clear all data - kw */
108
return FALSE; /* caller should check ch->allocated - kw */
110
outofmem(__FILE__, "HTChunkRealloc");
116
/* Append a character
117
** ------------------
119
/* Warning: the code of this function is defined as macro in SGML.c. Change
120
the macro or undefine it in SGML.c when changing this function. -VH */
121
PUBLIC void HTChunkPutc ARGS2 (HTChunk *,ch, char,c)
123
if (ch->size >= ch->allocated) {
124
if (!HTChunkRealloc(ch, ch->growby))
127
ch->data[ch->size++] = c;
131
/* Ensure a certain size
132
** ---------------------
134
PUBLIC void HTChunkEnsure ARGS2 (HTChunk *,ch, int,needed)
136
if (needed <= ch->allocated) return;
137
ch->allocated = needed-1 - ((needed-1) % ch->growby)
138
+ ch->growby; /* Round up */
139
ch->data = ch->data ? (char *)realloc(ch->data, ch->allocated)
140
: typecallocn(char, ch->allocated);
141
if (ch->data == NULL)
142
outofmem(__FILE__, "HTChunkEnsure");
145
PUBLIC void HTChunkPutb ARGS3 (HTChunk *,ch, CONST char *,b, int,l)
148
if (ch->size + l > ch->allocated) {
149
int growby = l - (l % ch->growby) + ch->growby; /* Round up */
150
if (!HTChunkRealloc(ch, growby))
153
memcpy(ch->data + ch->size, b, l);
157
#define PUTC(code) ch->data[ch->size++] = (char)(code)
158
#define PUTC2(code) ch->data[ch->size++] = (char)(0x80|(0x3f &(code)))
160
PUBLIC void HTChunkPutUtf8Char ARGS2(
166
if (TOASCII(code) < 128)
168
else if (code < 0x800L) {
170
} else if (code < 0x10000L) {
172
} else if (code < 0x200000L) {
174
} else if (code < 0x4000000L) {
176
} else if (code<=0x7fffffffL) {
181
if (ch->size + utflen > ch->allocated) {
182
int growby = (ch->growby >= utflen) ? ch->growby : utflen;
183
if (!HTChunkRealloc(ch, growby))
191
ch->data[ch->size++] = (char)code;
194
PUTC(0xc0 | (code>>6));
197
PUTC(0xe0 | (code>>12));
200
PUTC(0xf0 | (code>>18));
203
PUTC(0xf8 | (code>>24));
206
PUTC(0xfc | (code>>30));
231
PUBLIC void HTChunkTerminate ARGS1 (HTChunk *,ch)
233
HTChunkPutc(ch, (char)0);
240
PUBLIC void HTChunkPuts ARGS2 (HTChunk *,ch, CONST char *,s)
243
for (p = s; *p; p++) {
244
if (ch->size >= ch->allocated) {
245
if (!HTChunkRealloc(ch, ch->growby))
248
ch->data[ch->size++] = *p;