Blame WWW/Library/Implementation/HTChunk.h

Packit f574b8
/*
Packit f574b8
 * $LynxId: HTChunk.h,v 1.20 2010/09/24 08:37:39 tom Exp $
Packit f574b8
 *
Packit f574b8
 *				     HTChunk: Flexible array handling for libwww
Packit f574b8
 *					CHUNK HANDLING:
Packit f574b8
 *					FLEXIBLE ARRAYS
Packit f574b8
 *
Packit f574b8
 * This module implements a flexible array.  It is a general utility module.  A
Packit f574b8
 * chunk is a structure which may be extended.	These routines create and
Packit f574b8
 * append data to chunks, automatically reallocating them as necessary.
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
#ifndef HTCHUNK_H
Packit f574b8
#define HTCHUNK_H 1
Packit f574b8
Packit f574b8
#ifndef HTUTILS_H
Packit f574b8
#include <HTUtils.h>
Packit f574b8
#endif
Packit f574b8
Packit f574b8
#include <UCMap.h>
Packit f574b8
Packit f574b8
#ifdef __cplusplus
Packit f574b8
extern "C" {
Packit f574b8
#endif
Packit f574b8
    typedef struct _HTChunk HTChunk;
Packit f574b8
Packit f574b8
    struct _HTChunk {
Packit f574b8
	int size;		/* In bytes                     */
Packit f574b8
	int growby;		/* Allocation unit in bytes     */
Packit f574b8
	int allocated;		/* Current size of *data        */
Packit f574b8
	char *data;		/* Pointer to malloc'd area or 0 */
Packit f574b8
	int failok;		/* allowed to fail without exiting program? */
Packit f574b8
	HTChunk *next;		/* pointer to the next chunk */
Packit f574b8
    };
Packit f574b8
Packit f574b8
/*
Packit f574b8
 * Initialize a chunk's allocation data and allocation-increment.
Packit f574b8
 */
Packit f574b8
    extern void HTChunkInit(HTChunk *ch, int grow);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 * Create new chunk
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   growby		The number of bytes to allocate at a time when the chunk
Packit f574b8
 *			is later extended.  Arbitrary but normally a trade-off
Packit f574b8
 *			of time vs memory.
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   returns		A chunk pointer to the new chunk,
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
Packit f574b8
    extern HTChunk *HTChunkCreate(int growby);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *  Create a chunk for which an allocation error is not a fatal application
Packit f574b8
 *  error if failok != 0, but merely resets the chunk.  When using a chunk
Packit f574b8
 *  created this way, the caller should always check whether the contents
Packit f574b8
 *  are ok each time after data have been appended.
Packit f574b8
 *  The create call may also fail and will reurn NULL in that case. - kw
Packit f574b8
 */
Packit f574b8
    extern HTChunk *HTChunkCreateMayFail(int growby, int failok);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *  Like HTChunkCreate but with initial allocation - kw
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
    extern HTChunk *HTChunkCreate2(int growby, size_t needed);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 * Free a chunk
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   ch			A valid chunk pointer made by HTChunkCreate()
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   ch			is invalid and may not be used.
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
Packit f574b8
    extern void HTChunkFree(HTChunk *ch);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 * Clear a chunk
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   ch			A valid chunk pointer made by HTChunkCreate()
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   *ch		The size of the chunk is zero.
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
Packit f574b8
    extern void HTChunkClear(HTChunk *ch);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 * Realloc a chunk
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   ch			A valid chunk pointer made by HTChunkCreate()
Packit f574b8
 *
Packit f574b8
 *   growby		growby
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   *ch		Expanded by growby
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
Packit f574b8
    extern BOOL HTChunkRealloc(HTChunk *ch, int growby);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 * Ensure a chunk has a certain space in
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   ch			A valid chunk pointer made by HTChunkCreate()
Packit f574b8
 *
Packit f574b8
 *   s			The size required
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   *ch		Has size at least s
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
Packit f574b8
    extern void HTChunkEnsure(HTChunk *ch, int s);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 * Append a character to a  chunk
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   ch			A valid chunk pointer made by HTChunkCreate()
Packit f574b8
 *
Packit f574b8
 *   c			The character to be appended
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   *ch		Is one character bigger
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
    extern void HTChunkPutc(HTChunk *ch, unsigned c);
Packit f574b8
Packit f574b8
    extern void HTChunkPutb(HTChunk *ch, const char *b, int l);
Packit f574b8
Packit f574b8
    extern void HTChunkPutUtf8Char(HTChunk *ch, UCode_t code);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 * Append a string to a  chunk
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   ch			A valid chunk pointer made by HTChunkCreate()
Packit f574b8
 *
Packit f574b8
 *   str		Points to a zero-terminated string to be appended
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   *ch		Is bigger by strlen(str)
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
Packit f574b8
    extern void HTChunkPuts(HTChunk *ch, const char *str);
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 * Append a zero character to a  chunk
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   ch			A valid chunk pointer made by HTChunkCreate()
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   *ch		Is one character bigger
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
Packit f574b8
    extern void HTChunkTerminate(HTChunk *ch);
Packit f574b8
Packit f574b8
/* like the above but no realloc: extend to another chunk if necessary */
Packit f574b8
/*
Packit f574b8
 *
Packit f574b8
 * Append a character (string, data) to a chunk
Packit f574b8
 *
Packit f574b8
 *   ON ENTRY,
Packit f574b8
 *
Packit f574b8
 *   ch                        A valid chunk pointer made by HTChunkCreate()
Packit f574b8
 *
Packit f574b8
 *   c                 The character to be appended
Packit f574b8
 *
Packit f574b8
 *   ON EXIT,
Packit f574b8
 *
Packit f574b8
 *   returns           original chunk or a pointer to the new chunk
Packit f574b8
 *                     (orginal chunk is referenced to the new one
Packit f574b8
 *                     by the field 'next')
Packit f574b8
 *
Packit f574b8
 */
Packit f574b8
    extern HTChunk *HTChunkPutc2(HTChunk *ch, int c);
Packit f574b8
    extern HTChunk *HTChunkPuts2(HTChunk *ch, const char *str);
Packit f574b8
    extern HTChunk *HTChunkPutb2(HTChunk *ch, const char *b, int l);
Packit f574b8
Packit f574b8
/* New pool infrastructure: UNlike the above, store data using alignment */
Packit f574b8
    extern HTChunk *HTChunkPutb0(HTChunk *ch, const char *b, int l);
Packit f574b8
Packit f574b8
#ifdef __cplusplus
Packit f574b8
}
Packit f574b8
#endif
Packit f574b8
#endif				/* HTCHUNK_H */