Imported Upstream version 0.18.1.1
[platform/upstream/gettext.git] / gettext-tools / gnulib-lib / libcroco / cr-attr-sel.c
1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
2
3 /*
4  * This file is part of The Croco Library
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 3 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
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.
14  *
15  * You should have received a copy of the GNU 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
18  * USA
19  * 
20  * See COPYRIGHTS file for copyrights information.
21  */
22
23 #include <config.h>
24 #include <stdio.h>
25 #include "cr-attr-sel.h"
26
27 /**
28  * CRAttrSel:
29  *
30  * #CRAdditionalSel abstracts an attribute selector.
31  * Attributes selectors are described in the css2 spec [5.8].
32  * There are more generally used in the css2 selectors described in
33  * css2 spec [5] .
34  */
35
36 /**
37  * cr_attr_sel_new:
38  * The constructor of #CRAttrSel.
39  * Returns the newly allocated instance
40  * of #CRAttrSel.
41  */
42 CRAttrSel *
43 cr_attr_sel_new (void)
44 {
45         CRAttrSel *result = NULL;
46
47         result = g_malloc0 (sizeof (CRAttrSel));
48
49         return result;
50 }
51
52 /**
53  * cr_attr_sel_append_attr_sel:
54  * @a_this: the this pointer of the current instance of  #CRAttrSel.
55  * @a_attr_sel: selector to append.
56  *
57  * Appends an attribute selector to the current list of
58  * attribute selectors represented by a_this.
59  * Returns CR_OK upon successfull completion, an error code otherwise.
60  */
61 enum CRStatus
62 cr_attr_sel_append_attr_sel (CRAttrSel * a_this, CRAttrSel * a_attr_sel)
63 {
64         CRAttrSel *cur_sel = NULL;
65
66         g_return_val_if_fail (a_this && a_attr_sel, 
67                               CR_BAD_PARAM_ERROR);
68
69         for (cur_sel = a_this; 
70              cur_sel->next; 
71              cur_sel = cur_sel->next) ;
72
73         cur_sel->next = a_attr_sel;
74         a_attr_sel->prev = cur_sel;
75
76         return CR_OK;
77 }
78
79 /**
80  * cr_attr_sel_prepend_attr_sel:
81  *@a_this: the "this pointer" of the current instance *of #CRAttrSel.
82  *@a_attr_sel: the attribute selector to append.
83  *
84  *Prepends an attribute selector to the list of
85  *attributes selector represented by a_this.
86  *Returns CR_OK upon successfull completion, an error code otherwise.
87  */
88 enum CRStatus
89 cr_attr_sel_prepend_attr_sel (CRAttrSel * a_this, 
90                               CRAttrSel * a_attr_sel)
91 {
92         g_return_val_if_fail (a_this && a_attr_sel, 
93                               CR_BAD_PARAM_ERROR);
94
95         a_attr_sel->next = a_this;
96         a_this->prev = a_attr_sel;
97
98         return CR_OK;
99 }
100
101 /**
102  * cr_attr_sel_to_string:
103  * @a_this: the current instance of #CRAttrSel.
104  *
105  * Serializes an attribute selector into a string
106  * Returns the serialized attribute selector.
107  */
108 guchar *
109 cr_attr_sel_to_string (CRAttrSel * a_this)
110 {
111         CRAttrSel *cur = NULL;
112         guchar *result = NULL;
113         GString *str_buf = NULL;
114
115         g_return_val_if_fail (a_this, NULL);
116
117         str_buf = g_string_new (NULL);
118
119         for (cur = a_this; cur; cur = cur->next) {
120                 if (cur->prev) {
121                         g_string_append_c (str_buf, ' ');
122                 }
123
124                 if (cur->name) {
125                         guchar *name = NULL;
126
127                         name = g_strndup (cur->name->stryng->str, 
128                                           cur->name->stryng->len);
129                         if (name) {
130                                 g_string_append (str_buf, name);
131                                 g_free (name);
132                                 name = NULL;
133                         }
134                 }
135
136                 if (cur->value) {
137                         guchar *value = NULL;
138
139                         value = g_strndup (cur->value->stryng->str, 
140                                            cur->value->stryng->len);
141                         if (value) {
142                                 switch (cur->match_way) {
143                                 case SET:
144                                         break;
145
146                                 case EQUALS:
147                                         g_string_append_c (str_buf, '=');
148                                         break;
149
150                                 case INCLUDES:
151                                         g_string_append (str_buf, "~=");
152                                         break;
153
154                                 case DASHMATCH:
155                                         g_string_append (str_buf, "|=");
156                                         break;
157
158                                 default:
159                                         break;
160                                 }
161
162                                 g_string_append_printf
163                                         (str_buf, "\"%s\"", value);
164
165                                 g_free (value);
166                                 value = NULL;
167                         }
168                 }
169         }
170
171         if (str_buf) {
172                 result = str_buf->str;
173                 g_string_free (str_buf, FALSE);
174         }
175
176         return result;
177 }
178
179 /**
180  * cr_attr_sel_dump:
181  * @a_this: the "this pointer" of the current instance of
182  * #CRAttrSel.
183  * @a_fp: the destination file.
184  *
185  * Dumps the current instance of #CRAttrSel to a file.
186  */
187 void
188 cr_attr_sel_dump (CRAttrSel * a_this, FILE * a_fp)
189 {
190         guchar *tmp_str = NULL;
191
192         g_return_if_fail (a_this);
193
194         tmp_str = cr_attr_sel_to_string (a_this);
195
196         if (tmp_str) {
197                 fprintf (a_fp, "%s", tmp_str);
198                 g_free (tmp_str);
199                 tmp_str = NULL;
200         }
201 }
202
203 /**
204  *cr_attr_sel_destroy:
205  *@a_this: the "this pointer" of the current
206  *instance of #CRAttrSel.
207  *
208  *Destroys the current instance of #CRAttrSel.
209  *Frees all the fields if they are non null.
210  */
211 void
212 cr_attr_sel_destroy (CRAttrSel * a_this)
213 {
214         g_return_if_fail (a_this);
215
216         if (a_this->name) {
217                 cr_string_destroy (a_this->name);
218                 a_this->name = NULL;
219         }
220
221         if (a_this->value) {
222                 cr_string_destroy (a_this->value);
223                 a_this->value = NULL;
224         }
225
226         if (a_this->next) {
227                 cr_attr_sel_destroy (a_this->next);
228                 a_this->next = NULL;
229         }
230
231         if (a_this) {
232                 g_free (a_this);
233                 a_this = NULL;
234         }
235 }
236