]> git.draconx.ca Git - gob-dx.git/blobdiff - src/util.c
Release 0.92.2
[gob-dx.git] / src / util.c
diff --git a/src/util.c b/src/util.c
new file mode 100644 (file)
index 0000000..2eda8b0
--- /dev/null
@@ -0,0 +1,167 @@
+/* GOB C Preprocessor
+ * Copyright (C) 1999-2000 the Free Software Foundation.
+ *
+ * Author: George Lebl
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the  Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ * USA.
+ */
+
+#include "config.h"
+#include <string.h>
+#include <stdio.h>
+#include <glib.h>
+
+#include "main.h"
+
+#include "util.h"
+
+void
+print_error(int is_warn, char *error,int line)
+{
+       char *w;
+       if(is_warn)
+               w = "Warning:";
+       else {
+               w = "Error:";
+               got_error = TRUE;
+       }
+       if(line>0)
+               fprintf(stderr,"%s:%d: %s %s\n",filename,line,w,error);
+       else
+               fprintf(stderr,"%s: %s %s\n",filename,w,error);
+       if((!is_warn || exit_on_warn) && exit_on_error)
+               exit(1);
+}
+
+char *
+remove_sep(char *base)
+{
+       char *p;
+       char *s = g_strdup(base);
+       while((p=strchr(s,':')))
+               strcpy(p,p+1);
+       return s;
+}
+
+char *
+replace_sep(char *base, char r)
+{
+       char *p;
+       char *s = g_strdup(base);
+       while((p=strchr(s,':')))
+               *p = r;
+       if(*s == r) {
+               p = g_strdup(s+1);
+               g_free(s);
+               return p;
+       }
+       return s;
+}
+
+/*separate the namespace part and then replace rest of
+  separators with r*/
+void
+separns_replace_sep(char *base, char **ns, char **name, char r)
+{
+       char *p;
+       char *s = g_strdup(base);
+       *ns = NULL;
+       if((p=strchr(s,':')) && p!=s) {
+               *p = '\0';
+               *ns = g_strdup(s);
+               p = g_strdup(p+1);
+               g_free(s);
+               s = p;
+       }
+       while((p=strchr(s,':')))
+               *p = r;
+       if(*s == r) {
+               *name = g_strdup(s+1);
+               g_free(s);
+       } else
+               *name = s;
+}
+
+/* make a macro with some prefix before the name but after
+   namespace */
+char *
+make_pre_macro(char *base, char *pre)
+{
+       char *s1,*s2;
+       char *s;
+
+       separns_replace_sep(base,&s1,&s2,'_');
+       if(s1)
+               s = g_strconcat(s1,"_",pre,"_",s2,NULL);
+       else
+               s = g_strconcat(pre,"_",s2,NULL);
+
+       g_strup(s);
+       
+       g_free(s1);
+       g_free(s2);
+
+       return s;
+}
+
+/* here we will find out how inconsistent gtk really is :) */
+/* the commented out types mean that these types don't actually
+   exist. so we "emulate them" with an equivalent */
+const struct {
+       gboolean simple;
+       char *gtkname;
+       char *typename;
+} our_gtk_type_table[] = {
+       { TRUE, "NONE", "void " },
+       { TRUE, "CHAR", "gchar " },
+       { TRUE, "UCHAR",        "guchar " },
+       { TRUE, "BOOL", "gboolean " },
+       { TRUE, "INT",  "gint " },
+       { TRUE, "UINT", "guint " },
+       { TRUE, "LONG", "glong " },
+       { TRUE, "ULONG",        "gulong " },
+       { TRUE, "FLOAT",        "gfloat " },
+       { TRUE, "DOUBLE",       "gdouble " },
+       { TRUE, "STRING",       /*"GtkString"*/"gchar *" },
+       { TRUE, "ENUM", /*"GtkEnum"*/"gint " },
+       { TRUE, "FLAGS",        /*"GtkFlags"*/"guint " },
+       { TRUE, "BOXED",        /*"GtkBoxed"*/"gpointer " },
+       { TRUE, "POINTER",      "gpointer " },
+       { TRUE, "OBJECT",       "GtkObject *" },
+       { FALSE, "SIGNAL",      /*"GtkSignal"*/"___twopointertype " },
+       { FALSE, "ARGS",        /*"GtkArgs"*/"___twopointertype " },
+       { FALSE, "CALLBACK",    /*"GtkCallback"*/"___threepointertype " },
+       { FALSE, "C_CALLBACK",  /*"GtkCCallback"*/"___twopointertype " },
+       { FALSE, "FOREIGN",     /*"GtkForeign"*/"___twopointertype " },
+
+       { FALSE, NULL, NULL }
+};
+
+const char *
+get_cast(char *type, gboolean simple_only)
+{
+       int i;
+       for(i=0;our_gtk_type_table[i].gtkname;i++) {
+               if(strcmp(our_gtk_type_table[i].gtkname,type)==0) {
+                       if(simple_only &&
+                          !our_gtk_type_table[i].simple)
+                               return NULL;
+                       return our_gtk_type_table[i].typename;
+               }
+       }
+       return NULL;
+}
+