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

cr-attr-sel.c

Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
00002 
00003 /*
00004  * This file is part of The Croco Library
00005  *
00006  * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
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-attr-sel.c,v 1.7 2004/03/07 13:22:47 dodji Exp $
00025  */
00026 
00027 #include <stdio.h>
00028 #include "cr-attr-sel.h"
00029 
00030 /**
00031  *@file
00032  *The class that abstracts an attribute selector.
00033  *Attributes selectors are described in the css2 spec [5.8].
00034  *There are more generally used in the css2 selectors described in
00035  *css2 spec [5] .
00036  */
00037 
00038 /**
00039  *The constructor of #CRAttrSel.
00040  *@return the newly allocated instance
00041  *of #CRAttrSel.
00042  */
00043 CRAttrSel *
00044 cr_attr_sel_new (void)
00045 {
00046         CRAttrSel *result = NULL;
00047 
00048         result = g_malloc0 (sizeof (CRAttrSel));
00049 
00050         return result;
00051 }
00052 
00053 /**
00054  *Appends an attribute selector to the current list of
00055  *attribute selectors represented by a_this.
00056  *
00057  *@param a_this the this pointer of the current instance of
00058  *#CRAttrSel.
00059  *@param a_attr_sel selector to append.
00060  *@return CR_OK upon successfull completion, an error code otherwise.
00061  */
00062 enum CRStatus
00063 cr_attr_sel_append_attr_sel (CRAttrSel * a_this, CRAttrSel * a_attr_sel)
00064 {
00065         CRAttrSel *cur_sel = NULL;
00066 
00067         g_return_val_if_fail (a_this && a_attr_sel, CR_BAD_PARAM_ERROR);
00068 
00069         for (cur_sel = a_this; cur_sel->next; cur_sel = cur_sel->next) ;
00070 
00071         cur_sel->next = a_attr_sel;
00072         a_attr_sel->prev = cur_sel;
00073 
00074         return CR_OK;
00075 }
00076 
00077 /**
00078  *Prepends an attribute selector to the list of
00079  *attributes selector represented by a_this.
00080  *
00081  *@param a_this the "this pointer" of the current instance
00082  *of #CRAttrSel.
00083  *@param a_attr_sel the attribute selector to append.
00084  *@return CR_OK upon successfull completion, an error code otherwise.
00085  */
00086 enum CRStatus
00087 cr_attr_sel_prepend_attr_sel (CRAttrSel * a_this, CRAttrSel * a_attr_sel)
00088 {
00089         g_return_val_if_fail (a_this && a_attr_sel, CR_BAD_PARAM_ERROR);
00090 
00091         a_attr_sel->next = a_this;
00092         a_this->prev = a_attr_sel;
00093 
00094         return CR_OK;
00095 }
00096 
00097 guchar *
00098 cr_attr_sel_to_string (CRAttrSel * a_this)
00099 {
00100         CRAttrSel *cur = NULL;
00101         guchar *result = NULL;
00102         GString *str_buf = NULL;
00103 
00104         g_return_val_if_fail (a_this, NULL);
00105 
00106         str_buf = g_string_new (NULL);
00107 
00108         for (cur = a_this; cur; cur = cur->next) {
00109                 if (cur->prev) {
00110                         g_string_append_printf (str_buf, " ");
00111                 }
00112 
00113                 if (cur->name) {
00114                         guchar *name = NULL;
00115 
00116                         name = g_strndup (cur->name->str, cur->name->len);
00117                         if (name) {
00118                                 g_string_append_printf (str_buf, "%s", name);
00119                                 g_free (name);
00120                                 name = NULL;
00121                         }
00122                 }
00123 
00124                 if (cur->value) {
00125                         guchar *value = NULL;
00126 
00127                         value = g_strndup (cur->value->str, cur->value->len);
00128                         if (value) {
00129                                 switch (cur->match_way) {
00130                                 case SET:
00131                                         break;
00132 
00133                                 case EQUALS:
00134                                         g_string_append_printf (str_buf, "=");
00135                                         break;
00136 
00137                                 case INCLUDES:
00138                                         g_string_append_printf
00139                                                 (str_buf, "~=");
00140                                         break;
00141 
00142                                 case DASHMATCH:
00143                                         g_string_append_printf
00144                                                 (str_buf, "|=");
00145                                         break;
00146 
00147                                 default:
00148                                         break;
00149                                 }
00150 
00151                                 g_string_append_printf
00152                                         (str_buf, "\"%s\"", value);
00153 
00154                                 g_free (value);
00155                                 value = NULL;
00156                         }
00157                 }
00158         }
00159 
00160         if (str_buf) {
00161                 result = str_buf->str;
00162                 g_string_free (str_buf, FALSE);
00163         }
00164 
00165         return result;
00166 }
00167 
00168 /**
00169  *Dumps the current instance of #CRAttrSel to a file.
00170  *@param a_this the "this pointer" of the current instance of
00171  *#CRAttrSel.
00172  *@param a_fp the destination file.
00173  */
00174 void
00175 cr_attr_sel_dump (CRAttrSel * a_this, FILE * a_fp)
00176 {
00177         guchar *tmp_str = NULL;
00178 
00179         g_return_if_fail (a_this);
00180 
00181         tmp_str = cr_attr_sel_to_string (a_this);
00182 
00183         if (tmp_str) {
00184                 fprintf (a_fp, "%s", tmp_str);
00185                 g_free (tmp_str);
00186                 tmp_str = NULL;
00187         }
00188 }
00189 
00190 /**
00191  *Destroys the current instance of #CRAttrSel.
00192  *Frees all the fields if they are non null.
00193  *@param a_this the "this pointer" of the current
00194  *instance of #CRAttrSel.
00195  */
00196 void
00197 cr_attr_sel_destroy (CRAttrSel * a_this)
00198 {
00199         g_return_if_fail (a_this);
00200 
00201         if (a_this->name) {
00202                 g_string_free (a_this->name, TRUE);
00203                 a_this->name = NULL;
00204         }
00205 
00206         if (a_this->value) {
00207                 g_string_free (a_this->value, TRUE);
00208                 a_this->value = NULL;
00209         }
00210 
00211         if (a_this->next) {
00212                 cr_attr_sel_destroy (a_this->next);
00213                 a_this->next = NULL;
00214         }
00215 
00216         if (a_this) {
00217                 g_free (a_this);
00218                 a_this = NULL;
00219         }
00220 }

Generated on Sat Mar 20 02:38:42 2004 for Libcroco by doxygen 1.3.5