PageRenderTime 35ms CodeModel.GetById 17ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/cssed-0.4.0/libcroco/parser/cr-stylesheet.c

#
C | 178 lines | 98 code | 26 blank | 54 comment | 11 complexity | d4f43079ae129ec7be4cc048c4337719 MD5 | raw file
  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 "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 */
 36CRStyleSheet *
 37cr_stylesheet_new (CRStatement * a_stmts)
 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 */
 59gchar *
 60cr_stylesheet_to_string (CRStyleSheet *a_this)
 61{
 62	gchar *str = NULL;
 63	GString *stringue = NULL;
 64	CRStatement *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 */
 97void
 98cr_stylesheet_dump (CRStyleSheet * 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 */
117gint
118cr_stylesheet_nr_rules (CRStyleSheet * a_this)
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 */
132CRStatement *
133cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr)
134{
135        g_return_val_if_fail (a_this, NULL);
136
137        return cr_statement_get_from_list (a_this->statements, itemnr);
138}
139
140void
141cr_stylesheet_ref (CRStyleSheet * a_this)
142{
143        g_return_if_fail (a_this);
144
145        a_this->ref_count++;
146}
147
148gboolean
149cr_stylesheet_unref (CRStyleSheet * a_this)
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 */
168void
169cr_stylesheet_destroy (CRStyleSheet * a_this)
170{
171        g_return_if_fail (a_this);
172
173        if (a_this->statements) {
174                cr_statement_destroy (a_this->statements);
175                a_this->statements = NULL;
176        }
177        g_free (a_this);
178}