summaryrefslogtreecommitdiff
path: root/src/cr-pseudo.c
blob: 1e0cd698e73d15fb6432c3bd02df33589fa7e87b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */

/*
 * This file is part of The Croco Library
 *
 * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

/*
 *$Id$
 */

#include "cr-pseudo.h"

/**
 *@file
 *The definition of the #CRPseudo class.
 */

/**
 *Constructor of the #CRPseudo class.
 *@return the newly build instance.
 */
CRPseudo *
cr_pseudo_new (void)
{
        CRPseudo *result = NULL;

        result = g_malloc0 (sizeof (CRPseudo));

        return result;
}

guchar *
cr_pseudo_to_string (CRPseudo * a_this)
{
        guchar *result = NULL;
        GString *str_buf = NULL;

        g_return_val_if_fail (a_this, NULL);

        str_buf = g_string_new (NULL);

        if (a_this->type == IDENT_PSEUDO) {
                guchar *name = NULL;

                if (a_this->name == NULL) {
                        goto error;
                }

                name = g_strndup (a_this->name->str, a_this->name->len);

                if (name) {
                        g_string_append_printf (str_buf, "%s", name);
                        g_free (name);
                        name = NULL;
                }
        } else if (a_this->type == FUNCTION_PSEUDO) {
                guchar *name = NULL,
                        *arg = NULL;

                if (a_this->name == NULL)
                        goto error;

                name = g_strndup (a_this->name->str, a_this->name->len);

                if (a_this->extra) {
                        arg = g_strndup (a_this->extra->str,
                                         a_this->extra->len);
                }

                if (name) {
                        g_string_append_printf (str_buf, "%s(", name);
                        g_free (name);
                        name = NULL;

                        if (arg) {
                                g_string_append_printf (str_buf, "%s", arg);
                                g_free (arg);
                                arg = NULL;
                        }

                        g_string_append_printf (str_buf, ")");
                }
        }

        if (str_buf) {
                result = str_buf->str;
                g_string_free (str_buf, FALSE);
                str_buf = NULL;
        }

        return result;

      error:
        g_string_free (str_buf, TRUE);
        return NULL;
}

/**
 *Dumps the pseudo to a file.
 *@param a_this the current instance of pseudo
 *@param a_fp the destination file pointer.
 */
void
cr_pseudo_dump (CRPseudo * a_this, FILE * a_fp)
{
        guchar *tmp_str = NULL;

        if (a_this) {
                tmp_str = cr_pseudo_to_string (a_this);
                if (tmp_str) {
                        fprintf (a_fp, "%s", tmp_str);
                        g_free (tmp_str);
                        tmp_str = NULL;
                }
        }
}

/**
 *destructor of the #CRPseudo class.
 *@param a_this the current instance to destroy.
 */
void
cr_pseudo_destroy (CRPseudo * a_this)
{
        g_return_if_fail (a_this);

        if (a_this->name) {
                g_string_free (a_this->name, TRUE);
                a_this->name = NULL;
        }

        if (a_this->extra) {
                g_string_free (a_this->extra, TRUE);
                a_this->extra = NULL;
        }

        g_free (a_this);
}