Main Page | Alphabetical List | Data Structures | 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 /*
00024  *$Id: cr-stylesheet.c,v 1.9.2.1 2004/04/18 13:07:22 dodji Exp $
00025  */
00026 
00027 #include "string.h"
00028 #include "cr-stylesheet.h"
00029 
00030 /**
00031  *@file
00032  *The definition of the #CRStyleSheet class
00033  */
00034 
00035 /**
00036  *Constructor of the #CRStyleSheet class.
00037  *@param the initial list of css statements.
00038  *@return the newly built css2 stylesheet, or NULL in case of error.
00039  */
00040 CRStyleSheet *
00041 cr_stylesheet_new (CRStatement * a_stmts)
00042 {
00043         CRStyleSheet *result;
00044 
00045         result = g_try_malloc (sizeof (CRStyleSheet));
00046         if (!result) {
00047                 cr_utils_trace_info ("Out of memory");
00048                 return NULL;
00049         }
00050 
00051         memset (result, 0, sizeof (CRStyleSheet));
00052 
00053         if (a_stmts)
00054                 result->statements = a_stmts;
00055 
00056         return result;
00057 }
00058 
00059 /**
00060  *@param a_this the current instance of #CRStyleSheet
00061  *@return the serialized stylesheet.
00062  */
00063 gchar *
00064 cr_stylesheet_to_string (CRStyleSheet *a_this)
00065 {
00066         gchar *str = NULL;
00067         GString *stringue = NULL;
00068         CRStatement *cur_stmt = NULL;
00069 
00070         g_return_val_if_fail (a_this, NULL);
00071 
00072         if (a_this->statements) {
00073                 stringue = g_string_new (NULL) ;
00074                 g_return_val_if_fail (stringue, NULL) ;
00075         }
00076         for (cur_stmt = a_this->statements;
00077              cur_stmt; cur_stmt = cur_stmt->next) {
00078                 if (cur_stmt->prev) {
00079                         g_string_append (stringue, "\n\n") ;
00080                 }
00081                 str = cr_statement_to_string (cur_stmt, 0) ;
00082                 if (str) {
00083                         g_string_append (stringue, str) ;
00084                         g_free (str) ;
00085                         str = NULL ;
00086                 }
00087         }
00088         if (stringue) {
00089                 str = stringue->str ;
00090                 g_string_free (stringue, FALSE) ;
00091                 stringue = NULL ;
00092         }
00093         return str ;
00094 }
00095 
00096 /**
00097  *Dumps the current css2 stylesheet to a file.
00098  *@param a_this the current instance of #CRStyleSheet.
00099  *@param a_fp the destination file
00100  */
00101 void
00102 cr_stylesheet_dump (CRStyleSheet * a_this, FILE * a_fp)
00103 {
00104         gchar *str = NULL ;
00105 
00106         g_return_if_fail (a_this);
00107 
00108         str = cr_stylesheet_to_string (a_this) ;
00109         if (str) {
00110                 fprintf (a_fp, str) ;
00111                 g_free (str) ;
00112                 str = NULL ;
00113         }
00114 }
00115 
00116 /**
00117  *Return the number of rules in the stylesheet.
00118  *@param a_this the current instance of #CRStyleSheet.
00119  *@return number of rules in the stylesheet.
00120  */
00121 gint
00122 cr_stylesheet_nr_rules (CRStyleSheet * a_this)
00123 {
00124         g_return_val_if_fail (a_this, -1);
00125 
00126         return cr_statement_nr_rules (a_this->statements);
00127 }
00128 
00129 /**
00130  *Use an index to get a CRStatement from the rules in a given stylesheet.
00131  *@param a_this the current instance of #CRStatement.
00132  *@param itemnr the index into the rules.
00133  *@return CRStatement at position itemnr, if itemnr > number of rules - 1,
00134  *it will return NULL.
00135  */
00136 CRStatement *
00137 cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr)
00138 {
00139         g_return_val_if_fail (a_this, NULL);
00140 
00141         return cr_statement_get_from_list (a_this->statements, itemnr);
00142 }
00143 
00144 void
00145 cr_stylesheet_ref (CRStyleSheet * a_this)
00146 {
00147         g_return_if_fail (a_this);
00148 
00149         a_this->ref_count++;
00150 }
00151 
00152 gboolean
00153 cr_stylesheet_unref (CRStyleSheet * a_this)
00154 {
00155         g_return_val_if_fail (a_this, FALSE);
00156 
00157         if (a_this->ref_count)
00158                 a_this->ref_count--;
00159 
00160         if (!a_this->ref_count) {
00161                 cr_stylesheet_destroy (a_this);
00162                 return TRUE;
00163         }
00164 
00165         return FALSE;
00166 }
00167 
00168 /**
00169  *Destructor of the #CRStyleSheet class.
00170  *@param a_this the current instance of the #CRStyleSheet class.
00171  */
00172 void
00173 cr_stylesheet_destroy (CRStyleSheet * a_this)
00174 {
00175         g_return_if_fail (a_this);
00176 
00177         if (a_this->statements) {
00178                 cr_statement_destroy (a_this->statements);
00179                 a_this->statements = NULL;
00180         }
00181         g_free (a_this);
00182 }

Generated on Tue Apr 27 13:46:36 2004 for Libcroco by doxygen 1.3.6