1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "giomodule.h"
28 GTypeModule parent_instance;
33 void (* load) (GIOModule *module);
34 void (* unload) (GIOModule *module);
37 struct _GIOModuleClass
39 GTypeModuleClass parent_class;
43 static void g_io_module_finalize (GObject *object);
44 static gboolean g_io_module_load_module (GTypeModule *gmodule);
45 static void g_io_module_unload_module (GTypeModule *gmodule);
47 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
50 g_io_module_class_init (GIOModuleClass *class)
52 GObjectClass *object_class = G_OBJECT_CLASS (class);
53 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
55 object_class->finalize = g_io_module_finalize;
57 type_module_class->load = g_io_module_load_module;
58 type_module_class->unload = g_io_module_unload_module;
62 g_io_module_init (GIOModule *module)
67 g_io_module_finalize (GObject *object)
69 GIOModule *module = G_IO_MODULE (object);
71 g_free (module->filename);
73 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
77 g_io_module_load_module (GTypeModule *gmodule)
79 GIOModule *module = G_IO_MODULE (gmodule);
81 if (!module->filename)
83 g_warning ("GIOModule path not set");
87 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
91 g_printerr ("%s\n", g_module_error ());
95 /* Make sure that the loaded library contains the required methods */
96 if (! g_module_symbol (module->library,
98 (gpointer *) &module->load) ||
99 ! g_module_symbol (module->library,
100 "g_io_module_unload",
101 (gpointer *) &module->unload))
103 g_printerr ("%s\n", g_module_error ());
104 g_module_close (module->library);
109 /* Initialize the loaded module */
110 module->load (module);
116 g_io_module_unload_module (GTypeModule *gmodule)
118 GIOModule *module = G_IO_MODULE (gmodule);
120 module->unload (module);
122 g_module_close (module->library);
123 module->library = NULL;
126 module->unload = NULL;
131 * @filename: filename of the module to load.
133 * Returns: a new #GIOModule from given @filename,
137 g_io_module_new (const gchar *filename)
141 g_return_val_if_fail (filename != NULL, NULL);
143 module = g_object_new (G_IO_TYPE_MODULE, NULL);
144 module->filename = g_strdup (filename);
150 is_valid_module_name (const gchar *basename)
152 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
154 g_str_has_prefix (basename, "lib") &&
155 g_str_has_suffix (basename, ".so");
157 return g_str_has_suffix (basename, ".dll");
162 load_modules (const char *dirname)
168 if (!g_module_supported ())
171 dir = g_dir_open (dirname, 0, NULL);
176 while ((name = g_dir_read_name (dir)))
178 if (is_valid_module_name (name))
183 path = g_build_filename (dirname, name, NULL);
184 module = g_io_module_new (path);
186 if (!g_type_module_use (G_TYPE_MODULE (module)))
188 g_printerr ("Failed to load module: %s\n", path);
189 g_object_unref (module);
196 g_type_module_unuse (G_TYPE_MODULE (module));
198 modules = g_list_prepend (modules, module);
207 G_LOCK_DEFINE_STATIC (loaded_dirs);
208 static GHashTable *loaded_dirs = NULL;
211 * g_io_module_ensure_loaded:
212 * @directory: directory to ensure is loaded.
216 g_io_modules_ensure_loaded (const char *directory)
220 g_return_if_fail (directory != NULL);
222 G_LOCK (loaded_dirs);
224 if (loaded_dirs == NULL)
225 loaded_dirs = g_hash_table_new (g_str_hash, g_str_equal);
227 if (!g_hash_table_lookup_extended (loaded_dirs, directory,
230 modules = load_modules (directory);
231 g_hash_table_insert (loaded_dirs,
232 g_strdup (directory),
236 G_UNLOCK (loaded_dirs);