gi-decompile-repository: Port to girepository-2.0

Update the code to the latest standards and to use girepository-2.0
after it’s been imported to this repository.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3155
This commit is contained in:
Philip Withnall 2024-02-08 14:35:36 +00:00
parent d6d845aee3
commit aaed30f3dc

View File

@ -27,14 +27,15 @@
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <gmodule.h> #include <gmodule.h>
#include <girepository.h>
#include "girwriter.h" #include "girwriter-private.h"
#include "girepository.h"
#include "gitypelib-internal.h" #include "gitypelib-internal.h"
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
GIRepository *repository = NULL;
gboolean shlib = FALSE; gboolean shlib = FALSE;
gchar *output = NULL; gchar *output = NULL;
gchar **includedirs = NULL; gchar **includedirs = NULL;
@ -53,13 +54,11 @@ main (int argc, char *argv[])
{ "all", 0, 0, G_OPTION_ARG_NONE, &show_all, "show all available information", NULL, }, { "all", 0, 0, G_OPTION_ARG_NONE, &show_all, "show all available information", NULL, },
{ "version", 0, 0, G_OPTION_ARG_NONE, &show_version, "show program's version number and exit", NULL }, { "version", 0, 0, G_OPTION_ARG_NONE, &show_version, "show program's version number and exit", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL }, { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
{ NULL, } G_OPTION_ENTRY_NULL
}; };
g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL); g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
g_typelib_check_sanity ();
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
context = g_option_context_new (""); context = g_option_context_new ("");
@ -73,8 +72,8 @@ main (int argc, char *argv[])
if (show_version) if (show_version)
{ {
g_printf ("g-ir-generate %u.%u.%u\n", g_printf ("gi-decompile-typelib %u.%u.%u\n",
GI_MAJOR_VERSION, GI_MINOR_VERSION, GI_MICRO_VERSION); GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
return 0; return 0;
} }
@ -85,14 +84,17 @@ main (int argc, char *argv[])
return 1; return 1;
} }
repository = gi_repository_new ();
if (includedirs != NULL) if (includedirs != NULL)
for (i = 0; includedirs[i]; i++) for (i = 0; includedirs[i]; i++)
g_irepository_prepend_search_path (includedirs[i]); gi_repository_prepend_search_path (repository, includedirs[i]);
for (i = 0; input[i]; i++) for (i = 0; input[i]; i++)
{ {
const char *namespace; const char *namespace;
GMappedFile *mfile; GMappedFile *mfile = NULL;
GBytes *bytes = NULL;
GITypelib *typelib; GITypelib *typelib;
error = NULL; error = NULL;
@ -100,21 +102,23 @@ main (int argc, char *argv[])
if (!mfile) if (!mfile)
g_error ("failed to read '%s': %s", input[i], error->message); g_error ("failed to read '%s': %s", input[i], error->message);
bytes = g_mapped_file_get_bytes (mfile);
g_clear_pointer (&mfile, g_mapped_file_unref);
if (input[i + 1] && output) if (input[i + 1] && output)
needs_prefix = TRUE; needs_prefix = TRUE;
else else
needs_prefix = FALSE; needs_prefix = FALSE;
typelib = g_typelib_new_from_mapped_file (mfile, &error); typelib = gi_typelib_new_from_bytes (bytes, &error);
if (!typelib) if (!typelib)
g_error ("failed to create typelib '%s': %s", input[i], error->message); g_error ("failed to create typelib '%s': %s", input[i], error->message);
namespace = g_irepository_load_typelib (g_irepository_get_default (), typelib, 0, namespace = gi_repository_load_typelib (repository, typelib, 0, &error);
&error);
if (namespace == NULL) if (namespace == NULL)
g_error ("failed to load typelib: %s", error->message); g_error ("failed to load typelib: %s", error->message);
gir_writer_write (output, namespace, needs_prefix, show_all); gi_ir_writer_write (output, namespace, needs_prefix, show_all);
/* when writing to stdout, stop after the first module */ /* when writing to stdout, stop after the first module */
if (input[i + 1] && !output) if (input[i + 1] && !output)
@ -126,5 +130,7 @@ main (int argc, char *argv[])
} }
} }
g_clear_object (&repository);
return 0; return 0;
} }