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