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