libcoap  4.2.0rc2
str.h
Go to the documentation of this file.
1 /*
2  * str.h -- strings to be used in the CoAP library
3  *
4  * Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
10 #ifndef _COAP_STR_H_
11 #define _COAP_STR_H_
12 
13 #include <string.h>
14 
15 
25 typedef struct coap_string_t {
26  size_t length;
29 
33 typedef struct coap_str_const_t {
34  size_t length;
35  const uint8_t *s;
37 
43 
44 #define COAP_SET_STR(st,l,v) { (st)->length = (l), (st)->s = (v); }
45 
49 typedef struct coap_binary_t {
50  size_t length;
53 
62 coap_string_t *coap_new_string(size_t size);
63 
69 void coap_delete_string(coap_string_t *string);
70 
81 coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size);
82 
89 
99 #ifdef __cplusplus
100 namespace libcoap {
101  struct CoAPStrConst : coap_str_const_t {
102  operator coap_str_const_t *() { return this; }
103  };
104 }
105 #define coap_make_str_const(CStr) \
106  libcoap::CoAPStrConst{sizeof(CStr)-1, reinterpret_cast<const uint8_t *>(CStr)}
107 #else /* __cplusplus */
108 #define coap_make_str_const(string) \
109  (&(coap_str_const_t){sizeof(string)-1,(const uint8_t *)(string)})
110 #endif /* __cplusplus */
111 
121 #define coap_string_equal(string1,string2) \
122  ((string1)->length == (string2)->length && ((string1)->length == 0 || \
123  memcmp((string1)->s, (string2)->s, (string1)->length) == 0))
124 
127 #endif /* _COAP_STR_H_ */
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:18
struct coap_binary_t coap_binary_t
Coap binary data definition.
#define COAP_DEPRECATED
Definition: libcoap.h:49
struct coap_str_const_t coap_str_const_t
Coap string data definition with const data.
Coap binary data definition.
Definition: str.h:49
void coap_delete_string(coap_string_t *string)
Deletes the given string and releases any memory allocated.
Definition: str.c:34
void coap_delete_str_const(coap_str_const_t *string)
Deletes the given const string and releases any memory allocated.
Definition: str.c:47
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
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:38
size_t length
length of binary data
Definition: str.h:50
unsigned char uint8_t
Definition: uthash.h:79
uint8_t * s
binary data
Definition: str.h:51
COAP_DEPRECATED typedef coap_string_t str
Definition: str.h:42