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

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