[glib/resources] Add a name to the generated C code for resources



commit 9b7db85ac0dd06877cb0283a6ccc2ac46607e742
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Dec 22 11:35:14 2011 +0100

    Add a name to the generated C code for resources
    
    This is not very useful right now, but will be practical later

 gio/glib-compile-resources.c |   36 +++++++++++++++++++++++++++++++++---
 1 files changed, 33 insertions(+), 3 deletions(-)
---
diff --git a/gio/glib-compile-resources.c b/gio/glib-compile-resources.c
index 65f365d..4c56de6 100644
--- a/gio/glib-compile-resources.c
+++ b/gio/glib-compile-resources.c
@@ -405,11 +405,13 @@ main (int argc, char **argv)
   gchar *target = NULL;
   gchar *binary_target;
   gboolean generate_source = FALSE;
+  char *c_name = NULL;
   GOptionContext *context;
   GOptionEntry entries[] = {
     { "target", 0, 0, G_OPTION_ARG_FILENAME, &target, N_("name of the output file"), N_("FILE") },
     { "sourcedir", 0, 0, G_OPTION_ARG_FILENAME, &sourcedir, N_("The directory where files are to be read from (default to current directory)"), N_("DIRECTORY") },
     { "generate-source", 0, 0, G_OPTION_ARG_NONE, &generate_source, N_("Generate sourcecode used to link in the resource file into your code"), NULL },
+    { "c-name", 0, 0, G_OPTION_ARG_STRING, &c_name, N_("C identifier name used for the generated source code"), NULL },
     { NULL }
   };
 
@@ -506,6 +508,34 @@ main (int argc, char **argv)
 	  return 1;
 	}
       close (fd);
+
+      if (c_name == NULL)
+	{
+	  char *base = g_path_get_basename (srcfile);
+	  GString *s;
+	  char *dot;
+	  int i;
+
+	  /* Remove extensions */
+	  dot = strchr (base, '.');
+	  if (dot)
+	    *dot = 0;
+
+	  s = g_string_new ("");
+
+	  for (i = 0; base[i] != 0; i++)
+	    {
+	      const char *first = G_CSET_A_2_Z G_CSET_a_2_z "_";
+	      const char *rest = G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_";
+	      if (strchr ((i == 0) ? first : rest, base[i]) != NULL)
+		g_string_append_c (s, base[i]);
+	      else if (base[i] == '-')
+		g_string_append_c (s, '_');
+
+	    }
+
+	  c_name = g_string_free (s, FALSE);
+	}
     }
   else
     binary_target = g_strdup (target);
@@ -541,7 +571,7 @@ main (int argc, char **argv)
 
       fprintf (file, "#include <gio/gio.h>\n");
       fprintf (file, "\n");
-      fprintf (file, "static const guint8 resource_data[] = {\n");
+      fprintf (file, "static const guint8 %s_resource_data[] = {\n", c_name);
 
       for (i = 0; i < data_size; i++) {
 	if (i % 8 == 0)
@@ -580,13 +610,13 @@ main (int argc, char **argv)
 	       "\n"
 	       "static void register_resource (void)\n"
 	       "{\n"
-	       "  resource = g_resource_new_from_data (resource_data, sizeof (resource_data), NULL, NULL);\n"
+	       "  resource = g_resource_new_from_data (%s_resource_data, sizeof (%s_resource_data), NULL, NULL);\n"
 	       "  if (resource)\n"
 	       "    {\n"
 	       "      g_resources_register (resource);\n"
 	       "      g_atexit (unregister_resource);\n"
 	       "    }\n"
-	       "}\n");
+	       "}\n", c_name, c_name);
 
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]