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

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