cr-pseudo.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  * Author: Dodji Seketeli
00021  * See COPYRIGHTS file for copyright information.
00022  */
00023 
00024 #include "cr-pseudo.h"
00025 
00026 /**
00027  *@CRPseudo:
00028  *The definition of the #CRPseudo class.
00029  */
00030 
00031 /**
00032  * cr_pseudo_new:
00033  *Constructor of the #CRPseudo class.
00034  *
00035  *Returns the newly build instance.
00036  */
00037 CRPseudo *
00038 cr_pseudo_new (void)
00039 {
00040         CRPseudo *result = NULL;
00041 
00042         result = g_malloc0 (sizeof (CRPseudo));
00043 
00044         return result;
00045 }
00046 
00047 /**
00048  * cr_pseudo_to_string:
00049  * @a_this: the current instance of #CRPseud.
00050  * Returns the serialized pseudo. Caller must free the returned
00051  * string using g_free().
00052  */
00053 guchar *
00054 cr_pseudo_to_string (CRPseudo * a_this)
00055 {
00056         guchar *result = NULL;
00057         GString *str_buf = NULL;
00058 
00059         g_return_val_if_fail (a_this, NULL);
00060 
00061         str_buf = g_string_new (NULL);
00062 
00063         if (a_this->type == IDENT_PSEUDO) {
00064                 guchar *name = NULL;
00065 
00066                 if (a_this->name == NULL) {
00067                         goto error;
00068                 }
00069 
00070                 name = g_strndup (a_this->name->stryng->str, 
00071                                   a_this->name->stryng->len);
00072 
00073                 if (name) {
00074                         g_string_append (str_buf, name);
00075                         g_free (name);
00076                         name = NULL;
00077                 }
00078         } else if (a_this->type == FUNCTION_PSEUDO) {
00079                 guchar *name = NULL,
00080                         *arg = NULL;
00081 
00082                 if (a_this->name == NULL)
00083                         goto error;
00084 
00085                 name = g_strndup (a_this->name->stryng->str, 
00086                                   a_this->name->stryng->len);
00087 
00088                 if (a_this->extra) {
00089                         arg = g_strndup (a_this->extra->stryng->str,
00090                                          a_this->extra->stryng->len);
00091                 }
00092 
00093                 if (name) {
00094                         g_string_append_printf (str_buf, "%s(", name);
00095                         g_free (name);
00096                         name = NULL;
00097 
00098                         if (arg) {
00099                                 g_string_append (str_buf, arg);
00100                                 g_free (arg);
00101                                 arg = NULL;
00102                         }
00103 
00104                         g_string_append_c (str_buf, ')');
00105                 }
00106         }
00107 
00108         if (str_buf) {
00109                 result = str_buf->str;
00110                 g_string_free (str_buf, FALSE);
00111                 str_buf = NULL;
00112         }
00113 
00114         return result;
00115 
00116       error:
00117         g_string_free (str_buf, TRUE);
00118         return NULL;
00119 }
00120 
00121 /**
00122  * cr_pseudo_dump:
00123  *@a_this: the current instance of pseudo
00124  *@a_fp: the destination file pointer.
00125  *
00126  *Dumps the pseudo to a file.
00127  *
00128  */
00129 void
00130 cr_pseudo_dump (CRPseudo * a_this, FILE * a_fp)
00131 {
00132         guchar *tmp_str = NULL;
00133 
00134         if (a_this) {
00135                 tmp_str = cr_pseudo_to_string (a_this);
00136                 if (tmp_str) {
00137                         fprintf (a_fp, "%s", tmp_str);
00138                         g_free (tmp_str);
00139                         tmp_str = NULL;
00140                 }
00141         }
00142 }
00143 
00144 /**
00145  * cr_pseudo_destroy:
00146  *@a_this: the current instance to destroy.
00147  *
00148  *destructor of the #CRPseudo class.
00149  */
00150 void
00151 cr_pseudo_destroy (CRPseudo * a_this)
00152 {
00153         g_return_if_fail (a_this);
00154 
00155         if (a_this->name) {
00156                 cr_string_destroy (a_this->name);
00157                 a_this->name = NULL;
00158         }
00159 
00160         if (a_this->extra) {
00161                 cr_string_destroy (a_this->extra);
00162                 a_this->extra = NULL;
00163         }
00164 
00165         g_free (a_this);
00166 }

Generated on Thu Mar 9 19:19:08 2006 for Libcroco by  doxygen 1.4.6