]> git.draconx.ca Git - gob-dx.git/blobdiff - src/util.c
Allow building against Glib 1.x.
[gob-dx.git] / src / util.c
index acc112e0757ab42424ea10dd02612795128ff6ed..ddf9485eb159c239333b430ccff6720abef3e23f 100644 (file)
@@ -1,6 +1,7 @@
 /* GOB C Preprocessor
  * Copyright (C) 1999-2000 the Free Software Foundation.
  * Copyright (C) 2000 Eazel, Inc.
+ * Copyright (C) 2022 Nick Bowler
  *
  * Author: George Lebl
  *
@@ -20,7 +21,8 @@
  * USA.
  */
 
-#include "config.h"
+#include <config.h>
+#include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <glib.h>
 void
 error_print(int type, int line, const char *error)
 {
-       char *w = NULL;
+       const char *w = NULL;
+       const char *fname = NULL;
 
        switch(type) {
        case GOB_WARN:
                w = "Warning:";
+               if (exit_on_warn)
+                       got_error = TRUE;
                break;
        case GOB_ERROR:
                w = "Error:";
@@ -46,13 +51,14 @@ error_print(int type, int line, const char *error)
        default:
                g_assert_not_reached();
        }
-       if(line > 0)
-               fprintf(stderr, "%s:%d: %s %s\n", filename, line, w, error);
+       fname = filename;
+       if (fname == NULL)
+               fname = "gob2";
+       if (line > 0)
+               fprintf(stderr, "%s:%d: %s %s\n", fname, line, w, error);
        else
-               fprintf(stderr, "%s: %s %s\n", filename, w, error);
-       if(exit_on_error &&
-          (type == GOB_ERROR ||
-           (type == GOB_WARN && exit_on_warn)))
+               fprintf(stderr, "%s: %s %s\n", fname, w, error);
+       if (exit_on_error && got_error)
                exit(1);
 }
 
@@ -76,8 +82,17 @@ remove_sep(const char *base)
 {
        char *p;
        char *s = g_strdup(base);
-       while((p = strchr(s, ':')))
-               strcpy(p, p+1);
+       char *q=s;
+
+       /* don't eat C++ :: thingies */
+       if (for_cpp && strstr (s, "::") != NULL)
+               return s;
+
+       for(p=(char *)base;*p;p++){
+               if (*p!=':')
+                       *q++=*p;
+       }
+       *q='\0';
        return s;
 }
 
@@ -86,8 +101,23 @@ replace_sep(const char *base, char r)
 {
        char *p;
        char *s = g_strdup(base);
-       while((p=strchr(s,':')))
-               *p = r;
+
+       /* don't eat C++ :: thingies */
+       if (for_cpp && strstr (s, "::") != NULL)
+               return s;
+
+       if (r == '\0') {
+               while ((p=strchr(s,':')) != NULL) {
+                       char *t = p;
+                       while (*t != '\0') {
+                               *t = *(t+1);
+                               t++;
+                       }
+               }
+       } else {
+               while ((p=strchr(s,':')) != NULL)
+                       *p = r;
+       }
        if(*s == r) {
                p = g_strdup(s+1);
                g_free(s);
@@ -103,7 +133,15 @@ separns_replace_sep(const char *base, char **ns, char **name, char r)
 {
        char *p;
        char *s = g_strdup(base);
+
        *ns = NULL;
+       
+       /* don't eat C++ :: thingies */
+       if (for_cpp && strstr (s, "::") != NULL) {
+               *name = s;
+               return;
+       }
+
        if((p=strchr(s,':')) && p!=s) {
                *p = '\0';
                *ns = g_strdup(s);
@@ -146,7 +184,7 @@ make_pre_macro(const char *base, const char *pre)
        else
                s = g_strconcat(pre, "_", name, NULL);
 
-       g_strup(s);
+       gob_strup (s);
        
        g_free(ns);
        g_free(name);
@@ -163,26 +201,39 @@ typedef struct _OurGtkType OurGtkType;
 struct _OurGtkType {
        gboolean simple;
        char *gtkname;
-       char *typename;
+       char *cast;
+       char *type_name;
+       char *type_pointer;
        int special;
 };
 const OurGtkType our_gtk_type_table[] = {
-       { TRUE, "NONE",         "void ", -1 },
-       { TRUE, "CHAR",         "gchar ", -1 },
-       { TRUE, "UCHAR",        "guchar ", -1 },
-       { TRUE, "BOOL",         "gboolean ", -1 },
-       { TRUE, "INT",          "gint ", -1 },
-       { TRUE, "UINT",         "guint ", -1 },
-       { TRUE, "LONG",         "glong ", -1 },
-       { TRUE, "ULONG",        "gulong ", -1 },
-       { TRUE, "FLOAT",        "gfloat ", -1 },
-       { TRUE, "DOUBLE",       "gdouble ", -1 },
-       { TRUE, "STRING",       /*"GtkString"*/"gchar *", -1 },
-       { TRUE, "ENUM",         /*"GtkEnum"*/"gint ", -1 },
-       { TRUE, "FLAGS",        /*"GtkFlags"*/"guint ", -1 },
-       { TRUE, "BOXED",        /*"GtkBoxed"*/"gpointer ", -1 },
-       { TRUE, "POINTER",      "gpointer ", -1 },
-       { TRUE, "OBJECT",       "GtkObject *", -1 },
+       { TRUE, "NONE",         "void ",        "void",         NULL,   -1 },
+       { TRUE, "CHAR",         "gchar ",       "gchar",        NULL,   -1 },
+       { TRUE, "UCHAR",        "guchar ",      "guchar",       NULL,   -1 },
+       { TRUE, "UNICHAR",      "gunichar ",    "gunichar",     NULL,   -1 },
+       { TRUE, "BOOLEAN",      "gboolean ",    "gboolean",     NULL,   -1 },
+       { TRUE, "INT",          "gint ",        "gint",         NULL,   -1 },
+       { TRUE, "UINT",         "guint ",       "guint",        NULL,   -1 },
+       { TRUE, "LONG",         "glong ",       "glong",        NULL,   -1 },
+       { TRUE, "ULONG",        "gulong ",      "gulong",       NULL,   -1 },
+       { TRUE, "INT64",        "gint64 ",      "gint64",       NULL,   -1 },
+       { TRUE, "UINT64",       "guint64 ",     "guint64",      NULL,   -1 },
+       { TRUE, "ENUM",         /*"enum"*/"gint ", "gint",      NULL,   -1 },
+       { TRUE, "FLAGS",        /*"flags"*/"guint ", "guint",   NULL,   -1 },
+       { TRUE, "FLOAT",        "gfloat ",      "gfloat",       NULL,   -1 },
+       { TRUE, "DOUBLE",       "gdouble ",     "gdouble",      NULL,   -1 },
+       { TRUE, "STRING",       /*"string"*/"gchar *", "gchar", "*",    -1 },
+       { TRUE, "POINTER",      "gpointer ",    "gpointer",     NULL,   -1 },
+       { TRUE, "BOXED",        /*"boxed"*/"gpointer ", "gpointer", NULL, -1 },
+       { TRUE, "OBJECT",       "GObject *",    "GObject",      "*",    -1 },
+       { TRUE, "PARAM",        "GParamSpec *", "GParamSpec",   "*",    -1 },
+
+       /* FIXME: VALUE_ARRAY, CLOSURE */
+       /* Note that those have some issues with g_value_ calls etc... so
+        * we can't just add them */
+
+       /* Do we need this??? */
+#if 0
        { FALSE, "SIGNAL",      /*"GtkSignal"*/"___twopointertype ",
                SPECIAL_2POINTER },
        { FALSE, "ARGS",        /*"GtkArgs"*/"___intpointertype ",
@@ -193,6 +244,7 @@ const OurGtkType our_gtk_type_table[] = {
                SPECIAL_2POINTER },
        { FALSE, "FOREIGN",     /*"GtkForeign"*/"___twopointertype ",
                SPECIAL_2POINTER },
+#endif
 
        { FALSE, NULL, NULL }
 };
@@ -215,24 +267,50 @@ init_type_hash(void)
 }
 
 const char *
-get_cast(char *type, gboolean simple_only)
+get_cast (const char *type, gboolean simple_only)
 {
        OurGtkType *gtype;
 
-       init_type_hash();
+       init_type_hash ();
 
-       gtype = g_hash_table_lookup(type_hash, type);
+       if(strncmp(type, "BOXED_", 6) == 0)      
+         gtype = g_hash_table_lookup (type_hash, "BOXED");
+       else
+         gtype = g_hash_table_lookup (type_hash, type);
 
-       if(!gtype ||
-          (simple_only &&
-           !gtype->simple))
+       if (gtype == NULL ||
+           (simple_only &&
+            ! gtype->simple))
                return NULL;
 
-       return gtype->typename;
+       return gtype->cast;
+}
+
+Type *
+get_tree_type (const char *type, gboolean simple_only)
+{
+       OurGtkType *gtype;
+       Node *node;
+
+       init_type_hash ();
+
+       gtype = g_hash_table_lookup (type_hash, type);
+
+       if (gtype == NULL ||
+           (simple_only &&
+            ! gtype->simple))
+               return NULL;
+
+       node = node_new (TYPE_NODE,
+                        "name", gtype->type_name,
+                        "pointer", gtype->type_pointer,
+                        NULL);
+
+       return (Type *)node;
 }
 
 static void
-mask_special_array(char *type, gboolean *special_array, gboolean *any_special)
+mask_special_array (const char *type, gboolean *special_array, gboolean *any_special)
 {
        OurGtkType *gtype;
 
@@ -276,14 +354,93 @@ setup_special_array(Class *c, gboolean *special_array)
        return any_special;
 }
 
-/* get the id without the first underscore, but only if we're removing them */
-const char *
-get_real_id(const char *id)
+char *
+get_type (const Type *t, gboolean postfix_to_stars)
 {
-       if( ! no_kill_underscores &&
-           id[0] == '_' &&
-           id[1] != '\0')
-               return &id[1];
-       else
-               return id;
+       char *ret, *s;
+       int i;
+       int extra;
+       GString *gs;
+
+       s = remove_sep(t->name);
+       gs = g_string_new(s);
+       g_free(s);
+
+       extra = 0;
+       if (postfix_to_stars) {
+               const char *p;
+               /*XXX: this is ugly perhaps we can do this whole postfix thing
+                 in a nicer way, we just count the number of '[' s and from
+                 that we deduce the number of dimensions, so that we can print
+                 that many stars */
+               for (p = t->postfix; p && *p; p++)
+                       if(*p == '[') extra++;
+       }
+       g_string_append_c(gs, ' ');
+
+       if (t->pointer != NULL) {
+               g_string_append (gs, t->pointer);
+               for (i=0; i < extra; i++)
+                       g_string_append_c (gs, '*');
+               g_string_append_c (gs, ' ');
+       }
+       ret = gs->str;
+       g_string_free(gs, FALSE);
+       return ret;
+}
+
+#if HAVE_G_ASCII_STRCASECMP
+char *gob_strup(char *str)
+{
+       char *s;
+
+       for (s = str; *s; s++)
+               *s = g_ascii_toupper(*s);
+
+       return str;
+}
+
+char *gob_strdown(char *str)
+{
+       char *s;
+
+       for (s = str; *s; s++)
+               *s = g_ascii_tolower(*s);
+
+       return str;
+}
+#endif
+
+char *
+gob_str_delete_quotes(char *str)
+{
+       char *p, *i;
+       p=i=str;
+       while(*p!='\0')
+       {
+               if(*p=='\"')
+               {
+                       p++;
+                       continue;
+               }
+               *i=*p;
+               p++; i++;
+       }
+       *i=*p;
+       return str;
+}
+
+char *
+make_me_type (const char *type, const char *alt)
+{
+       if (type == NULL)
+               return g_strdup (alt);
+       /* HACK!  just in case someone made this
+        * work with 2.0.0 by using the TYPE
+        * macro directly */
+       if ((strstr (type, "_TYPE_") != NULL ||
+            strstr (type, "TYPE_") == type) &&
+           strchr (type, ':') == NULL)
+               return g_strdup (type);
+       return make_pre_macro (type, "TYPE");
 }