]> git.draconx.ca Git - gob-dx.git/blob - src/util.c
Release 2.0.7
[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         const char *w = NULL;
38         const char *fname = NULL;
39
40         switch(type) {
41         case GOB_WARN:
42                 w = "Warning:";
43                 if (exit_on_warn)
44                         got_error = TRUE;
45                 break;
46         case GOB_ERROR:
47                 w = "Error:";
48                 got_error = TRUE;
49                 break;
50         default:
51                 g_assert_not_reached();
52         }
53         fname = filename;
54         if (fname == NULL)
55                 fname = "gob2";
56         if (line > 0)
57                 fprintf(stderr, "%s:%d: %s %s\n", fname, line, w, error);
58         else
59                 fprintf(stderr, "%s: %s %s\n", fname, w, error);
60         if (exit_on_error && got_error)
61                 exit(1);
62 }
63
64 void
65 error_printf(int type, int line, const char *error, ...)
66 {
67         va_list ap;
68         char *s;
69
70         va_start(ap, error);
71         s = g_strdup_vprintf(error, ap);
72         va_end(ap);
73
74         error_print(type, line, s);
75
76         g_free(s);
77 }
78
79 char *
80 remove_sep(const char *base)
81 {
82         char *p;
83         char *s = g_strdup(base);
84
85         /* don't eat C++ :: thingies */
86         if (for_cpp && strstr (s, "::") != NULL)
87                 return s;
88
89         while((p = strchr(s, ':')))
90                 strcpy(p, p+1);
91         return s;
92 }
93
94 char *
95 replace_sep(const char *base, char r)
96 {
97         char *p;
98         char *s = g_strdup(base);
99
100         /* don't eat C++ :: thingies */
101         if (for_cpp && strstr (s, "::") != NULL)
102                 return s;
103
104         while((p=strchr(s,':')))
105                 *p = r;
106         if(*s == r) {
107                 p = g_strdup(s+1);
108                 g_free(s);
109                 return p;
110         }
111         return s;
112 }
113
114 /*separate the namespace part and then replace rest of
115   separators with r*/
116 void
117 separns_replace_sep(const char *base, char **ns, char **name, char r)
118 {
119         char *p;
120         char *s = g_strdup(base);
121
122         *ns = NULL;
123         
124         /* don't eat C++ :: thingies */
125         if (for_cpp && strstr (s, "::") != NULL) {
126                 *name = s;
127                 return;
128         }
129
130         if((p=strchr(s,':')) && p!=s) {
131                 *p = '\0';
132                 *ns = g_strdup(s);
133                 p = g_strdup(p+1);
134                 g_free(s);
135                 s = p;
136         }
137         while((p=strchr(s,':')))
138                 *p = r;
139         if(*s == r) {
140                 *name = g_strdup(s+1);
141                 g_free(s);
142         } else
143                 *name = s;
144 }
145
146 /* make a macro with some prefix before the name but after
147    namespace */
148 char *
149 make_pre_macro(const char *base, const char *pre)
150 {
151         char *ns, *name;
152         char *s;
153         char **v = NULL;
154
155         if(strchr(base, ' ')) {
156                 int i;
157                 v = g_strsplit(base, " ", 0);
158                 for(i = 0; v[i] != NULL; i++) {
159                         if(*v[i] && strcmp(v[i], "const") != 0) {
160                                 base = v[i];
161                                 break;
162                         }
163                 }
164         }
165
166         separns_replace_sep(base, &ns, &name, '_');
167         if(ns)
168                 s = g_strconcat(ns, "_", pre, "_", name,NULL);
169         else
170                 s = g_strconcat(pre, "_", name, NULL);
171
172         gob_strup (s);
173         
174         g_free(ns);
175         g_free(name);
176
177         g_strfreev(v);
178
179         return s;
180 }
181
182 /* here we will find out how inconsistent gtk really is :) */
183 /* the commented out types mean that these types don't actually
184    exist. so we "emulate them" with an equivalent */
185 typedef struct _OurGtkType OurGtkType;
186 struct _OurGtkType {
187         gboolean simple;
188         char *gtkname;
189         char *cast;
190         char *type_name;
191         char *type_pointer;
192         int special;
193 };
194 const OurGtkType our_gtk_type_table[] = {
195         { TRUE, "NONE",         "void ",        "void",         NULL,   -1 },
196         { TRUE, "CHAR",         "gchar ",       "gchar",        NULL,   -1 },
197         { TRUE, "UCHAR",        "guchar ",      "guchar",       NULL,   -1 },
198         { TRUE, "UNICHAR",      "gunichar ",    "gunichar",     NULL,   -1 },
199         { TRUE, "BOOLEAN",      "gboolean ",    "gboolean",     NULL,   -1 },
200         { TRUE, "INT",          "gint ",        "gint",         NULL,   -1 },
201         { TRUE, "UINT",         "guint ",       "guint",        NULL,   -1 },
202         { TRUE, "LONG",         "glong ",       "glong",        NULL,   -1 },
203         { TRUE, "ULONG",        "gulong ",      "gulong",       NULL,   -1 },
204         { TRUE, "INT64",        "gint64 ",      "gint64",       NULL,   -1 },
205         { TRUE, "UINT64",       "guint64 ",     "guint64",      NULL,   -1 },
206         { TRUE, "ENUM",         /*"enum"*/"gint ", "gint",      NULL,   -1 },
207         { TRUE, "FLAGS",        /*"flags"*/"guint ", "guint",   NULL,   -1 },
208         { TRUE, "FLOAT",        "gfloat ",      "gfloat",       NULL,   -1 },
209         { TRUE, "DOUBLE",       "gdouble ",     "gdouble",      NULL,   -1 },
210         { TRUE, "STRING",       /*"string"*/"gchar *", "gchar", "*",    -1 },
211         { TRUE, "POINTER",      "gpointer ",    "gpointer",     NULL,   -1 },
212         { TRUE, "BOXED",        /*"boxed"*/"gpointer ", "gpointer", NULL, -1 },
213         { TRUE, "OBJECT",       "GObject *",    "GObject",      "*",    -1 },
214         { TRUE, "PARAM",        "GParamSpec *", "GParamSpec",   "*",    -1 },
215
216         /* FIXME: VALUE_ARRAY, CLOSURE */
217         /* Note that those have some issues with g_value_ calls etc... so
218          * we can't just add them */
219
220         /* Do we need this??? */
221 #if 0
222         { FALSE, "SIGNAL",      /*"GtkSignal"*/"___twopointertype ",
223                 SPECIAL_2POINTER },
224         { FALSE, "ARGS",        /*"GtkArgs"*/"___intpointertype ",
225                 SPECIAL_INT_POINTER },
226         { FALSE, "CALLBACK",    /*"GtkCallback"*/"___threepointertype ",
227                 SPECIAL_3POINTER },
228         { FALSE, "C_CALLBACK",  /*"GtkCCallback"*/"___twopointertype ",
229                 SPECIAL_2POINTER },
230         { FALSE, "FOREIGN",     /*"GtkForeign"*/"___twopointertype ",
231                 SPECIAL_2POINTER },
232 #endif
233
234         { FALSE, NULL, NULL }
235 };
236
237 static GHashTable *type_hash = NULL;
238
239 static void
240 init_type_hash(void)
241 {
242         int i;
243
244         if(type_hash) return;
245
246         type_hash = g_hash_table_new(g_str_hash, g_str_equal);
247
248         for(i=0; our_gtk_type_table[i].gtkname; i++)
249                 g_hash_table_insert(type_hash,
250                                     our_gtk_type_table[i].gtkname,
251                                     (gpointer)&our_gtk_type_table[i]);
252 }
253
254 const char *
255 get_cast (const char *type, gboolean simple_only)
256 {
257         OurGtkType *gtype;
258
259         init_type_hash ();
260
261         gtype = g_hash_table_lookup (type_hash, type);
262
263         if (gtype == NULL ||
264             (simple_only &&
265              ! gtype->simple))
266                 return NULL;
267
268         return gtype->cast;
269 }
270
271 Type *
272 get_tree_type (const char *type, gboolean simple_only)
273 {
274         OurGtkType *gtype;
275         Node *node;
276
277         init_type_hash ();
278
279         gtype = g_hash_table_lookup (type_hash, type);
280
281         if (gtype == NULL ||
282             (simple_only &&
283              ! gtype->simple))
284                 return NULL;
285
286         node = node_new (TYPE_NODE,
287                          "name", gtype->type_name,
288                          "pointer", gtype->type_pointer,
289                          NULL);
290
291         return (Type *)node;
292 }
293
294 static void
295 mask_special_array (const char *type, gboolean *special_array, gboolean *any_special)
296 {
297         OurGtkType *gtype;
298
299         init_type_hash();
300
301         gtype = g_hash_table_lookup(type_hash, type);
302
303         if(gtype && gtype->special >= 0) {
304                 special_array[gtype->special] = TRUE;
305                 *any_special = TRUE;
306         }
307 }
308
309 gboolean
310 setup_special_array(Class *c, gboolean *special_array)
311 {
312         GList *li;
313         gboolean any_special = FALSE;
314
315         memset(special_array, 0, sizeof(gboolean)*SPECIAL_LAST);
316
317         for(li=c->nodes; li; li=g_list_next(li)) {
318                 Node *n = li->data;
319                 if(n->type == METHOD_NODE) {
320                         Method *m = (Method *)n;
321                         GList *l;
322                         if(m->method != SIGNAL_LAST_METHOD &&
323                            m->method != SIGNAL_FIRST_METHOD)
324                                 continue;
325
326                         for(l=m->gtktypes; l; l=l->next)
327                                 mask_special_array(l->data, special_array,
328                                                    &any_special);
329                 } else if(n->type == ARGUMENT_NODE) {
330                         Argument *a = (Argument *)n;
331                         mask_special_array(a->gtktype, special_array,
332                                            &any_special);
333                 }
334         }
335
336         return any_special;
337 }
338
339 char *
340 get_type (const Type *t, gboolean postfix_to_stars)
341 {
342         char *s;
343         int i;
344         int extra;
345         GString *gs;
346
347         s = remove_sep(t->name);
348         gs = g_string_new(s);
349         g_free(s);
350
351         extra = 0;
352         if (postfix_to_stars) {
353                 const char *p;
354                 /*XXX: this is ugly perhaps we can do this whole postfix thing
355                   in a nicer way, we just count the number of '[' s and from
356                   that we deduce the number of dimensions, so that we can print
357                   that many stars */
358                 for (p = t->postfix; p && *p; p++)
359                         if(*p == '[') extra++;
360         }
361         g_string_append_c(gs, ' ');
362
363         if (t->pointer != NULL) {
364                 g_string_append (gs, t->pointer);
365                 for (i=0; i < extra; i++)
366                         g_string_append_c (gs, '*');
367                 g_string_append_c (gs, ' ');
368         }
369         
370         return g_string_free (gs, FALSE);
371 }
372
373 char *
374 gob_strup (char *str)
375 {
376         char *s;
377         for (s = str; *s; s++)
378                 *s = g_ascii_toupper (*s);
379
380         return str;
381 }
382
383 char *
384 gob_strdown (char *str)
385 {
386         char *s;
387         for (s = str; *s; s++)
388                 *s = g_ascii_tolower (*s);
389
390         return str;
391 }
392