libcoap  4.2.1
str.c
Go to the documentation of this file.
1 /* str.c -- strings to be used in the CoAP library
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "coap_internal.h"
10 
11 #include <stdio.h>
12 
14  coap_string_t *s =
16  if ( !s ) {
17  coap_log(LOG_CRIT, "coap_new_string: malloc: failed\n");
18  return NULL;
19  }
20 
21  memset(s, 0, sizeof(coap_string_t));
22  s->s = ((unsigned char *)s) + sizeof(coap_string_t);
23  s->s[size] = '\000';
24  return s;
25 }
26 
29 }
30 
31 coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size) {
32  coap_string_t *s = coap_new_string(size);
33  if (!s)
34  return NULL;
35  memcpy (s->s, data, size);
36  s->length = size;
37  return (coap_str_const_t *)s;
38 }
39 
42 }
43 
45 {
46  static int ofs = 0;
48  if (++ofs == COAP_MAX_STR_CONST_FUNC) ofs = 0;
49  var[ofs].length = strlen(string);
50  var[ofs].s = (const uint8_t *)string;
51  return &var[ofs];
52 }
53 
size_t length
length of string
Definition: str.h:34
uint8_t * s
string data
Definition: str.h:27
Coap string data definition.
Definition: str.h:25
Coap string data definition with const data.
Definition: str.h:33
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_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
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:27
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:40
struct coap_string_t coap_string_t
Coap string data definition.
size_t length
length of string
Definition: str.h:26
const uint8_t * s
string data
Definition: str.h:35
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.
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:129
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
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
unsigned char uint8_t
Definition: uthash.h:79
Critical.
Definition: coap_debug.h:50
#define COAP_MAX_STR_CONST_FUNC
Definition: str.h:84
Pulls together all the internal only header files.