membuffer.h
Go to the documentation of this file.
/*******************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
* Copyright (c) 2012 France Telecom All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#ifndef GENLIB_UTIL_MEMBUFFER_H
#define GENLIB_UTIL_MEMBUFFER_H
#include "upnputil.h"
#include <stdlib.h>
#define MINVAL(a, b) ((a) < (b) ? (a) : (b))
#define MAXVAL(a, b) ((a) > (b) ? (a) : (b))
typedef struct
{
char *buf;
size_t length;
} memptr;
typedef struct
{
char *buf;
size_t length;
size_t capacity;
size_t size_inc;
#define MEMBUF_DEF_SIZE_INC (size_t)5
} membuffer;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
char *str_alloc(
const char *str,
size_t str_len);
int memptr_cmp(
memptr *m,
const char *s);
int memptr_cmp_nocase(
memptr *m,
const char *s);
int membuffer_set_size(
membuffer *m,
size_t new_length);
void membuffer_init(
membuffer *m);
void membuffer_destroy(
membuffer *m);
int membuffer_assign(
membuffer *m,
const void *buf,
size_t buf_len);
int membuffer_assign_str(
membuffer *m,
const char *c_str);
int membuffer_append(
membuffer *m,
const void *buf,
size_t buf_len);
int membuffer_append_str(
membuffer *m,
const char *c_str);
int membuffer_insert(
membuffer *m,
const void *buf,
size_t buf_len,
size_t index);
void membuffer_delete(
membuffer *m,
size_t index,
size_t num_bytes);
/*
* \brief Detaches current buffer and returns it. The caller must free the
* returned buffer using free(). After this call, length becomes 0.
*
* \return A pointer to the current buffer.
*/
char *membuffer_detach(
membuffer *m);
/*
* \brief Free existing memory in membuffer and assign the new buffer in its
* place.
*
* \note 'new_buf' must be allocted using malloc or realloc so that it can be
* freed using free().
*/
void membuffer_attach(
membuffer *m,
char *new_buf,
size_t buf_len);
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
#endif /* GENLIB_UTIL_MEMBUFFER_H */
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,...
Definition membuffer.c:66
membuffer_destroy
void membuffer_destroy(membuffer *m)
Free's memory allocated for membuffer* m.
Definition membuffer.c:165
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.
Definition membuffer.c:107
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.
Definition membuffer.c:51
membuffer_attach
void membuffer_attach(membuffer *m, char *new_buf, size_t buf_len)
Definition membuffer.c:292
membuffer_init
void membuffer_init(membuffer *m)
Wrapper to membuffer_initialize().
Definition membuffer.c:157
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.
Definition membuffer.c:205
membuffer_assign_str
int membuffer_assign_str(membuffer *m, const char *c_str)
Wrapper function for membuffer_assign().
Definition membuffer.c:200
membuffer_detach
char * membuffer_detach(membuffer *m)
Definition membuffer.c:278
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.
Definition membuffer.c:212
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.
Definition membuffer.c:81
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 ...
Definition membuffer.c:217
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.
Definition membuffer.c:175
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....
Definition membuffer.c:245
membuffer
Definition membuffer.h:58
membuffer::size_inc
size_t size_inc
Definition membuffer.h:66
membuffer::capacity
size_t capacity
Definition membuffer.h:64
membuffer::length
size_t length
Definition membuffer.h:62
membuffer::buf
char * buf
Definition membuffer.h:60
memptr
Definition membuffer.h:48
memptr::length
size_t length
Definition membuffer.h:52
memptr::buf
char * buf
Definition membuffer.h:50
upnputil.h