PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C | 468 lines | 299 code | 81 blank | 88 comment | 48 complexity | b68a54ff613f9a7b1ab3ce62bce51f14 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. * Author: Dodji Seketeli
  20. * See COPYRIGHTS file for copyright information.
  21. *
  22. */
  23. #include "cr-additional-sel.h"
  24. #include "string.h"
  25. /**
  26. *Default constructor of #CRAdditionalSel.
  27. *@return the newly build instance of #CRAdditionalSel.
  28. */
  29. CRAdditionalSel *
  30. cr_additional_sel_new (void)
  31. {
  32. CRAdditionalSel *result = NULL;
  33. result = g_try_malloc (sizeof (CRAdditionalSel));
  34. if (result == NULL) {
  35. cr_utils_trace_debug ("Out of memory");
  36. return NULL;
  37. }
  38. memset (result, 0, sizeof (CRAdditionalSel));
  39. return result;
  40. }
  41. /**
  42. *Constructor of #CRAdditionalSel.
  43. *@param a_sel_type the type of the newly built instance
  44. *of #CRAdditionalSel.
  45. *@return the newly built instance of #CRAdditionalSel.
  46. */
  47. CRAdditionalSel *
  48. cr_additional_sel_new_with_type (enum AddSelectorType a_sel_type)
  49. {
  50. CRAdditionalSel *result = NULL;
  51. result = cr_additional_sel_new ();
  52. g_return_val_if_fail (result, NULL);
  53. result->type = a_sel_type;
  54. return result;
  55. }
  56. /**
  57. *Sets a new class name to a
  58. *CLASS additional selector.
  59. *@param a_this the "this pointer" of the current instance
  60. *of #CRAdditionalSel .
  61. *@param a_class_name the new class name to set.
  62. *
  63. */
  64. void
  65. cr_additional_sel_set_class_name (CRAdditionalSel * a_this,
  66. CRString * a_class_name)
  67. {
  68. g_return_if_fail (a_this && a_this->type == CLASS_ADD_SELECTOR);
  69. if (a_this->content.class_name) {
  70. cr_string_destroy (a_this->content.class_name);
  71. }
  72. a_this->content.class_name = a_class_name;
  73. }
  74. /**
  75. *Sets a new id name to an
  76. *ID additional selector.
  77. *@param a_this the "this pointer" of the current instance
  78. *of #CRAdditionalSel .
  79. *@param a_id the new id to set.
  80. */
  81. void
  82. cr_additional_sel_set_id_name (CRAdditionalSel * a_this, CRString * a_id)
  83. {
  84. g_return_if_fail (a_this && a_this->type == ID_ADD_SELECTOR);
  85. if (a_this->content.id_name) {
  86. cr_string_destroy (a_this->content.id_name);
  87. }
  88. a_this->content.id_name = a_id;
  89. }
  90. /**
  91. *Sets a new pseudo to a
  92. *PSEUDO additional selector.
  93. *@param a_this the "this pointer" of the current instance
  94. *of #CRAdditionalSel .
  95. *@param a_pseudo the new pseudo to set.
  96. */
  97. void
  98. cr_additional_sel_set_pseudo (CRAdditionalSel * a_this, CRPseudo * a_pseudo)
  99. {
  100. g_return_if_fail (a_this
  101. && a_this->type == PSEUDO_CLASS_ADD_SELECTOR);
  102. if (a_this->content.pseudo) {
  103. cr_pseudo_destroy (a_this->content.pseudo);
  104. }
  105. a_this->content.pseudo = a_pseudo;
  106. }
  107. /**
  108. *Sets a new instance of #CRAttrSel to
  109. *a ATTRIBUTE additional selector.
  110. *@param a_this the "this pointer" of the current instance
  111. *of #CRAdditionalSel .
  112. *@param a_sel the new instance of #CRAttrSel to set.
  113. */
  114. void
  115. cr_additional_sel_set_attr_sel (CRAdditionalSel * a_this, CRAttrSel * a_sel)
  116. {
  117. g_return_if_fail (a_this && a_this->type == ATTRIBUTE_ADD_SELECTOR);
  118. if (a_this->content.attr_sel) {
  119. cr_attr_sel_destroy (a_this->content.attr_sel);
  120. }
  121. a_this->content.attr_sel = a_sel;
  122. }
  123. /**
  124. *Appends a new instance of #CRAdditional to the
  125. *current list of #CRAdditional.
  126. *@param a_this the "this pointer" of the current instance
  127. *of #CRAdditionalSel .
  128. *@param a_sel the new instance to #CRAdditional to append.
  129. *@return the new list of CRAdditionalSel or NULL if an error arises.
  130. */
  131. CRAdditionalSel *
  132. cr_additional_sel_append (CRAdditionalSel * a_this, CRAdditionalSel * a_sel)
  133. {
  134. CRAdditionalSel *cur_sel = NULL;
  135. g_return_val_if_fail (a_sel, NULL);
  136. if (a_this == NULL) {
  137. return a_sel;
  138. }
  139. if (a_sel == NULL)
  140. return NULL;
  141. for (cur_sel = a_this;
  142. cur_sel && cur_sel->next; cur_sel = cur_sel->next) ;
  143. g_return_val_if_fail (cur_sel != NULL, NULL);
  144. cur_sel->next = a_sel;
  145. a_sel->prev = cur_sel;
  146. return a_this;
  147. }
  148. /**
  149. *Preppends a new instance of #CRAdditional to the
  150. *current list of #CRAdditional.
  151. *@param a_this the "this pointer" of the current instance
  152. *of #CRAdditionalSel .
  153. *@param a_sel the new instance to #CRAdditional to preappend.
  154. *@return the new list of CRAdditionalSel or NULL if an error arises.
  155. */
  156. CRAdditionalSel *
  157. cr_additional_sel_prepend (CRAdditionalSel * a_this, CRAdditionalSel * a_sel)
  158. {
  159. g_return_val_if_fail (a_sel, NULL);
  160. if (a_this == NULL) {
  161. return a_sel;
  162. }
  163. a_sel->next = a_this;
  164. a_this->prev = a_sel;
  165. return a_sel;
  166. }
  167. guchar *
  168. cr_additional_sel_to_string (CRAdditionalSel * a_this)
  169. {
  170. guchar *result = NULL;
  171. GString *str_buf = NULL;
  172. CRAdditionalSel *cur = NULL;
  173. g_return_val_if_fail (a_this, NULL);
  174. str_buf = g_string_new (NULL);
  175. for (cur = a_this; cur; cur = cur->next) {
  176. switch (cur->type) {
  177. case CLASS_ADD_SELECTOR:
  178. {
  179. guchar *name = NULL;
  180. if (cur->content.class_name) {
  181. name = g_strndup
  182. (cur->content.class_name->stryng->str,
  183. cur->content.class_name->stryng->len);
  184. if (name) {
  185. g_string_append_printf
  186. (str_buf, ".%s",
  187. name);
  188. g_free (name);
  189. name = NULL;
  190. }
  191. }
  192. }
  193. break;
  194. case ID_ADD_SELECTOR:
  195. {
  196. guchar *name = NULL;
  197. if (cur->content.class_name) {
  198. name = g_strndup
  199. (cur->content.id_name->stryng->str,
  200. cur->content.id_name->stryng->len);
  201. if (name) {
  202. g_string_append_printf
  203. (str_buf, "#%s",
  204. name);
  205. g_free (name);
  206. name = NULL;
  207. }
  208. }
  209. }
  210. break;
  211. case PSEUDO_CLASS_ADD_SELECTOR:
  212. {
  213. if (cur->content.pseudo) {
  214. guchar *tmp_str = NULL;
  215. tmp_str = cr_pseudo_to_string
  216. (cur->content.pseudo);
  217. if (tmp_str) {
  218. g_string_append_printf
  219. (str_buf, ":%s",
  220. tmp_str);
  221. g_free (tmp_str);
  222. tmp_str = NULL;
  223. }
  224. }
  225. }
  226. break;
  227. case ATTRIBUTE_ADD_SELECTOR:
  228. if (cur->content.attr_sel) {
  229. guchar *tmp_str = NULL;
  230. g_string_append_c (str_buf, '[');
  231. tmp_str = cr_attr_sel_to_string
  232. (cur->content.attr_sel);
  233. if (tmp_str) {
  234. g_string_append_printf
  235. (str_buf, "%s]", tmp_str);
  236. g_free (tmp_str);
  237. tmp_str = NULL;
  238. }
  239. }
  240. break;
  241. default:
  242. break;
  243. }
  244. }
  245. if (str_buf) {
  246. result = str_buf->str;
  247. g_string_free (str_buf, FALSE);
  248. str_buf = NULL;
  249. }
  250. return result;
  251. }
  252. guchar *
  253. cr_additional_sel_one_to_string (CRAdditionalSel *a_this)
  254. {
  255. guchar *result = NULL;
  256. GString *str_buf = NULL;
  257. g_return_val_if_fail (a_this, NULL) ;
  258. str_buf = g_string_new (NULL) ;
  259. switch (a_this->type) {
  260. case CLASS_ADD_SELECTOR:
  261. {
  262. guchar *name = NULL;
  263. if (a_this->content.class_name) {
  264. name = g_strndup
  265. (a_this->content.class_name->stryng->str,
  266. a_this->content.class_name->stryng->len);
  267. if (name) {
  268. g_string_append_printf
  269. (str_buf, ".%s",
  270. name);
  271. g_free (name);
  272. name = NULL;
  273. }
  274. }
  275. }
  276. break;
  277. case ID_ADD_SELECTOR:
  278. {
  279. guchar *name = NULL;
  280. if (a_this->content.class_name) {
  281. name = g_strndup
  282. (a_this->content.id_name->stryng->str,
  283. a_this->content.id_name->stryng->len);
  284. if (name) {
  285. g_string_append_printf
  286. (str_buf, "#%s",
  287. name);
  288. g_free (name);
  289. name = NULL;
  290. }
  291. }
  292. }
  293. break;
  294. case PSEUDO_CLASS_ADD_SELECTOR:
  295. {
  296. if (a_this->content.pseudo) {
  297. guchar *tmp_str = NULL;
  298. tmp_str = cr_pseudo_to_string
  299. (a_this->content.pseudo);
  300. if (tmp_str) {
  301. g_string_append_printf
  302. (str_buf, ":%s",
  303. tmp_str);
  304. g_free (tmp_str);
  305. tmp_str = NULL;
  306. }
  307. }
  308. }
  309. break;
  310. case ATTRIBUTE_ADD_SELECTOR:
  311. if (a_this->content.attr_sel) {
  312. guchar *tmp_str = NULL;
  313. g_string_append_printf (str_buf, "[");
  314. tmp_str = cr_attr_sel_to_string
  315. (a_this->content.attr_sel);
  316. if (tmp_str) {
  317. g_string_append_printf
  318. (str_buf, "%s]", tmp_str);
  319. g_free (tmp_str);
  320. tmp_str = NULL;
  321. }
  322. }
  323. break;
  324. default:
  325. break;
  326. }
  327. if (str_buf) {
  328. result = str_buf->str;
  329. g_string_free (str_buf, FALSE);
  330. str_buf = NULL;
  331. }
  332. return result;
  333. }
  334. /**
  335. *Dumps the current instance of #CRAdditionalSel to a file
  336. *@param a_this the "this pointer" of the current instance of
  337. *#CRAdditionalSel.
  338. *@param a_fp the destination file.
  339. */
  340. void
  341. cr_additional_sel_dump (CRAdditionalSel * a_this, FILE * a_fp)
  342. {
  343. guchar *tmp_str = NULL;
  344. g_return_if_fail (a_fp);
  345. if (a_this) {
  346. tmp_str = cr_additional_sel_to_string (a_this);
  347. if (tmp_str) {
  348. fprintf (a_fp, "%s", tmp_str);
  349. g_free (tmp_str);
  350. tmp_str = NULL;
  351. }
  352. }
  353. }
  354. /**
  355. *Destroys an instance of #CRAdditional.
  356. *@param a_this the "this pointer" of the current instance
  357. *of #CRAdditionalSel .
  358. */
  359. void
  360. cr_additional_sel_destroy (CRAdditionalSel * a_this)
  361. {
  362. g_return_if_fail (a_this);
  363. switch (a_this->type) {
  364. case CLASS_ADD_SELECTOR:
  365. cr_string_destroy (a_this->content.class_name);
  366. a_this->content.class_name = NULL;
  367. break;
  368. case PSEUDO_CLASS_ADD_SELECTOR:
  369. cr_pseudo_destroy (a_this->content.pseudo);
  370. a_this->content.pseudo = NULL;
  371. break;
  372. case ID_ADD_SELECTOR:
  373. cr_string_destroy (a_this->content.id_name);
  374. a_this->content.id_name = NULL;
  375. break;
  376. case ATTRIBUTE_ADD_SELECTOR:
  377. cr_attr_sel_destroy (a_this->content.attr_sel);
  378. a_this->content.attr_sel = NULL;
  379. break;
  380. default:
  381. break;
  382. }
  383. if (a_this->next) {
  384. cr_additional_sel_destroy (a_this->next);
  385. }
  386. g_free (a_this);
  387. }