1/* -*- Mode: C; indent-tabs-mode: ni; 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 <config.h> 24#include "string.h" 25#include "cr-stylesheet.h" 26 27/** 28 *@file 29 *The definition of the #CRStyleSheet class 30 */ 31 32/** 33 *Constructor of the #CRStyleSheet class. 34 *@param the initial list of css statements. 35 *@return the newly built css2 stylesheet, or NULL in case of error. 36 */ 37CRStyleSheet * 38cr_stylesheet_new (CRStatement * a_stmts) 39{ 40 CRStyleSheet *result; 41 42 result = g_try_malloc (sizeof (CRStyleSheet)); 43 if (!result) { 44 cr_utils_trace_info ("Out of memory"); 45 return NULL; 46 } 47 48 memset (result, 0, sizeof (CRStyleSheet)); 49 50 if (a_stmts) 51 result->statements = a_stmts; 52 53 return result; 54} 55 56/** 57 *@param a_this the current instance of #CRStyleSheet 58 *@return the serialized stylesheet. 59 */ 60gchar * 61cr_stylesheet_to_string (CRStyleSheet *a_this) 62{ 63 gchar *str = NULL; 64 GString *stringue = NULL; 65 CRStatement *cur_stmt = NULL; 66 67 g_return_val_if_fail (a_this, NULL); 68 69 if (a_this->statements) { 70 stringue = g_string_new (NULL) ; 71 g_return_val_if_fail (stringue, NULL) ; 72 } 73 for (cur_stmt = a_this->statements; 74 cur_stmt; cur_stmt = cur_stmt->next) { 75 if (cur_stmt->prev) { 76 g_string_append (stringue, "\n\n") ; 77 } 78 str = cr_statement_to_string (cur_stmt, 0) ; 79 if (str) { 80 g_string_append (stringue, str) ; 81 g_free (str) ; 82 str = NULL ; 83 } 84 } 85 if (stringue) { 86 str = stringue->str ; 87 g_string_free (stringue, FALSE) ; 88 stringue = NULL ; 89 } 90 return str ; 91} 92 93/** 94 *Dumps the current css2 stylesheet to a file. 95 *@param a_this the current instance of #CRStyleSheet. 96 *@param a_fp the destination file 97 */ 98void 99cr_stylesheet_dump (CRStyleSheet * a_this, FILE * a_fp) 100{ 101 gchar *str = NULL ; 102 103 g_return_if_fail (a_this); 104 105 str = cr_stylesheet_to_string (a_this) ; 106 if (str) { 107 fprintf (a_fp, "%s", str) ; 108 g_free (str) ; 109 str = NULL ; 110 } 111} 112 113/** 114 *Return the number of rules in the stylesheet. 115 *@param a_this the current instance of #CRStyleSheet. 116 *@return number of rules in the stylesheet. 117 */ 118gint 119cr_stylesheet_nr_rules (CRStyleSheet * a_this) 120{ 121 g_return_val_if_fail (a_this, -1); 122 123 return cr_statement_nr_rules (a_this->statements); 124} 125 126/** 127 *Use an index to get a CRStatement from the rules in a given stylesheet. 128 *@param a_this the current instance of #CRStatement. 129 *@param itemnr the index into the rules. 130 *@return CRStatement at position itemnr, if itemnr > number of rules - 1, 131 *it will return NULL. 132 */ 133CRStatement * 134cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr) 135{ 136 g_return_val_if_fail (a_this, NULL); 137 138 return cr_statement_get_from_list (a_this->statements, itemnr); 139} 140 141void 142cr_stylesheet_ref (CRStyleSheet * a_this) 143{ 144 g_return_if_fail (a_this); 145 146 a_this->ref_count++; 147} 148 149gboolean 150cr_stylesheet_unref (CRStyleSheet * a_this) 151{ 152 g_return_val_if_fail (a_this, FALSE); 153 154 if (a_this->ref_count) 155 a_this->ref_count--; 156 157 if (!a_this->ref_count) { 158 cr_stylesheet_destroy (a_this); 159 return TRUE; 160 } 161 162 return FALSE; 163} 164 165/** 166 *Destructor of the #CRStyleSheet class. 167 *@param a_this the current instance of the #CRStyleSheet class. 168 */ 169void 170cr_stylesheet_destroy (CRStyleSheet * a_this) 171{ 172 g_return_if_fail (a_this); 173 174 if (a_this->statements) { 175 cr_statement_destroy (a_this->statements); 176 a_this->statements = NULL; 177 } 178 g_free (a_this); 179} 180