1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
2 
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  */
23 
24 #include "cr-pseudo.h"
25 
26 /**
27  *@CRPseudo:
28  *The definition of the #CRPseudo class.
29  */
30 
31 /**
32  * cr_pseudo_new:
33  *Constructor of the #CRPseudo class.
34  *
35  *Returns the newly build instance.
36  */
37 CRPseudo *
cr_pseudo_new(void)38 cr_pseudo_new (void)
39 {
40         CRPseudo *result = NULL;
41 
42         result = g_malloc0 (sizeof (CRPseudo));
43 
44         return result;
45 }
46 
47 /**
48  * cr_pseudo_to_string:
49  * @a_this: the current instance of #CRPseud.
50  *
51  * Returns the serialized pseudo. Caller must free the returned
52  * string using g_free().
53  */
54 guchar *
cr_pseudo_to_string(CRPseudo const * a_this)55 cr_pseudo_to_string (CRPseudo const * a_this)
56 {
57         guchar *result = NULL;
58         GString *str_buf = NULL;
59 
60         g_return_val_if_fail (a_this, NULL);
61 
62         str_buf = g_string_new (NULL);
63 
64         if (a_this->type == IDENT_PSEUDO) {
65                 gchar const *name = NULL;
66 
67                 if (a_this->name == NULL) {
68                         goto error;
69                 }
70 
71                 name = a_this->name->stryng->str;
72 
73                 if (name) {
74                         g_string_append (str_buf, (const gchar *) name);
75                         name = NULL;
76                 }
77         } else if (a_this->type == FUNCTION_PSEUDO) {
78                 gchar const *name = NULL,
79                         *arg = NULL;
80 
81                 if (a_this->name == NULL)
82                         goto error;
83 
84                 name = a_this->name->stryng->str;
85 
86                 if (a_this->term && a_this->term->type == TERM_IDENT) {
87                         arg = a_this->term->content.str->stryng->str;
88                 }
89 
90                 if (name) {
91                         g_string_append_printf (str_buf, "%s(", name);
92                         name = NULL;
93 
94                         if (arg) {
95                                 g_string_append (str_buf, (const gchar *) arg);
96                                 arg = NULL;
97                         }
98 
99                         g_string_append_c (str_buf, ')');
100                 }
101         }
102 
103         if (str_buf) {
104                 result = (guchar *) str_buf->str;
105                 g_string_free (str_buf, FALSE);
106                 str_buf = NULL;
107         }
108 
109         return result;
110 
111       error:
112         g_string_free (str_buf, TRUE);
113         return NULL;
114 }
115 
116 /**
117  * cr_pseudo_dump:
118  *@a_this: the current instance of pseudo
119  *@a_fp: the destination file pointer.
120  *
121  *Dumps the pseudo to a file.
122  *
123  */
124 void
cr_pseudo_dump(CRPseudo const * a_this,FILE * a_fp)125 cr_pseudo_dump (CRPseudo const * a_this, FILE * a_fp)
126 {
127         guchar *tmp_str = NULL;
128 
129         if (a_this) {
130                 tmp_str = cr_pseudo_to_string (a_this);
131                 if (tmp_str) {
132                         fprintf (a_fp, "%s", tmp_str);
133                         g_free (tmp_str);
134                         tmp_str = NULL;
135                 }
136         }
137 }
138 
139 /**
140  * cr_pseudo_destroy:
141  *@a_this: the current instance to destroy.
142  *
143  *destructor of the #CRPseudo class.
144  */
145 void
cr_pseudo_destroy(CRPseudo * a_this)146 cr_pseudo_destroy (CRPseudo * a_this)
147 {
148         g_return_if_fail (a_this);
149 
150         if (a_this->name) {
151                 cr_string_destroy (a_this->name);
152                 a_this->name = NULL;
153         }
154 
155         if (a_this->sel_name) {
156                 cr_string_destroy (a_this->sel_name);
157                 a_this->sel_name = NULL;
158         }
159 
160         if (a_this->term) {
161                 cr_term_destroy (a_this->term);
162                 a_this->term = NULL;
163         }
164 
165         g_free (a_this);
166 }
167