str.c
Go to the documentation of this file.
/* str.c -- strings to be used in the CoAP library
*
* Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap_internal.h"
#include <stdio.h>
coap_string_t *coap_new_string(size_t size) {
coap_string_t *s =
(coap_string_t *)coap_malloc_type(COAP_STRING, sizeof(coap_string_t) + size + 1);
if ( !s ) {
coap_log(LOG_CRIT, "coap_new_string: malloc: failed\n");
return NULL;
}
memset(s, 0, sizeof(coap_string_t));
s->s = ((unsigned char *)s) + sizeof(coap_string_t);
s->s[size] = '\000';
return s;
}
void coap_delete_string(coap_string_t *s) {
coap_free_type(COAP_STRING, s);
}
coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size) {
coap_string_t *s = coap_new_string(size);
if (!s)
return NULL;
memcpy (s->s, data, size);
s->length = size;
return (coap_str_const_t *)s;
}
void coap_delete_str_const(coap_str_const_t *s) {
coap_free_type(COAP_STRING, s);
}
coap_str_const_t *coap_make_str_const(const char *string)
{
static int ofs = 0;
static coap_str_const_t var[COAP_MAX_STR_CONST_FUNC];
if (++ofs == COAP_MAX_STR_CONST_FUNC) ofs = 0;
var[ofs].length = strlen(string);
var[ofs].s = (const uint8_t *)string;
return &var[ofs];
}
coap_str_const_t::length
size_t length
length of string
Definition: str.h:34
coap_string_t::s
uint8_t * s
string data
Definition: str.h:27
coap_string_t
Coap string data definition.
Definition: str.h:25
coap_str_const_t
Coap string data definition with const data.
Definition: str.h:33
coap_new_string
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition: str.c:13
coap_make_str_const
coap_str_const_t * coap_make_str_const(const char *string)
Take the specified string and create a coap_str_const_t *.
Definition: str.c:44
coap_delete_string
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:27
coap_delete_str_const
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:40
coap_string_t
struct coap_string_t coap_string_t
Coap string data definition.
coap_string_t::length
size_t length
length of string
Definition: str.h:26
coap_str_const_t::s
const uint8_t * s
string data
Definition: str.h:35
coap_malloc_type
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
COAP_STRING
Definition: mem.h:30
coap_log
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:129
coap_new_str_const
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
Definition: str.c:31
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t
unsigned char uint8_t
Definition: uthash.h:79
LOG_CRIT
Critical.
Definition: coap_debug.h:50
COAP_MAX_STR_CONST_FUNC
#define COAP_MAX_STR_CONST_FUNC
Definition: str.h:84
coap_internal.h
Pulls together all the internal only header files.