Libcroco
cr-pseudo.c
Go to the documentation of this file.
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 *
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 *
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  guchar *name = NULL;
66 
67  if (a_this->name == NULL) {
68  goto error;
69  }
70 
71  name = (guchar *) g_strndup (a_this->name->stryng->str,
72  a_this->name->stryng->len);
73 
74  if (name) {
75  g_string_append (str_buf, (const gchar *) name);
76  g_free (name);
77  name = NULL;
78  }
79  } else if (a_this->type == FUNCTION_PSEUDO) {
80  guchar *name = NULL,
81  *arg = NULL;
82 
83  if (a_this->name == NULL)
84  goto error;
85 
86  name = (guchar *) g_strndup (a_this->name->stryng->str,
87  a_this->name->stryng->len);
88 
89  if (a_this->extra) {
90  arg = (guchar *) g_strndup (a_this->extra->stryng->str,
91  a_this->extra->stryng->len);
92  }
93 
94  if (name) {
95  g_string_append_printf (str_buf, "%s(", name);
96  g_free (name);
97  name = NULL;
98 
99  if (arg) {
100  g_string_append (str_buf, (const gchar *) arg);
101  g_free (arg);
102  arg = NULL;
103  }
104 
105  g_string_append_c (str_buf, ')');
106  }
107  }
108 
109  if (str_buf) {
110  result = (guchar *) str_buf->str;
111  g_string_free (str_buf, FALSE);
112  str_buf = NULL;
113  }
114 
115  return result;
116 
117  error:
118  g_string_free (str_buf, TRUE);
119  return NULL;
120 }
121 
122 /**
123  * cr_pseudo_dump:
124  *@a_this: the current instance of pseudo
125  *@a_fp: the destination file pointer.
126  *
127  *Dumps the pseudo to a file.
128  *
129  */
130 void
131 cr_pseudo_dump (CRPseudo const * a_this, FILE * a_fp)
132 {
133  guchar *tmp_str = NULL;
134 
135  if (a_this) {
136  tmp_str = cr_pseudo_to_string (a_this);
137  if (tmp_str) {
138  fprintf (a_fp, "%s", tmp_str);
139  g_free (tmp_str);
140  tmp_str = NULL;
141  }
142  }
143 }
144 
145 /**
146  * cr_pseudo_destroy:
147  *@a_this: the current instance to destroy.
148  *
149  *destructor of the #CRPseudo class.
150  */
151 void
153 {
154  g_return_if_fail (a_this);
155 
156  if (a_this->name) {
157  cr_string_destroy (a_this->name);
158  a_this->name = NULL;
159  }
160 
161  if (a_this->extra) {
162  cr_string_destroy (a_this->extra);
163  a_this->extra = NULL;
164  }
165 
166  g_free (a_this);
167 }
CRPseudo * cr_pseudo_new(void)
@CRPseudo: The definition of the CRPseudo class.
Definition: cr-pseudo.c:38
guchar * cr_pseudo_to_string(CRPseudo const *a_this)
cr_pseudo_to_string: @a_this: the current instance of #CRPseud.
Definition: cr-pseudo.c:55
enum CRPseudoType type
Definition: cr-pseudo.h:48
void cr_string_destroy(CRString *a_this)
Definition: cr-string.c:159
CRString * name
Definition: cr-pseudo.h:49
void cr_pseudo_destroy(CRPseudo *a_this)
cr_pseudo_destroy: @a_this: the current instance to destroy.
Definition: cr-pseudo.c:152
CRString * extra
Definition: cr-pseudo.h:50
void cr_pseudo_dump(CRPseudo const *a_this, FILE *a_fp)
cr_pseudo_dump: @a_this: the current instance of pseudo @a_fp: the destination file pointer.
Definition: cr-pseudo.c:131
The CRPseudo Class.
Definition: cr-pseudo.h:46