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

cr-selector.c

Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode: ni; 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-selector.c,v 1.2 2003/06/15 18:00:30 dodji Exp $
00025  */
00026 #include <string.h>
00027 #include "cr-selector.h"
00028 #include "cr-parser.h"
00029 
00030 /**
00031  *Creates a new instance of #CRSelector.
00032  *@param a_simple_sel the initial simple selector list
00033  *of the current instance of #CRSelector.
00034  *@return the newly built instance of #CRSelector, or
00035  *NULL in case of failure.
00036  */
00037 CRSelector*
00038 cr_selector_new (CRSimpleSel *a_simple_sel)
00039 {
00040         CRSelector *result = NULL ;
00041 
00042         result = g_try_malloc (sizeof (CRSelector)) ;
00043         if (!result)
00044         {
00045                 cr_utils_trace_info ("Out of memory") ;
00046                 return NULL ;
00047         }
00048         memset (result, 0, sizeof (CRSelector)) ;
00049         result->simple_sel = a_simple_sel ;
00050         return result ;
00051 }
00052 
00053 CRSelector *
00054 cr_selector_parse_from_buf (const guchar * a_char_buf,
00055                             enum CREncoding a_enc)
00056 {
00057         CRParser * parser = NULL ;
00058         
00059         g_return_val_if_fail (a_char_buf, NULL) ;
00060 
00061         parser = cr_parser_new_from_buf (a_char_buf, strlen (a_char_buf),
00062                                          a_enc, FALSE) ;
00063         g_return_val_if_fail (parser, NULL) ;
00064 
00065         
00066         return NULL ;
00067 }
00068 
00069 /**
00070  *Appends a new instance of #CRSelector to the current selector list.
00071  *@param a_this the current instance of #CRSelector.
00072  *@param a_new the instance of #CRSelector to be appended.
00073  *@return the new list.
00074  */
00075 CRSelector*
00076 cr_selector_append (CRSelector *a_this, CRSelector *a_new)
00077 {
00078         CRSelector *cur = NULL ;
00079         
00080         if (!a_this)
00081         {
00082                 return a_new ;
00083         }
00084 
00085         /*walk forward the list headed by a_this to get the list tail*/
00086         for (cur = a_this ; cur && cur->next ; cur = cur->next) ;
00087 
00088         cur->next = a_new ;
00089         a_new->prev = cur ;
00090 
00091         return  a_this ;
00092 }
00093 
00094 /**
00095  *Prepends an element to the #CRSelector list.
00096  *@param a_this the current instance of #CRSelector list.
00097  *@param a_new the instance of #CRSelector.
00098  *@return the new list.
00099  */
00100 CRSelector*
00101 cr_selector_prepend (CRSelector *a_this, CRSelector *a_new)
00102 {
00103         CRSelector *cur = NULL ;
00104 
00105         a_new->next = a_this ;
00106         a_this->prev = a_new ;
00107         
00108         for (cur = a_new ; cur && cur->prev ; cur = cur->prev) ;
00109         
00110         return cur ;
00111 }
00112 
00113 /**
00114  *append a simple selector to the current #CRSelector list.
00115  *@param a_this the current instance of #CRSelector.
00116  *@param a_simple_sel the simple selector to append.
00117  *@return the new list or NULL in case of failure.
00118  */
00119 CRSelector*
00120 cr_selector_append_simple_sel (CRSelector *a_this, CRSimpleSel *a_simple_sel)
00121 {
00122         CRSelector * selector = NULL ;
00123 
00124         selector = cr_selector_new (a_simple_sel) ;
00125         g_return_val_if_fail (selector, NULL) ;
00126 
00127         return cr_selector_append (a_this, selector) ;
00128 }
00129 
00130 
00131 guchar *
00132 cr_selector_to_string (CRSelector *a_this)
00133 {
00134         guchar *result = NULL ;
00135         GString *str_buf = NULL ;
00136 
00137         str_buf = g_string_new (NULL) ;
00138         g_return_val_if_fail (str_buf, NULL) ;
00139 
00140         if (a_this)
00141         {
00142                 CRSelector *cur = NULL ;
00143                 
00144                 for (cur = a_this ; cur ; cur = cur->next)
00145                 {
00146                         if (cur->simple_sel)
00147                         {
00148                                 guchar *tmp_str = NULL ;
00149 
00150                                 tmp_str = cr_simple_sel_to_string 
00151                                         (cur->simple_sel) ;
00152                                 
00153                                 if (tmp_str)
00154                                 {
00155                                         if (cur->prev)
00156                                                 g_string_append_printf 
00157                                                         (str_buf,", ") ;
00158 
00159                                         g_string_append_printf 
00160                                                 (str_buf, "%s", 
00161                                                  tmp_str) ;
00162 
00163                                         g_free (tmp_str) ;
00164                                         tmp_str = NULL ;
00165                                 }
00166                         }
00167                 }
00168         }
00169 
00170         if (str_buf)
00171         {
00172                 result = str_buf->str ;
00173                 g_string_free (str_buf, FALSE) ;
00174                 str_buf = NULL ;
00175         }
00176 
00177         return result ;
00178 }
00179 
00180 
00181 /**
00182  *Serializes the current instance of #CRSelector to a file.
00183  *@param a_this the current instance of #CRSelector.
00184  *@param a_fp the destination file.
00185  */
00186 void
00187 cr_selector_dump (CRSelector *a_this, FILE *a_fp)
00188 {
00189         guchar *tmp_buf = NULL ;
00190         
00191         if (a_this)
00192         {
00193                 tmp_buf = cr_selector_to_string (a_this) ;
00194                 if (tmp_buf)
00195                 {
00196                         fprintf (a_fp, "%s", tmp_buf) ;
00197                         g_free (tmp_buf) ;
00198                         tmp_buf = NULL ;
00199                 }
00200         }
00201 }
00202 
00203 /**
00204  *Increments the ref count of the current instance
00205  *of #CRSelector.
00206  *@param a_this the current instance of #CRSelector.
00207  */
00208 void
00209 cr_selector_ref (CRSelector *a_this)
00210 {
00211         g_return_if_fail (a_this) ;
00212         
00213         a_this->ref_count ++ ;
00214 }
00215 
00216 /**
00217  *Decrements the ref count of the current instance of
00218  *#CRSelector.
00219  *If the ref count reaches zero, the current instance of
00220  *#CRSelector is destroyed.
00221  *@param a_this the current instance of #CRSelector.
00222  *@return TRUE if this function destroyed the current instance
00223  *of #CRSelector, FALSE otherwise.
00224  */
00225 gboolean
00226 cr_selector_unref (CRSelector *a_this)
00227 {
00228         g_return_val_if_fail (a_this, FALSE) ;
00229 
00230         if (a_this->ref_count)
00231         {
00232                 a_this->ref_count -- ;
00233         }
00234 
00235         if (a_this->ref_count == 0)
00236         {
00237                 cr_selector_destroy (a_this) ;
00238                 return TRUE ;
00239         }
00240         
00241         return FALSE ;
00242 }
00243 
00244 /**
00245  *Destroys the selector list.
00246  *@param a_this the current instance of #CRSelector.
00247  */
00248 void
00249 cr_selector_destroy (CRSelector *a_this)
00250 {
00251         CRSelector *cur = NULL ;
00252 
00253         g_return_if_fail (a_this) ;
00254         
00255         /*
00256          *go and get the list tail. In the same time, free
00257          *all the simple selectors contained in the list.
00258          */
00259         for (cur = a_this ;cur && cur->next ; cur = cur->next)
00260         {
00261                 if (cur->simple_sel)
00262                 {
00263                         cr_simple_sel_destroy (cur->simple_sel) ;
00264                         cur->simple_sel = NULL ;
00265                 }
00266         }
00267 
00268         if (cur)
00269         {
00270                 if (cur->simple_sel)
00271                 {
00272                         cr_simple_sel_destroy (cur->simple_sel) ;
00273                         cur->simple_sel = NULL ;
00274                 }
00275         }
00276 
00277         /*in case the list has only one element*/
00278         if (cur && !cur->prev)
00279         {
00280                 g_free (cur) ;
00281                 return ;
00282         }
00283 
00284         /*walk backward the list and free each "next element"*/
00285         for (cur = cur->prev ; cur && cur->prev ; cur = cur->prev)
00286         {
00287                 if (cur->next)
00288                 {
00289                         g_free (cur->next) ;
00290                         cur->next = NULL ;
00291                 }
00292         }
00293 
00294         if (!cur)
00295                 return ;
00296         
00297         if (cur->next)
00298         {
00299                 g_free (cur->next) ;
00300                 cur->next = NULL ;
00301         }
00302 
00303         g_free (cur) ;
00304 }

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