#include <stdio.h>
#include <locale.h>
#include <errno.h>
+#ifdef G_OS_WIN32
+#include <io.h>
+#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <glib.h>
#include "gvdb/gvdb-builder.h"
+#include "gconstructor_as_data.h"
+
typedef struct
{
char *filename;
GString *string; /* non-NULL when accepting text */
} ParseState;
-static gchar *sourcedir = NULL;
+static gchar **sourcedirs = NULL;
static gchar *xmllint = NULL;
+static gchar *gdk_pixbuf_pixdata = NULL;
static void
file_data_free (FileData *data)
return parent;
}
+static gchar *
+find_file (const gchar *filename)
+{
+ guint i;
+ gchar *real_file;
+ gboolean exists;
+
+ if (g_path_is_absolute (filename))
+ return g_strdup (filename);
+
+ /* search all the sourcedirs for the correct files in order */
+ for (i = 0; sourcedirs[i] != NULL; i++)
+ {
+ real_file = g_build_filename (sourcedirs[i], filename, NULL);
+ exists = g_file_test (real_file, G_FILE_TEST_EXISTS);
+ if (exists)
+ return real_file;
+ g_free (real_file);
+ }
+ return NULL;
+}
+
static void
end_element (GMarkupParseContext *context,
const gchar *element_name,
gchar *key;
FileData *data;
char *tmp_file = NULL;
+ char *tmp_file2 = NULL;
file = state->string->str;
key = file;
data = g_new0 (FileData, 1);
- if (sourcedir != NULL)
- real_file = g_build_filename (sourcedir, file, NULL);
+ if (sourcedirs != NULL)
+ {
+ real_file = find_file (file);
+ if (real_file == NULL)
+ {
+ g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ _("Failed to locate '%s' in any source directory"), file);
+ return;
+ }
+ }
else
- real_file = g_strdup (file);
+ {
+ gboolean exists;
+ exists = g_file_test (file, G_FILE_TEST_EXISTS);
+ if (!exists)
+ {
+ g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ _("Failed to locate '%s' in current directory"), file);
+ return;
+ }
+ real_file = g_strdup (file);
+ }
data->filename = g_strdup (real_file);
if (!state->collect_data)
gchar **options;
guint i;
gboolean xml_stripblanks = FALSE;
+ gboolean to_pixdata = FALSE;
options = g_strsplit (state->preproc_options, ",", -1);
{
if (!strcmp (options[i], "xml-stripblanks"))
xml_stripblanks = TRUE;
+ else if (!strcmp (options[i], "to-pixdata"))
+ to_pixdata = TRUE;
else
{
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
- _("Unknown proprocessing options \"%s\""), options[i]);
+ _("Unknown processing option \"%s\""), options[i]);
g_strfreev (options);
goto cleanup;
}
argc = 0;
argv[argc++] = (gchar *) xmllint;
argv[argc++] = "--nonet";
- argv[argc++] = "--noent";
argv[argc++] = "--noblanks";
argv[argc++] = "--output";
argv[argc++] = tmp_file;
g_free (real_file);
real_file = g_strdup (tmp_file);
}
- }
+
+ if (to_pixdata)
+ {
+ gchar *argv[4];
+ int status, fd, argc;
+
+ if (gdk_pixbuf_pixdata == NULL)
+ {
+ g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "to-pixbuf preprocessing requested but GDK_PIXBUF_PIXDATA "
+ "not set and gdk-pixbuf-pixdata not found in path");
+ goto cleanup;
+ }
+
+ tmp_file2 = g_strdup ("resource-XXXXXXXX");
+ if ((fd = g_mkstemp (tmp_file2)) == -1)
+ {
+ int errsv = errno;
+
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
+ _("Failed to create temp file: %s"),
+ g_strerror (errsv));
+ g_free (tmp_file2);
+ tmp_file2 = NULL;
+ goto cleanup;
+ }
+ close (fd);
+
+ argc = 0;
+ argv[argc++] = (gchar *) gdk_pixbuf_pixdata;
+ argv[argc++] = real_file;
+ argv[argc++] = tmp_file2;
+ argv[argc++] = NULL;
+ g_assert (argc <= G_N_ELEMENTS (argv));
+
+ if (!g_spawn_sync (NULL /* cwd */, argv, NULL /* envv */,
+ G_SPAWN_STDOUT_TO_DEV_NULL |
+ G_SPAWN_STDERR_TO_DEV_NULL,
+ NULL, NULL, NULL, NULL, &status, &my_error))
+ {
+ g_propagate_error (error, my_error);
+ goto cleanup;
+ }
+#ifdef HAVE_SYS_WAIT_H
+ if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
+ {
+ g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ _("Error processing input file with to-pixdata"));
+ goto cleanup;
+ }
+#endif
+
+ g_free (real_file);
+ real_file = g_strdup (tmp_file2);
+ }
+ }
if (!g_file_get_contents (real_file, &data->content, &data->size, &my_error))
{
state->preproc_options = NULL;
g_free (real_file);
+
if (tmp_file)
{
unlink (tmp_file);
g_free (tmp_file);
}
+
+ if (tmp_file2)
+ {
+ unlink (tmp_file2);
+ g_free (tmp_file2);
+ }
}
}
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;
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") },
+ { "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 },
srcfile = argv[1];
- if (sourcedir == NULL)
- sourcedir = "";
-
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);
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)
{
"\n"
"#include <gio/gio.h>\n"
"\n"
- "extern GResource *%s_resource;\n",
+ "extern GResource *%s_get_resource (void);\n",
c_name, c_name, c_name);
if (manual_register)
guint8 *data;
gsize data_size;
gsize i;
- char *static_str;
if (!g_file_get_contents (binary_target, (char **)&data,
&data_size, NULL))
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)
{
- static_str = "";
+ 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
{
- static_str = "static ";
+ fprintf (file, "%s", gconstructor_code);
fprintf (file,
"\n"
"#ifdef G_HAS_CONSTRUCTORS\n"
"#else\n"
"#warning \"Constructor not supported on this compiler, linking in resources will not work\"\n"
"#endif\n"
- "\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");
}
- fprintf (file,
- "\n"
- "GResource *%s_resource = NULL;\n"
- "\n"
- "%svoid %s_unregister_resource (void)\n"
- "{\n"
- " if (%s_resource)\n"
- " {\n"
- " g_resources_unregister (%s_resource);\n"
- " g_resource_unref (%s_resource);\n"
- " %s_resource = NULL;\n"
- " }\n"
- "}\n"
- "\n"
- "%svoid %s_register_resource (void)\n"
- "{\n"
- " if (%s_resource == NULL)\n"
- " {\n"
- " GBytes *bytes = g_bytes_new_static (%s_resource_data.data, sizeof (%s_resource_data.data));\n"
- " %s_resource = g_resource_new_from_data (bytes, NULL);\n"
- " if (%s_resource)\n"
- " g_resources_register (%s_resource);\n"
- " g_bytes_unref (bytes);\n"
- " }\n"
- "}\n", c_name, static_str, c_name, c_name, c_name, c_name, c_name, static_str, c_name, c_name, c_name, c_name, c_name, c_name, c_name);
-
- if (!manual_register)
- fprintf (file,
- "\n"
- "static void resource_constructor (void)\n"
- "{\n"
- " %s_register_resource ();\n"
- "}\n"
- "\n"
- "static void resource_destructor (void)\n"
- "{\n"
- " %s_unregister_resource ();\n"
- "}\n", c_name, c_name);
-
fclose (file);
g_free (data);