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