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 = (CRStyleSheet *)g_try_malloc (sizeof (CRStyleSheet));
40 if (!result) {
41 cr_utils_trace_info ("Out of memory");
42 return NULL;
43 }
45 memset (result, 0, sizeof (CRStyleSheet));
47 if (a_stmts)
48 result->statements = a_stmts;
50 return result;
51 }
53 /**
54 *@param a_this the current instance of #CRStyleSheet
55 *@return the serialized stylesheet.
56 */
57 gchar *
58 cr_stylesheet_to_string (CRStyleSheet *a_this)
59 {
60 gchar *str = NULL;
61 GString *stringue = NULL;
62 CRStatement *cur_stmt = NULL;
64 g_return_val_if_fail (a_this, NULL);
66 if (a_this->statements) {
67 stringue = g_string_new (NULL) ;
68 g_return_val_if_fail (stringue, NULL) ;
69 }
70 for (cur_stmt = a_this->statements;
71 cur_stmt; cur_stmt = cur_stmt->next) {
72 if (cur_stmt->prev) {
73 g_string_append (stringue, "\n\n") ;
74 }
75 str = cr_statement_to_string (cur_stmt, 0) ;
76 if (str) {
77 g_string_append (stringue, str) ;
78 g_free (str) ;
79 str = NULL ;
80 }
81 }
82 if (stringue) {
83 str = stringue->str ;
84 g_string_free (stringue, FALSE) ;
85 stringue = NULL ;
86 }
87 return str ;
88 }
90 /**
91 *Dumps the current css2 stylesheet to a file.
92 *@param a_this the current instance of #CRStyleSheet.
93 *@param a_fp the destination file
94 */
95 void
96 cr_stylesheet_dump (CRStyleSheet * a_this, FILE * a_fp)
97 {
98 gchar *str = NULL ;
100 g_return_if_fail (a_this);
102 str = cr_stylesheet_to_string (a_this) ;
103 if (str) {
104 fprintf (a_fp, "%s", str) ;
105 g_free (str) ;
106 str = NULL ;
107 }
108 }
110 /**
111 *Return the number of rules in the stylesheet.
112 *@param a_this the current instance of #CRStyleSheet.
113 *@return number of rules in the stylesheet.
114 */
115 gint
116 cr_stylesheet_nr_rules (CRStyleSheet * a_this)
117 {
118 g_return_val_if_fail (a_this, -1);
120 return cr_statement_nr_rules (a_this->statements);
121 }
123 /**
124 *Use an index to get a CRStatement from the rules in a given stylesheet.
125 *@param a_this the current instance of #CRStatement.
126 *@param itemnr the index into the rules.
127 *@return CRStatement at position itemnr, if itemnr > number of rules - 1,
128 *it will return NULL.
129 */
130 CRStatement *
131 cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr)
132 {
133 g_return_val_if_fail (a_this, NULL);
135 return cr_statement_get_from_list (a_this->statements, itemnr);
136 }
138 void
139 cr_stylesheet_ref (CRStyleSheet * a_this)
140 {
141 g_return_if_fail (a_this);
143 a_this->ref_count++;
144 }
146 gboolean
147 cr_stylesheet_unref (CRStyleSheet * a_this)
148 {
149 g_return_val_if_fail (a_this, FALSE);
151 if (a_this->ref_count)
152 a_this->ref_count--;
154 if (!a_this->ref_count) {
155 cr_stylesheet_destroy (a_this);
156 return TRUE;
157 }
159 return FALSE;
160 }
162 /**
163 *Destructor of the #CRStyleSheet class.
164 *@param a_this the current instance of the #CRStyleSheet class.
165 */
166 void
167 cr_stylesheet_destroy (CRStyleSheet * a_this)
168 {
169 g_return_if_fail (a_this);
171 if (a_this->statements) {
172 cr_statement_destroy (a_this->statements);
173 a_this->statements = NULL;
174 }
175 g_free (a_this);
176 }