gwenhywfar
5.11.1beta
|
#include <gwenhywfar/gwenhywfarapi.h>
#include <gwenhywfar/types.h>
#include <stdio.h>
#include <string.h>
Go to the source code of this file.
Macros | |
#define | GWEN_BUFFER_MAX_BOOKMARKS 64 |
#define | GWEN_BUFFER_MODE_ABORT_ON_MEMFULL 0x0002 |
#define | GWEN_BUFFER_MODE_DEFAULT |
#define | GWEN_BUFFER_MODE_DYNAMIC 0x0001 |
#define | GWEN_BUFFER_MODE_READONLY 0x0020 |
Typedefs | |
typedef struct GWEN_BUFFER | GWEN_BUFFER |
A dynamically resizeable text buffer. More... | |
Variables | |
GWENHYWFAR_API const char * | fmt |
#define GWEN_BUFFER_MODE_DEFAULT |
GWENHYWFAR_API void GWEN_Buffer_AddMode | ( | GWEN_BUFFER * | bf, |
uint32_t | 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 | ) |
The functions GWEN_Buffer_IncrementPos and GWEN_Buffer_DecrementPos only modify the internal position pointer. This function here adjusts the number of used bytes to just before the internal position pointer. This is often used to avoid copying, like in the following example:
GWENHYWFAR_API int GWEN_Buffer_AllocRoom | ( | GWEN_BUFFER * | bf, |
uint32_t | 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 | ||
) |
Append the content of the buffer sf
at the end of the buffer bf
. The size of bf
will be increased accordingly. Returns zero on success or nonzero if this failed (e.g. because of out of memory error).
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.
GWENHYWFAR_API int GWEN_Buffer_AppendBytes | ( | GWEN_BUFFER * | bf, |
const char * | buffer, | ||
uint32_t | size | ||
) |
Copies the contents of the given buffer to this GWEN_BUFFER, if there is enough room. The position pointer is adjusted accordingly.
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.
GWENHYWFAR_API void GWEN_Buffer_Attach | ( | GWEN_BUFFER * | bf | ) |
Increases the internal reference counter. It is decreased by GWEN_Buffer_free().
GWENHYWFAR_API int GWENHYWFAR_CB GWEN_Buffer_CacheFn_Attach | ( | void * | ptr | ) |
This is a callback function to be used by GWEN_MULTICACHE (and only for that). It internally calls GWEN_Buffer_Attach().
GWENHYWFAR_API int GWENHYWFAR_CB GWEN_Buffer_CacheFn_Free | ( | void * | ptr | ) |
This is a callback function to be used by GWEN_MULTICACHE (and only for that). It internally calls GWEN_Buffer_free().
GWENHYWFAR_API int GWEN_Buffer_Crop | ( | GWEN_BUFFER * | bf, |
uint32_t | pos, | ||
uint32_t | l | ||
) |
GWENHYWFAR_API int GWEN_Buffer_DecrementPos | ( | GWEN_BUFFER * | bf, |
uint32_t | i | ||
) |
Move the position pointer backward by the given number i
.
GWENHYWFAR_API void GWEN_Buffer_Dump | ( | GWEN_BUFFER * | bf, |
unsigned int | insert | ||
) |
Print the current content of buffer bf
into the file f
.
GWENHYWFAR_API GWEN_BUFFER* GWEN_Buffer_dup | ( | GWEN_BUFFER * | bf | ) |
Create a new copy as a duplicate of the buffer bf
.
GWENHYWFAR_API int GWEN_Buffer_FillLeftWithBytes | ( | GWEN_BUFFER * | bf, |
unsigned char | c, | ||
uint32_t | size | ||
) |
GWENHYWFAR_API const char GWENHYWFAR_API int GWEN_Buffer_FillWithBytes | ( | GWEN_BUFFER * | bf, |
unsigned char | c, | ||
uint32_t | size | ||
) |
GWENHYWFAR_API void GWEN_Buffer_free | ( | GWEN_BUFFER * | bf | ) |
Frees the given buffer.
If the internal storage was allocated for this new buffer, then it will freed here. If the internal storage is used from a different buffer
, then it will only be freed if the argument take_ownership
of GWEN_Buffer_new() was nonzero.
GWENHYWFAR_API uint32_t GWEN_Buffer_GetBookmark | ( | const GWEN_BUFFER * | bf, |
unsigned int | idx | ||
) |
Returns the given bookmark
GWENHYWFAR_API uint32_t GWEN_Buffer_GetBytesLeft | ( | GWEN_BUFFER * | bf | ) |
Returns the number of bytes from pos to the end of the used area.
GWENHYWFAR_API uint32_t GWEN_Buffer_GetHardLimit | ( | GWEN_BUFFER * | bf | ) |
Returns the hard limit. This is the maximum size of a GWEN_BUFFER in dynamic mode.
GWENHYWFAR_API uint32_t GWEN_Buffer_GetMaxUnsegmentedWrite | ( | GWEN_BUFFER * | bf | ) |
Returns the maximum number of bytes which can be written to the buffer at once (i.e. without reallocation).
GWENHYWFAR_API uint32_t GWEN_Buffer_GetMode | ( | GWEN_BUFFER * | bf | ) |
Returns the current mode of the buffer (such as GWEN_BUFFER_MODE_DYNAMIC).
GWENHYWFAR_API uint32_t GWEN_Buffer_GetPos | ( | const 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 | ( | const GWEN_BUFFER * | bf | ) |
Returns a pointer to the current position within the buffer.
GWENHYWFAR_API uint32_t GWEN_Buffer_GetSize | ( | const GWEN_BUFFER * | bf | ) |
Returns the size of the buffer (i.e. the number of bytes allocated).
GWENHYWFAR_API char* GWEN_Buffer_GetStart | ( | const GWEN_BUFFER * | bf | ) |
Returns the start of the buffer. You can use the function GWEN_Buffer_GetPos to navigate within the buffer.
GWENHYWFAR_API uint32_t 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 uint32_t GWEN_Buffer_GetUsedBytes | ( | const GWEN_BUFFER * | bf | ) |
GWENHYWFAR_API int GWEN_Buffer_IncrementPos | ( | GWEN_BUFFER * | bf, |
uint32_t | i | ||
) |
Move the position pointer forward by the given number i
.
GWENHYWFAR_API int GWEN_Buffer_InsertBuffer | ( | GWEN_BUFFER * | bf, |
GWEN_BUFFER * | sf | ||
) |
Insert the content of the buffer sf
into the buffer bf
at the position of its current position pointer. The size of bf
will be increased accordingly. Returns zero on success or nonzero if this failed (e.g. because of out of memory error).
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 ! This function updates the bookmarks accordingly.
GWENHYWFAR_API int GWEN_Buffer_InsertBytes | ( | GWEN_BUFFER * | bf, |
const char * | buffer, | ||
uint32_t | 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, |
uint32_t | 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 ! This function updates the bookmarks accordingly.
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 ! This function updates the bookmarks accordingly.
GWENHYWFAR_API int GWEN_Buffer_KeepTextBetweenStrings | ( | GWEN_BUFFER * | bf, |
const char * | openingString, | ||
const char * | closingString, | ||
int | onlyBetween | ||
) |
Look for the openingString and the closingString in the buffer, if they exist remove all outside those strings, in some cases including the opening/closing string.
bf | buffer to operate on |
openingString | string which starts the area you are interested in |
closingString | string which ends the area you are interested in |
onlyBetween | if !=0 the opening/closing string are also removed from the buffer |
GWENHYWFAR_API GWEN_BUFFER* GWEN_Buffer_new | ( | char * | buffer, |
uint32_t | size, | ||
uint32_t | used, | ||
int | take_ownership | ||
) |
Creates a new GWEN_BUFFER, which is a dynamically resizeable text buffer.
buffer | If non-NULL, then this buffer will be used as actual storage space. Otherwise a new buffer will be allocated (with size bytes) |
size | If buffer was non-NULL, then this argument must specifiy the size of that buffer. If buffer was NULL, then this argument specifies the number of bytes that will be allocated. |
used | Number of bytes of the buffer actually used. This is interesting when reading from a buffer. |
take_ownership | If buffer was non-NULL and this argument is nonzero, then the new GWEN_BUFFER object takes over the ownership of the given buffer so that it will be freed on GWEN_Buffer_free(). If this argument is zero, the given buffer will not be freed. If buffer was NULL, this argument has no effect. |
GWENHYWFAR_API void GWEN_Buffer_OverwriteContent | ( | GWEN_BUFFER * | bf, |
int | c | ||
) |
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.
GWENHYWFAR_API int GWEN_Buffer_ReadByte | ( | GWEN_BUFFER * | bf | ) |
Returns the byte from the current position. The position pointer is adjusted accordingly.
GWENHYWFAR_API int GWEN_Buffer_ReadBytes | ( | GWEN_BUFFER * | bf, |
char * | buffer, | ||
uint32_t * | size | ||
) |
Returns the bytes from the current position. The position pointer is adjusted accordingly.
GWENHYWFAR_API int GWEN_Buffer_Relinquish | ( | GWEN_BUFFER * | bf | ) |
This function relinquishes ownership of the internal buffer if possible. It returns an error if this object does not own the buffer (it can't give you what it doesn't possess) or if the internal pointer to the memory allocated does not match the internal pointer to the current start of the buffer (this can be the case when GWEN_Buffer_ReserveBytes() of GWEN_Buffer_Crop() have been called).
GWENHYWFAR_API int GWEN_Buffer_RemoveRoom | ( | GWEN_BUFFER * | bf, |
uint32_t | size | ||
) |
This function removes the given number of bytes at the current buffer position. It moves any existing bytes behind the area to be removed to the current position. The position pointer will not be altered, but after calling this function at that position there is the begin of the data behind the removed area. All pointers obtained from this module (e.g. via GWEN_Buffer_GetStart) become invalid ! This function updates the bookmarks accordingly.
GWENHYWFAR_API int GWEN_Buffer_ReplaceBytes | ( | GWEN_BUFFER * | bf, |
uint32_t | rsize, | ||
const char * | buffer, | ||
uint32_t | size | ||
) |
This function remplaces the given number of bytes at the current buffer position with some new bytes. If the number of bytes to be replaced does not equal the number of replacement bytes then the buffer is resized accordingly (e.g. shrunk or extended). The position pointer will not be altered. All pointers obtained from this module (e.g. via GWEN_Buffer_GetStart) become invalid ! This function updates the bookmarks accordingly.
GWENHYWFAR_API int GWEN_Buffer_ReserveBytes | ( | GWEN_BUFFER * | bf, |
uint32_t | 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 void GWEN_Buffer_SetBookmark | ( | GWEN_BUFFER * | bf, |
unsigned int | idx, | ||
uint32_t | 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, |
uint32_t | 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, |
uint32_t | mode | ||
) |
Changes the current mode of the buffer (such as GWEN_BUFFER_MODE_DYNAMIC).
GWENHYWFAR_API int GWEN_Buffer_SetPos | ( | GWEN_BUFFER * | bf, |
uint32_t | i | ||
) |
GWENHYWFAR_API void GWEN_Buffer_SetStep | ( | GWEN_BUFFER * | bf, |
uint32_t | 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 void GWEN_Buffer_SubMode | ( | GWEN_BUFFER * | bf, |
uint32_t | mode | ||
) |
Removes the give mode from the current mode of the buffer (such as GWEN_BUFFER_MODE_DYNAMIC).
GWENHYWFAR_API GWEN_FORMAT | ( | printf | , |
2 | , | ||
3 | |||
) |
Append strings to buffer with a sprintf()-like argument list.
bf | buffer to append data to |
fmt | format string like that for sprintf(). |
GWENHYWFAR_API const char* fmt |