libcoap  4.3.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 
22 /*
23  * Note: string and binary use equivalent objects.
24  * string is likely to contain readable textual information, binary will not.
25  */
26 
30 typedef struct coap_string_t {
31  size_t length;
32  uint8_t *s;
34 
38 typedef struct coap_str_const_t {
39  size_t length;
40  const uint8_t *s;
42 
43 #define COAP_SET_STR(st,l,v) { (st)->length = (l), (st)->s = (v); }
44 
48 typedef struct coap_binary_t {
49  size_t length;
50  uint8_t *s;
52 
56 typedef struct coap_bin_const_t {
57  size_t length;
58  const uint8_t *s;
60 
71 coap_string_t *coap_new_string(size_t size);
72 
78 void coap_delete_string(coap_string_t *string);
79 
90 coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size);
91 
98 
109 coap_binary_t *coap_new_binary(size_t size);
110 
116 void coap_delete_binary(coap_binary_t *binary);
117 
131 coap_binary_t *coap_resize_binary(coap_binary_t *binary, size_t new_size);
132 
144 coap_bin_const_t *coap_new_bin_const(const uint8_t *data, size_t size);
145 
152 
153 #ifndef COAP_MAX_STR_CONST_FUNC
154 #define COAP_MAX_STR_CONST_FUNC 2
155 #endif /* COAP_MAX_STR_CONST_FUNC */
156 
170 coap_str_const_t *coap_make_str_const(const char *string);
171 
181 #define coap_string_equal(string1,string2) \
182  ((string1)->length == (string2)->length && ((string1)->length == 0 || \
183  ((string1)->s && (string2)->s && \
184  memcmp((string1)->s, (string2)->s, (string1)->length) == 0)))
185 
195 #define coap_binary_equal(binary1,binary2) \
196  ((binary1)->length == (binary2)->length && ((binary1)->length == 0 || \
197  ((binary1)->s && (binary2)->s && \
198  memcmp((binary1)->s, (binary2)->s, (binary1)->length) == 0)))
199 
202 #endif /* COAP_STR_H_ */
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
void coap_delete_bin_const(coap_bin_const_t *binary)
Deletes the given const binary data and releases any memory allocated.
Definition: str.c:102
void coap_delete_str_const(coap_str_const_t *string)
Deletes the given const string and releases any memory allocated.
Definition: str.c:51
struct coap_str_const_t coap_str_const_t
CoAP string data definition with const data.
void coap_delete_binary(coap_binary_t *binary)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition: str.c:89
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition: str.c:93
struct coap_binary_t coap_binary_t
CoAP binary data definition.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition: str.c:65
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:42
struct coap_string_t coap_string_t
CoAP string data definition.
struct coap_bin_const_t coap_bin_const_t
CoAP binary data definition with const data.
void coap_delete_string(coap_string_t *string)
Deletes the given string and releases any memory allocated.
Definition: str.c:38
coap_binary_t * coap_resize_binary(coap_binary_t *binary, size_t new_size)
Resizes the given coap_binary_t object.
Definition: str.c:69
coap_str_const_t * coap_make_str_const(const char *string)
Take the specified byte array (text) and create a coap_str_const_t *.
Definition: str.c:55
CoAP binary data definition with const data.
Definition: str.h:56
size_t length
length of binary data
Definition: str.h:57
const uint8_t * s
read-only binary data
Definition: str.h:58
CoAP binary data definition.
Definition: str.h:48
size_t length
length of binary data
Definition: str.h:49
uint8_t * s
binary data
Definition: str.h:50
CoAP string data definition with const data.
Definition: str.h:38
const uint8_t * s
read-only string data
Definition: str.h:40
size_t length
length of string
Definition: str.h:39
CoAP string data definition.
Definition: str.h:30
uint8_t * s
string data
Definition: str.h:32
size_t length
length of string
Definition: str.h:31