tinyows 1.2.2
alist.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 * Alist is an array of list
34 */
35
36/*
37 * Initialize an alist structure
38 */
40{
41 alist *al = NULL;
42
43 al = malloc(sizeof(alist));
44 assert(al);
45
46 al->first = NULL;
47 al->last = NULL;
48
49 return al;
50}
51
52
53/*
54 * Free an alist structure
55 */
56void alist_free(alist * al)
57{
58 alist_node *an = NULL;
59 alist_node *an_to_free = NULL;
60
61 assert(al);
62
63 for (an = al->first ; an ; /* empty */) {
64 an_to_free = an;
65 an = an->next;
66
67 buffer_free(an_to_free->key);
68 list_free(an_to_free->value);
69 free(an_to_free);
70 an_to_free = NULL;
71 }
72
73 free(al);
74 al = NULL;
75}
76
77
78/*
79 * Add a given buffer to the end of an alist entry
80 * if key exist, value is add to the list
81 * if not new key entry is created
82 */
83void alist_add(alist * al, buffer * key, buffer * value)
84{
85 alist_node *an;
86
87 assert(al);
88 assert(key);
89 assert(value);
90
91 if (!alist_is_key(al, key->buf)) {
92 an = malloc(sizeof(alist_node));
93 assert(an);
94
95 an->key = key;
96 an->value = list_init();
97
98 if (!al->first) al->first = an;
99 else al->last->next = an;
100
101 al->last = an;
102 al->last->next = NULL;
103 } else {
104 for (an = al->first ; an ; an = an->next)
105 if (buffer_case_cmp(an->key, key->buf))
106 break;
107 }
108
109 list_add(an->value, value);
110}
111
112
113/*
114 * Check if a given key string is or not in the alist
115 */
116bool alist_is_key(const alist * al, const char *key)
117{
118 alist_node *an;
119 size_t ks;
120
121 assert(al);
122 assert(key);
123
124 for (ks = strlen(key), an = al->first ; an ; an = an->next)
125 if (ks == an->key->use)
126 if (buffer_case_cmp(an->key, key))
127 return true;
128
129 return false;
130}
131
132
133/*
134 * Return a value buffer from an alist (from key value)
135 * You must be sure key is defined for this array, see is_key() above
136 * Carreful return a reference on the alist buf itself !
137 */
138list *alist_get(const alist * al, const char *key)
139{
140 alist_node *an;
141 size_t ks;
142
143 assert(al);
144 assert(key);
145
146 for (ks = strlen(key), an = al->first ; an ; an = an->next) {
147 if (ks == an->key->use)
148 if (buffer_case_cmp(an->key, key))
149 break;
150 }
151
152 assert(an);
153
154 return an->value;
155}
156
157
158#ifdef OWS_DEBUG
159/*
160 * Flush an alist to a given file
161 * (mainly to debug purpose)
162 */
163void alist_flush(const alist * al, FILE * output)
164{
165 alist_node *an;
166
167 assert(al);
168 assert(output);
169
170 for (an = al->first ; an ; an = an->next) {
171 fprintf(output, "[");
172 buffer_flush(an->key, output);
173 fprintf(output, "] -> ");
174 list_flush(an->value, output);
175 fprintf(output, "\n");
176 }
177}
178#endif
179
180
181/*
182 * vim: expandtab sw=4 ts=4
183 */
void alist_free(alist *al)
Definition alist.c:56
bool alist_is_key(const alist *al, const char *key)
Definition alist.c:116
list * alist_get(const alist *al, const char *key)
Definition alist.c:138
void alist_add(alist *al, buffer *key, buffer *value)
Definition alist.c:83
alist * alist_init()
Definition alist.c:39
void list_free(list *l)
Definition list.c:54
void list_flush(const list *l, FILE *output)
list * list_init()
Definition list.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 alist_flush(const alist *al, FILE *output)
void list_add(list *l, buffer *value)
Definition list.c:71
void buffer_free(buffer *buf)
Definition buffer.c:83
buffer * key
Definition ows_struct.h:70
list * value
Definition ows_struct.h:71
struct Alist_node * next
Definition ows_struct.h:72
alist_node * last
Definition ows_struct.h:77
alist_node * first
Definition ows_struct.h:76
char * buf
size to next realloc
Definition ows_struct.h:39
size_t use
Definition ows_struct.h:36

Generated for tinyows by doxygen 1.10.0