earlybrowserreborn - Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 roytam 1 /*              Chunk handling: Flexible arrays
2 **              ===============================
3 **
4 */
5  
6 #include "HTUtils.h"
7 #include "HTChunk.h"
8 #include <stdio.h>
9  
10 /*      Create a chunk with a certain allocation unit
11 **      --------------
12 */
13 PUBLIC HTChunk * HTChunkCreate ARGS1 (int,grow)
14 {
15     HTChunk * ch = (HTChunk *) malloc(sizeof(HTChunk));
16     if (ch == NULL) outofmem(__FILE__, "cretion of chunk");
17  
18     ch->data = 0;
19     ch->growby = grow;
20     ch->size = 0;
21     ch->allocated = 0;
22     return ch;
23 }
24  
25  
26 /*      Clear a chunk of all data
27 **      --------------------------
28 */
29 PUBLIC void HTChunkClear ARGS1 (HTChunk *,ch)
30 {
31     if (ch->data) {
32         free(ch->data);
33         ch->data = 0;
34     }
35     ch->size = 0;
36     ch->allocated = 0;
37 }
38  
39  
40 /*      Free a chunk
41 **      ------------
42 */
43 PUBLIC void HTChunkFree ARGS1 (HTChunk *,ch)
44 {
45     if (ch->data) free(ch->data);
46     free(ch);
47 }
48  
49  
50 /*      Append a character
51 **      ------------------
52 */
53 PUBLIC void HTChunkPutc ARGS2 (HTChunk *,ch, char,c)
54 {
55     if (ch->size >= ch->allocated) {
56         ch->allocated = ch->allocated + ch->growby;
57         ch->data = ch->data ? (char *)realloc(ch->data, ch->allocated)
58                             : (char *)malloc(ch->allocated);
59       if (!ch->data) outofmem(__FILE__, "HTChunkPutc");
60     }
61     ch->data[ch->size++] = c;
62 }
63  
64  
65 /*      Ensure a certain size
66 **      ---------------------
67 */
68 PUBLIC void HTChunkEnsure ARGS2 (HTChunk *,ch, int,needed)
69 {
70     if (needed <= ch->allocated) return;
71     ch->allocated = needed-1 - ((needed-1) % ch->growby)
72                              + ch->growby; /* Round up */
73     ch->data = ch->data ? (char *)realloc(ch->data, ch->allocated)
74                         : (char *)malloc(ch->allocated);
75     if (ch->data == NULL) outofmem(__FILE__, "HTChunkEnsure");
76 }
77  
78  
79 /*      Terminate a chunk
80 **      -----------------
81 */
82 PUBLIC void HTChunkTerminate ARGS1 (HTChunk *,ch)
83 {
84     HTChunkPutc(ch, (char)0);
85 }
86  
87  
88 /*      Append a string
89 **      ---------------
90 */
91 PUBLIC void HTChunkPuts ARGS2 (HTChunk *,ch, CONST char *,s)
92 {
93     CONST char * p;
94     for (p=s; *p; p++)
95         HTChunkPutc(ch, *p);
96 }