static void ensure_gettext_initialized (void) { static gsize initialised; if (g_once_init_enter (&initialised)) { #ifdef G_OS_WIN32 gchar *tmp = _glib_get_locale_dir (); bindtextdomain (GETTEXT_PACKAGE, tmp); g_free (tmp); #else bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR); #endif # ifdef HAVE_BIND_TEXTDOMAIN_CODESET bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); # endif g_once_init_leave (&initialised, TRUE); } }
int main (int argc, char *argv[]) { gchar *section = NULL; gboolean details = FALSE; void (* function) (const gchar *, const gchar *, const gchar *, gboolean); #ifdef G_OS_WIN32 gchar *tmp; #endif setlocale (LC_ALL, ""); textdomain (GETTEXT_PACKAGE); #ifdef G_OS_WIN32 tmp = _glib_get_locale_dir (); bindtextdomain (GETTEXT_PACKAGE, tmp); g_free (tmp); #else bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR); #endif #ifdef HAVE_BIND_TEXTDOMAIN_CODESET bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); #endif if (argc < 2) return cmd_help (FALSE, NULL); if (argc > 3 && strcmp (argv[1], "--section") == 0) { section = argv[2]; argv = argv + 2; argc -= 2; } if (strcmp (argv[1], "help") == 0) return cmd_help (TRUE, argv[2]); else if (argc == 4 && strcmp (argv[1], "extract") == 0) function = cmd_extract; else if (argc == 3 && strcmp (argv[1], "sections") == 0) function = cmd_sections; else if ((argc == 3 || argc == 4) && strcmp (argv[1], "list") == 0) { function = cmd_list; details = FALSE; } else if ((argc == 3 || argc == 4) && strcmp (argv[1], "details") == 0) { function = cmd_list; details = TRUE; } else return cmd_help (FALSE, argv[1]); (* function) (argv[2], section, argc > 3 ? argv[3] : NULL, details); return 0; }
int main (int argc, char **argv) { GError *error; GHashTable *table; GHashTable *files; gchar *srcfile; gboolean show_version_and_exit = FALSE; gchar *target = NULL; gchar *binary_target = NULL; gboolean generate_automatic = FALSE; gboolean generate_source = FALSE; gboolean generate_header = FALSE; gboolean manual_register = FALSE; gboolean internal = FALSE; gboolean generate_dependencies = FALSE; gboolean generate_phony_targets = FALSE; char *dependency_file = NULL; char *c_name = NULL; char *c_name_no_underscores; const char *linkage = "extern"; GOptionContext *context; GOptionEntry entries[] = { { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL }, { "target", 0, 0, G_OPTION_ARG_FILENAME, &target, N_("name of the output file"), N_("FILE") }, { "sourcedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &sourcedirs, N_("The directories where files are to be read from (default to current directory)"), N_("DIRECTORY") }, { "generate", 0, 0, G_OPTION_ARG_NONE, &generate_automatic, N_("Generate output in the format selected for by the target filename extension"), NULL }, { "generate-header", 0, 0, G_OPTION_ARG_NONE, &generate_header, N_("Generate source header"), NULL }, { "generate-source", 0, 0, G_OPTION_ARG_NONE, &generate_source, N_("Generate sourcecode used to link in the resource file into your code"), NULL }, { "generate-dependencies", 0, 0, G_OPTION_ARG_NONE, &generate_dependencies, N_("Generate dependency list"), NULL }, { "dependency-file", 0, 0, G_OPTION_ARG_FILENAME, &dependency_file, N_("name of the dependency file to generate"), N_("FILE") }, { "generate-phony-targets", 0, 0, G_OPTION_ARG_NONE, &generate_phony_targets, N_("Include phony targets in the generated dependency file"), NULL }, { "manual-register", 0, 0, G_OPTION_ARG_NONE, &manual_register, N_("Don’t automatically create and register resource"), NULL }, { "internal", 0, 0, G_OPTION_ARG_NONE, &internal, N_("Don’t export functions; declare them G_GNUC_INTERNAL"), NULL }, { "c-name", 0, 0, G_OPTION_ARG_STRING, &c_name, N_("C identifier name used for the generated source code"), NULL }, { NULL } }; #ifdef G_OS_WIN32 gchar *tmp; #endif setlocale (LC_ALL, ""); textdomain (GETTEXT_PACKAGE); #ifdef G_OS_WIN32 tmp = _glib_get_locale_dir (); bindtextdomain (GETTEXT_PACKAGE, tmp); g_free (tmp); #else bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR); #endif #ifdef HAVE_BIND_TEXTDOMAIN_CODESET bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); #endif context = g_option_context_new (N_("FILE")); g_option_context_set_translation_domain (context, GETTEXT_PACKAGE); g_option_context_set_summary (context, N_("Compile a resource specification into a resource file.\n" "Resource specification files have the extension .gresource.xml,\n" "and the resource file have the extension called .gresource.")); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); error = NULL; if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("%s\n", error->message); return 1; } g_option_context_free (context); if (show_version_and_exit) { g_print (PACKAGE_VERSION "\n"); return 0; } if (argc != 2) { g_printerr (_("You should give exactly one file name\n")); g_free (c_name); return 1; } if (internal) linkage = "G_GNUC_INTERNAL"; srcfile = argv[1]; xmllint = g_strdup (g_getenv ("XMLLINT")); if (xmllint == NULL) xmllint = g_find_program_in_path ("xmllint"); if (xmllint == NULL) g_printerr ("XMLLINT not set and xmllint not found in path; skipping xml preprocessing.\n"); gdk_pixbuf_pixdata = g_strdup (g_getenv ("GDK_PIXBUF_PIXDATA")); if (gdk_pixbuf_pixdata == NULL) gdk_pixbuf_pixdata = g_find_program_in_path ("gdk-pixbuf-pixdata"); if (target == NULL) { char *dirname = g_path_get_dirname (srcfile); char *base = g_path_get_basename (srcfile); char *target_basename; if (g_str_has_suffix (base, ".xml")) base[strlen(base) - strlen (".xml")] = 0; if (generate_source) { if (g_str_has_suffix (base, ".gresource")) base[strlen(base) - strlen (".gresource")] = 0; target_basename = g_strconcat (base, ".c", NULL); } else if (generate_header) { if (g_str_has_suffix (base, ".gresource")) base[strlen(base) - strlen (".gresource")] = 0; target_basename = g_strconcat (base, ".h", NULL); } else { if (g_str_has_suffix (base, ".gresource")) target_basename = g_strdup (base); else target_basename = g_strconcat (base, ".gresource", NULL); } target = g_build_filename (dirname, target_basename, NULL); g_free (target_basename); g_free (dirname); g_free (base); } else if (generate_automatic) { if (extension_in_set (target, "c", "cc", "cpp", "cxx", "c++", NULL)) generate_source = TRUE; else if (extension_in_set (target, "h", "hh", "hpp", "hxx", "h++", NULL)) generate_header = TRUE; else if (extension_in_set (target, "gresource", NULL)) { } } files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)file_data_free); if ((table = parse_resource_file (srcfile, !generate_dependencies, files)) == NULL) { g_free (target); g_free (c_name); return 1; } /* This can be used in the same invocation as other generate commands */ if (dependency_file != NULL) { /* Generate a .d file that describes the dependencies for * build tools, gcc -M -MF style */ GString *dep_string; GHashTableIter iter; gpointer key, data; FileData *file_data; char *escaped; g_hash_table_iter_init (&iter, files); dep_string = g_string_new (NULL); escaped = escape_makefile_string (srcfile); g_string_printf (dep_string, "%s:", escaped); g_free (escaped); /* First rule: foo.xml: resource1 resource2.. */ while (g_hash_table_iter_next (&iter, &key, &data)) { file_data = data; if (!g_str_equal (file_data->filename, srcfile)) { escaped = escape_makefile_string (file_data->filename); g_string_append_printf (dep_string, " %s", escaped); g_free (escaped); } } g_string_append (dep_string, "\n"); /* Optionally include phony targets as it silences `make` but * isn't supported on `ninja` at the moment. See also: `gcc -MP` */ if (generate_phony_targets) { g_string_append (dep_string, "\n"); /* One rule for every resource: resourceN: */ g_hash_table_iter_init (&iter, files); while (g_hash_table_iter_next (&iter, &key, &data)) { file_data = data; if (!g_str_equal (file_data->filename, srcfile)) { escaped = escape_makefile_string (file_data->filename); g_string_append_printf (dep_string, "%s:\n\n", escaped); g_free (escaped); } } } if (g_str_equal (dependency_file, "-")) { g_print ("%s\n", dep_string->str); } else { if (!g_file_set_contents (dependency_file, dep_string->str, dep_string->len, &error)) { g_printerr ("Error writing dependency file: %s\n", error->message); g_string_free (dep_string, TRUE); g_free (dependency_file); g_error_free (error); return 1; } } g_string_free (dep_string, TRUE); g_free (dependency_file); } if (generate_dependencies) { GHashTableIter iter; gpointer key, data; FileData *file_data; g_hash_table_iter_init (&iter, files); /* Generate list of files for direct use as dependencies in a Makefile */ while (g_hash_table_iter_next (&iter, &key, &data)) { file_data = data; g_print ("%s\n", file_data->filename); } } else if (generate_source || generate_header) { if (generate_source) { int fd = g_file_open_tmp (NULL, &binary_target, NULL); if (fd == -1) { g_printerr ("Can't open temp file\n"); g_free (c_name); 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); c_name_no_underscores = c_name; while (c_name_no_underscores && *c_name_no_underscores == '_') c_name_no_underscores++; if (binary_target != NULL && !write_to_file (table, binary_target, &error)) { g_printerr ("%s\n", error->message); g_free (target); g_free (c_name); return 1; } if (generate_header) { FILE *file; file = fopen (target, "w"); if (file == NULL) { g_printerr ("can't write to file %s", target); g_free (c_name); return 1; } fprintf (file, "#ifndef __RESOURCE_%s_H__\n" "#define __RESOURCE_%s_H__\n" "\n" "#include <gio/gio.h>\n" "\n" "%s GResource *%s_get_resource (void);\n", c_name, c_name, linkage, c_name); if (manual_register) fprintf (file, "\n" "%s void %s_register_resource (void);\n" "%s void %s_unregister_resource (void);\n" "\n", linkage, c_name, linkage, c_name); fprintf (file, "#endif\n"); fclose (file); } else if (generate_source) { FILE *file; guint8 *data; gsize data_size; gsize i; if (!g_file_get_contents (binary_target, (char **)&data, &data_size, NULL)) { g_printerr ("can't read back temporary file"); g_free (c_name); return 1; } g_unlink (binary_target); file = fopen (target, "w"); if (file == NULL) { g_printerr ("can't write to file %s", target); g_free (c_name); return 1; } fprintf (file, "#include <gio/gio.h>\n" "\n" "#if defined (__ELF__) && ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6))\n" "# define SECTION __attribute__ ((section (\".gresource.%s\"), aligned (8)))\n" "#else\n" "# define SECTION\n" "#endif\n" "\n" "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double alignment; void * const ptr;} %s_resource_data = { {\n", c_name_no_underscores, data_size, c_name); for (i = 0; i < data_size; i++) { if (i % 8 == 0) fprintf (file, " "); fprintf (file, "0x%2.2x", (int)data[i]); if (i != data_size - 1) fprintf (file, ", "); if ((i % 8 == 7) || (i == data_size - 1)) fprintf (file, "\n"); } fprintf (file, "} };\n"); fprintf (file, "\n" "static GStaticResource static_resource = { %s_resource_data.data, sizeof (%s_resource_data.data), NULL, NULL, NULL };\n" "%s GResource *%s_get_resource (void);\n" "GResource *%s_get_resource (void)\n" "{\n" " return g_static_resource_get_resource (&static_resource);\n" "}\n", c_name, c_name, linkage, c_name, c_name); if (manual_register) { fprintf (file, "\n" "%s void %s_unregister_resource (void);\n" "void %s_unregister_resource (void)\n" "{\n" " g_static_resource_fini (&static_resource);\n" "}\n" "\n" "%s void %s_register_resource (void);\n" "void %s_register_resource (void)\n" "{\n" " g_static_resource_init (&static_resource);\n" "}\n", linkage, c_name, c_name, linkage, c_name, c_name); } else { fprintf (file, "%s", gconstructor_code); fprintf (file, "\n" "#ifdef G_HAS_CONSTRUCTORS\n" "\n" "#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA\n" "#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(resource_constructor)\n" "#endif\n" "G_DEFINE_CONSTRUCTOR(resource_constructor)\n" "#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA\n" "#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(resource_destructor)\n" "#endif\n" "G_DEFINE_DESTRUCTOR(resource_destructor)\n" "\n" "#else\n" "#warning \"Constructor not supported on this compiler, linking in resources will not work\"\n" "#endif\n" "\n" "static void resource_constructor (void)\n" "{\n" " g_static_resource_init (&static_resource);\n" "}\n" "\n" "static void resource_destructor (void)\n" "{\n" " g_static_resource_fini (&static_resource);\n" "}\n"); } fclose (file); g_free (data); } g_free (binary_target); g_free (target); g_hash_table_destroy (table); g_free (xmllint); g_free (c_name); return 0; }
int main (int argc, char **argv) { GError *error; GHashTable *table; gchar *srcfile; gchar *target = NULL; gchar *binary_target = NULL; gboolean generate_automatic = FALSE; gboolean generate_source = FALSE; gboolean generate_header = FALSE; gboolean manual_register = FALSE; gboolean generate_dependencies = FALSE; char *c_name = NULL; char *c_name_no_underscores; 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_ARRAY, &sourcedirs, N_("The directories where files are to be read from (default to current directory)"), N_("DIRECTORY") }, { "generate", 0, 0, G_OPTION_ARG_NONE, &generate_automatic, N_("Generate output in the format selected for by the target filename extension"), NULL }, { "generate-header", 0, 0, G_OPTION_ARG_NONE, &generate_header, N_("Generate source header"), NULL }, { "generate-source", 0, 0, G_OPTION_ARG_NONE, &generate_source, N_("Generate sourcecode used to link in the resource file into your code"), NULL }, { "generate-dependencies", 0, 0, G_OPTION_ARG_NONE, &generate_dependencies, N_("Generate dependency list"), NULL }, { "manual-register", 0, 0, G_OPTION_ARG_NONE, &manual_register, N_("Don't automatically create and register resource"), NULL }, { "c-name", 0, 0, G_OPTION_ARG_STRING, &c_name, N_("C identifier name used for the generated source code"), NULL }, { NULL } }; #ifdef G_OS_WIN32 extern gchar *_glib_get_locale_dir (void); gchar *tmp; #endif setlocale (LC_ALL, ""); textdomain (GETTEXT_PACKAGE); #ifdef G_OS_WIN32 tmp = _glib_get_locale_dir (); bindtextdomain (GETTEXT_PACKAGE, tmp); g_free (tmp); #else bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR); #endif #ifdef HAVE_BIND_TEXTDOMAIN_CODESET bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); #endif g_type_init (); context = g_option_context_new (N_("FILE")); g_option_context_set_translation_domain (context, GETTEXT_PACKAGE); g_option_context_set_summary (context, N_("Compile a resource specification into a resource file.\n" "Resource specification files have the extension .gresource.xml,\n" "and the resource file have the extension called .gresource.")); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); error = NULL; if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("%s\n", error->message); return 1; } g_option_context_free (context); if (argc != 2) { g_printerr (_("You should give exactly one file name\n")); return 1; } srcfile = argv[1]; xmllint = g_strdup (g_getenv ("XMLLINT")); if (xmllint == NULL) xmllint = g_find_program_in_path ("xmllint"); if (xmllint == NULL) g_printerr ("XMLLINT not set and xmllint not found in path; skipping xml preprocessing.\n"); gdk_pixbuf_pixdata = g_strdup (g_getenv ("GDK_PIXBUF_PIXDATA")); if (gdk_pixbuf_pixdata == NULL) gdk_pixbuf_pixdata = g_find_program_in_path ("gdk-pixbuf-pixdata"); if (target == NULL) { char *dirname = g_path_get_dirname (srcfile); char *base = g_path_get_basename (srcfile); char *target_basename; if (g_str_has_suffix (base, ".xml")) base[strlen(base) - strlen (".xml")] = 0; if (generate_source) { if (g_str_has_suffix (base, ".gresource")) base[strlen(base) - strlen (".gresource")] = 0; target_basename = g_strconcat (base, ".c", NULL); } else { if (g_str_has_suffix (base, ".gresource")) target_basename = g_strdup (base); else target_basename = g_strconcat (base, ".gresource", NULL); } target = g_build_filename (dirname, target_basename, NULL); g_free (target_basename); g_free (dirname); g_free (base); } else if (generate_automatic) { if (g_str_has_suffix (target, ".c")) generate_source = TRUE; else if (g_str_has_suffix (target, ".h")) generate_header = TRUE; else if (g_str_has_suffix (target, ".gresource")) ; } if ((table = parse_resource_file (srcfile, !generate_dependencies)) == NULL) { g_free (target); return 1; } if (generate_dependencies) { GHashTableIter iter; gpointer key, data; FileData *file_data; g_hash_table_iter_init (&iter, table); while (g_hash_table_iter_next (&iter, &key, &data)) { file_data = data; g_print ("%s\n",file_data->filename); } } else if (generate_source || generate_header) { if (generate_source) { int fd = g_file_open_tmp (NULL, &binary_target, NULL); if (fd == -1) { g_printerr ("Can't open temp file\n"); 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); c_name_no_underscores = c_name; while (c_name_no_underscores && *c_name_no_underscores == '_') c_name_no_underscores++; if (binary_target != NULL && !write_to_file (table, binary_target, &error)) { g_printerr ("%s\n", error->message); g_free (target); return 1; } if (generate_header) { FILE *file; file = fopen (target, "w"); if (file == NULL) { g_printerr ("can't write to file %s", target); return 1; } fprintf (file, "#ifndef __RESOURCE_%s_H__\n" "#define __RESOURCE_%s_H__\n" "\n" "#include <gio/gio.h>\n" "\n" "extern GResource *%s_get_resource (void);\n", c_name, c_name, c_name); if (manual_register) fprintf (file, "\n" "extern void %s_register_resource (void);\n" "extern void %s_unregister_resource (void);\n" "\n", c_name, c_name); fprintf (file, "#endif\n"); fclose (file); } else if (generate_source) { FILE *file; guint8 *data; gsize data_size; gsize i; if (!g_file_get_contents (binary_target, (char **)&data, &data_size, NULL)) { g_printerr ("can't read back temporary file"); return 1; } g_unlink (binary_target); file = fopen (target, "w"); if (file == NULL) { g_printerr ("can't write to file %s", target); return 1; } fprintf (file, "#include <gio/gio.h>\n" "\n" "#if defined (__ELF__) && ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6))\n" "# define SECTION __attribute__ ((section (\".gresource.%s\"), aligned (8)))\n" "#else\n" "# define SECTION\n" "#endif\n" "\n" "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double alignment; void * const ptr;} %s_resource_data = { {\n", c_name_no_underscores, data_size, c_name); for (i = 0; i < data_size; i++) { if (i % 8 == 0) fprintf (file, " "); fprintf (file, "0x%2.2x", (int)data[i]); if (i != data_size - 1) fprintf (file, ", "); if ((i % 8 == 7) || (i == data_size - 1)) fprintf (file, "\n"); } fprintf (file, "} };\n"); fprintf (file, "\n" "static GStaticResource static_resource = { %s_resource_data.data, sizeof (%s_resource_data.data) };\n" "extern GResource *%s_get_resource (void);\n" "GResource *%s_get_resource (void)\n" "{\n" " return g_static_resource_get_resource (&static_resource);\n" "}\n", c_name, c_name, c_name, c_name); if (manual_register) { fprintf (file, "\n" "extern void %s_unregister_resource (void);\n" "void %s_unregister_resource (void)\n" "{\n" " g_static_resource_fini (&static_resource);\n" "}\n" "\n" "extern void %s_register_resource (void);\n" "void %s_register_resource (void)\n" "{\n" " g_static_resource_init (&static_resource);\n" "}\n", c_name, c_name, c_name, c_name); } else { fprintf (file, "%s", gconstructor_code); fprintf (file, "\n" "#ifdef G_HAS_CONSTRUCTORS\n" "\n" "#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA\n" "#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(resource_constructor)\n" "#endif\n" "G_DEFINE_CONSTRUCTOR(resource_constructor)\n" "#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA\n" "#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(resource_destructor)\n" "#endif\n" "G_DEFINE_DESTRUCTOR(resource_destructor)\n" "\n" "#else\n" "#warning \"Constructor not supported on this compiler, linking in resources will not work\"\n" "#endif\n" "\n" "static void resource_constructor (void)\n" "{\n" " g_static_resource_init (&static_resource);\n" "}\n" "\n" "static void resource_destructor (void)\n" "{\n" " g_static_resource_fini (&static_resource);\n" "}\n"); } fclose (file); g_free (data); } g_free (binary_target); g_free (target); g_hash_table_destroy (table); g_free (xmllint); return 0; }