1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * This file is part of The Croco Library
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2.1 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * See COPYRIGHTS file for copyright information.
24 #include "cr-selector.h"
25 #include "cr-parser.h"
30 *@a_simple_sel: the initial simple selector list
31 *of the current instance of #CRSelector.
33 *Creates a new instance of #CRSelector.
35 *Returns the newly built instance of #CRSelector, or
36 *NULL in case of failure.
39 cr_selector_new (CRSimpleSel * a_simple_sel)
41 CRSelector *result = NULL;
43 result = g_try_malloc (sizeof (CRSelector));
45 cr_utils_trace_info ("Out of memory");
48 memset (result, 0, sizeof (CRSelector));
49 result->simple_sel = a_simple_sel;
54 cr_selector_parse_from_buf (const guchar * a_char_buf, enum CREncoding a_enc)
56 CRParser *parser = NULL;
58 g_return_val_if_fail (a_char_buf, NULL);
60 parser = cr_parser_new_from_buf ((guchar*)a_char_buf, strlen (a_char_buf),
62 g_return_val_if_fail (parser, NULL);
70 *@a_this: the current instance of #CRSelector.
71 *@a_new: the instance of #CRSelector to be appended.
73 *Appends a new instance of #CRSelector to the current selector list.
75 *Returns the new list.
78 cr_selector_append (CRSelector * a_this, CRSelector * a_new)
80 CRSelector *cur = NULL;
86 /*walk forward the list headed by a_this to get the list tail */
87 for (cur = a_this; cur && cur->next; cur = cur->next) ;
96 * cr_selector_prepend:
98 *@a_this: the current instance of #CRSelector list.
99 *@a_new: the instance of #CRSelector.
101 *Prepends an element to the #CRSelector list.
103 *Returns the new list.
106 cr_selector_prepend (CRSelector * a_this, CRSelector * a_new)
108 CRSelector *cur = NULL;
110 a_new->next = a_this;
111 a_this->prev = a_new;
113 for (cur = a_new; cur && cur->prev; cur = cur->prev) ;
119 * cr_selector_append_simple_sel:
121 *@a_this: the current instance of #CRSelector.
122 *@a_simple_sel: the simple selector to append.
124 *append a simple selector to the current #CRSelector list.
126 *Returns the new list or NULL in case of failure.
129 cr_selector_append_simple_sel (CRSelector * a_this,
130 CRSimpleSel * a_simple_sel)
132 CRSelector *selector = NULL;
134 selector = cr_selector_new (a_simple_sel);
135 g_return_val_if_fail (selector, NULL);
137 return cr_selector_append (a_this, selector);
141 cr_selector_to_string (CRSelector const * a_this)
143 guchar *result = NULL;
144 GString *str_buf = NULL;
146 str_buf = g_string_new (NULL);
147 g_return_val_if_fail (str_buf, NULL);
150 CRSelector const *cur = NULL;
152 for (cur = a_this; cur; cur = cur->next) {
153 if (cur->simple_sel) {
154 guchar *tmp_str = NULL;
156 tmp_str = cr_simple_sel_to_string
161 g_string_append (str_buf,
164 g_string_append (str_buf, tmp_str);
174 result = str_buf->str;
175 g_string_free (str_buf, FALSE);
185 *@a_this: the current instance of #CRSelector.
186 *@a_fp: the destination file.
188 *Serializes the current instance of #CRSelector to a file.
191 cr_selector_dump (CRSelector const * a_this, FILE * a_fp)
193 guchar *tmp_buf = NULL;
196 tmp_buf = cr_selector_to_string (a_this);
198 fprintf (a_fp, "%s", tmp_buf);
208 *@a_this: the current instance of #CRSelector.
210 *Increments the ref count of the current instance
214 cr_selector_ref (CRSelector * a_this)
216 g_return_if_fail (a_this);
224 *@a_this: the current instance of #CRSelector.
226 *Decrements the ref count of the current instance of
228 *If the ref count reaches zero, the current instance of
229 *#CRSelector is destroyed.
231 *Returns TRUE if this function destroyed the current instance
232 *of #CRSelector, FALSE otherwise.
235 cr_selector_unref (CRSelector * a_this)
237 g_return_val_if_fail (a_this, FALSE);
239 if (a_this->ref_count) {
243 if (a_this->ref_count == 0) {
244 cr_selector_destroy (a_this);
252 * cr_selector_destroy:
254 *@a_this: the current instance of #CRSelector.
256 *Destroys the selector list.
259 cr_selector_destroy (CRSelector * a_this)
261 CRSelector *cur = NULL;
263 g_return_if_fail (a_this);
266 *go and get the list tail. In the same time, free
267 *all the simple selectors contained in the list.
269 for (cur = a_this; cur && cur->next; cur = cur->next) {
270 if (cur->simple_sel) {
271 cr_simple_sel_destroy (cur->simple_sel);
272 cur->simple_sel = NULL;
277 if (cur->simple_sel) {
278 cr_simple_sel_destroy (cur->simple_sel);
279 cur->simple_sel = NULL;
283 /*in case the list has only one element */
284 if (cur && !cur->prev) {
289 /*walk backward the list and free each "next element" */
290 for (cur = cur->prev; cur && cur->prev; cur = cur->prev) {