# Buffer Management
[Basic Stuff]

This file contains the definition of a GWEN_BUFFER. More...

## Defines

#define | GWEN_BUFFER_MAX_BOOKMARKS 4
#define | GWEN_BUFFER_MODE_ABORT_ON_MEMFULL 0x0002
#define | GWEN_BUFFER_MODE_DEFAULT
#define | GWEN_BUFFER_MODE_DYNAMIC 0x0001
#define | GWEN_BUFFER_MODE_USE_BIO 0x0004

## Functions

GWENHYWFAR_API void | GWEN_Buffer_AddMode (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 mode)
GWENHYWFAR_API int | GWEN_Buffer_AdjustUsedBytes (GWEN_BUFFER *bf)
GWENHYWFAR_API int | GWEN_Buffer_AllocRoom (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 size)
GWENHYWFAR_API int | GWEN_Buffer_AppendBuffer (GWEN_BUFFER *bf, GWEN_BUFFER *sf)
GWENHYWFAR_API int | GWEN_Buffer_AppendByte (GWEN_BUFFER *bf, char c)
GWENHYWFAR_API int | GWEN_Buffer_AppendBytes (GWEN_BUFFER *bf, const char *buffer, GWEN_TYPE_UINT32 size)
GWENHYWFAR_API int | GWEN_Buffer_AppendString (GWEN_BUFFER *bf, const char *buffer)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_BytesLeft (GWEN_BUFFER *bf)
GWENHYWFAR_API int | GWEN_Buffer_Crop (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 pos, GWEN_TYPE_UINT32 l)
GWENHYWFAR_API int | GWEN_Buffer_DecrementPos (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 i)
GWENHYWFAR_API void | GWEN_Buffer_Dump (GWEN_BUFFER *bf, FILE *f, unsigned int insert)
GWENHYWFAR_API GWEN_BUFFER * | GWEN_Buffer_dup (GWEN_BUFFER *bf)
GWENHYWFAR_API int | GWEN_Buffer_FillLeftWithBytes (GWEN_BUFFER *bf, unsigned char c, GWEN_TYPE_UINT32 size)
GWENHYWFAR_API int | GWEN_Buffer_FillWithBytes (GWEN_BUFFER *bf, unsigned char c, GWEN_TYPE_UINT32 size)
GWENHYWFAR_API void | GWEN_Buffer_free (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetBookmark (GWEN_BUFFER *bf, unsigned int idx)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetBytesLeft (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetHardLimit (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetMaxUnsegmentedWrite (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetMode (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetPos (GWEN_BUFFER *bf)
GWENHYWFAR_API char * | GWEN_Buffer_GetPosPointer (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetSize (GWEN_BUFFER *bf)
GWENHYWFAR_API char * | GWEN_Buffer_GetStart (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetStep (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_GetUsedBytes (GWEN_BUFFER *bf)
GWENHYWFAR_API int | GWEN_Buffer_IncrementPos (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 i)
GWENHYWFAR_API int | GWEN_Buffer_InsertBuffer (GWEN_BUFFER *bf, GWEN_BUFFER *sf)
GWENHYWFAR_API int | GWEN_Buffer_InsertByte (GWEN_BUFFER *bf, char c)
GWENHYWFAR_API int | GWEN_Buffer_InsertBytes (GWEN_BUFFER *bf, const char *buffer, GWEN_TYPE_UINT32 size)
GWENHYWFAR_API int | GWEN_Buffer_InsertRoom (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 size)
GWENHYWFAR_API int | GWEN_Buffer_InsertString (GWEN_BUFFER *bf, const char *buffer)
GWENHYWFAR_API GWEN_BUFFER * | GWEN_Buffer_new (char *buffer, GWEN_TYPE_UINT32 size, GWEN_TYPE_UINT32 used, int take)
GWENHYWFAR_API int | GWEN_Buffer_PeekByte (GWEN_BUFFER *bf)
GWENHYWFAR_API int | GWEN_Buffer_ReadByte (GWEN_BUFFER *bf)
GWENHYWFAR_API int | GWEN_Buffer_ReadBytes (GWEN_BUFFER *bf, char *buffer, GWEN_TYPE_UINT32 *size)
GWENHYWFAR_API int | GWEN_Buffer_ReserveBytes (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 res)
GWENHYWFAR_API void | GWEN_Buffer_Reset (GWEN_BUFFER *bf)
GWENHYWFAR_API void | GWEN_Buffer_Rewind (GWEN_BUFFER *bf)
GWENHYWFAR_API GWEN_TYPE_UINT32 | GWEN_Buffer_RoomLeft (GWEN_BUFFER *bf)
GWENHYWFAR_API void | GWEN_Buffer_SetBookmark (GWEN_BUFFER *bf, unsigned int idx, GWEN_TYPE_UINT32 v)
GWENHYWFAR_API void | GWEN_Buffer_SetHardLimit (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 l)
GWENHYWFAR_API void | GWEN_Buffer_SetMode (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 mode)
GWENHYWFAR_API int | GWEN_Buffer_SetPos (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 i)
GWENHYWFAR_API void | GWEN_Buffer_SetSourceBIO (GWEN_BUFFER *bf, GWEN_BUFFEREDIO *bio, int take)
GWENHYWFAR_API void | GWEN_Buffer_SetStep (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 step)
GWENHYWFAR_API int | GWEN_Buffer_SetUsedBytes (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 i)
GWENHYWFAR_API void | GWEN_Buffer_SubMode (GWEN_BUFFER *bf, GWEN_TYPE_UINT32 mode)

## Detailed Description

This file contains the definition of a GWEN_BUFFER.

## Define Documentation

#define GWEN_BUFFER_MAX_BOOKMARKS 4

#define GWEN_BUFFER_MODE_ABORT_ON_MEMFULL 0x0002

#define GWEN_BUFFER_MODE_DEFAULT

Value:

    (\
      GWEN_BUFFER_MODE_DYNAMIC | \
      GWEN_BUFFER_MODE_ABORT_ON_MEMFULL\
      )

#define GWEN_BUFFER_MODE_DYNAMIC 0x0001

#define GWEN_BUFFER_MODE_USE_BIO 0x0004

## Function Documentation

GWENHYWFAR_API void GWEN_Buffer_AddMode | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | mode
| ) |

| Adds the give mode to the current mode of the buffer (such as GWEN_BUFFER_MODE_DYNAMIC).

GWENHYWFAR_API int GWEN_Buffer_AdjustUsedBytes | ( | GWEN_BUFFER * | bf | ) |

GWENHYWFAR_API int GWEN_Buffer_AllocRoom | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | size
| ) |

| Make sure that the buffer has enough room for the given bytes.

GWENHYWFAR_API int GWEN_Buffer_AppendBuffer | ( | GWEN_BUFFER * | bf,
|  | GWEN_BUFFER * | sf
| ) |

GWENHYWFAR_API int GWEN_Buffer_AppendByte | ( | GWEN_BUFFER * | bf,
|  | char | c
| ) |

Appends a single byte to this GWEN_BUFFER, if there is enough room. The position pointer is adjusted accordingly.

Returns:
  0 if ok, !=0 on error

GWENHYWFAR_API int GWEN_Buffer_AppendBytes | ( | GWEN_BUFFER * | bf,
|  | const char * | buffer,
|  | GWEN_TYPE_UINT32 | size
| ) |

Copies the contents of the given buffer to this GWEN_BUFFER, if there is enough room. The position pointer is adjusted accordingly.

Returns:
  0 if ok, !=0 on error

GWENHYWFAR_API int GWEN_Buffer_AppendString | ( | GWEN_BUFFER * | bf,
|  | const char * | buffer
| ) |

Appends a string to the buffer (without the trailing null char!) The position pointer is adjusted accordingly.

Returns:
  0 if ok, !=0 on error

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_BytesLeft | ( | GWEN_BUFFER * | bf | ) |

| Returns the number of bytes from pos to the end of the used area. Deprecated, please use GWEN_Buffer_GetBytesLeft instead.

GWENHYWFAR_API int GWEN_Buffer_Crop | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | pos,
|  | GWEN_TYPE_UINT32 | l
| ) |

GWENHYWFAR_API int GWEN_Buffer_DecrementPos | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | i
| ) |

GWENHYWFAR_API void GWEN_Buffer_Dump | ( | GWEN_BUFFER * | bf,
|  | FILE * | f,
|  | unsigned int | insert
| ) |

GWENHYWFAR_API GWEN_BUFFER* GWEN_Buffer_dup | ( | GWEN_BUFFER * | bf | ) |

GWENHYWFAR_API int GWEN_Buffer_FillLeftWithBytes | ( | GWEN_BUFFER * | bf,
|  | unsigned char | c,
|  | GWEN_TYPE_UINT32 | size
| ) |

GWENHYWFAR_API int GWEN_Buffer_FillWithBytes | ( | GWEN_BUFFER * | bf,
|  | unsigned char | c,
|  | GWEN_TYPE_UINT32 | size
| ) |

GWENHYWFAR_API void GWEN_Buffer_free | ( | GWEN_BUFFER * | bf | ) |

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetBookmark | ( | GWEN_BUFFER * | bf,
|  | unsigned int | idx
| ) |

| Returns the given bookmark

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetBytesLeft | ( | GWEN_BUFFER * | bf | ) |

| Returns the number of bytes from pos to the end of the used area.

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetHardLimit | ( | GWEN_BUFFER * | bf | ) |

| Returns the hard limit. This is the maximum size of a GWEN_BUFFER in dynamic mode.

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetMaxUnsegmentedWrite | ( | GWEN_BUFFER * | bf | ) |

| Returns the maximum number of bytes which can be written to the buffer at once (i.e. with reallocation).

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetMode | ( | GWEN_BUFFER * | bf | ) |

| Returns the current mode of the buffer (such as GWEN_BUFFER_MODE_DYNAMIC).

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetPos | ( | GWEN_BUFFER * | bf | ) |

| Returns the current position within the buffer. This pointer is adjusted by the various read and write functions.

GWENHYWFAR_API char* GWEN_Buffer_GetPosPointer | ( | GWEN_BUFFER * | bf | ) |

| Returns a pointer to the current position within the buffer.

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetSize | ( | GWEN_BUFFER * | bf | ) |

| Returns the size of the buffer (i.e. the number of bytes allocated).

GWENHYWFAR_API char* GWEN_Buffer_GetStart | ( | GWEN_BUFFER * | bf | ) |

| Returns the start of the buffer. You can use the function GWEN_Buffer_GetPos to navigate within the buffer.

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetStep | ( | GWEN_BUFFER * | bf | ) |

| In dynamic mode, whenever there is new data to allocate then this value specifies how much data to allocate in addition. The allocated data in total for this buffer will be aligned to this value.

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_GetUsedBytes | ( | GWEN_BUFFER * | bf | ) |

GWENHYWFAR_API int GWEN_Buffer_IncrementPos | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | i
| ) |

GWENHYWFAR_API int GWEN_Buffer_InsertBuffer | ( | GWEN_BUFFER * | bf,
|  | GWEN_BUFFER * | sf
| ) |

GWENHYWFAR_API int GWEN_Buffer_InsertByte | ( | GWEN_BUFFER * | bf,
|  | char | c
| ) |

Inserts a byte at the current position. If the current position is 0 and there is reserved space at the beginning of the buffer then that space will be used. Otherwise the data at the current position will be moved out of the way and the new byte inserted. The position pointer will not be altered, but after calling this function at that position there is the begin of the newly inserted byte. All pointers obtained from this module (e.g. via GWEN_Buffer_GetStart) become invalid !

GWENHYWFAR_API int GWEN_Buffer_InsertBytes | ( | GWEN_BUFFER * | bf,
|  | const char * | buffer,
|  | GWEN_TYPE_UINT32 | size
| ) |

Inserts multiple bytes at the current position. If the current position is 0 and there is reserved space at the beginning of the buffer then that space will be used. Otherwise the data at the current position will be moved out of the way and the new bytes inserted. The position pointer will not be altered, but all pointers obtained from this module (e.g. via GWEN_Buffer_GetStart) become invalid !

GWENHYWFAR_API int GWEN_Buffer_InsertRoom | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | size
| ) |

| This function makes room for the given number of bytes at the current buffer position. It moves any existing data at the current position out of the way. The position pointer will not be altered, but after calling this function at that position there is the begin of the newly inserted room. All pointers obtained from this module (e.g. via GWEN_Buffer_GetStart) become invalid !

GWENHYWFAR_API int GWEN_Buffer_InsertString | ( | GWEN_BUFFER * | bf,
|  | const char * | buffer
| ) |

| Inserts the given string at the current position (without the trailing null byte) The position pointer will not be altered, but after calling this function at that position there is the begin of the newly inserted string. All pointers obtained from this module (e.g. via GWEN_Buffer_GetStart) become invalid !

GWENHYWFAR_API GWEN_BUFFER* GWEN_Buffer_new | ( | char * | buffer,
|  | GWEN_TYPE_UINT32 | size,
|  | GWEN_TYPE_UINT32 | used,
|  | int | take
| ) |

Parameters:

  | buffer | if !=0, then the given buffer will be used. Otherwise a new buffer will be allocated (with size bytes)
  | size | real size of the buffer (if buffer is null, then this number of bytes will be allocated)
  | used | number of bytes of the buffer actually used. This is interesting when reading from a buffer
  | take | if buffer!=0 then this function takes over the ownership of the given buffer, if take is !=0

GWENHYWFAR_API int GWEN_Buffer_PeekByte | ( | GWEN_BUFFER * | bf | ) |

Returns the byte from the current position without changing the position pointer. So multiple calls to this function will result in returning the same character.

Returns:
  -1 on error, read char otherwise (in low byte)

GWENHYWFAR_API int GWEN_Buffer_ReadByte | ( | GWEN_BUFFER * | bf | ) |

Returns the byte from the current position. The position pointer is adjusted accordingly.

Returns:
  -1 on error, read char otherwise (in low byte)

GWENHYWFAR_API int GWEN_Buffer_ReadBytes | ( | GWEN_BUFFER * | bf,
|  | char * | buffer,
|  | GWEN_TYPE_UINT32 * | size
| ) |

Returns the bytes from the current position. The position pointer is adjusted accordingly.

Returns:
  -1 on error, 0 if ok

GWENHYWFAR_API int GWEN_Buffer_ReserveBytes | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | res
| ) |

| Reserves the given amount of bytes at the beginning of the buffer. Please note that this most likely results in a shift of the current position inside the buffer, so after this call all pointers obtained from this module (e.g. via GWEN_Buffer_GetStart) are invalid ! You can use this function to save some memory copy actions when inserting bytes at the beginning of the buffer.

GWENHYWFAR_API void GWEN_Buffer_Reset | ( | GWEN_BUFFER * | bf | ) |

| Resets the position pointer and the byte counter.

GWENHYWFAR_API void GWEN_Buffer_Rewind | ( | GWEN_BUFFER * | bf | ) |

| Resets the pos pointer

GWENHYWFAR_API GWEN_TYPE_UINT32 GWEN_Buffer_RoomLeft | ( | GWEN_BUFFER * | bf | ) |

GWENHYWFAR_API void GWEN_Buffer_SetBookmark | ( | GWEN_BUFFER * | bf,
|  | unsigned int | idx,
|  | GWEN_TYPE_UINT32 | v
| ) |

| Set a bookmark. These bookmarks are not used by the GWEN_BUFFER functions, but may be usefull for an application.

GWENHYWFAR_API void GWEN_Buffer_SetHardLimit | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | l
| ) |

| Changes the hard limit. This is the maximum size of a GWEN_BUFFER in dynamic mode.

GWENHYWFAR_API void GWEN_Buffer_SetMode | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | mode
| ) |

| Changes the current mode of the buffer (such as GWEN_BUFFER_MODE_DYNAMIC).

GWENHYWFAR_API int GWEN_Buffer_SetPos | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | i
| ) |

Returns:
  0 if ok, !=0 on error

GWENHYWFAR_API void GWEN_Buffer_SetSourceBIO | ( | GWEN_BUFFER * | bf,
|  | GWEN_BUFFEREDIO * | bio,
|  | int | take
| ) |

| Sets the buffered input to be used as a source. This BIO is used when a byte is to be returned while the buffer is empty (or the end of the buffer is reached). In such a case the missing bytes are read from this BIO if the mode contains GWEN_BUFFER_MODE_USE_BIO.

GWENHYWFAR_API void GWEN_Buffer_SetStep | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | step
| ) |

| In dynamic mode, whenever there is new data to allocate then this value specifies how much data to allocate in addition. The allocated data in total for this buffer will be aligned to this value. 1024 is a reasonable value. This value NEEDS to be aligned 2^n (i.e. only ONE bit must be set !)

GWENHYWFAR_API int GWEN_Buffer_SetUsedBytes | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | i
| ) |

Returns:
  0 if ok, !=0 on error

GWENHYWFAR_API void GWEN_Buffer_SubMode | ( | GWEN_BUFFER * | bf,
|  | GWEN_TYPE_UINT32 | mode
| ) |

| Removes the give mode from the current mode of the buffer (such as GWEN_BUFFER_MODE_DYNAMIC).