tinyows 1.2.2
mlist.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 a multiple list structure
35 */
37{
38 mlist *ml = NULL;
39
40 ml = malloc(sizeof(mlist));
41 assert(ml);
42
43 ml->first = NULL;
44 ml->last = NULL;
45 ml->size = 0;
46
47 return ml;
48}
49
50
51/*
52 * Free a multiple list structure
53 */
54void mlist_free(mlist * ml)
55{
56 assert(ml);
57
58 while (ml->first) mlist_node_free(ml, ml->first);
59
60 ml->last = NULL;
61 free(ml);
62 ml = NULL;
63}
64
65
66/*
67 * Add a given list to the end of a mlist
68 * Careful list is passed by reference,
69 * and must be free with mlist_free()
70 */
71void mlist_add(mlist * ml, list * value)
72{
73 mlist_node *mln;
74
75 assert(ml);
76 assert(value);
77 assert(ml->size < UINT_MAX);
78
79 mln = mlist_node_init();
80
81 mln->value = value;
82
83 if (!ml->first) {
84 mln->prev = NULL;
85 ml->first = mln;
86 } else {
87 mln->prev = ml->last;
88 ml->last->next = mln;
89 }
90
91 ml->last = mln;
92 ml->last->next = NULL;
93 ml->size++;
94}
95
96
97/*
98 * Initialize a multiple list node
99 */
101{
102 mlist_node *mln;
103
104 mln = malloc(sizeof(mlist_node));
105 assert(mln);
106
107 mln->value = NULL;
108 mln->prev = NULL;
109 mln->next = NULL;
110
111 return mln;
112}
113
114
115/*
116 * Free a multiple list node
117 */
119{
120 assert(mln);
121 assert(ml);
122
123 if (mln->prev)
124 mln->prev = NULL;
125
126 if (mln->next) {
127 ml->first = mln->next;
128 mln->next = NULL;
129 } else
130 ml->first = NULL;
131
132 if (mln->value) list_free(mln->value);
133
134 free(mln);
135 mln = NULL;
136}
137
138
139/*
140 * Trunk an initial buffer into several pieces upon two separators
141 * Inside multiple list, each element is separated by a comma
142 * Careful returned multiple list must then be free with mlist_free()
143 */
144mlist *mlist_explode(char separator_start, char separator_end, buffer * value)
145{
146 size_t i;
147 mlist *ml;
148 list *l;
149 buffer *buf;
150
151 assert(value);
152
153 ml = mlist_init();
154
155 /* if first char doesn't match separator, mlist contains only one element */
156 if (value->buf[0] != separator_start) {
157 /* according to wfs specification, elements inside a multiple list
158 are separated by a comma */
159 l = list_explode(',', value);
160 mlist_add(ml, l);
161 } else {
162 buf = buffer_init();
163
164 for (i = 1; i < value->use; i++)
165 if (value->buf[i] == separator_end) {
166 /* explode the mlist's element */
167 l = list_explode(',', buf);
168 /* add the list to the multiple list */
169 mlist_add(ml, l);
170 buffer_free(buf);
171 } else if (value->buf[i] != separator_start) {
172 buffer_add(buf, value->buf[i]);
173 }
174 /* separator start */
175 else
176 buf = buffer_init();
177
178 }
179
180 return ml;
181}
182
183
184#ifdef OWS_DEBUG
185/*
186 * Flush a mlist to a given file
187 * (mainly to debug purpose)
188 */
189void mlist_flush(const mlist * ml, FILE * output)
190{
191 mlist_node *mln;
192
193 assert(ml);
194 assert(output);
195
196 for (mln = ml->first ; mln ; mln = mln->next) {
197 fprintf(output, "(\n");
198 list_flush(mln->value, output);
199 fprintf(output, ")\n");
200 }
201}
202#endif
203
204
205/*
206 * vim: expandtab sw=4 ts=4
207 */
void mlist_add(mlist *ml, list *value)
Definition mlist.c:71
mlist * mlist_explode(char separator_start, char separator_end, buffer *value)
Definition mlist.c:144
mlist * mlist_init()
Definition mlist.c:36
mlist_node * mlist_node_init()
Definition mlist.c:100
void mlist_node_free(mlist *ml, mlist_node *mln)
Definition mlist.c:118
void mlist_free(mlist *ml)
Definition mlist.c:54
void buffer_add(buffer *buf, char c)
Definition buffer.c:123
void list_free(list *l)
Definition list.c:54
void list_flush(const list *l, FILE *output)
void buffer_free(buffer *buf)
Definition buffer.c:83
void mlist_flush(const mlist *ml, FILE *output)
buffer * buffer_init()
Definition buffer.c:61
list * list_explode(char separator, const buffer *value)
Definition list.c:296
char * buf
size to next realloc
Definition ows_struct.h:39
size_t use
Definition ows_struct.h:36
struct Mlist_node * prev
Definition ows_struct.h:59
struct Mlist_node * next
Definition ows_struct.h:58
list * value
Definition ows_struct.h:57
mlist_node * last
Definition ows_struct.h:64
mlist_node * first
Definition ows_struct.h:63
unsigned int size
Definition ows_struct.h:65

Generated for tinyows by doxygen 1.12.0