Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

cr-stylesheet.c

Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode: ni; c-basic-offset: 8 -*- */
00002 
00003 /*
00004  * This file is part of The Croco Library
00005  *
00006  * Copyright (C) 2002-2004 Dodji Seketeli
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of version 2.1 of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00020  * USA
00021  */
00022 
00023 #include "string.h"
00024 #include "cr-stylesheet.h"
00025 
00026 /**
00027  *@file
00028  *The definition of the #CRStyleSheet class
00029  */
00030 
00031 /**
00032  *Constructor of the #CRStyleSheet class.
00033  *@param the initial list of css statements.
00034  *@return the newly built css2 stylesheet, or NULL in case of error.
00035  */
00036 CRStyleSheet *
00037 cr_stylesheet_new (CRStatement * a_stmts)
00038 {
00039         CRStyleSheet *result;
00040 
00041         result = g_try_malloc (sizeof (CRStyleSheet));
00042         if (!result) {
00043                 cr_utils_trace_info ("Out of memory");
00044                 return NULL;
00045         }
00046 
00047         memset (result, 0, sizeof (CRStyleSheet));
00048 
00049         if (a_stmts)
00050                 result->statements = a_stmts;
00051 
00052         return result;
00053 }
00054 
00055 /**
00056  *@param a_this the current instance of #CRStyleSheet
00057  *@return the serialized stylesheet.
00058  */
00059 gchar *
00060 cr_stylesheet_to_string (CRStyleSheet *a_this)
00061 {
00062         gchar *str = NULL;
00063         GString *stringue = NULL;
00064         CRStatement *cur_stmt = NULL;
00065 
00066         g_return_val_if_fail (a_this, NULL);
00067 
00068         if (a_this->statements) {
00069                 stringue = g_string_new (NULL) ;
00070                 g_return_val_if_fail (stringue, NULL) ;
00071         }
00072         for (cur_stmt = a_this->statements;
00073              cur_stmt; cur_stmt = cur_stmt->next) {
00074                 if (cur_stmt->prev) {
00075                         g_string_append (stringue, "\n\n") ;
00076                 }
00077                 str = cr_statement_to_string (cur_stmt, 0) ;
00078                 if (str) {
00079                         g_string_append (stringue, str) ;
00080                         g_free (str) ;
00081                         str = NULL ;
00082                 }
00083         }
00084         if (stringue) {
00085                 str = stringue->str ;
00086                 g_string_free (stringue, FALSE) ;
00087                 stringue = NULL ;
00088         }
00089         return str ;
00090 }
00091 
00092 /**
00093  *Dumps the current css2 stylesheet to a file.
00094  *@param a_this the current instance of #CRStyleSheet.
00095  *@param a_fp the destination file
00096  */
00097 void
00098 cr_stylesheet_dump (CRStyleSheet * a_this, FILE * a_fp)
00099 {
00100         gchar *str = NULL ;
00101 
00102         g_return_if_fail (a_this);
00103 
00104         str = cr_stylesheet_to_string (a_this) ;
00105         if (str) {
00106                 fprintf (a_fp, "%s", str) ;
00107                 g_free (str) ;
00108                 str = NULL ;
00109         }
00110 }
00111 
00112 /**
00113  *Return the number of rules in the stylesheet.
00114  *@param a_this the current instance of #CRStyleSheet.
00115  *@return number of rules in the stylesheet.
00116  */
00117 gint
00118 cr_stylesheet_nr_rules (CRStyleSheet * a_this)
00119 {
00120         g_return_val_if_fail (a_this, -1);
00121 
00122         return cr_statement_nr_rules (a_this->statements);
00123 }
00124 
00125 /**
00126  *Use an index to get a CRStatement from the rules in a given stylesheet.
00127  *@param a_this the current instance of #CRStatement.
00128  *@param itemnr the index into the rules.
00129  *@return CRStatement at position itemnr, if itemnr > number of rules - 1,
00130  *it will return NULL.
00131  */
00132 CRStatement *
00133 cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr)
00134 {
00135         g_return_val_if_fail (a_this, NULL);
00136 
00137         return cr_statement_get_from_list (a_this->statements, itemnr);
00138 }
00139 
00140 void
00141 cr_stylesheet_ref (CRStyleSheet * a_this)
00142 {
00143         g_return_if_fail (a_this);
00144 
00145         a_this->ref_count++;
00146 }
00147 
00148 gboolean
00149 cr_stylesheet_unref (CRStyleSheet * a_this)
00150 {
00151         g_return_val_if_fail (a_this, FALSE);
00152 
00153         if (a_this->ref_count)
00154                 a_this->ref_count--;
00155 
00156         if (!a_this->ref_count) {
00157                 cr_stylesheet_destroy (a_this);
00158                 return TRUE;
00159         }
00160 
00161         return FALSE;
00162 }
00163 
00164 /**
00165  *Destructor of the #CRStyleSheet class.
00166  *@param a_this the current instance of the #CRStyleSheet class.
00167  */
00168 void
00169 cr_stylesheet_destroy (CRStyleSheet * a_this)
00170 {
00171         g_return_if_fail (a_this);
00172 
00173         if (a_this->statements) {
00174                 cr_statement_destroy (a_this->statements);
00175                 a_this->statements = NULL;
00176         }
00177         g_free (a_this);
00178 }

Generated on Fri Oct 29 08:29:12 2004 for Libcroco by  doxygen 1.3.9.1