Annotation of libwww/Library/src/HTChunk.c, revision 2.6
1.1 timbl 1: /* Chunk handling: Flexible arrays
2: ** ===============================
3: **
2.6 ! frystyk 4: ** history: AL, HF 28 Apr 94, Now chunk->data is filled by '\0' so
! 5: ** that the string is terminated at any time. That makes
! 6: ** HTChunkTerminate not needed any more, but never mind.
! 7: **
1.1 timbl 8: */
9:
2.6 ! frystyk 10: /* Implementation dependent include files */
! 11: #include "tcp.h"
! 12:
1.1 timbl 13: #include "HTUtils.h"
14: #include "HTChunk.h"
15: #include <stdio.h>
16:
17: /* Create a chunk with a certain allocation unit
18: ** --------------
19: */
20: PUBLIC HTChunk * HTChunkCreate ARGS1 (int,grow)
21: {
2.6 ! frystyk 22: HTChunk * ch = (HTChunk *) calloc(1, sizeof(HTChunk));
1.1 timbl 23: if (ch == NULL) outofmem(__FILE__, "cretion of chunk");
24: ch->growby = grow;
25: return ch;
26: }
27:
28:
29: /* Clear a chunk of all data
30: ** --------------------------
31: */
32: PUBLIC void HTChunkClear ARGS1 (HTChunk *,ch)
33: {
34: if (ch->data) {
35: free(ch->data);
36: ch->data = 0;
37: }
38: ch->size = 0;
39: ch->allocated = 0;
40: }
41:
42:
43: /* Free a chunk
44: ** ------------
45: */
46: PUBLIC void HTChunkFree ARGS1 (HTChunk *,ch)
47: {
48: if (ch->data) free(ch->data);
49: free(ch);
50: }
51:
52:
53: /* Append a character
54: ** ------------------
55: */
56: PUBLIC void HTChunkPutc ARGS2 (HTChunk *,ch, char,c)
57: {
2.6 ! frystyk 58: #ifdef OLD_CODE
1.1 timbl 59: if (ch->size >= ch->allocated) {
2.6 ! frystyk 60: ch->allocated = ch->allocated + ch->growby;
1.1 timbl 61: ch->data = ch->data ? (char *)realloc(ch->data, ch->allocated)
2.6 ! frystyk 62: : (char *)malloc(ch->allocated);
1.1 timbl 63: if (!ch->data) outofmem(__FILE__, "HTChunkPutc");
2.6 ! frystyk 64: }
! 65: ch->data[ch->size++] = c;
! 66: #endif
! 67: if (ch->size >= ch->allocated) {
! 68: if (ch->data) {
! 69: ch->data = (char *) realloc(ch->data, ch->allocated + ch->growby);
! 70: memset((void *) ch->data + ch->allocated, '\0', ch->growby);
! 71: } else {
! 72: ch->data = (char *) calloc(1, ch->allocated + ch->growby);
! 73: }
! 74: ch->allocated = ch->allocated + ch->growby;
! 75: if (!ch->data) outofmem(__FILE__, "HTChunkPutc");
1.1 timbl 76: }
77: ch->data[ch->size++] = c;
78: }
79:
80:
81: /* Ensure a certain size
82: ** ---------------------
83: */
84: PUBLIC void HTChunkEnsure ARGS2 (HTChunk *,ch, int,needed)
85: {
86: if (needed <= ch->allocated) return;
87: ch->allocated = needed-1 - ((needed-1) % ch->growby)
88: + ch->growby; /* Round up */
89: ch->data = ch->data ? (char *)realloc(ch->data, ch->allocated)
90: : (char *)malloc(ch->allocated);
91: if (ch->data == NULL) outofmem(__FILE__, "HTChunkEnsure");
92: }
93:
94:
95: /* Terminate a chunk
96: ** -----------------
97: */
98: PUBLIC void HTChunkTerminate ARGS1 (HTChunk *,ch)
99: {
100: HTChunkPutc(ch, (char)0);
101: }
102:
103:
104: /* Append a string
105: ** ---------------
106: */
107: PUBLIC void HTChunkPuts ARGS2 (HTChunk *,ch, CONST char *,s)
108: {
109: CONST char * p;
110: for (p=s; *p; p++)
111: HTChunkPutc(ch, *p);
112: }
Webmaster