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 = NULL;
40 result = g_malloc0 (sizeof (CRPseudo));
42 return result;
43 }
45 guchar *
46 cr_pseudo_to_string (CRPseudo * a_this)
47 {
48 guchar *result = NULL;
49 GString *str_buf = NULL;
51 g_return_val_if_fail (a_this, NULL);
53 str_buf = g_string_new (NULL);
55 if (a_this->type == IDENT_PSEUDO) {
56 guchar *name = NULL;
58 if (a_this->name == NULL) {
59 goto error;
60 }
62 name = g_strndup (a_this->name->stryng->str,
63 a_this->name->stryng->len);
65 if (name) {
66 g_string_append (str_buf, name);
67 g_free (name);
68 name = NULL;
69 }
70 } else if (a_this->type == FUNCTION_PSEUDO) {
71 guchar *name = NULL,
72 *arg = NULL;
74 if (a_this->name == NULL)
75 goto error;
77 name = g_strndup (a_this->name->stryng->str,
78 a_this->name->stryng->len);
80 if (a_this->extra) {
81 arg = g_strndup (a_this->extra->stryng->str,
82 a_this->extra->stryng->len);
83 }
85 if (name) {
86 g_string_append_printf (str_buf, "%s(", name);
87 g_free (name);
88 name = NULL;
90 if (arg) {
91 g_string_append (str_buf, arg);
92 g_free (arg);
93 arg = NULL;
94 }
96 g_string_append_c (str_buf, ')');
97 }
98 }
100 if (str_buf) {
101 result = str_buf->str;
102 g_string_free (str_buf, FALSE);
103 str_buf = NULL;
104 }
106 return result;
108 error:
109 g_string_free (str_buf, TRUE);
110 return NULL;
111 }
113 /**
114 *Dumps the pseudo to a file.
115 *@param a_this the current instance of pseudo
116 *@param a_fp the destination file pointer.
117 */
118 void
119 cr_pseudo_dump (CRPseudo * a_this, FILE * a_fp)
120 {
121 guchar *tmp_str = NULL;
123 if (a_this) {
124 tmp_str = cr_pseudo_to_string (a_this);
125 if (tmp_str) {
126 fprintf (a_fp, "%s", tmp_str);
127 g_free (tmp_str);
128 tmp_str = NULL;
129 }
130 }
131 }
133 /**
134 *destructor of the #CRPseudo class.
135 *@param a_this the current instance to destroy.
136 */
137 void
138 cr_pseudo_destroy (CRPseudo * a_this)
139 {
140 g_return_if_fail (a_this);
142 if (a_this->name) {
143 cr_string_destroy (a_this->name);
144 a_this->name = NULL;
145 }
147 if (a_this->extra) {
148 cr_string_destroy (a_this->extra);
149 a_this->extra = NULL;
150 }
152 g_free (a_this);
153 }