1 /* -*- mode: C; c-file-style: "linux" -*- */
5 * Copyright (C) 2002 The Free Software Foundation
7 * Author: Matthias Clasen <maclas@gmx.de>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
28 #include <glib/gprintf.h>
37 #include "gdk-pixbuf/gdk-pixbuf.h"
38 #include "gdk-pixbuf/gdk-pixbuf-private.h"
39 #include "gdk-pixbuf/gdk-pixbuf-io.h"
44 #define SOEXT ("." G_MODULE_SUFFIX)
46 #define SOEXT_LEN (strlen (SOEXT))
53 print_escaped (const char *str)
55 gchar *tmp = g_strescape (str, "");
56 g_printf ("\"%s\" ", tmp);
61 loader_sanity_check (const char *path, GdkPixbufFormat *info, GdkPixbufModule *vtable)
63 const GdkPixbufModulePattern *pattern;
64 const char *error = "";
66 for (pattern = info->signature; pattern->prefix; pattern++)
68 int prefix_len = strlen (pattern->prefix);
71 error = "empty pattern";
77 int mask_len = strlen (pattern->mask);
78 if (mask_len != prefix_len)
80 error = "mask length mismatch";
84 if (strspn (pattern->mask, " !xzn*") < mask_len)
86 error = "bad char in mask";
93 if (!vtable->load && !vtable->begin_load && !vtable->load_animation)
95 error = "no load method implemented";
100 if (vtable->begin_load && (!vtable->stop_load || !vtable->load_increment))
102 error = "incremental loading support incomplete";
107 if ((info->flags & GDK_PIXBUF_FORMAT_WRITABLE) && !(vtable->save || vtable->save_to_callback))
109 error = "loader claims to support saving but doesn't implement save";
116 g_fprintf (stderr, "Loader sanity check failed for %s: %s\n",
123 write_loader_info (const char *path, GdkPixbufFormat *info)
125 const GdkPixbufModulePattern *pattern;
129 g_printf("\"%s\"\n", path);
130 g_printf ("\"%s\" %u \"%s\" \"%s\" \"%s\"\n",
133 info->domain ? info->domain : GETTEXT_PACKAGE,
135 info->license ? info->license : "");
136 for (mime = info->mime_types; *mime; mime++) {
137 g_printf ("\"%s\" ", *mime);
140 for (ext = info->extensions; *ext; ext++) {
141 g_printf ("\"%s\" ", *ext);
144 for (pattern = info->signature; pattern->prefix; pattern++) {
145 print_escaped (pattern->prefix);
146 print_escaped (pattern->mask ? (const char *)pattern->mask : "");
147 g_printf ("%d\n", pattern->relevance);
153 query_module (const char *dir, const char *file)
157 void (*fill_info) (GdkPixbufFormat *info);
158 void (*fill_vtable) (GdkPixbufModule *module);
159 gpointer fill_info_ptr;
160 gpointer fill_vtable_ptr;
162 if (g_path_is_absolute (file))
163 path = g_strdup (file);
165 path = g_build_filename (dir, file, NULL);
167 module = g_module_open (path, 0);
169 g_module_symbol (module, "fill_info", &fill_info_ptr) &&
170 g_module_symbol (module, "fill_vtable", &fill_vtable_ptr)) {
171 GdkPixbufFormat *info;
172 GdkPixbufModule *vtable;
175 /* Replace backslashes in path with forward slashes, so that
176 * it reads in without problems.
187 info = g_new0 (GdkPixbufFormat, 1);
188 vtable = g_new0 (GdkPixbufModule, 1);
190 vtable->module = module;
192 fill_info = fill_info_ptr;
193 fill_vtable = fill_vtable_ptr;
196 (*fill_vtable) (vtable);
198 if (loader_sanity_check (path, info, vtable))
199 write_loader_info (path, info);
206 g_fprintf (stderr, "g_module_open() failed for %s: %s\n", path,
209 g_fprintf (stderr, "Cannot load loader %s\n", path);
212 g_module_close (module);
216 int main (int argc, char **argv)
223 gchar *runtime_prefix;
226 if (g_ascii_strncasecmp (PIXBUF_LIBDIR, GTK_PREFIX, strlen (GTK_PREFIX)) == 0 &&
227 G_IS_DIR_SEPARATOR (PIXBUF_LIBDIR[strlen (GTK_PREFIX)])) {
228 /* GTK_PREFIX is a prefix of PIXBUF_LIBDIR, as it
229 * normally is. Replace that prefix in PIXBUF_LIBDIR
230 * with the installation directory on this machine.
231 * We assume this invokation of
232 * gdk-pixbuf-query-loaders is run from either a "bin"
233 * subdirectory of the installation directory, or in
234 * the installation directory itself.
237 GetModuleFileNameW (NULL, fn, G_N_ELEMENTS (fn));
238 runtime_prefix = g_utf16_to_utf8 (fn, -1, NULL, NULL, NULL);
239 slash = strrchr (runtime_prefix, '\\');
241 slash = strrchr (runtime_prefix, '\\');
242 /* If running from some weird location, or from the
243 * build directory (either in the .libs folder where
244 * libtool places the real executable when using a
245 * wrapper, or directly from the gdk-pixbuf folder),
246 * use the compile-time libdir.
249 g_ascii_strcasecmp (slash + 1, ".libs") == 0 ||
250 g_ascii_strcasecmp (slash + 1, "gdk-pixbuf") == 0) {
251 libdir = PIXBUF_LIBDIR;
254 if (slash != NULL && g_ascii_strcasecmp (slash + 1, "bin") == 0) {
258 libdir = g_strconcat (runtime_prefix,
260 PIXBUF_LIBDIR + strlen (GTK_PREFIX) + 1,
265 libdir = PIXBUF_LIBDIR;
269 #define PIXBUF_LIBDIR libdir
272 prgname = g_get_prgname ();
273 g_printf ("# GdkPixbuf Image Loader Modules file\n"
274 "# Automatically generated file, do not edit\n"
275 "# Created by %s from gtk+-%s\n"
277 (prgname ? prgname : "gdk-pixbuf-query-loaders-3.0"),
285 path = g_getenv ("GDK_PIXBUF_MODULEDIR");
287 if (path != NULL && *path != '\0')
288 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
290 if (path == NULL || *path == '\0')
291 path = PIXBUF_LIBDIR;
293 g_printf ("# LoaderDir = %s\n#\n", path);
295 dir = g_dir_open (path, 0, NULL);
299 while ((dent = g_dir_read_name (dir))) {
300 gint len = strlen (dent);
301 if (len > SOEXT_LEN &&
302 strcmp (dent + len - SOEXT_LEN, SOEXT) == 0) {
303 query_module (path, dent);
309 g_printf ("# dynamic loading of modules not supported\n");
313 char *cwd = g_get_current_dir ();
315 for (i = 1; i < argc; i++) {
316 char *infilename = argv[i];
318 infilename = g_locale_to_utf8 (infilename,
319 -1, NULL, NULL, NULL);
321 query_module (cwd, infilename);