Blame gio/glib-compile-resources.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2011 Red Hat, Inc
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include <gstdio.h>
Packit ae235b
#include <gi18n.h>
Packit ae235b
#include <gioenums.h>
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <locale.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <io.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include <gio/gmemoryoutputstream.h>
Packit ae235b
#include <gio/gzlibcompressor.h>
Packit ae235b
#include <gio/gconverteroutputstream.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include "gvdb/gvdb-builder.h"
Packit ae235b
Packit ae235b
#include "gconstructor_as_data.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include "glib/glib-private.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  char *filename;
Packit ae235b
  char *content;
Packit ae235b
  gsize content_size;
Packit ae235b
  gsize size;
Packit ae235b
  guint32 flags;
Packit ae235b
} FileData;
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GHashTable *table; /* resource path -> FileData */
Packit ae235b
Packit ae235b
  gboolean collect_data;
Packit ae235b
Packit ae235b
  /* per gresource */
Packit ae235b
  char *prefix;
Packit ae235b
Packit ae235b
  /* per file */
Packit ae235b
  char *alias;
Packit ae235b
  gboolean compressed;
Packit ae235b
  char *preproc_options;
Packit ae235b
Packit ae235b
  GString *string;  /* non-NULL when accepting text */
Packit ae235b
} ParseState;
Packit ae235b
Packit ae235b
static gchar **sourcedirs = NULL;
Packit ae235b
static gchar *xmllint = NULL;
Packit ae235b
static gchar *gdk_pixbuf_pixdata = NULL;
Packit ae235b
Packit ae235b
static void
Packit ae235b
file_data_free (FileData *data)
Packit ae235b
{
Packit ae235b
  g_free (data->filename);
Packit ae235b
  g_free (data->content);
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
start_element (GMarkupParseContext  *context,
Packit ae235b
	       const gchar          *element_name,
Packit ae235b
	       const gchar         **attribute_names,
Packit ae235b
	       const gchar         **attribute_values,
Packit ae235b
	       gpointer              user_data,
Packit ae235b
	       GError              **error)
Packit ae235b
{
Packit ae235b
  ParseState *state = user_data;
Packit ae235b
  const GSList *element_stack;
Packit ae235b
  const gchar *container;
Packit ae235b
Packit ae235b
  element_stack = g_markup_parse_context_get_element_stack (context);
Packit ae235b
  container = element_stack->next ? element_stack->next->data : NULL;
Packit ae235b
Packit ae235b
#define COLLECT(first, ...) \
Packit ae235b
  g_markup_collect_attributes (element_name,                                 \
Packit ae235b
			       attribute_names, attribute_values, error,     \
Packit ae235b
			       first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
Packit ae235b
#define OPTIONAL   G_MARKUP_COLLECT_OPTIONAL
Packit ae235b
#define STRDUP     G_MARKUP_COLLECT_STRDUP
Packit ae235b
#define STRING     G_MARKUP_COLLECT_STRING
Packit ae235b
#define BOOL       G_MARKUP_COLLECT_BOOLEAN
Packit ae235b
#define NO_ATTRS()  COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
Packit ae235b
Packit ae235b
  if (container == NULL)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "gresources") == 0)
Packit ae235b
	return;
Packit ae235b
    }
Packit ae235b
  else if (strcmp (container, "gresources") == 0)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "gresource") == 0)
Packit ae235b
	{
Packit ae235b
	  COLLECT (OPTIONAL | STRDUP,
Packit ae235b
		   "prefix", &state->prefix);
Packit ae235b
	  return;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else if (strcmp (container, "gresource") == 0)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "file") == 0)
Packit ae235b
	{
Packit ae235b
	  COLLECT (OPTIONAL | STRDUP, "alias", &state->alias,
Packit ae235b
		   OPTIONAL | BOOL, "compressed", &state->compressed,
Packit ae235b
                   OPTIONAL | STRDUP, "preprocess", &state->preproc_options);
Packit ae235b
	  state->string = g_string_new ("");
Packit ae235b
	  return;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (container)
Packit ae235b
    g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
Packit ae235b
		 _("Element <%s> not allowed inside <%s>"),
Packit ae235b
		 element_name, container);
Packit ae235b
  else
Packit ae235b
    g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
Packit ae235b
		 _("Element <%s> not allowed at toplevel"), element_name);
Packit ae235b
Packit ae235b
}
Packit ae235b
Packit ae235b
static GvdbItem *
Packit ae235b
get_parent (GHashTable *table,
Packit ae235b
	    gchar      *key,
Packit ae235b
	    gint        length)
Packit ae235b
{
Packit ae235b
  GvdbItem *grandparent, *parent;
Packit ae235b
Packit ae235b
  if (length == 1)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  while (key[--length - 1] != '/');
Packit ae235b
  key[length] = '\0';
Packit ae235b
Packit ae235b
  parent = g_hash_table_lookup (table, key);
Packit ae235b
Packit ae235b
  if (parent == NULL)
Packit ae235b
    {
Packit ae235b
      parent = gvdb_hash_table_insert (table, key);
Packit ae235b
Packit ae235b
      grandparent = get_parent (table, key, length);
Packit ae235b
Packit ae235b
      if (grandparent != NULL)
Packit ae235b
	gvdb_item_set_parent (parent, grandparent);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return parent;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
find_file (const gchar *filename)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
  gchar *real_file;
Packit ae235b
  gboolean exists;
Packit ae235b
Packit ae235b
  if (g_path_is_absolute (filename))
Packit ae235b
    return g_strdup (filename);
Packit ae235b
Packit ae235b
  /* search all the sourcedirs for the correct files in order */
Packit ae235b
  for (i = 0; sourcedirs[i] != NULL; i++)
Packit ae235b
    {
Packit ae235b
	real_file = g_build_path ("/", sourcedirs[i], filename, NULL);
Packit ae235b
	exists = g_file_test (real_file, G_FILE_TEST_EXISTS);
Packit ae235b
	if (exists)
Packit ae235b
	  return real_file;
Packit ae235b
	g_free (real_file);
Packit ae235b
    }
Packit ae235b
    return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
end_element (GMarkupParseContext  *context,
Packit ae235b
	     const gchar          *element_name,
Packit ae235b
	     gpointer              user_data,
Packit ae235b
	     GError              **error)
Packit ae235b
{
Packit ae235b
  ParseState *state = user_data;
Packit ae235b
  GError *my_error = NULL;
Packit ae235b
Packit ae235b
  if (strcmp (element_name, "gresource") == 0)
Packit ae235b
    {
Packit ae235b
      g_free (state->prefix);
Packit ae235b
      state->prefix = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (strcmp (element_name, "file") == 0)
Packit ae235b
    {
Packit ae235b
      gchar *file;
Packit ae235b
      gchar *real_file = NULL;
Packit ae235b
      gchar *key;
Packit ae235b
      FileData *data = NULL;
Packit ae235b
      char *tmp_file = NULL;
Packit ae235b
      char *tmp_file2 = NULL;
Packit ae235b
Packit ae235b
      file = state->string->str;
Packit ae235b
      key = file;
Packit ae235b
      if (state->alias)
Packit ae235b
	key = state->alias;
Packit ae235b
Packit ae235b
      if (state->prefix)
Packit ae235b
	key = g_build_path ("/", "/", state->prefix, key, NULL);
Packit ae235b
      else
Packit ae235b
	key = g_build_path ("/", "/", key, NULL);
Packit ae235b
Packit ae235b
      if (g_hash_table_lookup (state->table, key) != NULL)
Packit ae235b
	{
Packit ae235b
	  g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
		       _("File %s appears multiple times in the resource"),
Packit ae235b
		       key);
Packit ae235b
	  return;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (sourcedirs != NULL)
Packit ae235b
        {
Packit ae235b
	  real_file = find_file (file);
Packit ae235b
	  if (real_file == NULL && state->collect_data)
Packit ae235b
	    {
Packit ae235b
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
			     _("Failed to locate “%s” in any source directory"), file);
Packit ae235b
		return;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
	  gboolean exists;
Packit ae235b
	  exists = g_file_test (file, G_FILE_TEST_EXISTS);
Packit ae235b
	  if (!exists && state->collect_data)
Packit ae235b
	    {
Packit ae235b
	      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
			   _("Failed to locate “%s” in current directory"), file);
Packit ae235b
	      return;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (real_file == NULL)
Packit ae235b
        real_file = g_strdup (file);
Packit ae235b
Packit ae235b
      data = g_new0 (FileData, 1);
Packit ae235b
      data->filename = g_strdup (real_file);
Packit ae235b
      if (!state->collect_data)
Packit ae235b
        goto done;
Packit ae235b
Packit ae235b
      if (state->preproc_options)
Packit ae235b
        {
Packit ae235b
          gchar **options;
Packit ae235b
          guint i;
Packit ae235b
          gboolean xml_stripblanks = FALSE;
Packit ae235b
          gboolean to_pixdata = FALSE;
Packit ae235b
Packit ae235b
          options = g_strsplit (state->preproc_options, ",", -1);
Packit ae235b
Packit ae235b
          for (i = 0; options[i]; i++)
Packit ae235b
            {
Packit ae235b
              if (!strcmp (options[i], "xml-stripblanks"))
Packit ae235b
                xml_stripblanks = TRUE;
Packit ae235b
              else if (!strcmp (options[i], "to-pixdata"))
Packit ae235b
                to_pixdata = TRUE;
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                               _("Unknown processing option “%s”"), options[i]);
Packit ae235b
                  g_strfreev (options);
Packit ae235b
                  goto cleanup;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          g_strfreev (options);
Packit ae235b
Packit ae235b
          if (xml_stripblanks && xmllint != NULL)
Packit ae235b
            {
Packit ae235b
              int fd;
Packit ae235b
	      GSubprocess *proc;
Packit ae235b
Packit ae235b
              tmp_file = g_strdup ("resource-XXXXXXXX");
Packit ae235b
              if ((fd = g_mkstemp (tmp_file)) == -1)
Packit ae235b
                {
Packit ae235b
                  int errsv = errno;
Packit ae235b
Packit ae235b
                  g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
Packit ae235b
                               _("Failed to create temp file: %s"),
Packit ae235b
                              g_strerror (errsv));
Packit ae235b
                  g_free (tmp_file);
Packit ae235b
                  tmp_file = NULL;
Packit ae235b
                  goto cleanup;
Packit ae235b
                }
Packit ae235b
              close (fd);
Packit ae235b
Packit ae235b
              proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error,
Packit ae235b
                                       xmllint, "--nonet", "--noblanks", "--output", tmp_file, real_file, NULL);
Packit ae235b
              g_free (real_file);
Packit ae235b
	      real_file = NULL;
Packit ae235b
Packit ae235b
	      if (!proc)
Packit ae235b
		goto cleanup;
Packit ae235b
Packit ae235b
	      if (!g_subprocess_wait_check (proc, NULL, error))
Packit ae235b
		{
Packit ae235b
		  g_object_unref (proc);
Packit ae235b
                  goto cleanup;
Packit ae235b
                }
Packit ae235b
Packit ae235b
	      g_object_unref (proc);
Packit ae235b
Packit ae235b
              real_file = g_strdup (tmp_file);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          if (to_pixdata)
Packit ae235b
            {
Packit ae235b
              int fd;
Packit ae235b
	      GSubprocess *proc;
Packit ae235b
Packit ae235b
              if (gdk_pixbuf_pixdata == NULL)
Packit ae235b
                {
Packit ae235b
                  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
                                       "to-pixbuf preprocessing requested but GDK_PIXBUF_PIXDATA "
Packit ae235b
                                       "not set and gdk-pixbuf-pixdata not found in path");
Packit ae235b
                  goto cleanup;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              tmp_file2 = g_strdup ("resource-XXXXXXXX");
Packit ae235b
              if ((fd = g_mkstemp (tmp_file2)) == -1)
Packit ae235b
                {
Packit ae235b
                  int errsv = errno;
Packit ae235b
Packit ae235b
                  g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
Packit ae235b
                               _("Failed to create temp file: %s"),
Packit ae235b
			       g_strerror (errsv));
Packit ae235b
                  g_free (tmp_file2);
Packit ae235b
                  tmp_file2 = NULL;
Packit ae235b
                  goto cleanup;
Packit ae235b
                }
Packit ae235b
              close (fd);
Packit ae235b
Packit ae235b
              proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error,
Packit ae235b
                                       gdk_pixbuf_pixdata, real_file, tmp_file2, NULL);
Packit ae235b
              g_free (real_file);
Packit ae235b
              real_file = NULL;
Packit ae235b
Packit ae235b
	      if (!g_subprocess_wait_check (proc, NULL, error))
Packit ae235b
		{
Packit ae235b
		  g_object_unref (proc);
Packit ae235b
                  goto cleanup;
Packit ae235b
		}
Packit ae235b
Packit ae235b
	      g_object_unref (proc);
Packit ae235b
Packit ae235b
              real_file = g_strdup (tmp_file2);
Packit ae235b
            }
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (!g_file_get_contents (real_file, &data->content, &data->size, &my_error))
Packit ae235b
	{
Packit ae235b
	  g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
		       _("Error reading file %s: %s"),
Packit ae235b
		       real_file, my_error->message);
Packit ae235b
	  g_clear_error (&my_error);
Packit ae235b
	  goto cleanup;
Packit ae235b
	}
Packit ae235b
      /* Include zero termination in content_size for uncompressed files (but not in size) */
Packit ae235b
      data->content_size = data->size + 1;
Packit ae235b
Packit ae235b
      if (state->compressed)
Packit ae235b
	{
Packit ae235b
	  GOutputStream *out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
Packit ae235b
	  GZlibCompressor *compressor =
Packit ae235b
	    g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB, 9);
Packit ae235b
	  GOutputStream *out2 = g_converter_output_stream_new (out, G_CONVERTER (compressor));
Packit ae235b
Packit ae235b
	  if (!g_output_stream_write_all (out2, data->content, data->size,
Packit ae235b
					  NULL, NULL, NULL) ||
Packit ae235b
	      !g_output_stream_close (out2, NULL, NULL))
Packit ae235b
	    {
Packit ae235b
	      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
			   _("Error compressing file %s"),
Packit ae235b
			   real_file);
Packit ae235b
	      goto cleanup;
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  g_free (data->content);
Packit ae235b
	  data->content_size = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (out));
Packit ae235b
	  data->content = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out));
Packit ae235b
Packit ae235b
	  g_object_unref (compressor);
Packit ae235b
	  g_object_unref (out);
Packit ae235b
	  g_object_unref (out2);
Packit ae235b
Packit ae235b
	  data->flags |= G_RESOURCE_FLAGS_COMPRESSED;
Packit ae235b
	}
Packit ae235b
Packit ae235b
done:
Packit ae235b
      g_hash_table_insert (state->table, key, data);
Packit ae235b
      data = NULL;
Packit ae235b
Packit ae235b
    cleanup:
Packit ae235b
      /* Cleanup */
Packit ae235b
Packit ae235b
      g_free (state->alias);
Packit ae235b
      state->alias = NULL;
Packit ae235b
      g_string_free (state->string, TRUE);
Packit ae235b
      state->string = NULL;
Packit ae235b
      g_free (state->preproc_options);
Packit ae235b
      state->preproc_options = NULL;
Packit ae235b
Packit ae235b
      g_free (real_file);
Packit ae235b
Packit ae235b
      if (tmp_file)
Packit ae235b
        {
Packit ae235b
          unlink (tmp_file);
Packit ae235b
          g_free (tmp_file);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (tmp_file2)
Packit ae235b
        {
Packit ae235b
          unlink (tmp_file2);
Packit ae235b
          g_free (tmp_file2);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (data != NULL)
Packit ae235b
        file_data_free (data);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
text (GMarkupParseContext  *context,
Packit ae235b
      const gchar          *text,
Packit ae235b
      gsize                 text_len,
Packit ae235b
      gpointer              user_data,
Packit ae235b
      GError              **error)
Packit ae235b
{
Packit ae235b
  ParseState *state = user_data;
Packit ae235b
  gsize i;
Packit ae235b
Packit ae235b
  for (i = 0; i < text_len; i++)
Packit ae235b
    if (!g_ascii_isspace (text[i]))
Packit ae235b
      {
Packit ae235b
	if (state->string)
Packit ae235b
	  g_string_append_len (state->string, text, text_len);
Packit ae235b
Packit ae235b
	else
Packit ae235b
	  g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
		       _("text may not appear inside <%s>"),
Packit ae235b
		       g_markup_parse_context_get_element (context));
Packit ae235b
Packit ae235b
	break;
Packit ae235b
      }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GHashTable *
Packit ae235b
parse_resource_file (const gchar *filename,
Packit ae235b
                     gboolean     collect_data,
Packit ae235b
                     GHashTable  *files)
Packit ae235b
{
Packit ae235b
  GMarkupParser parser = { start_element, end_element, text };
Packit ae235b
  ParseState state = { 0, };
Packit ae235b
  GMarkupParseContext *context;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gchar *contents;
Packit ae235b
  GHashTable *table = NULL;
Packit ae235b
  gsize size;
Packit ae235b
Packit ae235b
  if (!g_file_get_contents (filename, &contents, &size, &error))
Packit ae235b
    {
Packit ae235b
      g_printerr ("%s\n", error->message);
Packit ae235b
      g_clear_error (&error);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  state.collect_data = collect_data;
Packit ae235b
  state.table = g_hash_table_ref (files);
Packit ae235b
Packit ae235b
  context = g_markup_parse_context_new (&parser,
Packit ae235b
					G_MARKUP_TREAT_CDATA_AS_TEXT |
Packit ae235b
					G_MARKUP_PREFIX_ERROR_POSITION,
Packit ae235b
					&state, NULL);
Packit ae235b
Packit ae235b
  if (!g_markup_parse_context_parse (context, contents, size, &error) ||
Packit ae235b
      !g_markup_parse_context_end_parse (context, &error))
Packit ae235b
    {
Packit ae235b
      g_printerr ("%s: %s.\n", filename, error->message);
Packit ae235b
      g_clear_error (&error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      GHashTableIter iter;
Packit ae235b
      const char *key;
Packit ae235b
      char *mykey;
Packit ae235b
      gsize key_len;
Packit ae235b
      FileData *data;
Packit ae235b
      GVariant *v_data;
Packit ae235b
      GVariantBuilder builder;
Packit ae235b
      GvdbItem *item;
Packit ae235b
Packit ae235b
      table = gvdb_hash_table_new (NULL, NULL);
Packit ae235b
Packit ae235b
      g_hash_table_iter_init (&iter, state.table);
Packit ae235b
      while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&data))
Packit ae235b
	{
Packit ae235b
	  key_len = strlen (key);
Packit ae235b
	  mykey = g_strdup (key);
Packit ae235b
Packit ae235b
	  item = gvdb_hash_table_insert (table, key);
Packit ae235b
	  gvdb_item_set_parent (item,
Packit ae235b
				get_parent (table, mykey, key_len));
Packit ae235b
Packit ae235b
	  g_free (mykey);
Packit ae235b
Packit ae235b
	  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(uuay)"));
Packit ae235b
Packit ae235b
	  g_variant_builder_add (&builder, "u", data->size); /* Size */
Packit ae235b
	  g_variant_builder_add (&builder, "u", data->flags); /* Flags */
Packit ae235b
Packit ae235b
	  v_data = g_variant_new_from_data (G_VARIANT_TYPE("ay"),
Packit ae235b
					    data->content, data->content_size, TRUE,
Packit ae235b
					    g_free, data->content);
Packit ae235b
	  g_variant_builder_add_value (&builder, v_data);
Packit ae235b
	  data->content = NULL; /* Take ownership */
Packit ae235b
Packit ae235b
	  gvdb_item_set_value (item,
Packit ae235b
			       g_variant_builder_end (&builder));
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_hash_table_unref (state.table);
Packit ae235b
  g_markup_parse_context_free (context);
Packit ae235b
  g_free (contents);
Packit ae235b
Packit ae235b
  return table;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
write_to_file (GHashTable   *table,
Packit ae235b
	       const gchar  *filename,
Packit ae235b
	       GError      **error)
Packit ae235b
{
Packit ae235b
  gboolean success;
Packit ae235b
Packit ae235b
  success = gvdb_table_write_contents (table, filename,
Packit ae235b
				       G_BYTE_ORDER != G_LITTLE_ENDIAN,
Packit ae235b
				       error);
Packit ae235b
Packit ae235b
  return success;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
extension_in_set (const char *str,
Packit ae235b
                  ...)
Packit ae235b
{
Packit ae235b
  va_list list;
Packit ae235b
  const char *ext, *value;
Packit ae235b
  gboolean rv = FALSE;
Packit ae235b
Packit ae235b
  ext = strrchr (str, '.');
Packit ae235b
  if (ext == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  ext++;
Packit ae235b
  va_start (list, str);
Packit ae235b
  while ((value = va_arg (list, const char *)) != NULL)
Packit ae235b
    {
Packit ae235b
      if (g_ascii_strcasecmp (ext, value) != 0)
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      rv = TRUE;
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  va_end (list);
Packit ae235b
  return rv;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * We must escape any characters that `make` finds significant.
Packit ae235b
 * This is largely a duplicate of the logic in gcc's `mkdeps.c:munge()`.
Packit ae235b
 */
Packit ae235b
static char *
Packit ae235b
escape_makefile_string (const char *string)
Packit ae235b
{
Packit ae235b
  GString *str;
Packit ae235b
  const char *p, *q;
Packit ae235b
Packit ae235b
  str = g_string_sized_new (strlen (string) + 1);
Packit ae235b
  for (p = string; *p != '\0'; ++p)
Packit ae235b
    {
Packit ae235b
      switch (*p)
Packit ae235b
        {
Packit ae235b
        case ' ':
Packit ae235b
        case '\t':
Packit ae235b
          /* GNU make uses a weird quoting scheme for white space.
Packit ae235b
             A space or tab preceded by 2N+1 backslashes represents
Packit ae235b
             N backslashes followed by space; a space or tab
Packit ae235b
             preceded by 2N backslashes represents N backslashes at
Packit ae235b
             the end of a file name; and backslashes in other
Packit ae235b
             contexts should not be doubled.  */
Packit ae235b
          for (q = p - 1; string <= q && *q == '\\';  q--)
Packit ae235b
            g_string_append_c (str, '\\');
Packit ae235b
          g_string_append_c (str, '\\');
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case '$':
Packit ae235b
          g_string_append_c (str, '$');
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case '#':
Packit ae235b
          g_string_append_c (str, '\\');
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
      g_string_append_c (str, *p);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_string_free (str, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char **argv)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GHashTable *table;
Packit ae235b
  GHashTable *files;
Packit ae235b
  gchar *srcfile;
Packit ae235b
  gboolean show_version_and_exit = FALSE;
Packit ae235b
  gchar *target = NULL;
Packit ae235b
  gchar *binary_target = NULL;
Packit ae235b
  gboolean generate_automatic = FALSE;
Packit ae235b
  gboolean generate_source = FALSE;
Packit ae235b
  gboolean generate_header = FALSE;
Packit ae235b
  gboolean manual_register = FALSE;
Packit ae235b
  gboolean internal = FALSE;
Packit ae235b
  gboolean generate_dependencies = FALSE;
Packit ae235b
  gboolean generate_phony_targets = FALSE;
Packit ae235b
  char *dependency_file = NULL;
Packit ae235b
  char *c_name = NULL;
Packit ae235b
  char *c_name_no_underscores;
Packit ae235b
  const char *linkage = "extern";
Packit ae235b
  GOptionContext *context;
Packit ae235b
  GOptionEntry entries[] = {
Packit ae235b
    { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL },
Packit ae235b
    { "target", 0, 0, G_OPTION_ARG_FILENAME, &target, N_("name of the output file"), N_("FILE") },
Packit ae235b
    { "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") },
Packit ae235b
    { "generate", 0, 0, G_OPTION_ARG_NONE, &generate_automatic, N_("Generate output in the format selected for by the target filename extension"), NULL },
Packit ae235b
    { "generate-header", 0, 0, G_OPTION_ARG_NONE, &generate_header, N_("Generate source header"), NULL },
Packit ae235b
    { "generate-source", 0, 0, G_OPTION_ARG_NONE, &generate_source, N_("Generate sourcecode used to link in the resource file into your code"), NULL },
Packit ae235b
    { "generate-dependencies", 0, 0, G_OPTION_ARG_NONE, &generate_dependencies, N_("Generate dependency list"), NULL },
Packit ae235b
    { "dependency-file", 0, 0, G_OPTION_ARG_FILENAME, &dependency_file, N_("name of the dependency file to generate"), N_("FILE") },
Packit ae235b
    { "generate-phony-targets", 0, 0, G_OPTION_ARG_NONE, &generate_phony_targets, N_("Include phony targets in the generated dependency file"), NULL },
Packit ae235b
    { "manual-register", 0, 0, G_OPTION_ARG_NONE, &manual_register, N_("Don’t automatically create and register resource"), NULL },
Packit ae235b
    { "internal", 0, 0, G_OPTION_ARG_NONE, &internal, N_("Don’t export functions; declare them G_GNUC_INTERNAL"), NULL },
Packit ae235b
    { "c-name", 0, 0, G_OPTION_ARG_STRING, &c_name, N_("C identifier name used for the generated source code"), NULL },
Packit ae235b
    { NULL }
Packit ae235b
  };
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  gchar *tmp;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  setlocale (LC_ALL, "");
Packit ae235b
  textdomain (GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  tmp = _glib_get_locale_dir ();
Packit ae235b
  bindtextdomain (GETTEXT_PACKAGE, tmp);
Packit ae235b
  g_free (tmp);
Packit ae235b
#else
Packit ae235b
  bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
Packit ae235b
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  context = g_option_context_new (N_("FILE"));
Packit ae235b
  g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
Packit ae235b
  g_option_context_set_summary (context,
Packit ae235b
    N_("Compile a resource specification into a resource file.\n"
Packit ae235b
       "Resource specification files have the extension .gresource.xml,\n"
Packit ae235b
       "and the resource file have the extension called .gresource."));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      g_printerr ("%s\n", error->message);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  if (show_version_and_exit)
Packit ae235b
    {
Packit ae235b
      g_print (PACKAGE_VERSION "\n");
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (argc != 2)
Packit ae235b
    {
Packit ae235b
      g_printerr (_("You should give exactly one file name\n"));
Packit ae235b
      g_free (c_name);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (internal)
Packit ae235b
    linkage = "G_GNUC_INTERNAL";
Packit ae235b
Packit ae235b
  srcfile = argv[1];
Packit ae235b
Packit ae235b
  xmllint = g_strdup (g_getenv ("XMLLINT"));
Packit ae235b
  if (xmllint == NULL)
Packit ae235b
    xmllint = g_find_program_in_path ("xmllint");
Packit ae235b
  if (xmllint == NULL)
Packit ae235b
    g_printerr ("XMLLINT not set and xmllint not found in path; skipping xml preprocessing.\n");
Packit ae235b
Packit ae235b
  gdk_pixbuf_pixdata = g_strdup (g_getenv ("GDK_PIXBUF_PIXDATA"));
Packit ae235b
  if (gdk_pixbuf_pixdata == NULL)
Packit ae235b
    gdk_pixbuf_pixdata = g_find_program_in_path ("gdk-pixbuf-pixdata");
Packit ae235b
Packit ae235b
  if (target == NULL)
Packit ae235b
    {
Packit ae235b
      char *dirname = g_path_get_dirname (srcfile);
Packit ae235b
      char *base = g_path_get_basename (srcfile);
Packit ae235b
      char *target_basename;
Packit ae235b
      if (g_str_has_suffix (base, ".xml"))
Packit ae235b
	base[strlen(base) - strlen (".xml")] = 0;
Packit ae235b
Packit ae235b
      if (generate_source)
Packit ae235b
	{
Packit ae235b
	  if (g_str_has_suffix (base, ".gresource"))
Packit ae235b
	    base[strlen(base) - strlen (".gresource")] = 0;
Packit ae235b
	  target_basename = g_strconcat (base, ".c", NULL);
Packit ae235b
	}
Packit ae235b
      else if (generate_header)
Packit ae235b
        {
Packit ae235b
          if (g_str_has_suffix (base, ".gresource"))
Packit ae235b
            base[strlen(base) - strlen (".gresource")] = 0;
Packit ae235b
          target_basename = g_strconcat (base, ".h", NULL);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  if (g_str_has_suffix (base, ".gresource"))
Packit ae235b
	    target_basename = g_strdup (base);
Packit ae235b
	  else
Packit ae235b
	    target_basename = g_strconcat (base, ".gresource", NULL);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      target = g_build_filename (dirname, target_basename, NULL);
Packit ae235b
      g_free (target_basename);
Packit ae235b
      g_free (dirname);
Packit ae235b
      g_free (base);
Packit ae235b
    }
Packit ae235b
  else if (generate_automatic)
Packit ae235b
    {
Packit ae235b
      if (extension_in_set (target, "c", "cc", "cpp", "cxx", "c++", NULL))
Packit ae235b
        generate_source = TRUE;
Packit ae235b
      else if (extension_in_set (target, "h", "hh", "hpp", "hxx", "h++", NULL))
Packit ae235b
        generate_header = TRUE;
Packit ae235b
      else if (extension_in_set (target, "gresource", NULL))
Packit ae235b
        { }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)file_data_free);
Packit ae235b
Packit ae235b
  if ((table = parse_resource_file (srcfile, !generate_dependencies, files)) == NULL)
Packit ae235b
    {
Packit ae235b
      g_free (target);
Packit ae235b
      g_free (c_name);
Packit ae235b
      g_hash_table_unref (files);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* This can be used in the same invocation
Packit ae235b
     as other generate commands */
Packit ae235b
  if (dependency_file != NULL)
Packit ae235b
    {
Packit ae235b
      /* Generate a .d file that describes the dependencies for
Packit ae235b
       * build tools, gcc -M -MF style */
Packit ae235b
      GString *dep_string;
Packit ae235b
      GHashTableIter iter;
Packit ae235b
      gpointer key, data;
Packit ae235b
      FileData *file_data;
Packit ae235b
      char *escaped;
Packit ae235b
Packit ae235b
      g_hash_table_iter_init (&iter, files);
Packit ae235b
Packit ae235b
      dep_string = g_string_new (NULL);
Packit ae235b
      escaped = escape_makefile_string (srcfile);
Packit ae235b
      g_string_printf (dep_string, "%s:", escaped);
Packit ae235b
      g_free (escaped);
Packit ae235b
Packit ae235b
      /* First rule: foo.xml: resource1 resource2.. */
Packit ae235b
      while (g_hash_table_iter_next (&iter, &key, &data))
Packit ae235b
        {
Packit ae235b
          file_data = data;
Packit ae235b
          if (!g_str_equal (file_data->filename, srcfile))
Packit ae235b
            {
Packit ae235b
              escaped = escape_makefile_string (file_data->filename);
Packit ae235b
              g_string_append_printf (dep_string, " %s", escaped);
Packit ae235b
              g_free (escaped);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_string_append (dep_string, "\n");
Packit ae235b
Packit ae235b
      /* Optionally include phony targets as it silences `make` but
Packit ae235b
       * isn't supported on `ninja` at the moment. See also: `gcc -MP`
Packit ae235b
       */
Packit ae235b
      if (generate_phony_targets)
Packit ae235b
        {
Packit ae235b
					g_string_append (dep_string, "\n");
Packit ae235b
Packit ae235b
          /* One rule for every resource: resourceN: */
Packit ae235b
          g_hash_table_iter_init (&iter, files);
Packit ae235b
          while (g_hash_table_iter_next (&iter, &key, &data))
Packit ae235b
            {
Packit ae235b
              file_data = data;
Packit ae235b
              if (!g_str_equal (file_data->filename, srcfile))
Packit ae235b
                {
Packit ae235b
                  escaped = escape_makefile_string (file_data->filename);
Packit ae235b
                  g_string_append_printf (dep_string, "%s:\n\n", escaped);
Packit ae235b
                  g_free (escaped);
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (g_str_equal (dependency_file, "-"))
Packit ae235b
        {
Packit ae235b
          g_print ("%s\n", dep_string->str);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          if (!g_file_set_contents (dependency_file, dep_string->str, dep_string->len, &error))
Packit ae235b
            {
Packit ae235b
              g_printerr ("Error writing dependency file: %s\n", error->message);
Packit ae235b
              g_string_free (dep_string, TRUE);
Packit ae235b
              g_free (dependency_file);
Packit ae235b
              g_error_free (error);
Packit ae235b
              g_hash_table_unref (files);
Packit ae235b
              return 1;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_string_free (dep_string, TRUE);
Packit ae235b
      g_free (dependency_file);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (generate_dependencies)
Packit ae235b
    {
Packit ae235b
      GHashTableIter iter;
Packit ae235b
      gpointer key, data;
Packit ae235b
      FileData *file_data;
Packit ae235b
Packit ae235b
      g_hash_table_iter_init (&iter, files);
Packit ae235b
Packit ae235b
      /* Generate list of files for direct use as dependencies in a Makefile */
Packit ae235b
      while (g_hash_table_iter_next (&iter, &key, &data))
Packit ae235b
        {
Packit ae235b
          file_data = data;
Packit ae235b
          g_print ("%s\n", file_data->filename);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else if (generate_source || generate_header)
Packit ae235b
    {
Packit ae235b
      if (generate_source)
Packit ae235b
	{
Packit ae235b
	  int fd = g_file_open_tmp (NULL, &binary_target, NULL);
Packit ae235b
	  if (fd == -1)
Packit ae235b
	    {
Packit ae235b
	      g_printerr ("Can't open temp file\n");
Packit ae235b
	      g_free (c_name);
Packit ae235b
              g_hash_table_unref (files);
Packit ae235b
	      return 1;
Packit ae235b
	    }
Packit ae235b
	  close (fd);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (c_name == NULL)
Packit ae235b
	{
Packit ae235b
	  char *base = g_path_get_basename (srcfile);
Packit ae235b
	  GString *s;
Packit ae235b
	  char *dot;
Packit ae235b
	  int i;
Packit ae235b
Packit ae235b
	  /* Remove extensions */
Packit ae235b
	  dot = strchr (base, '.');
Packit ae235b
	  if (dot)
Packit ae235b
	    *dot = 0;
Packit ae235b
Packit ae235b
	  s = g_string_new ("");
Packit ae235b
Packit ae235b
	  for (i = 0; base[i] != 0; i++)
Packit ae235b
	    {
Packit ae235b
	      const char *first = G_CSET_A_2_Z G_CSET_a_2_z "_";
Packit ae235b
	      const char *rest = G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_";
Packit ae235b
	      if (strchr ((i == 0) ? first : rest, base[i]) != NULL)
Packit ae235b
		g_string_append_c (s, base[i]);
Packit ae235b
	      else if (base[i] == '-')
Packit ae235b
		g_string_append_c (s, '_');
Packit ae235b
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  c_name = g_string_free (s, FALSE);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    binary_target = g_strdup (target);
Packit ae235b
Packit ae235b
  c_name_no_underscores = c_name;
Packit ae235b
  while (c_name_no_underscores && *c_name_no_underscores == '_')
Packit ae235b
    c_name_no_underscores++;
Packit ae235b
Packit ae235b
  if (binary_target != NULL &&
Packit ae235b
      !write_to_file (table, binary_target, &error))
Packit ae235b
    {
Packit ae235b
      g_printerr ("%s\n", error->message);
Packit ae235b
      g_free (target);
Packit ae235b
      g_free (c_name);
Packit ae235b
      g_hash_table_unref (files);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (generate_header)
Packit ae235b
    {
Packit ae235b
      FILE *file;
Packit ae235b
Packit ae235b
      file = fopen (target, "w");
Packit ae235b
      if (file == NULL)
Packit ae235b
	{
Packit ae235b
	  g_printerr ("can't write to file %s", target);
Packit ae235b
	  g_free (c_name);
Packit ae235b
          g_hash_table_unref (files);
Packit ae235b
	  return 1;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      fprintf (file,
Packit ae235b
	       "#ifndef __RESOURCE_%s_H__\n"
Packit ae235b
	       "#define __RESOURCE_%s_H__\n"
Packit ae235b
	       "\n"
Packit ae235b
	       "#include <gio/gio.h>\n"
Packit ae235b
	       "\n"
Packit ae235b
	       "%s GResource *%s_get_resource (void);\n",
Packit ae235b
	       c_name, c_name, linkage, c_name);
Packit ae235b
Packit ae235b
      if (manual_register)
Packit ae235b
	fprintf (file,
Packit ae235b
		 "\n"
Packit ae235b
		 "%s void %s_register_resource (void);\n"
Packit ae235b
		 "%s void %s_unregister_resource (void);\n"
Packit ae235b
		 "\n",
Packit ae235b
		 linkage, c_name, linkage, c_name);
Packit ae235b
Packit ae235b
      fprintf (file,
Packit ae235b
	       "#endif\n");
Packit ae235b
Packit ae235b
      fclose (file);
Packit ae235b
    }
Packit ae235b
  else if (generate_source)
Packit ae235b
    {
Packit ae235b
      FILE *file;
Packit ae235b
      guint8 *data;
Packit ae235b
      gsize data_size;
Packit ae235b
      gsize i;
Packit ae235b
Packit ae235b
      if (!g_file_get_contents (binary_target, (char **)&data,
Packit ae235b
				&data_size, NULL))
Packit ae235b
	{
Packit ae235b
	  g_printerr ("can't read back temporary file");
Packit ae235b
	  g_free (c_name);
Packit ae235b
          g_hash_table_unref (files);
Packit ae235b
	  return 1;
Packit ae235b
	}
Packit ae235b
      g_unlink (binary_target);
Packit ae235b
Packit ae235b
      file = fopen (target, "w");
Packit ae235b
      if (file == NULL)
Packit ae235b
	{
Packit ae235b
	  g_printerr ("can't write to file %s", target);
Packit ae235b
	  g_free (c_name);
Packit ae235b
          g_hash_table_unref (files);
Packit ae235b
	  return 1;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      fprintf (file,
Packit ae235b
	       "#include <gio/gio.h>\n"
Packit ae235b
	       "\n"
Packit ae235b
	       "#if defined (__ELF__) && ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6))\n"
Packit ae235b
	       "# define SECTION __attribute__ ((section (\".gresource.%s\"), aligned (8)))\n"
Packit ae235b
	       "#else\n"
Packit ae235b
	       "# define SECTION\n"
Packit ae235b
	       "#endif\n"
Packit ae235b
	       "\n"
Packit ae235b
	       "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double alignment; void * const ptr;}  %s_resource_data = { {\n",
Packit ae235b
	       c_name_no_underscores, data_size, c_name);
Packit ae235b
Packit ae235b
      for (i = 0; i < data_size; i++) {
Packit ae235b
	if (i % 8 == 0)
Packit ae235b
	  fprintf (file, "  ");
Packit ae235b
	fprintf (file, "0x%2.2x", (int)data[i]);
Packit ae235b
	if (i != data_size - 1)
Packit ae235b
	  fprintf (file, ", ");
Packit ae235b
	if ((i % 8 == 7) || (i == data_size - 1))
Packit ae235b
	  fprintf (file, "\n");
Packit ae235b
      }
Packit ae235b
Packit ae235b
      fprintf (file, "} };\n");
Packit ae235b
Packit ae235b
      fprintf (file,
Packit ae235b
	       "\n"
Packit ae235b
	       "static GStaticResource static_resource = { %s_resource_data.data, sizeof (%s_resource_data.data), NULL, NULL, NULL };\n"
Packit ae235b
	       "%s GResource *%s_get_resource (void);\n"
Packit ae235b
	       "GResource *%s_get_resource (void)\n"
Packit ae235b
	       "{\n"
Packit ae235b
	       "  return g_static_resource_get_resource (&static_resource);\n"
Packit ae235b
	       "}\n",
Packit ae235b
	       c_name, c_name, linkage, c_name, c_name);
Packit ae235b
Packit ae235b
Packit ae235b
      if (manual_register)
Packit ae235b
	{
Packit ae235b
	  fprintf (file,
Packit ae235b
		   "\n"
Packit ae235b
		   "%s void %s_unregister_resource (void);\n"
Packit ae235b
		   "void %s_unregister_resource (void)\n"
Packit ae235b
		   "{\n"
Packit ae235b
		   "  g_static_resource_fini (&static_resource);\n"
Packit ae235b
		   "}\n"
Packit ae235b
		   "\n"
Packit ae235b
		   "%s void %s_register_resource (void);\n"
Packit ae235b
		   "void %s_register_resource (void)\n"
Packit ae235b
		   "{\n"
Packit ae235b
		   "  g_static_resource_init (&static_resource);\n"
Packit ae235b
		   "}\n",
Packit ae235b
		   linkage, c_name, c_name, linkage, c_name, c_name);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  fprintf (file, "%s", gconstructor_code);
Packit ae235b
	  fprintf (file,
Packit ae235b
		   "\n"
Packit ae235b
		   "#ifdef G_HAS_CONSTRUCTORS\n"
Packit ae235b
		   "\n"
Packit ae235b
		   "#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA\n"
Packit ae235b
		   "#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(resource_constructor)\n"
Packit ae235b
		   "#endif\n"
Packit ae235b
		   "G_DEFINE_CONSTRUCTOR(resource_constructor)\n"
Packit ae235b
		   "#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA\n"
Packit ae235b
		   "#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(resource_destructor)\n"
Packit ae235b
		   "#endif\n"
Packit ae235b
		   "G_DEFINE_DESTRUCTOR(resource_destructor)\n"
Packit ae235b
		   "\n"
Packit ae235b
		   "#else\n"
Packit ae235b
		   "#warning \"Constructor not supported on this compiler, linking in resources will not work\"\n"
Packit ae235b
		   "#endif\n"
Packit ae235b
		   "\n"
Packit ae235b
		   "static void resource_constructor (void)\n"
Packit ae235b
		   "{\n"
Packit ae235b
		   "  g_static_resource_init (&static_resource);\n"
Packit ae235b
		   "}\n"
Packit ae235b
		   "\n"
Packit ae235b
		   "static void resource_destructor (void)\n"
Packit ae235b
		   "{\n"
Packit ae235b
		   "  g_static_resource_fini (&static_resource);\n"
Packit ae235b
		   "}\n");
Packit ae235b
	}
Packit ae235b
Packit ae235b
      fclose (file);
Packit ae235b
Packit ae235b
      g_free (data);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (binary_target);
Packit ae235b
  g_free (target);
Packit ae235b
  g_hash_table_destroy (table);
Packit ae235b
  g_free (xmllint);
Packit ae235b
  g_free (c_name);
Packit ae235b
  g_hash_table_unref (files);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}