Main Page | Alphabetical List | Data Structures | Directories | 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  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of version 2.1 of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00018  * USA
00019  * 
00020  * See COPYRIGHTS file for copyrights information.
00021  */
00022 
00023 #include <stdio.h>
00024 #include "cr-attr-sel.h"
00025 
00026 /**
00027  *@file
00028  *The class that abstracts an attribute selector.
00029  *Attributes selectors are described in the css2 spec [5.8].
00030  *There are more generally used in the css2 selectors described in
00031  *css2 spec [5] .
00032  */
00033 
00034 /**
00035  *The constructor of #CRAttrSel.
00036  *@return the newly allocated instance
00037  *of #CRAttrSel.
00038  */
00039 CRAttrSel *
00040 cr_attr_sel_new (void)
00041 {
00042         CRAttrSel *result = NULL;
00043 
00044         result = g_malloc0 (sizeof (CRAttrSel));
00045 
00046         return result;
00047 }
00048 
00049 /**
00050  *Appends an attribute selector to the current list of
00051  *attribute selectors represented by a_this.
00052  *
00053  *@param a_this the this pointer of the current instance of
00054  *#CRAttrSel.
00055  *@param a_attr_sel selector to append.
00056  *@return CR_OK upon successfull completion, an error code otherwise.
00057  */
00058 enum CRStatus
00059 cr_attr_sel_append_attr_sel (CRAttrSel * a_this, CRAttrSel * a_attr_sel)
00060 {
00061         CRAttrSel *cur_sel = NULL;
00062 
00063         g_return_val_if_fail (a_this && a_attr_sel, 
00064                               CR_BAD_PARAM_ERROR);
00065 
00066         for (cur_sel = a_this; 
00067              cur_sel->next; 
00068              cur_sel = cur_sel->next) ;
00069 
00070         cur_sel->next = a_attr_sel;
00071         a_attr_sel->prev = cur_sel;
00072 
00073         return CR_OK;
00074 }
00075 
00076 /**
00077  *Prepends an attribute selector to the list of
00078  *attributes selector represented by a_this.
00079  *
00080  *@param a_this the "this pointer" of the current instance
00081  *of #CRAttrSel.
00082  *@param a_attr_sel the attribute selector to append.
00083  *@return CR_OK upon successfull completion, an error code otherwise.
00084  */
00085 enum CRStatus
00086 cr_attr_sel_prepend_attr_sel (CRAttrSel * a_this, 
00087                               CRAttrSel * a_attr_sel)
00088 {
00089         g_return_val_if_fail (a_this && a_attr_sel, 
00090                               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                 if (cur->prev) {
00111                         g_string_append_c (str_buf, ' ');
00112                 }
00113 
00114                 if (cur->name) {
00115                         guchar *name = NULL;
00116 
00117                         name = g_strndup (cur->name->stryng->str, 
00118                                           cur->name->stryng->len);
00119                         if (name) {
00120                                 g_string_append (str_buf, name);
00121                                 g_free (name);
00122                                 name = NULL;
00123                         }
00124                 }
00125 
00126                 if (cur->value) {
00127                         guchar *value = NULL;
00128 
00129                         value = g_strndup (cur->value->stryng->str, 
00130                                            cur->value->stryng->len);
00131                         if (value) {
00132                                 switch (cur->match_way) {
00133                                 case SET:
00134                                         break;
00135 
00136                                 case EQUALS:
00137                                         g_string_append_c (str_buf, '=');
00138                                         break;
00139 
00140                                 case INCLUDES:
00141                                         g_string_append (str_buf, "~=");
00142                                         break;
00143 
00144                                 case DASHMATCH:
00145                                         g_string_append (str_buf, "|=");
00146                                         break;
00147 
00148                                 default:
00149                                         break;
00150                                 }
00151 
00152                                 g_string_append_printf
00153                                         (str_buf, "\"%s\"", value);
00154 
00155                                 g_free (value);
00156                                 value = NULL;
00157                         }
00158                 }
00159         }
00160 
00161         if (str_buf) {
00162                 result = str_buf->str;
00163                 g_string_free (str_buf, FALSE);
00164         }
00165 
00166         return result;
00167 }
00168 
00169 /**
00170  *Dumps the current instance of #CRAttrSel to a file.
00171  *@param a_this the "this pointer" of the current instance of
00172  *#CRAttrSel.
00173  *@param a_fp the destination file.
00174  */
00175 void
00176 cr_attr_sel_dump (CRAttrSel * a_this, FILE * a_fp)
00177 {
00178         guchar *tmp_str = NULL;
00179 
00180         g_return_if_fail (a_this);
00181 
00182         tmp_str = cr_attr_sel_to_string (a_this);
00183 
00184         if (tmp_str) {
00185                 fprintf (a_fp, "%s", tmp_str);
00186                 g_free (tmp_str);
00187                 tmp_str = NULL;
00188         }
00189 }
00190 
00191 /**
00192  *Destroys the current instance of #CRAttrSel.
00193  *Frees all the fields if they are non null.
00194  *@param a_this the "this pointer" of the current
00195  *instance of #CRAttrSel.
00196  */
00197 void
00198 cr_attr_sel_destroy (CRAttrSel * a_this)
00199 {
00200         g_return_if_fail (a_this);
00201 
00202         if (a_this->name) {
00203                 cr_string_destroy (a_this->name);
00204                 a_this->name = NULL;
00205         }
00206 
00207         if (a_this->value) {
00208                 cr_string_destroy (a_this->value);
00209                 a_this->value = NULL;
00210         }
00211 
00212         if (a_this->next) {
00213                 cr_attr_sel_destroy (a_this->next);
00214                 a_this->next = NULL;
00215         }
00216 
00217         if (a_this) {
00218                 g_free (a_this);
00219                 a_this = NULL;
00220         }
00221 }

Generated on Fri Oct 29 08:29:11 2004 for Libcroco by  doxygen 1.3.9.1