PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cssed-0.4.0/libcroco/parser/cr-stylesheet.c

#
C | 178 lines | 98 code | 26 blank | 54 comment | 11 complexity | d4f43079ae129ec7be4cc048c4337719 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- Mode: C; indent-tabs-mode: ni; c-basic-offset: 8 -*- */
  2. /*
  3. * This file is part of The Croco Library
  4. *
  5. * Copyright (C) 2002-2004 Dodji Seketeli
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of version 2.1 of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  19. * USA
  20. */
  21. #include "string.h"
  22. #include "cr-stylesheet.h"
  23. /**
  24. *@file
  25. *The definition of the #CRStyleSheet class
  26. */
  27. /**
  28. *Constructor of the #CRStyleSheet class.
  29. *@param the initial list of css statements.
  30. *@return the newly built css2 stylesheet, or NULL in case of error.
  31. */
  32. CRStyleSheet *
  33. cr_stylesheet_new (CRStatement * a_stmts)
  34. {
  35. CRStyleSheet *result;
  36. result = g_try_malloc (sizeof (CRStyleSheet));
  37. if (!result) {
  38. cr_utils_trace_info ("Out of memory");
  39. return NULL;
  40. }
  41. memset (result, 0, sizeof (CRStyleSheet));
  42. if (a_stmts)
  43. result->statements = a_stmts;
  44. return result;
  45. }
  46. /**
  47. *@param a_this the current instance of #CRStyleSheet
  48. *@return the serialized stylesheet.
  49. */
  50. gchar *
  51. cr_stylesheet_to_string (CRStyleSheet *a_this)
  52. {
  53. gchar *str = NULL;
  54. GString *stringue = NULL;
  55. CRStatement *cur_stmt = NULL;
  56. g_return_val_if_fail (a_this, NULL);
  57. if (a_this->statements) {
  58. stringue = g_string_new (NULL) ;
  59. g_return_val_if_fail (stringue, NULL) ;
  60. }
  61. for (cur_stmt = a_this->statements;
  62. cur_stmt; cur_stmt = cur_stmt->next) {
  63. if (cur_stmt->prev) {
  64. g_string_append (stringue, "\n\n") ;
  65. }
  66. str = cr_statement_to_string (cur_stmt, 0) ;
  67. if (str) {
  68. g_string_append (stringue, str) ;
  69. g_free (str) ;
  70. str = NULL ;
  71. }
  72. }
  73. if (stringue) {
  74. str = stringue->str ;
  75. g_string_free (stringue, FALSE) ;
  76. stringue = NULL ;
  77. }
  78. return str ;
  79. }
  80. /**
  81. *Dumps the current css2 stylesheet to a file.
  82. *@param a_this the current instance of #CRStyleSheet.
  83. *@param a_fp the destination file
  84. */
  85. void
  86. cr_stylesheet_dump (CRStyleSheet * a_this, FILE * a_fp)
  87. {
  88. gchar *str = NULL ;
  89. g_return_if_fail (a_this);
  90. str = cr_stylesheet_to_string (a_this) ;
  91. if (str) {
  92. fprintf (a_fp, "%s", str) ;
  93. g_free (str) ;
  94. str = NULL ;
  95. }
  96. }
  97. /**
  98. *Return the number of rules in the stylesheet.
  99. *@param a_this the current instance of #CRStyleSheet.
  100. *@return number of rules in the stylesheet.
  101. */
  102. gint
  103. cr_stylesheet_nr_rules (CRStyleSheet * a_this)
  104. {
  105. g_return_val_if_fail (a_this, -1);
  106. return cr_statement_nr_rules (a_this->statements);
  107. }
  108. /**
  109. *Use an index to get a CRStatement from the rules in a given stylesheet.
  110. *@param a_this the current instance of #CRStatement.
  111. *@param itemnr the index into the rules.
  112. *@return CRStatement at position itemnr, if itemnr > number of rules - 1,
  113. *it will return NULL.
  114. */
  115. CRStatement *
  116. cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr)
  117. {
  118. g_return_val_if_fail (a_this, NULL);
  119. return cr_statement_get_from_list (a_this->statements, itemnr);
  120. }
  121. void
  122. cr_stylesheet_ref (CRStyleSheet * a_this)
  123. {
  124. g_return_if_fail (a_this);
  125. a_this->ref_count++;
  126. }
  127. gboolean
  128. cr_stylesheet_unref (CRStyleSheet * a_this)
  129. {
  130. g_return_val_if_fail (a_this, FALSE);
  131. if (a_this->ref_count)
  132. a_this->ref_count--;
  133. if (!a_this->ref_count) {
  134. cr_stylesheet_destroy (a_this);
  135. return TRUE;
  136. }
  137. return FALSE;
  138. }
  139. /**
  140. *Destructor of the #CRStyleSheet class.
  141. *@param a_this the current instance of the #CRStyleSheet class.
  142. */
  143. void
  144. cr_stylesheet_destroy (CRStyleSheet * a_this)
  145. {
  146. g_return_if_fail (a_this);
  147. if (a_this->statements) {
  148. cr_statement_destroy (a_this->statements);
  149. a_this->statements = NULL;
  150. }
  151. g_free (a_this);
  152. }