Libcroco
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  *
00051  * Returns the serialized pseudo. Caller must free the returned
00052  * string using g_free().
00053  */
00054 guchar *
00055 cr_pseudo_to_string (CRPseudo const * a_this)
00056 {
00057         guchar *result = NULL;
00058         GString *str_buf = NULL;
00059 
00060         g_return_val_if_fail (a_this, NULL);
00061 
00062         str_buf = g_string_new (NULL);
00063 
00064         if (a_this->type == IDENT_PSEUDO) {
00065                 guchar *name = NULL;
00066 
00067                 if (a_this->name == NULL) {
00068                         goto error;
00069                 }
00070 
00071                 name = (guchar *) g_strndup (a_this->name->stryng->str, 
00072                                   a_this->name->stryng->len);
00073 
00074                 if (name) {
00075                         g_string_append (str_buf, (const gchar *) name);
00076                         g_free (name);
00077                         name = NULL;
00078                 }
00079         } else if (a_this->type == FUNCTION_PSEUDO) {
00080                 guchar *name = NULL,
00081                         *arg = NULL;
00082 
00083                 if (a_this->name == NULL)
00084                         goto error;
00085 
00086                 name = (guchar *) g_strndup (a_this->name->stryng->str, 
00087                                   a_this->name->stryng->len);
00088 
00089                 if (a_this->extra) {
00090                         arg = (guchar *) g_strndup (a_this->extra->stryng->str,
00091                                          a_this->extra->stryng->len);
00092                 }
00093 
00094                 if (name) {
00095                         g_string_append_printf (str_buf, "%s(", name);
00096                         g_free (name);
00097                         name = NULL;
00098 
00099                         if (arg) {
00100                                 g_string_append (str_buf, (const gchar *) arg);
00101                                 g_free (arg);
00102                                 arg = NULL;
00103                         }
00104 
00105                         g_string_append_c (str_buf, ')');
00106                 }
00107         }
00108 
00109         if (str_buf) {
00110                 result = (guchar *) str_buf->str;
00111                 g_string_free (str_buf, FALSE);
00112                 str_buf = NULL;
00113         }
00114 
00115         return result;
00116 
00117       error:
00118         g_string_free (str_buf, TRUE);
00119         return NULL;
00120 }
00121 
00122 /**
00123  * cr_pseudo_dump:
00124  *@a_this: the current instance of pseudo
00125  *@a_fp: the destination file pointer.
00126  *
00127  *Dumps the pseudo to a file.
00128  *
00129  */
00130 void
00131 cr_pseudo_dump (CRPseudo const * a_this, FILE * a_fp)
00132 {
00133         guchar *tmp_str = NULL;
00134 
00135         if (a_this) {
00136                 tmp_str = cr_pseudo_to_string (a_this);
00137                 if (tmp_str) {
00138                         fprintf (a_fp, "%s", tmp_str);
00139                         g_free (tmp_str);
00140                         tmp_str = NULL;
00141                 }
00142         }
00143 }
00144 
00145 /**
00146  * cr_pseudo_destroy:
00147  *@a_this: the current instance to destroy.
00148  *
00149  *destructor of the #CRPseudo class.
00150  */
00151 void
00152 cr_pseudo_destroy (CRPseudo * a_this)
00153 {
00154         g_return_if_fail (a_this);
00155 
00156         if (a_this->name) {
00157                 cr_string_destroy (a_this->name);
00158                 a_this->name = NULL;
00159         }
00160 
00161         if (a_this->extra) {
00162                 cr_string_destroy (a_this->extra);
00163                 a_this->extra = NULL;
00164         }
00165 
00166         g_free (a_this);
00167 }