tinyows 1.2.2
array.c
Go to the documentation of this file.
1/*
2 Copyright (c) <2007-2012> <Barbara Philippot - Olivier Courtin>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 IN THE SOFTWARE.
21*/
22
23
24#include <stdlib.h>
25#include <stdio.h> /* FILE */
26#include <string.h> /* strncmp */
27#include <limits.h>
28#include <assert.h>
29
30#include "../ows/ows.h"
31
32
33/*
34 * Initialize an array structure
35 */
37{
38 array *arr = NULL;
39
40 arr = malloc(sizeof(array));
41 assert(arr);
42
43 arr->first = NULL;
44 arr->last = NULL;
45
46 return arr;
47}
48
49
50/*
51 * Free an array structure
52 */
54{
55 array_node *an = NULL;
56 array_node *an_to_free = NULL;
57
58 assert(a);
59
60 for (an = a->first ; an ; /* empty */) {
61 an_to_free = an;
62 an = an->next;
63
64 buffer_free(an_to_free->key);
65 buffer_free(an_to_free->value);
66 free(an_to_free);
67 an_to_free = NULL;
68 }
69
70 free(a);
71 a = NULL;
72}
73
74
75/*
76 * Add a given buffer to the end of an array
77 * Carefull key and value are passed by reference
78 * and will be released by array_free !
79 */
80void array_add(array * a, buffer * key, buffer * value)
81{
82 array_node *an;
83
84 assert(a);
85 assert(key);
86 assert(value);
87
88 an = malloc(sizeof(array_node));
89 assert(an);
90
91 an->key = key;
92 an->value = value;
93
94 if (!a->first) a->first = an;
95 else a->last->next = an;
96
97 a->last = an;
98 a->last->next = NULL;
99}
100
101
102/*
103 * Check if a given key string is or not in the array
104 */
105bool array_is_key(const array * a, const char *key)
106{
107 array_node *an;
108 size_t ks;
109
110 assert(a);
111 assert(key);
112
113 for (ks = strlen(key), an = a->first ; an ; an = an->next)
114 if (ks == an->key->use)
115 if (buffer_case_cmp(an->key, key))
116 return true;
117
118 return false;
119}
120
121
122/*
123 * Check if a given value string is or not in the array
124 */
125bool array_is_value(const array * a, const char *value)
126{
127 array_node *an;
128 size_t vs;
129
130 assert(a);
131 assert(value);
132
133 for (vs = strlen(value), an = a->first ; an ; an = an->next)
134 if (vs == an->value->use)
135 if (buffer_case_cmp(an->value, value))
136 return true;
137
138 return false;
139}
140
141
142/*
143 * Return a value buffer from an array (from key value)
144 * You must be sure key is defined for this array, see is_key() above
145 * Carreful return a reference on the array buf itself !
146 */
147buffer *array_get(const array * a, const char *key)
148{
149 array_node *an;
150 size_t ks;
151
152 assert(a);
153 assert(key);
154
155 for (ks = strlen(key), an = a->first ; an ; an = an->next) {
156 if (ks == an->key->use)
157 if (buffer_case_cmp(an->key, key))
158 break;
159 }
160
161 assert(an);
162
163 return an->value;
164}
165
166
167/*
168 * Return a key buffer from an array (from value)
169 */
170buffer *array_get_key(const array * a, const char *value)
171{
172 array_node *an;
173 size_t vs;
174
175 assert(a);
176 assert(value);
177
178 for (vs = strlen(value), an = a->first ; an ; an = an->next) {
179 if (vs == an->value->use)
180 if (buffer_case_cmp(an->value, value))
181 break;
182 }
183
184 assert(an);
185
186 return an->key;
187}
188
189
190#ifdef OWS_DEBUG
191/*
192 * Flush an array to a given file
193 * (mainly to debug purpose)
194 */
195void array_flush(const array * a, FILE * output)
196{
197 array_node *an;
198
199 assert(a);
200 assert(output);
201
202 for (an = a->first ; an ; an = an->next) {
203 fprintf(output, "[");
204 buffer_flush(an->key, output);
205 fprintf(output, "] -> ");
206 buffer_flush(an->value, output);
207 fprintf(output, "\n");
208 }
209}
210#endif
bool array_is_value(const array *a, const char *value)
Definition array.c:125
buffer * array_get_key(const array *a, const char *value)
Definition array.c:170
buffer * array_get(const array *a, const char *key)
Definition array.c:147
void array_free(array *a)
Definition array.c:53
bool array_is_key(const array *a, const char *key)
Definition array.c:105
void array_add(array *a, buffer *key, buffer *value)
Definition array.c:80
array * array_init()
Definition array.c:36
void buffer_flush(buffer *buf, FILE *output)
Definition buffer.c:112
bool buffer_case_cmp(const buffer *buf, const char *str)
Definition buffer.c:330
void array_flush(const array *a, FILE *output)
void buffer_free(buffer *buf)
Definition buffer.c:83
buffer * value
Definition ows_struct.h:83
buffer * key
Definition ows_struct.h:82
struct Array_node * next
Definition ows_struct.h:84
array_node * last
Definition ows_struct.h:89
array_node * first
Definition ows_struct.h:88
size_t use
Definition ows_struct.h:36

Generated for tinyows by doxygen 1.9.7