PageRenderTime 39ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/cssed-0.4.0/libcroco/parser/cr-attr-sel.c

#
C | 221 lines | 120 code | 39 blank | 62 comment | 16 complexity | 44a4ef77c81a6aa24c12e6592e21f0fa MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
  2. /*
  3. * This file is part of The Croco Library
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2.1 of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. *
  19. * See COPYRIGHTS file for copyrights information.
  20. */
  21. #include <stdio.h>
  22. #include "cr-attr-sel.h"
  23. /**
  24. *@file
  25. *The class that abstracts an attribute selector.
  26. *Attributes selectors are described in the css2 spec [5.8].
  27. *There are more generally used in the css2 selectors described in
  28. *css2 spec [5] .
  29. */
  30. /**
  31. *The constructor of #CRAttrSel.
  32. *@return the newly allocated instance
  33. *of #CRAttrSel.
  34. */
  35. CRAttrSel *
  36. cr_attr_sel_new (void)
  37. {
  38. CRAttrSel *result = NULL;
  39. result = g_malloc0 (sizeof (CRAttrSel));
  40. return result;
  41. }
  42. /**
  43. *Appends an attribute selector to the current list of
  44. *attribute selectors represented by a_this.
  45. *
  46. *@param a_this the this pointer of the current instance of
  47. *#CRAttrSel.
  48. *@param a_attr_sel selector to append.
  49. *@return CR_OK upon successfull completion, an error code otherwise.
  50. */
  51. enum CRStatus
  52. cr_attr_sel_append_attr_sel (CRAttrSel * a_this, CRAttrSel * a_attr_sel)
  53. {
  54. CRAttrSel *cur_sel = NULL;
  55. g_return_val_if_fail (a_this && a_attr_sel,
  56. CR_BAD_PARAM_ERROR);
  57. for (cur_sel = a_this;
  58. cur_sel->next;
  59. cur_sel = cur_sel->next) ;
  60. cur_sel->next = a_attr_sel;
  61. a_attr_sel->prev = cur_sel;
  62. return CR_OK;
  63. }
  64. /**
  65. *Prepends an attribute selector to the list of
  66. *attributes selector represented by a_this.
  67. *
  68. *@param a_this the "this pointer" of the current instance
  69. *of #CRAttrSel.
  70. *@param a_attr_sel the attribute selector to append.
  71. *@return CR_OK upon successfull completion, an error code otherwise.
  72. */
  73. enum CRStatus
  74. cr_attr_sel_prepend_attr_sel (CRAttrSel * a_this,
  75. CRAttrSel * a_attr_sel)
  76. {
  77. g_return_val_if_fail (a_this && a_attr_sel,
  78. CR_BAD_PARAM_ERROR);
  79. a_attr_sel->next = a_this;
  80. a_this->prev = a_attr_sel;
  81. return CR_OK;
  82. }
  83. guchar *
  84. cr_attr_sel_to_string (CRAttrSel * a_this)
  85. {
  86. CRAttrSel *cur = NULL;
  87. guchar *result = NULL;
  88. GString *str_buf = NULL;
  89. g_return_val_if_fail (a_this, NULL);
  90. str_buf = g_string_new (NULL);
  91. for (cur = a_this; cur; cur = cur->next) {
  92. if (cur->prev) {
  93. g_string_append_c (str_buf, ' ');
  94. }
  95. if (cur->name) {
  96. guchar *name = NULL;
  97. name = g_strndup (cur->name->stryng->str,
  98. cur->name->stryng->len);
  99. if (name) {
  100. g_string_append (str_buf, name);
  101. g_free (name);
  102. name = NULL;
  103. }
  104. }
  105. if (cur->value) {
  106. guchar *value = NULL;
  107. value = g_strndup (cur->value->stryng->str,
  108. cur->value->stryng->len);
  109. if (value) {
  110. switch (cur->match_way) {
  111. case SET:
  112. break;
  113. case EQUALS:
  114. g_string_append_c (str_buf, '=');
  115. break;
  116. case INCLUDES:
  117. g_string_append (str_buf, "~=");
  118. break;
  119. case DASHMATCH:
  120. g_string_append (str_buf, "|=");
  121. break;
  122. default:
  123. break;
  124. }
  125. g_string_append_printf
  126. (str_buf, "\"%s\"", value);
  127. g_free (value);
  128. value = NULL;
  129. }
  130. }
  131. }
  132. if (str_buf) {
  133. result = str_buf->str;
  134. g_string_free (str_buf, FALSE);
  135. }
  136. return result;
  137. }
  138. /**
  139. *Dumps the current instance of #CRAttrSel to a file.
  140. *@param a_this the "this pointer" of the current instance of
  141. *#CRAttrSel.
  142. *@param a_fp the destination file.
  143. */
  144. void
  145. cr_attr_sel_dump (CRAttrSel * a_this, FILE * a_fp)
  146. {
  147. guchar *tmp_str = NULL;
  148. g_return_if_fail (a_this);
  149. tmp_str = cr_attr_sel_to_string (a_this);
  150. if (tmp_str) {
  151. fprintf (a_fp, "%s", tmp_str);
  152. g_free (tmp_str);
  153. tmp_str = NULL;
  154. }
  155. }
  156. /**
  157. *Destroys the current instance of #CRAttrSel.
  158. *Frees all the fields if they are non null.
  159. *@param a_this the "this pointer" of the current
  160. *instance of #CRAttrSel.
  161. */
  162. void
  163. cr_attr_sel_destroy (CRAttrSel * a_this)
  164. {
  165. g_return_if_fail (a_this);
  166. if (a_this->name) {
  167. cr_string_destroy (a_this->name);
  168. a_this->name = NULL;
  169. }
  170. if (a_this->value) {
  171. cr_string_destroy (a_this->value);
  172. a_this->value = NULL;
  173. }
  174. if (a_this->next) {
  175. cr_attr_sel_destroy (a_this->next);
  176. a_this->next = NULL;
  177. }
  178. if (a_this) {
  179. g_free (a_this);
  180. a_this = NULL;
  181. }
  182. }