membuffer.h File Reference
`#include "upnputil.h"`
`#include <stdlib.h>`
Include dependency graph for membuffer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures

struct | memptr
struct | membuffer

## Macros

#define | MINVAL(a, b) ((a) < (b) ? (a) : (b))
#define | MAXVAL(a, b) ((a) > (b) ? (a) : (b))
#define | MEMBUF_DEF_SIZE_INC (size_t)5

## Functions

char * | str_alloc (const char *str, size_t str_len)
| Allocate memory and copy information from the input string to the newly allocated memory.
int | memptr_cmp (memptr *m, const char *s)
| Compares characters of strings passed for number of bytes. If equal for the number of bytes, the length of the bytes determines which buffer is shorter.
int | memptr_cmp_nocase (memptr *m, const char *s)
| Compares characters of 2 strings irrespective of the case for a specific count of bytes.
int | membuffer_set_size (membuffer *m, size_t new_length)
| Increases or decreases buffer cap so that at least 'new_length' bytes can be stored.
void | membuffer_init (membuffer *m)
| Wrapper to membuffer_initialize().
void | membuffer_destroy (membuffer *m)
| Free's memory allocated for membuffer* m.
int | membuffer_assign (membuffer *m, const void *buf, size_t buf_len)
| Allocate memory to membuffer *m and copy the contents of the in parameter const void *buf.
int | membuffer_assign_str (membuffer *m, const char *c_str)
| Wrapper function for membuffer_assign().
int | membuffer_append (membuffer *m, const void *buf, size_t buf_len)
| Invokes function to appends data from a constant buffer to the buffer.
int | membuffer_append_str (membuffer *m, const char *c_str)
| Invokes function to appends data from a constant string to the buffer.
int | membuffer_insert (membuffer *m, const void *buf, size_t buf_len, size_t index)
| Allocates memory for the new data to be inserted. Does memory management by moving the data from the existing memory to the newly allocated memory and then appending the new data.
void | membuffer_delete (membuffer *m, size_t index, size_t num_bytes)
| Shrink the size of the buffer depending on the current size of the bufer and te input parameters. Move contents from the old buffer to the new sized buffer.
char * | membuffer_detach (membuffer *m)
void | membuffer_attach (membuffer *m, char *new_buf, size_t buf_len)

## Macro Definition Documentation

## MEMBUF_DEF_SIZE_INC

#define MEMBUF_DEF_SIZE_INC (size_t)5

default value of size_inc.

## Function Documentation

## membuffer_append()

int membuffer_append | ( | membuffer * | m,
|  | const void * | buf,
|  | size_t | buf_len
| ) |  |

Invokes function to appends data from a constant buffer to the buffer.

Returns
  int.

Parameters

  [in,out] | m | Buffer whose memory is to be appended.
  [in] | buf | Source buffer whose contents will be copied.
  [in] | buf_len | Length of the source buffer.

References membuffer::length.

## membuffer_append_str()

int membuffer_append_str | ( | membuffer * | m,
|  | const char * | c_str
| ) |  |

Invokes function to appends data from a constant string to the buffer.

Returns
  int.

Parameters

  [in,out] | m | Buffer whose memory is to be appended.
  [in] | c_str | Source buffer whose contents will be copied.

References membuffer::length.

## membuffer_assign()

int membuffer_assign | ( | membuffer * | m,
|  | const void * | buf,
|  | size_t | buf_len
| ) |  |

Allocate memory to membuffer *m and copy the contents of the in parameter const void *buf.

Returns
  - UPNP_E_SUCCESS
  - UPNP_E_OUTOF_MEMORY

Parameters

  [in,out] | m | Buffer whose memory is to be allocated and assigned.
  [in] | buf | Source buffer whose contents will be copied.
  [in] | buf_len | Length of the source buffer.

References membuffer::buf, and membuffer::length.

## membuffer_assign_str()

int membuffer_assign_str | ( | membuffer * | m,
|  | const char * | c_str
| ) |  |

Wrapper function for membuffer_assign().

Returns
  - UPNP_E_SUCCESS
  - UPNP_E_OUTOF_MEMORY

Parameters

  [in,out] | m | Buffer to be allocated and assigned.
  [in] | c_str | Source buffer whose contents will be copied.

## membuffer_attach()

void membuffer_attach | ( | membuffer * | m,
|  | char * | new_buf,
|  | size_t | buf_len
| ) |  |

Parameters

  [in,out] | m | Buffer to be updated.
  [in] | new_buf | Source buffer which will be assigned to the buffer to be updated.
  [in] | buf_len | Length of the source buffer.

References membuffer::buf, membuffer::capacity, and membuffer::length.

## membuffer_delete()

void membuffer_delete | ( | membuffer * | m,
|  | size_t | index,
|  | size_t | num_bytes
| ) |  |

Shrink the size of the buffer depending on the current size of the bufer and te input parameters. Move contents from the old buffer to the new sized buffer.

Parameters

  [in,out] | m | Buffer whose memory size is to be decreased and copied to the modified location.
  [in] | index | Index to determine bounds while moving data.
  [in] | num_bytes | Number of bytes that the data needs to shrink by.

References membuffer::buf, and membuffer::length.

## membuffer_destroy()

void membuffer_destroy | ( | membuffer * | m | ) |

Free's memory allocated for membuffer* m.

Parameters

  [in,out] | m | Buffer to be destroyed.

References membuffer::buf.

## membuffer_detach()

char * membuffer_detach | ( | membuffer * | m | ) |

Parameters

  [in,out] | m | Buffer to be returned and updated.

References membuffer::buf.

## membuffer_init()

void membuffer_init | ( | membuffer * | m | ) |

Wrapper to membuffer_initialize().

Set the size of the buffer to MEMBUF_DEF_SIZE_INC and Initializes m->buf to NULL, length = 0.

Parameters

  [in,out] | m | Buffer to be initialized.

References MEMBUF_DEF_SIZE_INC, and membuffer::size_inc.

## membuffer_insert()

int membuffer_insert | ( | membuffer * | m,
|  | const void * | buf,
|  | size_t | buf_len,
|  | size_t | index
| ) |  |

Allocates memory for the new data to be inserted. Does memory management by moving the data from the existing memory to the newly allocated memory and then appending the new data.

Returns
  0 if successful, error code if error.

Parameters

  [in,out] | m | Buffer whose memory size is to be increased and appended.
  [in] | buf | source buffer whose contents will be copied.
  [in] | buf_len | size of the source buffer.
  [in] | index | index to determine the bounds while movinf the data.

References membuffer::buf, and membuffer::length.

## membuffer_set_size()

int membuffer_set_size | ( | membuffer * | m,
|  | size_t | new_length
| ) |  |

Increases or decreases buffer cap so that at least 'new_length' bytes can be stored.

Returns
  - UPNP_E_SUCCESS - On Success
  - UPNP_E_OUTOF_MEMORY - On failure to allocate memory.

Parameters

  [in,out] | m | buffer whose size is to be modified.
  [in] | new_length | new size to which the buffer will be modified.

References membuffer::buf, membuffer::capacity, membuffer::length, membuffer::size_inc, and UPNP_E_OUTOF_MEMORY.

## memptr_cmp()

int memptr_cmp | ( | memptr * | m,
|  | const char * | s
| ) |  |

Compares characters of strings passed for number of bytes. If equal for the number of bytes, the length of the bytes determines which buffer is shorter.

Returns
  - < 0 string1 substring less than string2 substring
  - == 0 string1 substring identical to string2 substring
  - > 0 string1 substring greater than string2 substring

Parameters

  [in] | m | Input memory object.
  [in] | s | Constatnt string for the memory object to be compared with.

References memptr::buf, and memptr::length.

## memptr_cmp_nocase()

int memptr_cmp_nocase | ( | memptr * | m,
|  | const char * | s
| ) |  |

Compares characters of 2 strings irrespective of the case for a specific count of bytes.

If the character comparison is the same the length of the 2 srings determines the shorter of the 2 strings.

Returns
  - < 0 string1 substring less than string2 substring
  - == 0 string1 substring identical to string2 substring
  - > 0 string1 substring greater than string2 substring

Parameters

  [in] | m | Input memory object.
  [in] | s | Constatnt string for the memory object to be compared with.

References memptr::buf, and memptr::length.

## str_alloc()

char * str_alloc | ( | const char * | str,
|  | size_t | str_len
| ) |  |

Allocate memory and copy information from the input string to the newly allocated memory.

Returns
  Pointer to the newly allocated memory. NULL if memory cannot be allocated.

Parameters

  [in] | str | Input string object.
  [in] | str_len | Input string length.