Libcroco
cr-stylesheet.c
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 
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  */
22 
23 #include "string.h"
24 #include "cr-stylesheet.h"
25 
26 /**
27  *@file
28  *The definition of the #CRStyleSheet class
29  */
30 
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  */
38 {
39  CRStyleSheet *result;
40 
41  result = g_try_malloc (sizeof (CRStyleSheet));
42  if (!result) {
43  cr_utils_trace_info ("Out of memory");
44  return NULL;
45  }
46 
47  memset (result, 0, sizeof (CRStyleSheet));
48 
49  if (a_stmts)
50  result->statements = a_stmts;
51 
52  return result;
53 }
54 
55 /**
56  *@param a_this the current instance of #CRStyleSheet
57  *@return the serialized stylesheet.
58  */
59 gchar *
61 {
62  gchar *str = NULL;
63  GString *stringue = NULL;
64  CRStatement const *cur_stmt = NULL;
65 
66  g_return_val_if_fail (a_this, NULL);
67 
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 }
91 
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 const * a_this, FILE * a_fp)
99 {
100  gchar *str = NULL ;
101 
102  g_return_if_fail (a_this);
103 
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 }
111 
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
119 {
120  g_return_val_if_fail (a_this, -1);
121 
122  return cr_statement_nr_rules (a_this->statements);
123 }
124 
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 *
134 {
135  g_return_val_if_fail (a_this, NULL);
136 
137  return cr_statement_get_from_list (a_this->statements, itemnr);
138 }
139 
140 void
142 {
143  g_return_if_fail (a_this);
144 
145  a_this->ref_count++;
146 }
147 
148 gboolean
150 {
151  g_return_val_if_fail (a_this, FALSE);
152 
153  if (a_this->ref_count)
154  a_this->ref_count--;
155 
156  if (!a_this->ref_count) {
157  cr_stylesheet_destroy (a_this);
158  return TRUE;
159  }
160 
161  return FALSE;
162 }
163 
164 /**
165  *Destructor of the #CRStyleSheet class.
166  *@param a_this the current instance of the #CRStyleSheet class.
167  */
168 void
170 {
171  g_return_if_fail (a_this);
172 
173  if (a_this->statements) {
175  a_this->statements = NULL;
176  }
177  g_free (a_this);
178 }
_CRStyleSheet
An abstraction of a css stylesheet as defined by the css2 spec in chapter 4.
Definition: cr-stylesheet.h:57
cr_stylesheet_statement_get_from_list
CRStatement * cr_stylesheet_statement_get_from_list(CRStyleSheet *a_this, int itemnr)
Use an index to get a CRStatement from the rules in a given stylesheet.
Definition: cr-stylesheet.c:133
_CRStatement::prev
CRStatement * prev
Definition: cr-statement.h:213
cr_stylesheet_ref
void cr_stylesheet_ref(CRStyleSheet *a_this)
Definition: cr-stylesheet.c:141
cr_stylesheet_nr_rules
gint cr_stylesheet_nr_rules(CRStyleSheet const *a_this)
Return the number of rules in the stylesheet.
Definition: cr-stylesheet.c:118
cr-stylesheet.h
_CRStatement::next
CRStatement * next
Definition: cr-statement.h:212
cr_stylesheet_destroy
void cr_stylesheet_destroy(CRStyleSheet *a_this)
Destructor of the CRStyleSheet class.
Definition: cr-stylesheet.c:169
_CRStatement
The abstraction of css statement as defined in the chapter 4 and appendix D.1 of the css2 spec.
Definition: cr-statement.h:182
cr_stylesheet_unref
gboolean cr_stylesheet_unref(CRStyleSheet *a_this)
Definition: cr-stylesheet.c:149
cr_statement_to_string
gchar * cr_statement_to_string(CRStatement const *a_this, gulong a_indent)
cr_statement_to_string:
Definition: cr-statement.c:2492
cr_stylesheet_dump
void cr_stylesheet_dump(CRStyleSheet const *a_this, FILE *a_fp)
Dumps the current css2 stylesheet to a file.
Definition: cr-stylesheet.c:98
cr_statement_nr_rules
gint cr_statement_nr_rules(CRStatement const *a_this)
cr_statement_nr_rules:
Definition: cr-statement.c:1932
_CRStyleSheet::ref_count
gulong ref_count
the reference count of this insance Please, don't never ever modify it directly.
Definition: cr-stylesheet.h:82
_CRStyleSheet::statements
CRStatement * statements
The css statements list.
Definition: cr-stylesheet.h:60
cr_statement_destroy
void cr_statement_destroy(CRStatement *a_this)
cr_statement_destroy:
Definition: cr-statement.c:2756
cr_utils_trace_info
#define cr_utils_trace_info(a_msg)
Traces an info message.
Definition: cr-utils.h:127
cr_statement_get_from_list
CRStatement * cr_statement_get_from_list(CRStatement *a_this, int itemnr)
cr_statement_get_from_list:
Definition: cr-statement.c:1956
cr_stylesheet_to_string
gchar * cr_stylesheet_to_string(CRStyleSheet const *a_this)
Definition: cr-stylesheet.c:60
cr_stylesheet_new
CRStyleSheet * cr_stylesheet_new(CRStatement *a_stmts)
Constructor of the CRStyleSheet class.
Definition: cr-stylesheet.c:37