1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
2
3 /* libcroco - Library for parsing and applying CSS
4 * Copyright (C) 2006-2019 Free Software Foundation, Inc.
5 *
6 * This file is not part of the GNU gettext program, but is used with
7 * GNU gettext.
8 *
9 * The original copyright notice is as follows:
10 */
11
12 /*
13 * This file is part of The Croco Library
14 *
15 * Copyright (C) 2003-2004 Dodji Seketeli. All Rights Reserved.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of version 2.1 of the GNU Lesser General Public
19 * License as published by the Free Software Foundation.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 * USA
30 *
31 * Author: Dodji Seketeli
32 */
33
34 #include <config.h>
35 #include "cr-pseudo.h"
36
37 /**
38 *@CRPseudo:
39 *The definition of the #CRPseudo class.
40 */
41
42 /**
43 * cr_pseudo_new:
44 *Constructor of the #CRPseudo class.
45 *
46 *Returns the newly build instance.
47 */
48 CRPseudo *
cr_pseudo_new(void)49 cr_pseudo_new (void)
50 {
51 CRPseudo *result = NULL;
52
53 result = g_malloc0 (sizeof (CRPseudo));
54
55 return result;
56 }
57
58 /**
59 * cr_pseudo_to_string:
60 * @a_this: the current instance of #CRPseud.
61 *
62 * Returns the serialized pseudo. Caller must free the returned
63 * string using g_free().
64 */
65 guchar *
cr_pseudo_to_string(CRPseudo const * a_this)66 cr_pseudo_to_string (CRPseudo const * a_this)
67 {
68 guchar *result = NULL;
69 GString *str_buf = NULL;
70
71 g_return_val_if_fail (a_this, NULL);
72
73 str_buf = g_string_new (NULL);
74
75 if (a_this->type == IDENT_PSEUDO) {
76 guchar *name = NULL;
77
78 if (a_this->name == NULL) {
79 goto error;
80 }
81
82 name = (guchar *) g_strndup (a_this->name->stryng->str,
83 a_this->name->stryng->len);
84
85 if (name) {
86 g_string_append (str_buf, (const gchar *) name);
87 g_free (name);
88 name = NULL;
89 }
90 } else if (a_this->type == FUNCTION_PSEUDO) {
91 guchar *name = NULL,
92 *arg = NULL;
93
94 if (a_this->name == NULL)
95 goto error;
96
97 name = (guchar *) g_strndup (a_this->name->stryng->str,
98 a_this->name->stryng->len);
99
100 if (a_this->extra) {
101 arg = (guchar *) g_strndup (a_this->extra->stryng->str,
102 a_this->extra->stryng->len);
103 }
104
105 if (name) {
106 g_string_append_printf (str_buf, "%s(", name);
107 g_free (name);
108 name = NULL;
109
110 if (arg) {
111 g_string_append (str_buf, (const gchar *) arg);
112 g_free (arg);
113 arg = NULL;
114 }
115
116 g_string_append_c (str_buf, ')');
117 }
118 }
119
120 if (str_buf) {
121 result = (guchar *) str_buf->str;
122 g_string_free (str_buf, FALSE);
123 str_buf = NULL;
124 }
125
126 return result;
127
128 error:
129 g_string_free (str_buf, TRUE);
130 return NULL;
131 }
132
133 /**
134 * cr_pseudo_dump:
135 *@a_this: the current instance of pseudo
136 *@a_fp: the destination file pointer.
137 *
138 *Dumps the pseudo to a file.
139 *
140 */
141 void
cr_pseudo_dump(CRPseudo const * a_this,FILE * a_fp)142 cr_pseudo_dump (CRPseudo const * a_this, FILE * a_fp)
143 {
144 guchar *tmp_str = NULL;
145
146 if (a_this) {
147 tmp_str = cr_pseudo_to_string (a_this);
148 if (tmp_str) {
149 fprintf (a_fp, "%s", tmp_str);
150 g_free (tmp_str);
151 tmp_str = NULL;
152 }
153 }
154 }
155
156 /**
157 * cr_pseudo_destroy:
158 *@a_this: the current instance to destroy.
159 *
160 *destructor of the #CRPseudo class.
161 */
162 void
cr_pseudo_destroy(CRPseudo * a_this)163 cr_pseudo_destroy (CRPseudo * a_this)
164 {
165 g_return_if_fail (a_this);
166
167 if (a_this->name) {
168 cr_string_destroy (a_this->name);
169 a_this->name = NULL;
170 }
171
172 if (a_this->extra) {
173 cr_string_destroy (a_this->extra);
174 a_this->extra = NULL;
175 }
176
177 g_free (a_this);
178 }
179