]> git.draconx.ca Git - gob-dx.git/blob - src/util.c
Release 0.92.2
[gob-dx.git] / src / util.c
1 /* GOB C Preprocessor
2  * Copyright (C) 1999-2000 the Free Software Foundation.
3  *
4  * Author: George Lebl
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the  Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19  * USA.
20  */
21
22 #include "config.h"
23 #include <string.h>
24 #include <stdio.h>
25 #include <glib.h>
26
27 #include "main.h"
28
29 #include "util.h"
30
31 void
32 print_error(int is_warn, char *error,int line)
33 {
34         char *w;
35         if(is_warn)
36                 w = "Warning:";
37         else {
38                 w = "Error:";
39                 got_error = TRUE;
40         }
41         if(line>0)
42                 fprintf(stderr,"%s:%d: %s %s\n",filename,line,w,error);
43         else
44                 fprintf(stderr,"%s: %s %s\n",filename,w,error);
45         if((!is_warn || exit_on_warn) && exit_on_error)
46                 exit(1);
47 }
48
49 char *
50 remove_sep(char *base)
51 {
52         char *p;
53         char *s = g_strdup(base);
54         while((p=strchr(s,':')))
55                 strcpy(p,p+1);
56         return s;
57 }
58
59 char *
60 replace_sep(char *base, char r)
61 {
62         char *p;
63         char *s = g_strdup(base);
64         while((p=strchr(s,':')))
65                 *p = r;
66         if(*s == r) {
67                 p = g_strdup(s+1);
68                 g_free(s);
69                 return p;
70         }
71         return s;
72 }
73
74 /*separate the namespace part and then replace rest of
75   separators with r*/
76 void
77 separns_replace_sep(char *base, char **ns, char **name, char r)
78 {
79         char *p;
80         char *s = g_strdup(base);
81         *ns = NULL;
82         if((p=strchr(s,':')) && p!=s) {
83                 *p = '\0';
84                 *ns = g_strdup(s);
85                 p = g_strdup(p+1);
86                 g_free(s);
87                 s = p;
88         }
89         while((p=strchr(s,':')))
90                 *p = r;
91         if(*s == r) {
92                 *name = g_strdup(s+1);
93                 g_free(s);
94         } else
95                 *name = s;
96 }
97
98 /* make a macro with some prefix before the name but after
99    namespace */
100 char *
101 make_pre_macro(char *base, char *pre)
102 {
103         char *s1,*s2;
104         char *s;
105
106         separns_replace_sep(base,&s1,&s2,'_');
107         if(s1)
108                 s = g_strconcat(s1,"_",pre,"_",s2,NULL);
109         else
110                 s = g_strconcat(pre,"_",s2,NULL);
111
112         g_strup(s);
113         
114         g_free(s1);
115         g_free(s2);
116
117         return s;
118 }
119
120 /* here we will find out how inconsistent gtk really is :) */
121 /* the commented out types mean that these types don't actually
122    exist. so we "emulate them" with an equivalent */
123 const struct {
124         gboolean simple;
125         char *gtkname;
126         char *typename;
127 } our_gtk_type_table[] = {
128         { TRUE, "NONE", "void " },
129         { TRUE, "CHAR", "gchar " },
130         { TRUE, "UCHAR",        "guchar " },
131         { TRUE, "BOOL", "gboolean " },
132         { TRUE, "INT",  "gint " },
133         { TRUE, "UINT", "guint " },
134         { TRUE, "LONG", "glong " },
135         { TRUE, "ULONG",        "gulong " },
136         { TRUE, "FLOAT",        "gfloat " },
137         { TRUE, "DOUBLE",       "gdouble " },
138         { TRUE, "STRING",       /*"GtkString"*/"gchar *" },
139         { TRUE, "ENUM", /*"GtkEnum"*/"gint " },
140         { TRUE, "FLAGS",        /*"GtkFlags"*/"guint " },
141         { TRUE, "BOXED",        /*"GtkBoxed"*/"gpointer " },
142         { TRUE, "POINTER",      "gpointer " },
143         { TRUE, "OBJECT",       "GtkObject *" },
144         { FALSE, "SIGNAL",      /*"GtkSignal"*/"___twopointertype " },
145         { FALSE, "ARGS",        /*"GtkArgs"*/"___twopointertype " },
146         { FALSE, "CALLBACK",    /*"GtkCallback"*/"___threepointertype " },
147         { FALSE, "C_CALLBACK",  /*"GtkCCallback"*/"___twopointertype " },
148         { FALSE, "FOREIGN",     /*"GtkForeign"*/"___twopointertype " },
149
150         { FALSE, NULL, NULL }
151 };
152
153 const char *
154 get_cast(char *type, gboolean simple_only)
155 {
156         int i;
157         for(i=0;our_gtk_type_table[i].gtkname;i++) {
158                 if(strcmp(our_gtk_type_table[i].gtkname,type)==0) {
159                         if(simple_only &&
160                            !our_gtk_type_table[i].simple)
161                                 return NULL;
162                         return our_gtk_type_table[i].typename;
163                 }
164         }
165         return NULL;
166 }
167