HomePort
value_api.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Aalborg University. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Aalborg University ''AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Aalborg University OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  */
27 
28 #include "hpd/hpd_api.h"
29 #include "value.h"
30 #include "log.h"
31 #include <stdarg.h>
32 
33 hpd_error_t hpd_value_alloc(hpd_value_t **value, const char *body, int len)
34 {
35  if (!value) LOG_RETURN_E_NULL();
36  if (len < 0 && len != HPD_NULL_TERMINATED)
37  LOG_RETURN(HPD_E_ARGUMENT, "len must be >= 0 or HPD_NULL_TERMINATED.", __func__);
38  return value_alloc(value, body, len);
39 }
40 
42 {
43  if (!value) LOG_RETURN_E_NULL();
44  va_list vp;
45  va_start(vp, fmt);
46  return value_vallocf(value, fmt, vp);
47  va_end(vp);
48 }
49 
50 hpd_error_t hpd_value_vallocf(hpd_value_t **value, const char *fmt, va_list vp)
51 {
52  if (!value) LOG_RETURN_E_NULL();
53  return value_vallocf(value, fmt, vp);
54 }
55 
57 {
58  if (!dst || ! src) LOG_RETURN_E_NULL();
59  return value_copy(dst, src);
60 }
61 
63 {
64  if (!value) LOG_RETURN_E_NULL();
65  return value_free(value);
66 }
67 
68 hpd_error_t hpd_value_set_header(hpd_value_t *value, const char *key, const char *val)
69 {
70  if (!value || !key) LOG_RETURN_E_NULL();
71  if (key[0] == '_') LOG_RETURN(HPD_E_ARGUMENT, "Keys starting with '_' is reserved for generated headers");
72  return value_set_header(value, key, val);
73 }
74 
76 {
77  if (!value) LOG_RETURN_E_NULL();
78 
79  va_list vp;
80  va_start(vp, value);
81  hpd_error_t rc = value_set_headers_v(value, vp);
82  va_end(vp);
83 
84  return rc;
85 }
86 
87 hpd_error_t hpd_value_get_body(const hpd_value_t *value, const char **body, size_t *len)
88 {
89  if (!value || (!body && !len)) LOG_RETURN_E_NULL();
90  return value_get_body(value, body, len);
91 }
92 
93 hpd_error_t hpd_value_get_header(const hpd_value_t *value, const char *key, const char **val)
94 {
95  if (!value || !key || !val) LOG_RETURN_E_NULL();
96  return value_get_header(value, key, val);
97 }
98 
100 {
101  if (!value) LOG_RETURN_E_NULL();
102 
103  va_list vp;
104  va_start(vp, value);
105  hpd_error_t rc = value_get_headers_v(value, vp);
106  va_end(vp);
107 
108  return rc;
109 }
110 
112 {
113  if (!value || !pair) LOG_RETURN_E_NULL();
114  return value_first_header(value, pair);
115 }
116 
118 {
119  if (!pair || !*pair) LOG_RETURN_E_NULL();
120  return value_next_header(pair);
121 }
hpd_error_t value_copy(hpd_value_t **dst, const hpd_value_t *src)
Definition: value.c:80
#define LOG_RETURN_E_NULL()
Definition: log.h:50
hpd_error_t hpd_value_copy(hpd_value_t **dst, const hpd_value_t *src)
Definition: value_api.c:56
hpd_error_t hpd_value_set_headers(hpd_value_t *value,...)
Definition: value_api.c:75
data value
hpd_error_t hpd_value_free(hpd_value_t *value)
Definition: value_api.c:62
Definition: map.c:34
hpd_error_t hpd_value_get_headers(const hpd_value_t *value,...)
Definition: value_api.c:99
hpd_error_t value_set_headers_v(hpd_value_t *value, va_list vp)
Definition: value.c:115
#define HPD_NULL_TERMINATED
Value to be used in len parameters on \0 terminated strings.
Definition: hpd_types.h:62
hpd_error_t value_get_headers_v(const hpd_value_t *value, va_list vp)
Definition: value.c:140
hpd_error_t hpd_value_first_header(const hpd_value_t *value, hpd_pair_t **pair)
Definition: value_api.c:111
hpd_error_t hpd_value_alloc(hpd_value_t **value, const char *body, int len)
[Browsing foreach loops]
Definition: value_api.c:33
hpd_error_t value_get_header(const hpd_value_t *value, const char *key, const char **val)
Definition: value.c:135
data key
hpd_error_t hpd_value_next_header(hpd_pair_t **pair)
Definition: value_api.c:117
Definition: comm.h:70
hpd_error_t value_alloc(hpd_value_t **value, const char *body, int len)
Definition: value.c:34
enum hpd_error hpd_error_t
Definition: hpd_types.h:167
hpd_error_t hpd_value_get_header(const hpd_value_t *value, const char *key, const char **val)
Definition: value_api.c:93
#define LOG_RETURN(E, FMT,...)
Definition: log.h:48
hpd_error_t value_vallocf(hpd_value_t **value, const char *fmt, va_list vp)
Definition: value.c:55
hpd_error_t hpd_value_allocf(hpd_value_t **value, const char *fmt,...)
Definition: value_api.c:41
hpd_error_t value_next_header(hpd_pair_t **pair)
Definition: value.c:158
hpd_error_t value_free(hpd_value_t *value)
Definition: value.c:99
hpd_error_t value_get_body(const hpd_value_t *value, const char **body, size_t *len)
Definition: value.c:128
hpd_error_t hpd_value_get_body(const hpd_value_t *value, const char **body, size_t *len)
Definition: value_api.c:87
hpd_error_t value_set_header(hpd_value_t *value, const char *key, const char *val)
Definition: value.c:110
hpd_error_t hpd_value_vallocf(hpd_value_t **value, const char *fmt, va_list vp)
Definition: value_api.c:50
hpd_error_t hpd_value_set_header(hpd_value_t *value, const char *key, const char *val)
Definition: value_api.c:68
hpd_error_t value_first_header(const hpd_value_t *value, hpd_pair_t **pair)
Definition: value.c:153