]> git.draconx.ca Git - gob-dx.git/blob - src/util.c
Release 2.0.1
[gob-dx.git] / src / util.c
1 /* GOB C Preprocessor
2  * Copyright (C) 1999-2000 the Free Software Foundation.
3  * Copyright (C) 2000 Eazel, Inc.
4  *
5  * Author: George Lebl
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the  Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20  * USA.
21  */
22
23 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <glib.h>
28
29 #include "treefuncs.h"
30 #include "main.h"
31
32 #include "util.h"
33
34 void
35 error_print(int type, int line, const char *error)
36 {
37         char *w = NULL;
38
39         switch(type) {
40         case GOB_WARN:
41                 w = "Warning:";
42                 break;
43         case GOB_ERROR:
44                 w = "Error:";
45                 got_error = TRUE;
46                 break;
47         default:
48                 g_assert_not_reached();
49         }
50         if(line > 0)
51                 fprintf(stderr, "%s:%d: %s %s\n", filename, line, w, error);
52         else
53                 fprintf(stderr, "%s: %s %s\n", filename, w, error);
54         if(exit_on_error &&
55            (type == GOB_ERROR ||
56             (type == GOB_WARN && exit_on_warn)))
57                 exit(1);
58 }
59
60 void
61 error_printf(int type, int line, const char *error, ...)
62 {
63         va_list ap;
64         char *s;
65
66         va_start(ap, error);
67         s = g_strdup_vprintf(error, ap);
68         va_end(ap);
69
70         error_print(type, line, s);
71
72         g_free(s);
73 }
74
75 char *
76 remove_sep(const char *base)
77 {
78         char *p;
79         char *s = g_strdup(base);
80         while((p = strchr(s, ':')))
81                 strcpy(p, p+1);
82         return s;
83 }
84
85 char *
86 replace_sep(const char *base, char r)
87 {
88         char *p;
89         char *s = g_strdup(base);
90         while((p=strchr(s,':')))
91                 *p = r;
92         if(*s == r) {
93                 p = g_strdup(s+1);
94                 g_free(s);
95                 return p;
96         }
97         return s;
98 }
99
100 /*separate the namespace part and then replace rest of
101   separators with r*/
102 void
103 separns_replace_sep(const char *base, char **ns, char **name, char r)
104 {
105         char *p;
106         char *s = g_strdup(base);
107         *ns = NULL;
108         if((p=strchr(s,':')) && p!=s) {
109                 *p = '\0';
110                 *ns = g_strdup(s);
111                 p = g_strdup(p+1);
112                 g_free(s);
113                 s = p;
114         }
115         while((p=strchr(s,':')))
116                 *p = r;
117         if(*s == r) {
118                 *name = g_strdup(s+1);
119                 g_free(s);
120         } else
121                 *name = s;
122 }
123
124 /* make a macro with some prefix before the name but after
125    namespace */
126 char *
127 make_pre_macro(const char *base, const char *pre)
128 {
129         char *ns, *name;
130         char *s;
131         char **v = NULL;
132
133         if(strchr(base, ' ')) {
134                 int i;
135                 v = g_strsplit(base, " ", 0);
136                 for(i = 0; v[i] != NULL; i++) {
137                         if(*v[i] && strcmp(v[i], "const") != 0) {
138                                 base = v[i];
139                                 break;
140                         }
141                 }
142         }
143
144         separns_replace_sep(base, &ns, &name, '_');
145         if(ns)
146                 s = g_strconcat(ns, "_", pre, "_", name,NULL);
147         else
148                 s = g_strconcat(pre, "_", name, NULL);
149
150         g_strup(s);
151         
152         g_free(ns);
153         g_free(name);
154
155         g_strfreev(v);
156
157         return s;
158 }
159
160 /* here we will find out how inconsistent gtk really is :) */
161 /* the commented out types mean that these types don't actually
162    exist. so we "emulate them" with an equivalent */
163 typedef struct _OurGtkType OurGtkType;
164 struct _OurGtkType {
165         gboolean simple;
166         char *gtkname;
167         char *cast;
168         char *type_name;
169         char *type_pointer;
170         int special;
171 };
172 const OurGtkType our_gtk_type_table[] = {
173         { TRUE, "NONE",         "void ",        "void",         NULL,   -1 },
174         { TRUE, "CHAR",         "gchar ",       "gchar",        NULL,   -1 },
175         { TRUE, "UCHAR",        "guchar ",      "guchar",       NULL,   -1 },
176         { TRUE, "BOOLEAN",      "gboolean ",    "gboolean",     NULL,   -1 },
177         { TRUE, "INT",          "gint ",        "gint",         NULL,   -1 },
178         { TRUE, "UINT",         "guint ",       "guint",        NULL,   -1 },
179         { TRUE, "LONG",         "glong ",       "glong",        NULL,   -1 },
180         { TRUE, "ULONG",        "gulong ",      "gulong",       NULL,   -1 },
181         { TRUE, "ENUM",         /*"enum"*/"gint ", "gint",      NULL,   -1 },
182         { TRUE, "FLAGS",        /*"flags"*/"guint ", "guint",   NULL,   -1 },
183         { TRUE, "FLOAT",        "gfloat ",      "gfloat",       NULL,   -1 },
184         { TRUE, "DOUBLE",       "gdouble ",     "gdouble",      NULL,   -1 },
185         { TRUE, "STRING",       /*"string"*/"gchar *", "gchar", "*",    -1 },
186         { TRUE, "POINTER",      "gpointer ",    "gpointer",     NULL,   -1 },
187         { TRUE, "BOXED",        /*"boxed"*/"gpointer ", "gpointer", NULL, -1 },
188         { TRUE, "OBJECT",       "GObject *",    "GObject",      "*",    -1 },
189         { TRUE, "PARAM",        "GParamSpec *", "GParamSpec",   "*",    -1 },
190
191         /* FIXME: VALUE_ARRAY, CLOSURE, UNICHAR */
192         /* Note that those have some issues with g_value_ calls etc... so
193          * we can't just add them */
194
195         /* Do we need this??? */
196 #if 0
197         { FALSE, "SIGNAL",      /*"GtkSignal"*/"___twopointertype ",
198                 SPECIAL_2POINTER },
199         { FALSE, "ARGS",        /*"GtkArgs"*/"___intpointertype ",
200                 SPECIAL_INT_POINTER },
201         { FALSE, "CALLBACK",    /*"GtkCallback"*/"___threepointertype ",
202                 SPECIAL_3POINTER },
203         { FALSE, "C_CALLBACK",  /*"GtkCCallback"*/"___twopointertype ",
204                 SPECIAL_2POINTER },
205         { FALSE, "FOREIGN",     /*"GtkForeign"*/"___twopointertype ",
206                 SPECIAL_2POINTER },
207 #endif
208
209         { FALSE, NULL, NULL }
210 };
211
212 static GHashTable *type_hash = NULL;
213
214 static void
215 init_type_hash(void)
216 {
217         int i;
218
219         if(type_hash) return;
220
221         type_hash = g_hash_table_new(g_str_hash, g_str_equal);
222
223         for(i=0; our_gtk_type_table[i].gtkname; i++)
224                 g_hash_table_insert(type_hash,
225                                     our_gtk_type_table[i].gtkname,
226                                     (gpointer)&our_gtk_type_table[i]);
227 }
228
229 const char *
230 get_cast (const char *type, gboolean simple_only)
231 {
232         OurGtkType *gtype;
233
234         init_type_hash ();
235
236         gtype = g_hash_table_lookup (type_hash, type);
237
238         if (gtype == NULL ||
239             (simple_only &&
240              ! gtype->simple))
241                 return NULL;
242
243         return gtype->cast;
244 }
245
246 Type *
247 get_tree_type (const char *type, gboolean simple_only)
248 {
249         OurGtkType *gtype;
250         Node *node;
251
252         init_type_hash ();
253
254         gtype = g_hash_table_lookup (type_hash, type);
255
256         if (gtype == NULL ||
257             (simple_only &&
258              ! gtype->simple))
259                 return NULL;
260
261         node = node_new (TYPE_NODE,
262                          "name", gtype->type_name,
263                          "pointer", gtype->type_pointer,
264                          NULL);
265
266         return (Type *)node;
267 }
268
269 static void
270 mask_special_array(char *type, gboolean *special_array, gboolean *any_special)
271 {
272         OurGtkType *gtype;
273
274         init_type_hash();
275
276         gtype = g_hash_table_lookup(type_hash, type);
277
278         if(gtype && gtype->special >= 0) {
279                 special_array[gtype->special] = TRUE;
280                 *any_special = TRUE;
281         }
282 }
283
284 gboolean
285 setup_special_array(Class *c, gboolean *special_array)
286 {
287         GList *li;
288         gboolean any_special = FALSE;
289
290         memset(special_array, 0, sizeof(gboolean)*SPECIAL_LAST);
291
292         for(li=c->nodes; li; li=g_list_next(li)) {
293                 Node *n = li->data;
294                 if(n->type == METHOD_NODE) {
295                         Method *m = (Method *)n;
296                         GList *l;
297                         if(m->method != SIGNAL_LAST_METHOD &&
298                            m->method != SIGNAL_FIRST_METHOD)
299                                 continue;
300
301                         for(l=m->gtktypes; l; l=l->next)
302                                 mask_special_array(l->data, special_array,
303                                                    &any_special);
304                 } else if(n->type == ARGUMENT_NODE) {
305                         Argument *a = (Argument *)n;
306                         mask_special_array(a->gtktype, special_array,
307                                            &any_special);
308                 }
309         }
310
311         return any_special;
312 }
313
314 char *
315 get_type (const Type *t, gboolean postfix_to_stars)
316 {
317         char *s;
318         int i;
319         int extra;
320         GString *gs;
321
322         s = remove_sep(t->name);
323         gs = g_string_new(s);
324         g_free(s);
325
326         extra = 0;
327         if (postfix_to_stars) {
328                 const char *p;
329                 /*XXX: this is ugly perhaps we can do this whole postfix thing
330                   in a nicer way, we just count the number of '[' s and from
331                   that we deduce the number of dimensions, so that we can print
332                   that many stars */
333                 for (p = t->postfix; p && *p; p++)
334                         if(*p == '[') extra++;
335         }
336         g_string_append_c(gs, ' ');
337
338         if (t->pointer != NULL) {
339                 g_string_append (gs, t->pointer);
340                 for (i=0; i < extra; i++)
341                         g_string_append_c (gs, '*');
342                 g_string_append_c (gs, ' ');
343         }
344         
345         return g_string_free (gs, FALSE);
346 }
347