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.1 2003/04/12 16:50:30 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 /**
00040  *The constructor of #CRAttrSel.
00041  *@return the newly allocated instance
00042  *of #CRAttrSel.
00043  */
00044 CRAttrSel *
00045 cr_attr_sel_new (void)
00046 {
00047         CRAttrSel *result = NULL ;
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, 
00068                               CR_BAD_PARAM_ERROR) ;
00069         
00070         for (cur_sel = a_this ; cur_sel->next ; cur_sel = cur_sel->next) ;
00071         
00072         cur_sel->next = a_attr_sel ;
00073         a_attr_sel->prev = cur_sel ;
00074         
00075         return CR_OK ;
00076 }
00077 
00078 /**
00079  *Prepends an attribute selector to the list of
00080  *attributes selector represented by a_this.
00081  *
00082  *@param a_this the "this pointer" of the current instance
00083  *of #CRAttrSel.
00084  *@param a_attr_sel the attribute selector to append.
00085  *@return CR_OK upon successfull completion, an error code otherwise.
00086  */
00087 enum CRStatus
00088 cr_attr_sel_prepend_attr_sel (CRAttrSel *a_this, CRAttrSel *a_attr_sel)
00089 {
00090         g_return_val_if_fail (a_this && a_attr_sel, CR_BAD_PARAM_ERROR);
00091 
00092         a_attr_sel->next = a_this ;
00093         a_this->prev = a_attr_sel ;
00094 
00095         return CR_OK ;
00096 }
00097 
00098 guchar *
00099 cr_attr_sel_to_string (CRAttrSel *a_this)
00100 {
00101         CRAttrSel *cur = NULL ;
00102         guchar * result = NULL ;
00103         GString *str_buf = NULL ;
00104 
00105         g_return_val_if_fail (a_this, NULL) ;
00106 
00107         str_buf = g_string_new (NULL) ;
00108 
00109         for (cur = a_this ; cur ; cur = cur->next)
00110         {
00111                 if (cur->prev)
00112                 {
00113                         g_string_append_printf (str_buf," ") ;
00114                 }
00115 
00116                 if (cur->name)
00117                 {
00118                         guchar *name = NULL ;
00119 
00120                         name = g_strndup (cur->name->str,
00121                                           cur->name->len) ;
00122                         if (name)
00123                         {
00124                                 g_string_append_printf (str_buf, 
00125                                                         "%s", name) ;
00126                                 g_free (name) ;
00127                                 name = NULL ;
00128                         }
00129                 }
00130         
00131                 if (cur->value)
00132                 {
00133                         guchar *value = NULL ;
00134 
00135                         value = g_strndup (cur->value->str,
00136                                            cur->value->len) ;
00137                         if (value)
00138                         {
00139                                 switch (cur->match_way)
00140                                 {
00141                                 case SET:
00142                                         break ;
00143 
00144                                 case EQUALS:
00145                                         g_string_append_printf 
00146                                                 (str_buf,"=") ;
00147                                         break ;
00148 
00149                                 case INCLUDES:
00150                                         g_string_append_printf 
00151                                                 (str_buf,"~=") ;
00152                                         break ;
00153 
00154                                 case DASHMATCH:
00155                                         g_string_append_printf 
00156                                                 (str_buf,"|=") ;
00157                                                 break ;
00158 
00159                                 default:
00160                                         break ;
00161                                 }
00162                         
00163                                 g_string_append_printf 
00164                                         (str_buf,"\"%s\"",value) ;
00165 
00166                                 g_free (value) ;
00167                                 value = NULL ;
00168                         }
00169                 }
00170         }
00171 
00172         if (str_buf)
00173         {
00174                 result = str_buf->str ;
00175                 g_string_free (str_buf, FALSE) ;                
00176         }
00177 
00178         return result ;
00179 }
00180 
00181 /**
00182  *Dumps the current instance of #CRAttrSel to a file.
00183  *@param a_this the "this pointer" of the current instance of
00184  *#CRAttrSel.
00185  *@param a_fp the destination file.
00186  */
00187 void
00188 cr_attr_sel_dump (CRAttrSel *a_this, FILE *a_fp)
00189 {
00190         guchar * tmp_str = NULL ;
00191 
00192         g_return_if_fail (a_this) ;
00193 
00194         tmp_str = cr_attr_sel_to_string (a_this) ;
00195 
00196         if (tmp_str)
00197         {
00198                 fprintf (a_fp, "%s", tmp_str) ;
00199                 g_free (tmp_str) ;
00200                 tmp_str = NULL ;
00201         }
00202 }
00203 
00204 
00205 /**
00206  *Destroys the current instance of #CRAttrSel.
00207  *Frees all the fields if they are non null.
00208  *@param a_this the "this pointer" of the current
00209  *instance of #CRAttrSel.
00210  */
00211 void
00212 cr_attr_sel_destroy (CRAttrSel *a_this)
00213 {
00214         g_return_if_fail (a_this) ;
00215 
00216         if (a_this->name)
00217         {
00218                 g_string_free (a_this->name, TRUE) ;
00219                 a_this->name = NULL ;
00220         }
00221 
00222         if (a_this->value)
00223         {
00224                 g_string_free (a_this->value, TRUE) ;
00225                 a_this->value = NULL ;
00226         }
00227 
00228         if (a_this->next)
00229         {
00230                 cr_attr_sel_destroy (a_this->next) ;
00231                 a_this->next = NULL ;
00232         }
00233 
00234         if (a_this)
00235         {
00236                 g_free (a_this) ;
00237                 a_this = NULL ;
00238         }
00239 }

Generated on Wed Oct 1 01:36:44 2003 for Libcroco by doxygen 1.3.3