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

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