]> git.draconx.ca Git - gob-dx.git/blob - src/util.c
e47be5ef6110be81001f7c279afdb18571f843c9
[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 <string.h>
25 #include <stdio.h>
26 #include <glib.h>
27
28 #include "treefuncs.h"
29 #include "main.h"
30
31 #include "util.h"
32
33 void
34 print_error(gboolean is_warn, char *error, int line)
35 {
36         char *w;
37         if(is_warn)
38                 w = "Warning:";
39         else {
40                 w = "Error:";
41                 got_error = TRUE;
42         }
43         if(line>0)
44                 fprintf(stderr, "%s:%d: %s %s\n", filename, line, w, error);
45         else
46                 fprintf(stderr, "%s: %s %s\n", filename, w, error);
47         if((!is_warn || exit_on_warn) && exit_on_error)
48                 exit(1);
49 }
50
51 char *
52 remove_sep(char *base)
53 {
54         char *p;
55         char *s = g_strdup(base);
56         while((p=strchr(s,':')))
57                 strcpy(p,p+1);
58         return s;
59 }
60
61 char *
62 replace_sep(char *base, char r)
63 {
64         char *p;
65         char *s = g_strdup(base);
66         while((p=strchr(s,':')))
67                 *p = r;
68         if(*s == r) {
69                 p = g_strdup(s+1);
70                 g_free(s);
71                 return p;
72         }
73         return s;
74 }
75
76 /*separate the namespace part and then replace rest of
77   separators with r*/
78 void
79 separns_replace_sep(char *base, char **ns, char **name, char r)
80 {
81         char *p;
82         char *s = g_strdup(base);
83         *ns = NULL;
84         if((p=strchr(s,':')) && p!=s) {
85                 *p = '\0';
86                 *ns = g_strdup(s);
87                 p = g_strdup(p+1);
88                 g_free(s);
89                 s = p;
90         }
91         while((p=strchr(s,':')))
92                 *p = r;
93         if(*s == r) {
94                 *name = g_strdup(s+1);
95                 g_free(s);
96         } else
97                 *name = s;
98 }
99
100 /* make a macro with some prefix before the name but after
101    namespace */
102 char *
103 make_pre_macro(char *base, char *pre)
104 {
105         char *s1,*s2;
106         char *s;
107
108         separns_replace_sep(base,&s1,&s2,'_');
109         if(s1)
110                 s = g_strconcat(s1,"_",pre,"_",s2,NULL);
111         else
112                 s = g_strconcat(pre,"_",s2,NULL);
113
114         g_strup(s);
115         
116         g_free(s1);
117         g_free(s2);
118
119         return s;
120 }
121
122 /* here we will find out how inconsistent gtk really is :) */
123 /* the commented out types mean that these types don't actually
124    exist. so we "emulate them" with an equivalent */
125 typedef struct _OurGtkType OurGtkType;
126 struct _OurGtkType {
127         gboolean simple;
128         char *gtkname;
129         char *typename;
130         int special;
131 };
132 const OurGtkType our_gtk_type_table[] = {
133         { TRUE, "NONE",         "void ", -1 },
134         { TRUE, "CHAR",         "gchar ", -1 },
135         { TRUE, "UCHAR",        "guchar ", -1 },
136         { TRUE, "BOOL",         "gboolean ", -1 },
137         { TRUE, "INT",          "gint ", -1 },
138         { TRUE, "UINT",         "guint ", -1 },
139         { TRUE, "LONG",         "glong ", -1 },
140         { TRUE, "ULONG",        "gulong ", -1 },
141         { TRUE, "FLOAT",        "gfloat ", -1 },
142         { TRUE, "DOUBLE",       "gdouble ", -1 },
143         { TRUE, "STRING",       /*"GtkString"*/"gchar *", -1 },
144         { TRUE, "ENUM",         /*"GtkEnum"*/"gint ", -1 },
145         { TRUE, "FLAGS",        /*"GtkFlags"*/"guint ", -1 },
146         { TRUE, "BOXED",        /*"GtkBoxed"*/"gpointer ", -1 },
147         { TRUE, "POINTER",      "gpointer ", -1 },
148         { TRUE, "OBJECT",       "GtkObject *", -1 },
149         { FALSE, "SIGNAL",      /*"GtkSignal"*/"___twopointertype ",
150                 SPECIAL_2POINTER },
151         { FALSE, "ARGS",        /*"GtkArgs"*/"___intpointertype ",
152                 SPECIAL_INT_POINTER },
153         { FALSE, "CALLBACK",    /*"GtkCallback"*/"___threepointertype ",
154                 SPECIAL_3POINTER },
155         { FALSE, "C_CALLBACK",  /*"GtkCCallback"*/"___twopointertype ",
156                 SPECIAL_2POINTER },
157         { FALSE, "FOREIGN",     /*"GtkForeign"*/"___twopointertype ",
158                 SPECIAL_2POINTER },
159
160         { FALSE, NULL, NULL }
161 };
162
163 static GHashTable *type_hash = NULL;
164
165 static void
166 init_type_hash(void)
167 {
168         int i;
169
170         if(type_hash) return;
171
172         type_hash = g_hash_table_new(g_str_hash, g_str_equal);
173
174         for(i=0; our_gtk_type_table[i].gtkname; i++)
175                 g_hash_table_insert(type_hash,
176                                     our_gtk_type_table[i].gtkname,
177                                     (gpointer)&our_gtk_type_table[i]);
178 }
179
180 const char *
181 get_cast(char *type, gboolean simple_only)
182 {
183         OurGtkType *gtype;
184
185         init_type_hash();
186
187         gtype = g_hash_table_lookup(type_hash, type);
188
189         if(!gtype ||
190            (simple_only &&
191             !gtype->simple))
192                 return NULL;
193
194         return gtype->typename;
195 }
196
197 static void
198 mask_special_array(char *type, gboolean *special_array, gboolean *any_special)
199 {
200         OurGtkType *gtype;
201
202         init_type_hash();
203
204         gtype = g_hash_table_lookup(type_hash, type);
205
206         if(gtype && gtype->special >= 0) {
207                 special_array[gtype->special] = TRUE;
208                 *any_special = TRUE;
209         }
210 }
211
212 gboolean
213 setup_special_array(Class *c, gboolean *special_array)
214 {
215         GList *li;
216         gboolean any_special = FALSE;
217
218         memset(special_array, 0, sizeof(gboolean)*SPECIAL_LAST);
219
220         for(li=c->nodes; li; li=g_list_next(li)) {
221                 Node *n = li->data;
222                 if(n->type == METHOD_NODE) {
223                         Method *m = (Method *)n;
224                         GList *l;
225                         if(m->method != SIGNAL_LAST_METHOD &&
226                            m->method != SIGNAL_FIRST_METHOD)
227                                 continue;
228
229                         for(l=m->gtktypes; l; l=l->next)
230                                 mask_special_array(l->data, special_array,
231                                                    &any_special);
232                 } else if(n->type == ARGUMENT_NODE) {
233                         Argument *a = (Argument *)n;
234                         mask_special_array(a->gtktype, special_array,
235                                            &any_special);
236                 }
237         }
238
239         return any_special;
240 }
241
242 /* get the id without the first underscore, but only if we're removing them */
243 char *
244 get_real_id(char *id)
245 {
246         if(!no_kill_underscores &&
247            id[0] == '_' &&
248            id[1] != '\0')
249                 return &id[1];
250         else
251                 return id;
252 }