• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 /**
17  * @defgroup string String handling support
18  * API functions for handling strings
19  * @{
20  */
21 
22 /**
23  * Coap string data definition
24  */
25 typedef struct coap_string_t {
26   size_t length;    /**< length of string */
27   uint8_t *s;       /**< string data */
28 } coap_string_t;
29 
30 /**
31  * Coap string data definition with const data
32  */
33 typedef struct coap_str_const_t {
34   size_t length;    /**< length of string */
35   const uint8_t *s; /**< string data */
36 } coap_str_const_t;
37 
38 #define COAP_SET_STR(st,l,v) { (st)->length = (l), (st)->s = (v); }
39 
40 /**
41  * Coap binary data definition
42  */
43 typedef struct coap_binary_t {
44   size_t length;    /**< length of binary data */
45   uint8_t *s;       /**< binary data */
46 } coap_binary_t;
47 
48 /**
49  * Returns a new string object with at least size+1 bytes storage allocated.
50  * The string must be released using coap_delete_string().
51  *
52  * @param size The size to allocate for the binary string data.
53  *
54  * @return       A pointer to the new object or @c NULL on error.
55  */
56 coap_string_t *coap_new_string(size_t size);
57 
58 /**
59  * Deletes the given string and releases any memory allocated.
60  *
61  * @param string The string to free off.
62  */
63 void coap_delete_string(coap_string_t *string);
64 
65 /**
66  * Returns a new const string object with at least size+1 bytes storage
67  * allocated, and the provided data copied into the string object.
68  * The string must be released using coap_delete_str_const().
69  *
70  * @param data The data to put in the new string object.
71  * @param size The size to allocate for the binary string data.
72  *
73  * @return       A pointer to the new object or @c NULL on error.
74  */
75 coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size);
76 
77 /**
78  * Deletes the given const string and releases any memory allocated.
79  *
80  * @param string The string to free off.
81  */
82 void coap_delete_str_const(coap_str_const_t *string);
83 
84 #define COAP_MAX_STR_CONST_FUNC 2
85 /**
86  * Take the specified string and create a coap_str_const_t *
87  *
88  * Note: the array is 2 deep as there are up to two callings of
89  * coap_make_str_const in a function call. e.g. coap_add_attr().
90  * Caution: If there are local variable assignments, these will cycle around
91  * the var[COAP_MAX_STR_CONST_FUNC] set.  No current examples do this.
92  *
93  * @param string The const string to convert to a coap_str_const_t *
94  *
95  * @return       A pointer to one of two static variables containing the
96  *               coap_str_const_t * result
97  */
98 coap_str_const_t *coap_make_str_const(const char *string);
99 
100 /**
101  * Compares the two strings for equality
102  *
103  * @param string1 The first string.
104  * @param string2 The second string.
105  *
106  * @return         @c 1 if the strings are equal
107  *                 @c 0 otherwise.
108  */
109 #define coap_string_equal(string1,string2) \
110         ((string1)->length == (string2)->length && ((string1)->length == 0 || \
111          memcmp((string1)->s, (string2)->s, (string1)->length) == 0))
112 
113 /** @} */
114 
115 #endif /* COAP_STR_H_ */
116