libcoap  4.2.0
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 
38 #define COAP_SET_STR(st,l,v) { (st)->length = (l), (st)->s = (v); }
39 
43 typedef struct coap_binary_t {
44  size_t length;
47 
56 coap_string_t *coap_new_string(size_t size);
57 
63 void coap_delete_string(coap_string_t *string);
64 
75 coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size);
76 
83 
93 #ifdef __cplusplus
94 namespace libcoap {
95  struct CoAPStrConst : coap_str_const_t {
96  operator coap_str_const_t *() { return this; }
97  };
98 }
99 #define coap_make_str_const(CStr) \
100  libcoap::CoAPStrConst{sizeof(CStr)-1, reinterpret_cast<const uint8_t *>(CStr)}
101 #else /* __cplusplus */
102 #define coap_make_str_const(string) \
103  (&(coap_str_const_t){sizeof(string)-1,(const uint8_t *)(string)})
104 #endif /* __cplusplus */
105 
115 #define coap_string_equal(string1,string2) \
116  ((string1)->length == (string2)->length && ((string1)->length == 0 || \
117  memcmp((string1)->s, (string2)->s, (string1)->length) == 0))
118 
121 #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.
struct coap_str_const_t coap_str_const_t
Coap string data definition with const data.
Coap binary data definition.
Definition: str.h:43
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:44
unsigned char uint8_t
Definition: uthash.h:79
uint8_t * s
binary data
Definition: str.h:45