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