summaryrefslogtreecommitdiff
path: root/src/cr-attr-sel.c
blob: 787f0c03a183edfd5bd8187594579c532f6e090c (plain)
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
/* -*- Mode: C; indent-tabs-mode:nil; 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 copyrights information.
 */

#include <stdio.h>
#include "cr-attr-sel.h"

/**
 * CRAttrSel:
 *
 * #CRAdditionalSel abstracts an attribute selector.
 * Attributes selectors are described in the css2 spec [5.8].
 * There are more generally used in the css2 selectors described in
 * css2 spec [5] .
 */

/**
 * cr_attr_sel_new:
 * The constructor of #CRAttrSel.
 * Returns the newly allocated instance
 * of #CRAttrSel.
 */
CRAttrSel *
cr_attr_sel_new (void)
{
        CRAttrSel *result = NULL;

        result = g_malloc0 (sizeof (CRAttrSel));

        return result;
}

/**
 * cr_attr_sel_append_attr_sel:
 * @a_this: the this pointer of the current instance of  #CRAttrSel.
 * @a_attr_sel: selector to append.
 *
 * Appends an attribute selector to the current list of
 * attribute selectors represented by a_this.
 * Returns CR_OK upon successfull completion, an error code otherwise.
 */
enum CRStatus
cr_attr_sel_append_attr_sel (CRAttrSel * a_this, CRAttrSel * a_attr_sel)
{
        CRAttrSel *cur_sel = NULL;

        g_return_val_if_fail (a_this && a_attr_sel, 
                              CR_BAD_PARAM_ERROR);

        for (cur_sel = a_this; 
             cur_sel->next; 
             cur_sel = cur_sel->next) ;

        cur_sel->next = a_attr_sel;
        a_attr_sel->prev = cur_sel;

        return CR_OK;
}

/**
 * cr_attr_sel_prepend_attr_sel:
 *@a_this: the "this pointer" of the current instance *of #CRAttrSel.
 *@a_attr_sel: the attribute selector to append.
 *
 *Prepends an attribute selector to the list of
 *attributes selector represented by a_this.
 *Returns CR_OK upon successfull completion, an error code otherwise.
 */
enum CRStatus
cr_attr_sel_prepend_attr_sel (CRAttrSel * a_this, 
                              CRAttrSel * a_attr_sel)
{
        g_return_val_if_fail (a_this && a_attr_sel, 
                              CR_BAD_PARAM_ERROR);

        a_attr_sel->next = a_this;
        a_this->prev = a_attr_sel;

        return CR_OK;
}

/**
 * cr_attr_sel_to_string:
 * @a_this: the current instance of #CRAttrSel.
 *
 * Serializes an attribute selector into a string
 * Returns the serialized attribute selector.
 */
guchar *
cr_attr_sel_to_string (CRAttrSel const * a_this)
{
        CRAttrSel const *cur = NULL;
        guchar *result = NULL;
        GString *str_buf = NULL;

        g_return_val_if_fail (a_this, NULL);

        str_buf = g_string_new (NULL);

        for (cur = a_this; cur; cur = cur->next) {
                if (cur->prev) {
                        g_string_append_c (str_buf, ' ');
                }

                if (cur->name) {
                        guchar *name = NULL;

                        name = g_strndup (cur->name->stryng->str, 
                                          cur->name->stryng->len);
                        if (name) {
                                g_string_append (str_buf, name);
                                g_free (name);
                                name = NULL;
                        }
                }

                if (cur->value) {
                        guchar *value = NULL;

                        value = g_strndup (cur->value->stryng->str, 
                                           cur->value->stryng->len);
                        if (value) {
                                switch (cur->match_way) {
                                case SET:
                                        break;

                                case EQUALS:
                                        g_string_append_c (str_buf, '=');
                                        break;

                                case INCLUDES:
                                        g_string_append (str_buf, "~=");
                                        break;

                                case DASHMATCH:
                                        g_string_append (str_buf, "|=");
                                        break;

                                default:
                                        break;
                                }

                                g_string_append_printf
                                        (str_buf, "\"%s\"", value);

                                g_free (value);
                                value = NULL;
                        }
                }
        }

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

        return result;
}

/**
 * cr_attr_sel_dump:
 * @a_this: the "this pointer" of the current instance of
 * #CRAttrSel.
 * @a_fp: the destination file.
 *
 * Dumps the current instance of #CRAttrSel to a file.
 */
void
cr_attr_sel_dump (CRAttrSel const * a_this, FILE * a_fp)
{
        guchar *tmp_str = NULL;

        g_return_if_fail (a_this);

        tmp_str = cr_attr_sel_to_string (a_this);

        if (tmp_str) {
                fprintf (a_fp, "%s", tmp_str);
                g_free (tmp_str);
                tmp_str = NULL;
        }
}

/**
 *cr_attr_sel_destroy:
 *@a_this: the "this pointer" of the current
 *instance of #CRAttrSel.
 *
 *Destroys the current instance of #CRAttrSel.
 *Frees all the fields if they are non null.
 */
void
cr_attr_sel_destroy (CRAttrSel * a_this)
{
        g_return_if_fail (a_this);

        if (a_this->name) {
                cr_string_destroy (a_this->name);
                a_this->name = NULL;
        }

        if (a_this->value) {
                cr_string_destroy (a_this->value);
                a_this->value = NULL;
        }

        if (a_this->next) {
                cr_attr_sel_destroy (a_this->next);
                a_this->next = NULL;
        }

        if (a_this) {
                g_free (a_this);
                a_this = NULL;
        }
}