File: cr-selector.c

package info (click to toggle)
screem 0.12.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 19,728 kB
  • ctags: 8,333
  • sloc: ansic: 98,234; sh: 8,278; xml: 2,278; makefile: 1,054
file content (277 lines) | stat: -rw-r--r-- 7,513 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -*- Mode: C; indent-tabs-mode: ni; c-basic-offset: 8 -*- */

/*
 * This file is part of The Croco Library
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * See COPYRIGHTS file for copyright information.
 */

#include <string.h>
#include "cr-selector.h"
#include "cr-parser.h"

/**
 *Creates a new instance of #CRSelector.
 *@param a_simple_sel the initial simple selector list
 *of the current instance of #CRSelector.
 *@return the newly built instance of #CRSelector, or
 *NULL in case of failure.
 */
CRSelector *
cr_selector_new (CRSimpleSel * a_simple_sel)
{
        CRSelector *result = NULL;

        result = g_try_malloc (sizeof (CRSelector));
        if (!result) {
                cr_utils_trace_info ("Out of memory");
                return NULL;
        }
        memset (result, 0, sizeof (CRSelector));
        result->simple_sel = a_simple_sel;
        return result;
}

CRSelector *
cr_selector_parse_from_buf (const guchar * a_char_buf, enum CREncoding a_enc)
{
        CRParser *parser = NULL;

        g_return_val_if_fail (a_char_buf, NULL);

        parser = cr_parser_new_from_buf ((guchar*)a_char_buf, strlen (a_char_buf),
                                         a_enc, FALSE);
        g_return_val_if_fail (parser, NULL);

        return NULL;
}

/**
 *Appends a new instance of #CRSelector to the current selector list.
 *@param a_this the current instance of #CRSelector.
 *@param a_new the instance of #CRSelector to be appended.
 *@return the new list.
 */
CRSelector *
cr_selector_append (CRSelector * a_this, CRSelector * a_new)
{
        CRSelector *cur = NULL;

        if (!a_this) {
                return a_new;
        }

        /*walk forward the list headed by a_this to get the list tail */
        for (cur = a_this; cur && cur->next; cur = cur->next) ;

        cur->next = a_new;
        a_new->prev = cur;

        return a_this;
}

/**
 *Prepends an element to the #CRSelector list.
 *@param a_this the current instance of #CRSelector list.
 *@param a_new the instance of #CRSelector.
 *@return the new list.
 */
CRSelector *
cr_selector_prepend (CRSelector * a_this, CRSelector * a_new)
{
        CRSelector *cur = NULL;

        a_new->next = a_this;
        a_this->prev = a_new;

        for (cur = a_new; cur && cur->prev; cur = cur->prev) ;

        return cur;
}

/**
 *append a simple selector to the current #CRSelector list.
 *@param a_this the current instance of #CRSelector.
 *@param a_simple_sel the simple selector to append.
 *@return the new list or NULL in case of failure.
 */
CRSelector *
cr_selector_append_simple_sel (CRSelector * a_this,
                               CRSimpleSel * a_simple_sel)
{
        CRSelector *selector = NULL;

        selector = cr_selector_new (a_simple_sel);
        g_return_val_if_fail (selector, NULL);

        return cr_selector_append (a_this, selector);
}

guchar *
cr_selector_to_string (CRSelector * a_this)
{
        guchar *result = NULL;
        GString *str_buf = NULL;

        str_buf = g_string_new (NULL);
        g_return_val_if_fail (str_buf, NULL);

        if (a_this) {
                CRSelector *cur = NULL;

                for (cur = a_this; cur; cur = cur->next) {
                        if (cur->simple_sel) {
                                guchar *tmp_str = NULL;

                                tmp_str = cr_simple_sel_to_string
                                        (cur->simple_sel);

                                if (tmp_str) {
                                        if (cur->prev)
                                                g_string_append (str_buf, 
								 ", ");

                                        g_string_append (str_buf, tmp_str);

                                        g_free (tmp_str);
                                        tmp_str = NULL;
                                }
                        }
                }
        }

        if (str_buf) {
                result = str_buf->str;
                g_string_free (str_buf, FALSE);
                str_buf = NULL;
        }

        return result;
}

/**
 *Serializes the current instance of #CRSelector to a file.
 *@param a_this the current instance of #CRSelector.
 *@param a_fp the destination file.
 */
void
cr_selector_dump (CRSelector * a_this, FILE * a_fp)
{
        guchar *tmp_buf = NULL;

        if (a_this) {
                tmp_buf = cr_selector_to_string (a_this);
                if (tmp_buf) {
                        fprintf (a_fp, "%s", tmp_buf);
                        g_free (tmp_buf);
                        tmp_buf = NULL;
                }
        }
}

/**
 *Increments the ref count of the current instance
 *of #CRSelector.
 *@param a_this the current instance of #CRSelector.
 */
void
cr_selector_ref (CRSelector * a_this)
{
        g_return_if_fail (a_this);

        a_this->ref_count++;
}

/**
 *Decrements the ref count of the current instance of
 *#CRSelector.
 *If the ref count reaches zero, the current instance of
 *#CRSelector is destroyed.
 *@param a_this the current instance of #CRSelector.
 *@return TRUE if this function destroyed the current instance
 *of #CRSelector, FALSE otherwise.
 */
gboolean
cr_selector_unref (CRSelector * a_this)
{
        g_return_val_if_fail (a_this, FALSE);

        if (a_this->ref_count) {
                a_this->ref_count--;
        }

        if (a_this->ref_count == 0) {
                cr_selector_destroy (a_this);
                return TRUE;
        }

        return FALSE;
}

/**
 *Destroys the selector list.
 *@param a_this the current instance of #CRSelector.
 */
void
cr_selector_destroy (CRSelector * a_this)
{
        CRSelector *cur = NULL;

        g_return_if_fail (a_this);

        /*
         *go and get the list tail. In the same time, free
         *all the simple selectors contained in the list.
         */
        for (cur = a_this; cur && cur->next; cur = cur->next) {
                if (cur->simple_sel) {
                        cr_simple_sel_destroy (cur->simple_sel);
                        cur->simple_sel = NULL;
                }
        }

        if (cur) {
                if (cur->simple_sel) {
                        cr_simple_sel_destroy (cur->simple_sel);
                        cur->simple_sel = NULL;
                }
        }

        /*in case the list has only one element */
        if (cur && !cur->prev) {
                g_free (cur);
                return;
        }

        /*walk backward the list and free each "next element" */
        for (cur = cur->prev; cur && cur->prev; cur = cur->prev) {
                if (cur->next) {
                        g_free (cur->next);
                        cur->next = NULL;
                }
        }

        if (!cur)
                return;

        if (cur->next) {
                g_free (cur->next);
                cur->next = NULL;
        }

        g_free (cur);
}