]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/queryloaders.c
Updated Czech translation.
[~andy/gtk] / gdk-pixbuf / queryloaders.c
1 /* -*- mode: C; c-file-style: "linux" -*- */
2 /* GdkPixbuf library
3  * queryloaders.c:
4  *
5  * Copyright (C) 2002 The Free Software Foundation
6  * 
7  * Author: Matthias Clasen <maclas@gmx.de>
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include "config.h"
26
27 #include <glib.h>
28 #include <glib/gprintf.h>
29 #include <gmodule.h>
30
31 #include <errno.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include "gdk-pixbuf/gdk-pixbuf.h"
38 #include "gdk-pixbuf/gdk-pixbuf-private.h"
39 #include "gdk-pixbuf/gdk-pixbuf-io.h"
40
41 #ifdef USE_LA_MODULES
42 #define SOEXT ".la"
43 #else
44 #define SOEXT ("." G_MODULE_SUFFIX)
45 #endif
46 #define SOEXT_LEN (strlen (SOEXT))
47
48 #ifdef G_OS_WIN32
49 #include <windows.h>
50 #endif
51
52 static void
53 print_escaped (const char *str)
54 {
55         gchar *tmp = g_strescape (str, "");
56         g_printf ("\"%s\" ", tmp);
57         g_free (tmp);
58 }
59
60 static int
61 loader_sanity_check (const char *path, GdkPixbufFormat *info, GdkPixbufModule *vtable)
62 {
63         const GdkPixbufModulePattern *pattern;
64         const char *error = "";
65
66         for (pattern = info->signature; pattern->prefix; pattern++) 
67         {
68                 int prefix_len = strlen (pattern->prefix);
69                 if (prefix_len == 0) 
70                 {
71                         error = "empty pattern";
72
73                         goto error;
74                 }
75                 if (pattern->mask)
76                 {
77                         int mask_len = strlen (pattern->mask);
78                         if (mask_len != prefix_len)
79                         {
80                                 error = "mask length mismatch";
81                                 
82                                 goto error;
83                         }
84                         if (strspn (pattern->mask, " !xzn*") < mask_len) 
85                         {
86                                 error = "bad char in mask";
87                                 
88                                 goto error;
89                         }
90                 }
91         }
92
93         if (!vtable->load && !vtable->begin_load && !vtable->load_animation)
94         {
95                 error = "no load method implemented";
96
97                 goto error;
98         }
99
100         if (vtable->begin_load && (!vtable->stop_load || !vtable->load_increment))
101         {
102                 error = "incremental loading support incomplete";
103
104                 goto error;
105         }
106
107         if ((info->flags & GDK_PIXBUF_FORMAT_WRITABLE) && !(vtable->save || vtable->save_to_callback)) 
108         {
109                 error = "loader claims to support saving but doesn't implement save";
110                 goto error;
111         }
112             
113         return 1;
114
115  error:
116         g_fprintf (stderr, "Loader sanity check failed for %s: %s\n", 
117                    path, error);
118         
119         return 0;
120 }
121
122 static void 
123 write_loader_info (const char *path, GdkPixbufFormat *info)
124 {
125         const GdkPixbufModulePattern *pattern;
126         char **mime; 
127         char **ext; 
128
129         g_printf("\"%s\"\n", path);
130         g_printf ("\"%s\" %u \"%s\" \"%s\" \"%s\"\n", 
131                   info->name, info->flags, 
132                   info->domain ? info->domain : GETTEXT_PACKAGE, info->description, info->license);
133         for (mime = info->mime_types; *mime; mime++) {
134                 g_printf ("\"%s\" ", *mime);
135         }
136         g_printf ("\"\"\n");
137         for (ext = info->extensions; *ext; ext++) {
138                 g_printf ("\"%s\" ", *ext);
139         }
140         g_printf ("\"\"\n");
141         for (pattern = info->signature; pattern->prefix; pattern++) {
142                 print_escaped (pattern->prefix);
143                 print_escaped (pattern->mask ? (const char *)pattern->mask : "");
144                 g_printf ("%d\n", pattern->relevance);
145         }
146         g_printf ("\n");
147 }
148
149 static void
150 query_module (const char *dir, const char *file)
151 {
152         char *path;
153         GModule *module;
154         void                    (*fill_info)     (GdkPixbufFormat *info);
155         void                    (*fill_vtable)   (GdkPixbufModule *module);
156         gpointer fill_info_ptr;
157         gpointer fill_vtable_ptr;
158
159         if (g_path_is_absolute (file)) 
160                 path = g_strdup (file);
161         else
162                 path = g_build_filename (dir, file, NULL);
163
164         module = g_module_open (path, 0);
165         if (module &&
166             g_module_symbol (module, "fill_info", &fill_info_ptr) &&
167             g_module_symbol (module, "fill_vtable", &fill_vtable_ptr)) {
168                 GdkPixbufFormat *info;
169                 GdkPixbufModule *vtable;
170                 
171 #ifdef G_OS_WIN32
172                 /* Replace backslashes in path with forward slashes, so that
173                  * it reads in without problems.
174                  */
175                 {
176                         char *p = path;
177                         while (*p) {
178                                 if (*p == '\\')
179                                         *p = '/';
180                                 p++;
181                         }
182                 }
183 #endif  
184                 info = g_new0 (GdkPixbufFormat, 1);
185                 vtable = g_new0 (GdkPixbufModule, 1);
186
187                 vtable->module = module;
188
189                 fill_info = fill_info_ptr;
190                 fill_vtable = fill_vtable_ptr;
191
192                 (*fill_info) (info);
193                 (*fill_vtable) (vtable);
194                 
195                 if (loader_sanity_check (path, info, vtable)) 
196                         write_loader_info (path, info);
197                 
198                 g_free (info);
199                 g_free (vtable);
200         }
201         else {
202                 if (module == NULL)
203                         g_fprintf (stderr, "g_module_open() failed for %s: %s\n", path,
204                                    g_module_error());
205                 else
206                         g_fprintf (stderr, "Cannot load loader %s\n", path);
207         }
208         if (module)
209                 g_module_close (module);
210         g_free (path);
211 }
212
213 int main (int argc, char **argv)
214 {
215         gint i;
216         gchar *prgname;
217
218 #ifdef G_OS_WIN32
219         gchar *libdir;
220         gchar *runtime_prefix;
221         gchar *slash;
222
223         if (g_ascii_strncasecmp (PIXBUF_LIBDIR, GTK_PREFIX, strlen (GTK_PREFIX)) == 0 &&
224             G_IS_DIR_SEPARATOR (PIXBUF_LIBDIR[strlen (GTK_PREFIX)])) {
225                 /* GTK_PREFIX is a prefix of PIXBUF_LIBDIR, as it
226                  * normally is. Replace that prefix in PIXBUF_LIBDIR
227                  * with the installation directory on this machine.
228                  * We assume this invokation of
229                  * gdk-pixbuf-query-loaders is run from either a "bin"
230                  * subdirectory of the installation directory, or in
231                  * the installation directory itself.
232                  */
233                 if (G_WIN32_HAVE_WIDECHAR_API ()) {
234                         wchar_t fn[1000];
235                         GetModuleFileNameW (NULL, fn, G_N_ELEMENTS (fn));
236                         runtime_prefix = g_utf16_to_utf8 (fn, -1, NULL, NULL, NULL);
237                 }
238                 else {
239                         char fn[1000];
240                         GetModuleFileNameA (NULL, fn, G_N_ELEMENTS (fn));
241                         runtime_prefix = g_locale_to_utf8 (fn, -1, NULL, NULL, NULL);
242                 }
243                 slash = strrchr (runtime_prefix, '\\');
244                 *slash = '\0';
245                 slash = strrchr (runtime_prefix, '\\');
246                 if (slash != NULL && g_ascii_strcasecmp (slash + 1, "bin") == 0) {
247                         *slash = '\0';
248                 }
249                 
250                 libdir = g_strconcat (runtime_prefix,
251                                       "/",
252                                       PIXBUF_LIBDIR + strlen (GTK_PREFIX) + 1,
253                                       NULL);
254         }
255         else {
256                 libdir = PIXBUF_LIBDIR;
257         }
258
259 #undef PIXBUF_LIBDIR
260 #define PIXBUF_LIBDIR libdir
261
262 #endif
263         prgname = g_get_prgname ();
264         g_printf ("# GdkPixbuf Image Loader Modules file\n"
265                   "# Automatically generated file, do not edit\n"
266                   "# Created by %s from gtk+-%s\n"
267                   "#\n",
268                   (prgname ? prgname : "gdk-pixbuf-query-loaders"),
269                   GDK_PIXBUF_VERSION);
270   
271         if (argc == 1) {
272 #ifdef USE_GMODULE
273                 const char *path;
274                 GDir *dir;
275     
276                 path = g_getenv ("GDK_PIXBUF_MODULEDIR");
277 #ifdef G_OS_WIN32
278                 if (path != NULL && *path != '\0')
279                         path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
280 #endif
281                 if (path == NULL || *path == '\0')
282                         path = PIXBUF_LIBDIR;
283
284                 g_printf ("# LoaderDir = %s\n#\n", path);
285
286                 dir = g_dir_open (path, 0, NULL);
287                 if (dir) {
288                         const char *dent;
289
290                         while ((dent = g_dir_read_name (dir))) {
291                                 gint len = strlen (dent);
292                                 if (len > SOEXT_LEN && 
293                                     strcmp (dent + len - SOEXT_LEN, SOEXT) == 0) {
294                                         query_module (path, dent);
295                                 }
296                         }
297                         g_dir_close (dir);
298                 }
299 #else
300                 g_printf ("# dynamic loading of modules not supported\n");
301 #endif
302         }
303         else {
304                 char *cwd = g_get_current_dir ();
305
306                 for (i = 1; i < argc; i++) {
307                         char *infilename = argv[i];
308 #ifdef G_OS_WIN32
309                         infilename = g_locale_to_utf8 (infilename,
310                                                        -1, NULL, NULL, NULL);
311 #endif
312                         query_module (cwd, infilename);
313                 }
314                 g_free (cwd);
315         }
316
317         return 0;
318 }