home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libpics / htchunk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.0 KB  |  216 lines

  1.  
  2. /*  W3 Copyright statement 
  3. Copyright 1995 by: Massachusetts Institute of Technology (MIT), INRIA</H2>
  4.  
  5. This W3C software is being provided by the copyright holders under the
  6. following license. By obtaining, using and/or copying this software,
  7. you agree that you have read, understood, and will comply with the
  8. following terms and conditions: 
  9.  
  10. Permission to use, copy, modify, and distribute this software and its
  11. documentation for any purpose and without fee or royalty is hereby
  12. granted, provided that the full text of this NOTICE appears on
  13. <EM>ALL</EM> copies of the software and documentation or portions
  14. thereof, including modifications, that you make. 
  15.  
  16. <B>THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
  17. REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF EXAMPLE,
  18. BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
  19. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
  20. THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
  21. THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
  22. COPYRIGHT HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE
  23. OR DOCUMENTATION.
  24.  
  25. The name and trademarks of copyright holders may NOT be used
  26. in advertising or publicity pertaining to the software without
  27. specific, written prior permission.  Title to copyright in this
  28. software and any associated documentation will at all times remain
  29. with copyright holders. 
  30. */
  31. /*                                      HTChunk.c
  32. **    CHUNK HANDLING:    FLEXIBLE ARRAYS
  33. **
  34. **    (c) COPYRIGHT MIT 1995.
  35. **    Please first read the full copyright statement in the file COPYRIGH.
  36. **    @(#) $Id: htchunk.c,v 3.1 1998/03/28 03:32:06 ltabb Exp $
  37. **
  38. ** history:    AL, HF    28 Apr 94, Now chunk->data is filled by '\0' so
  39. **            that the string is terminated at any time. That makes
  40. **            HTChunk_terminate not needed any more, but never mind.
  41. **        EGP    15 Mar 96, Added CString conversions.
  42. **
  43. */
  44.  
  45. /* Library include files */
  46. /* --- BEGIN added by mharmsen@netscape.com on 7/9/97 --- */
  47. #include "xp.h"
  48. /* --- END added by mharmsen@netscape.com on 7/9/97 --- */
  49. /* #include "sysdep.h"  7/9/97 -- jhines */
  50. #include "htutils.h"
  51. #include "htchunk.h"                         /* Implemented here */
  52.  
  53. /*    Create a chunk with a certain allocation unit
  54. **    --------------
  55. */
  56. PUBLIC HTChunk * HTChunk_new (int grow)
  57. {
  58.     HTChunk * ch;
  59.     if ((ch = (HTChunk  *) HT_CALLOC(1, sizeof(HTChunk))) == NULL)
  60.         HT_OUTOFMEM("HTChunk_new");
  61.     ch->growby = grow;
  62.     return ch;
  63. }
  64.  
  65.  
  66. /*    Clear a chunk of all data
  67. **    --------------------------
  68. **    Zero the space but do NOT HT_FREE it. We zero because we promise to have
  69. **    a NUL terminated string at all times.
  70. */
  71. PUBLIC void HTChunk_clear (HTChunk * ch)
  72. {
  73.     if (ch) {
  74.     ch->size = 0;
  75.         /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  76.     XP_MEMSET((void *) ch->data, '\0', ch->allocated);
  77.         /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  78.     }
  79. }
  80.  
  81.  
  82. /*    Free a chunk
  83. **    ------------
  84. */
  85. PUBLIC void HTChunk_delete (HTChunk * ch)
  86. {
  87.     if (ch) {
  88.     HT_FREE(ch->data);
  89.         HT_FREE(ch);
  90.     }
  91. }
  92.  
  93. /*    Create a chunk from an allocated string
  94. **    ---------------------------------------
  95. */
  96. PUBLIC HTChunk * HTChunk_fromCString (char * str, int grow)
  97. {
  98.     HTChunk * ch;
  99.     ch = HTChunk_new(grow);
  100.     if (str) {
  101.     ch->data = str;            /* can't handle non-allocated str */
  102.         /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  103.     ch->size = XP_STRLEN(str);
  104.         /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  105.     }
  106.     return ch;
  107. }
  108.  
  109. /*    Free a chunk but keep the data
  110. **    ------------------------------
  111. */
  112. PUBLIC char * HTChunk_toCString (HTChunk * ch)
  113. {
  114.     char * ret = 0;
  115.     if (ch) {
  116.     ret = ch->data;
  117.         HT_FREE(ch);
  118.     }
  119.     return ret;
  120. }
  121.  
  122. /*    Append a character
  123. **    ------------------
  124. */
  125. PUBLIC void HTChunk_putc (HTChunk * ch, char c)
  126. {
  127.     if (ch) {
  128.     if (ch->size >= ch->allocated-1) {
  129.         if (ch->data) {
  130.         if ((ch->data = (char  *) HT_REALLOC(ch->data,ch->allocated+ch->growby)) == NULL)
  131.             HT_OUTOFMEM("HTChunk_putc");
  132.                 /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  133.         XP_MEMSET((void *) (ch->data + ch->allocated), '\0', ch->growby);
  134.                 /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  135.         } else {
  136.         if ((ch->data = (char  *) HT_CALLOC(1, ch->allocated+ch->growby)) == NULL)
  137.             HT_OUTOFMEM("HTChunk_putc");
  138.         }
  139.         ch->allocated += ch->growby;
  140.     }
  141.     *(ch->data+ch->size++) = c;
  142.     }
  143. }
  144.  
  145. /*    Append a string
  146. **    ---------------
  147. */
  148. PUBLIC void HTChunk_puts (HTChunk * ch, const char * s)
  149. {
  150.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  151.     HTChunk_putb(ch, s, (int) XP_STRLEN(s));
  152.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  153. }
  154.  
  155. /*    Append a block
  156. **    ---------------
  157. **    The string is always zero terminated
  158. */
  159. PUBLIC void HTChunk_putb (HTChunk * ch, const char * block, int len)
  160. {
  161.     if (ch && block && len) {
  162.     int needed = ch->size+len;
  163.     if (needed >= ch->allocated) {
  164.         ch->allocated = needed - needed%ch->growby + ch->growby;
  165.         if (ch->data) {
  166.         if ((ch->data = (char *) HT_REALLOC(ch->data, ch->allocated)) == NULL)
  167.             HT_OUTOFMEM("HTChunk_putb");
  168.                 /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  169.             XP_MEMSET((void *) (ch->data + needed), '\0', ch->allocated-needed);
  170.                 /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  171.         } else {
  172.         if ((ch->data = (char *) HT_CALLOC(1, ch->allocated)) == NULL)
  173.             HT_OUTOFMEM("HTChunk_putb");
  174.         }
  175.     }
  176.         /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  177.     XP_MEMCPY((void *) (ch->data+ch->size), block, len);
  178.         /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  179.     ch->size = needed;
  180.     }
  181. }
  182.  
  183.  
  184. /*    Ensure a certain size
  185. **    ---------------------
  186. */
  187. PUBLIC void HTChunk_ensure (HTChunk * ch, int len)
  188. {
  189.     if (ch && len) {
  190.     int needed = ch->size+len;
  191.     if (needed >= ch->allocated) {
  192.         ch->allocated = needed - needed%ch->growby + ch->growby;
  193.         if (ch->data) {
  194.         if ((ch->data = (char  *) HT_REALLOC(ch->data, ch->allocated)) == NULL)
  195.             HT_OUTOFMEM("HTChunk_putb");
  196.                 /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  197.             XP_MEMSET((void *) (ch->data + ch->size), '\0', ch->allocated-ch->size);
  198.                 /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  199.         } else {
  200.         if ((ch->data = (char  *) HT_CALLOC(1, ch->allocated)) == NULL)
  201.             HT_OUTOFMEM("ch->data ");
  202.         }
  203.     }
  204.     }
  205. #if 0
  206.     if (needed <= ch->allocated) return;
  207.     ch->allocated = needed-1 - ((needed-1) % ch->growby)
  208.                      + ch->growby; /* Round up */
  209.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  210.     ch->data = ch->data ? (char *)XP_REALLOC(ch->data, ch->allocated)
  211.             : (char *)HT_MALLOC(ch->allocated);
  212.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  213.     if (ch->data == NULL) HT_OUTOFMEM(__FILE__, "HTChunk_ensure");
  214. #endif
  215. }
  216.