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

cr-additional-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-additional-sel.c,v 1.1 2003/04/12 16:50:30 dodji Exp $
00025  */
00026 
00027 #include "cr-additional-sel.h"
00028 #include "string.h"
00029 
00030 /**
00031  *Default constructor of #CRAdditionalSel.
00032  *@return the newly build instance of #CRAdditionalSel.
00033  */
00034 CRAdditionalSel *
00035 cr_additional_sel_new (void)
00036 {
00037         CRAdditionalSel *result = NULL ;
00038 
00039         result = g_try_malloc (sizeof (CRAdditionalSel)) ;
00040 
00041         if (result == NULL)
00042         {
00043                 cr_utils_trace_debug ("Out of memory") ;
00044                 return NULL ;
00045         }
00046 
00047         memset (result, 0, sizeof (CRAdditionalSel)) ;
00048 
00049         return result ;
00050 }
00051 
00052 
00053 /**
00054  *Constructor of #CRAdditionalSel.
00055  *@param a_sel_type the type of the newly built instance 
00056  *of #CRAdditionalSel.
00057  *@return the newly built instance of #CRAdditionalSel.
00058  */
00059 CRAdditionalSel *
00060 cr_additional_sel_new_with_type (enum AddSelectorType a_sel_type)
00061 {
00062         CRAdditionalSel * result = NULL ;
00063 
00064         result = cr_additional_sel_new () ;
00065 
00066         g_return_val_if_fail (result, NULL) ;
00067 
00068         result->type = a_sel_type ;
00069 
00070         return result ;
00071 }
00072 
00073 /**
00074  *Sets a new class name to a
00075  *CLASS additional selector.
00076  *@param a_this the "this pointer" of the current instance
00077  *of #CRAdditionalSel .
00078  *@param a_class_name the new class name to set.
00079  *
00080  */
00081 void
00082 cr_additional_sel_set_class_name (CRAdditionalSel *a_this,
00083                                   GString *a_class_name)
00084 {
00085         g_return_if_fail (a_this 
00086                           && a_this->type == CLASS_ADD_SELECTOR) ;
00087         
00088         if (a_this->content.class_name)
00089         {
00090                 g_string_free (a_this->content.class_name, TRUE) ;
00091         }
00092 
00093         a_this->content.class_name = a_class_name ;
00094 }
00095 
00096 /**
00097  *Sets a new id name to an
00098  *ID additional selector.
00099  *@param a_this the "this pointer" of the current instance
00100  *of #CRAdditionalSel .
00101  *@param a_id the new id to set.
00102  */
00103 void
00104 cr_additional_sel_set_id_name (CRAdditionalSel *a_this,
00105                                GString *a_id)
00106 {
00107         g_return_if_fail (a_this
00108                           && a_this->type == ID_ADD_SELECTOR) ;
00109         
00110         if (a_this->content.id_name)
00111         {
00112                 g_string_free (a_this->content.id_name, TRUE) ;
00113         }
00114 
00115         a_this->content.id_name = a_id ;
00116 }
00117 
00118 /**
00119  *Sets a new pseudo to a
00120  *PSEUDO additional selector.
00121  *@param a_this the "this pointer" of the current instance
00122  *of #CRAdditionalSel .
00123  *@param a_pseudo the new pseudo to set.
00124  */
00125 void
00126 cr_additional_sel_set_pseudo (CRAdditionalSel *a_this,
00127                               CRPseudo *a_pseudo)
00128 {
00129         g_return_if_fail (a_this 
00130                           && a_this->type == PSEUDO_CLASS_ADD_SELECTOR) ;
00131 
00132         if (a_this->content.pseudo)
00133         {
00134                 cr_pseudo_destroy (a_this->content.pseudo) ;
00135         }
00136 
00137         a_this->content.pseudo = a_pseudo ;
00138 }
00139 
00140 /**
00141  *Sets a new instance of #CRAttrSel to 
00142  *a ATTRIBUTE additional selector.
00143  *@param a_this the "this pointer" of the current instance
00144  *of #CRAdditionalSel .
00145  *@param a_sel the new instance of #CRAttrSel to set.
00146  */
00147 void
00148 cr_additional_sel_set_attr_sel (CRAdditionalSel *a_this,
00149                                 CRAttrSel *a_sel)
00150 {
00151         g_return_if_fail (a_this 
00152                           && a_this->type == ATTRIBUTE_ADD_SELECTOR) ;
00153 
00154         if (a_this->content.attr_sel)
00155         {
00156                 cr_attr_sel_destroy (a_this->content.attr_sel) ;
00157         }
00158         
00159         a_this->content.attr_sel = a_sel ;
00160 }
00161 
00162 /**
00163  *Appends a new instance of #CRAdditional to the
00164  *current list of #CRAdditional.
00165  *@param a_this the "this pointer" of the current instance
00166  *of #CRAdditionalSel .
00167  *@param a_sel the new instance to #CRAdditional to append.
00168  *@return the new list of CRAdditionalSel or NULL if an error arises.
00169  */
00170 CRAdditionalSel *
00171 cr_additional_sel_append (CRAdditionalSel *a_this, 
00172                           CRAdditionalSel *a_sel)
00173 {
00174         CRAdditionalSel *cur_sel = NULL ;
00175 
00176         g_return_val_if_fail (a_sel, NULL) ;
00177 
00178         if (a_this == NULL)
00179         {
00180                 return a_sel ;
00181         }
00182 
00183         if (a_sel == NULL)
00184                 return NULL ;
00185         
00186         for (cur_sel = a_this ; 
00187              cur_sel && cur_sel->next ;
00188              cur_sel = cur_sel->next) ;
00189 
00190         g_return_val_if_fail (cur_sel != NULL, NULL) ;
00191 
00192         cur_sel->next = a_sel ;
00193         a_sel->prev = cur_sel ;
00194         
00195         return a_this ;
00196 }
00197 
00198 /**
00199  *Preppends a new instance of #CRAdditional to the
00200  *current list of #CRAdditional.
00201  *@param a_this the "this pointer" of the current instance
00202  *of #CRAdditionalSel .
00203  *@param a_sel the new instance to #CRAdditional to preappend.
00204  *@return the new list of CRAdditionalSel or NULL if an error arises.
00205  */
00206 CRAdditionalSel *
00207 cr_additional_sel_prepend (CRAdditionalSel *a_this, 
00208                            CRAdditionalSel *a_sel)
00209 {
00210         g_return_val_if_fail (a_sel, NULL) ;
00211 
00212         if (a_this == NULL)
00213         {
00214                 return a_sel ;
00215         }
00216 
00217         a_sel->next = a_this ;
00218         a_this->prev = a_sel ;
00219 
00220         return a_sel ;  
00221 }
00222 
00223 
00224 guchar *
00225 cr_additional_sel_to_string (CRAdditionalSel *a_this)
00226 {
00227         guchar * result = NULL ;
00228         GString * str_buf = NULL ;
00229         CRAdditionalSel *cur = NULL ;
00230 
00231         g_return_val_if_fail (a_this, NULL) ;
00232 
00233         str_buf = g_string_new (NULL) ;
00234 
00235         for (cur = a_this ;cur ; cur = cur->next) 
00236         {
00237                 switch (cur->type)
00238                 {
00239                 case CLASS_ADD_SELECTOR:
00240                 {
00241                         guchar * name = NULL ;
00242                         if (cur->content.class_name)
00243                         {
00244                                 name = g_strndup 
00245                                         (cur->content.class_name->str,
00246                                          cur->content.class_name->len);
00247 
00248                                 if (name)
00249                                 {
00250                                         g_string_append_printf 
00251                                                 (str_buf, ".%s", name) ;
00252                                         g_free (name) ;
00253                                         name = NULL ;
00254                                 }
00255                         }
00256                 }
00257                         break ;
00258 
00259                 case ID_ADD_SELECTOR:
00260                 {
00261                         guchar * name = NULL ;
00262                         if (cur->content.class_name)
00263                         {
00264                                 name = g_strndup 
00265                                         (cur->content.id_name->str,
00266                                          cur->content.id_name->len);
00267 
00268                                 if (name)
00269                                 {
00270                                         g_string_append_printf 
00271                                                 (str_buf, "#%s", name) ;
00272                                         g_free (name) ;
00273                                         name = NULL ;
00274                                 }
00275                         }
00276                 }
00277                         
00278                         break ;
00279 
00280                 case PSEUDO_CLASS_ADD_SELECTOR:
00281                 {
00282                         if (cur->content.pseudo)
00283                         {
00284                                 guchar *tmp_str = NULL ;
00285                                 
00286                                 tmp_str = cr_pseudo_to_string 
00287                                         (cur->content.pseudo) ;
00288                                 if (tmp_str)
00289                                 {
00290                                         g_string_append_printf 
00291                                                 (str_buf, ":%s", 
00292                                                  tmp_str) ;
00293                                         g_free (tmp_str) ;
00294                                         tmp_str = NULL ;
00295                                 }
00296                         }
00297                 }
00298                 break ;
00299                 
00300                 case ATTRIBUTE_ADD_SELECTOR:
00301                         if (cur->content.attr_sel)
00302                         {
00303                                 guchar *tmp_str = NULL ;
00304 
00305                                 g_string_append_printf (str_buf,"[") ;
00306                                 tmp_str = cr_attr_sel_to_string 
00307                                         (cur->content.attr_sel) ;
00308                                 if (tmp_str)
00309                                 {
00310                                         g_string_append_printf 
00311                                                 (str_buf, "%s]", 
00312                                                  tmp_str) ;
00313                                         g_free (tmp_str) ;
00314                                         tmp_str = NULL ;
00315                                 }
00316                         }
00317                         break ;
00318 
00319                 default:
00320                         break ;
00321                 }
00322         }
00323 
00324         if (str_buf)
00325         {
00326                 result = str_buf->str ;
00327                 g_string_free (str_buf, FALSE) ;
00328                 str_buf = NULL ;
00329         }
00330 
00331         return result ;
00332 }
00333 
00334 /**
00335  *Dumps the current instance of #CRAdditionalSel to a file
00336  *@param a_this the "this pointer" of the current instance of
00337  *#CRAdditionalSel.
00338  *@param a_fp the destination file.
00339  */
00340 void
00341 cr_additional_sel_dump (CRAdditionalSel *a_this, FILE *a_fp)
00342 {
00343         guchar *tmp_str = NULL ;
00344 
00345         g_return_if_fail (a_fp) ;
00346 
00347         if (a_this)
00348         {
00349                 tmp_str = cr_additional_sel_to_string (a_this) ;
00350                 if (tmp_str)
00351                 {
00352                         fprintf (a_fp, "%s", tmp_str) ;
00353                         g_free (tmp_str) ;
00354                         tmp_str = NULL ;
00355                 }
00356         }
00357 }
00358 
00359 /**
00360  *Destroys an instance of #CRAdditional.
00361  *@param a_this the "this pointer" of the current instance
00362  *of #CRAdditionalSel .
00363  */
00364 void
00365 cr_additional_sel_destroy (CRAdditionalSel *a_this)
00366 {
00367         g_return_if_fail (a_this) ;
00368 
00369         switch (a_this->type)
00370         {
00371         case CLASS_ADD_SELECTOR:
00372                 g_string_free (a_this->content.class_name, TRUE) ;
00373                 a_this->content.class_name = NULL ;
00374                 break ;
00375 
00376         case PSEUDO_CLASS_ADD_SELECTOR:
00377                 cr_pseudo_destroy (a_this->content.pseudo) ;
00378                 a_this->content.pseudo = NULL ;
00379                 break ;
00380                 
00381         case ID_ADD_SELECTOR:
00382                 g_string_free (a_this->content.id_name, TRUE) ;
00383                 a_this->content.id_name = NULL ;
00384                 break ;
00385 
00386         case ATTRIBUTE_ADD_SELECTOR:
00387                 cr_attr_sel_destroy (a_this->content.attr_sel) ;
00388                 a_this->content.attr_sel = NULL ;
00389                 break ;
00390 
00391         default :
00392                 break ;
00393         }
00394 
00395         if (a_this->next)
00396         {
00397                 cr_additional_sel_destroy (a_this->next) ;
00398         }
00399 
00400         g_free (a_this) ;
00401 }

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