Annotation of libwww/Library/src/HTChunk.c, revision 2.13

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

Webmaster