1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
3 /*
4 * This file is part of The Croco Library
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2.1 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 *
20 * Author: Dodji Seketeli
21 * See COPYRIGHTS file for copyright information.
22 */
24 #include "cr-pseudo.h"
26 /**
27 *@file
28 *The definition of the #CRPseudo class.
29 */
31 /**
32 *Constructor of the #CRPseudo class.
33 *@return the newly build instance.
34 */
35 CRPseudo *
36 cr_pseudo_new (void)
37 {
38 CRPseudo *result = (CRPseudo *)g_malloc0 (sizeof (CRPseudo));
40 return result;
41 }
43 guchar *
44 cr_pseudo_to_string (CRPseudo * a_this)
45 {
46 guchar *result = NULL;
48 g_return_val_if_fail (a_this, NULL);
50 GString *str_buf = (GString *)g_string_new (NULL);
52 if (a_this->type == IDENT_PSEUDO) {
54 if (a_this->name == NULL)
55 goto error;
57 gchar *name = g_strndup (a_this->name->stryng->str,
58 a_this->name->stryng->len);
60 if (name) {
61 g_string_append (str_buf, name);
62 g_free (name);
63 name = NULL;
64 }
65 } else if (a_this->type == FUNCTION_PSEUDO) {
66 gchar *arg = NULL;
68 if (a_this->name == NULL)
69 goto error;
71 gchar *name = g_strndup (a_this->name->stryng->str,
72 a_this->name->stryng->len);
74 if (a_this->extra) {
75 arg = g_strndup (a_this->extra->stryng->str,
76 a_this->extra->stryng->len);
77 }
79 if (name) {
80 g_string_append_printf (str_buf, "%s(", name);
81 g_free (name);
82 name = NULL;
84 if (arg) {
85 g_string_append (str_buf, arg);
86 g_free (arg);
87 arg = NULL;
88 }
90 g_string_append_c (str_buf, ')');
91 }
92 }
94 if (str_buf) {
95 result = (guchar *)str_buf->str;
96 g_string_free (str_buf, FALSE);
97 str_buf = NULL;
98 }
100 return result;
102 error:
103 g_string_free (str_buf, TRUE);
104 return NULL;
105 }
107 /**
108 *Dumps the pseudo to a file.
109 *@param a_this the current instance of pseudo
110 *@param a_fp the destination file pointer.
111 */
112 void
113 cr_pseudo_dump (CRPseudo * a_this, FILE * a_fp)
114 {
115 guchar *tmp_str = NULL;
117 if (a_this) {
118 tmp_str = cr_pseudo_to_string (a_this);
119 if (tmp_str) {
120 fprintf (a_fp, "%s", tmp_str);
121 g_free (tmp_str);
122 tmp_str = NULL;
123 }
124 }
125 }
127 /**
128 *destructor of the #CRPseudo class.
129 *@param a_this the current instance to destroy.
130 */
131 void
132 cr_pseudo_destroy (CRPseudo * a_this)
133 {
134 g_return_if_fail (a_this);
136 if (a_this->name) {
137 cr_string_destroy (a_this->name);
138 a_this->name = NULL;
139 }
141 if (a_this->extra) {
142 cr_string_destroy (a_this->extra);
143 a_this->extra = NULL;
144 }
146 g_free (a_this);
147 }