Kouhei Sutou
kous****@users*****
2006年 11月 29日 (水) 11:53:11 JST
Index: tomoe/lib/Makefile.am diff -u tomoe/lib/Makefile.am:1.43 tomoe/lib/Makefile.am:1.44 --- tomoe/lib/Makefile.am:1.43 Wed Nov 29 10:38:40 2006 +++ tomoe/lib/Makefile.am Wed Nov 29 11:53:11 2006 @@ -38,14 +38,11 @@ tomoe-context.h \ tomoe-config.h \ tomoe-dict.h \ - tomoe-dict-impl.h \ - tomoe-dict-loader.h \ tomoe-module.h \ + tomoe-module-impl.h \ tomoe-query.h \ tomoe-reading.h \ tomoe-recognizer.h \ - tomoe-recognizer-impl.h \ - tomoe-recognizer-loader.h \ tomoe-shelf.h \ tomoe-writing.h @@ -77,12 +74,10 @@ tomoe-context.c \ tomoe-config.c \ tomoe-dict.c \ - tomoe-dict-loader.c \ tomoe-module.c \ tomoe-query.c \ tomoe-reading.c \ tomoe-recognizer.c \ - tomoe-recognizer-loader.c \ tomoe-shelf.c \ tomoe-writing.c Index: tomoe/lib/tomoe-config.c diff -u tomoe/lib/tomoe-config.c:1.43 tomoe/lib/tomoe-config.c:1.44 --- tomoe/lib/tomoe-config.c:1.43 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-config.c Wed Nov 29 11:53:11 2006 @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-config.c,v 1.43 2006/11/28 15:07:02 kous Exp $ + * $Id: tomoe-config.c,v 1.44 2006/11/29 02:53:11 kous Exp $ */ #ifdef HAVE_CONFIG_H @@ -31,7 +31,7 @@ #include <glib/gi18n.h> #include "tomoe-config.h" -#include "tomoe-dict-loader.h" +#include "tomoe-dict.h" #include "tomoe-config.h" #include "glib-utils.h" @@ -276,11 +276,13 @@ if (_tomoe_dict_key_file_get_boolean_value (key_file, dict_name, "user", TRUE)) { - dict = tomoe_dict_loader_instantiate ("xml", filename, TRUE); + dict = tomoe_dict_new ("xml", "filename", filename, + "editable", TRUE, NULL); } else { gchar *dict_filename; dict_filename = g_build_filename (TOMOEDATADIR, filename, NULL); - dict = tomoe_dict_loader_instantiate ("xml", filename, TRUE); + dict = tomoe_dict_new ("xml", "filename", filename, + "editable", TRUE, NULL); g_free (dict_filename); } @@ -347,7 +349,8 @@ continue; } - dict = tomoe_dict_loader_instantiate ("xml", path, FALSE); + dict = tomoe_dict_new ("xml", "filename", path, "editable", FALSE, + NULL); if (dict) { tomoe_shelf_add_dict (shelf, dict); g_object_unref (dict); Index: tomoe/lib/tomoe-context.c diff -u tomoe/lib/tomoe-context.c:1.40 tomoe/lib/tomoe-context.c:1.41 --- tomoe/lib/tomoe-context.c:1.40 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-context.c Wed Nov 29 11:53:11 2006 @@ -17,11 +17,11 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-context.c,v 1.40 2006/11/28 15:07:02 kous Exp $ + * $Id: tomoe-context.c,v 1.41 2006/11/29 02:53:11 kous Exp $ */ #include "tomoe-dict.h" -#include "tomoe-recognizer-loader.h" +#include "tomoe-recognizer.h" #include "tomoe-context.h" #include "tomoe-config.h" #include "tomoe-shelf.h" @@ -142,7 +142,7 @@ if (!names) return matched; if (!priv->recognizer) - priv->recognizer = tomoe_recognizer_loader_instantiate ("simple"); + priv->recognizer = tomoe_recognizer_new ("simple", NULL); for (name = names; name; name = name->next) { TomoeDict *dict; Index: tomoe/lib/tomoe-dict-impl.h diff -u tomoe/lib/tomoe-dict-impl.h:1.4 tomoe/lib/tomoe-dict-impl.h:removed --- tomoe/lib/tomoe-dict-impl.h:1.4 Wed Nov 29 11:28:09 2006 +++ tomoe/lib/tomoe-dict-impl.h Wed Nov 29 11:53:11 2006 @@ -1,57 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * Copyright (C) 2006 Kouhei Sutou <kou****@cozmi*****> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA - * - * $Id: tomoe-dict-impl.h,v 1.4 2006/11/29 02:28:09 ikezoe Exp $ - */ - -/** - * @file tomoe-dict-impl.h - * @brief Provide a set of API to access to a tomoe dictionary. - */ - -#ifndef __TOMOE_DICT_IMPL_H__ -#define __TOMOE_DICT_IMPL_H__ - -#include <glib-object.h> - -G_BEGIN_DECLS - -#include "tomoe-dict.h" - -typedef void (*TomoeDictInitFunc) (GTypeModule *module); -typedef void (*TomoeDictExitFunc) (void); -typedef TomoeDict *(*TomoeDictInstantiateFunc) (const gchar *filename, - gboolean editable); - -#define TOMOE_DICT_IMPL_INIT tomoe_dict_impl_init -#define TOMOE_DICT_IMPL_EXIT tomoe_dict_impl_exit -#define TOMOE_DICT_IMPL_INSTANTIATE tomoe_dict_impl_instantiate - - -void TOMOE_DICT_IMPL_INIT (GTypeModule *module); -void TOMOE_DICT_IMPL_EXIT (void); -GObject *TOMOE_DICT_IMPL_INSTANTIATE (const gchar *first_property, va_list args); - -G_END_DECLS - -#endif /* __TOMOE_DICT_IMPL_H__ */ - -/* -vi:ts=4:nowrap:ai:expandtab -*/ Index: tomoe/lib/tomoe-dict-loader.c diff -u tomoe/lib/tomoe-dict-loader.c:1.4 tomoe/lib/tomoe-dict-loader.c:removed --- tomoe/lib/tomoe-dict-loader.c:1.4 Wed Nov 29 10:02:56 2006 +++ tomoe/lib/tomoe-dict-loader.c Wed Nov 29 11:53:11 2006 @@ -1,189 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * Copyright (C) 2006 Kouhei Sutou <kou****@cozmi*****> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA - * - * $Id: tomoe-dict-loader.c,v 1.4 2006/11/29 01:02:56 kous Exp $ - */ - -#include <stdlib.h> - -#include "tomoe-dict-loader.h" -#include "tomoe-dict-impl.h" - -typedef struct _TomoeDictLoader TomoeDictLoader; -typedef struct _TomoeDictLoaderClass TomoeDictLoaderClass; - -static GType tomoe_dict_loader_get_type (void); - -#define TOMOE_TYPE_DICT_LOADER (tomoe_dict_loader_get_type ()) -#define TOMOE_DICT_LOADER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TOMOE_TYPE_DICT_LOADER, TomoeDictLoader)) -#define TOMOE_IS_DICT_LOADER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TOMOE_TYPE_DICT_LOADER)) - -struct _TomoeDictLoader -{ - GTypeModule parent_instance; - - GModule *module; - - TomoeDictInitFunc init; - TomoeDictExitFunc exit; - TomoeDictInstantiateFunc instantiate; - - gchar *mod_path; -}; - -struct _TomoeDictLoaderClass -{ - GTypeModuleClass parent_class; -}; - -static GList *dicts = NULL; -static GObjectClass *parent_class = NULL; - -static gboolean -load (GTypeModule *module) -{ - TomoeDictLoader *loader = TOMOE_DICT_LOADER (module); - - loader->module = tomoe_module_open (loader->mod_path); - if (!loader->module) - return FALSE; - - if (!tomoe_module_load_func (loader->module, - G_STRINGIFY (TOMOE_DICT_IMPL_INIT), - (gpointer )&loader->init) || - !tomoe_module_load_func (loader->module, - G_STRINGIFY (TOMOE_DICT_IMPL_EXIT), - (gpointer )&loader->exit) || - !tomoe_module_load_func (loader->module, - G_STRINGIFY (TOMOE_DICT_IMPL_INSTANTIATE), - (gpointer )&loader->instantiate)) - { - tomoe_module_close (loader->module); - loader->module = NULL; - return FALSE; - } - - loader->init (module); - - return TRUE; -} - -static void -unload (GTypeModule *module) -{ - TomoeDictLoader *loader = TOMOE_DICT_LOADER (module); - - loader->exit (); - - tomoe_module_close (loader->module); - loader->module = NULL; - - loader->init = NULL; - loader->exit = NULL; - loader->instantiate = NULL; -} - -static void -finalize (GObject *object) -{ - TomoeDictLoader *loader = TOMOE_DICT_LOADER (object); - - g_free (loader->mod_path); - loader->mod_path = NULL; - - parent_class->finalize (object); -} - -G_DEFINE_TYPE (TomoeDictLoader, tomoe_dict_loader, G_TYPE_TYPE_MODULE) - -static void -tomoe_dict_loader_class_init (TomoeDictLoaderClass *class) -{ - GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class); - GObjectClass *gobject_class = G_OBJECT_CLASS (class); - - parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class)); - - module_class->load = load; - module_class->unload = unload; - - gobject_class->finalize = finalize; -} - -static void -tomoe_dict_loader_init (TomoeDictLoader *object) -{ -} - -static gpointer -instantiate (const gchar *mod_path) -{ - TomoeDictLoader *loader; - - loader = g_object_new (TOMOE_TYPE_DICT_LOADER, NULL); - loader->mod_path = g_strdup (mod_path); - g_type_module_set_name (G_TYPE_MODULE (loader), loader->mod_path); - - return loader; -} - -void -tomoe_dict_loader_load (const gchar *base_dir) -{ - if (!base_dir) - base_dir = DICTDIR; - - dicts = g_list_concat (tomoe_module_load_modules (base_dir, instantiate), - dicts); -} - -void -tomoe_dict_loader_unload (void) -{ - g_list_foreach (dicts, (GFunc) g_object_unref, NULL); - g_list_free (dicts); - dicts = NULL; -} - -TomoeDict * -tomoe_dict_loader_instantiate (const gchar *name, const gchar *filename, - gboolean editable) -{ - GList *node; - - for (node = dicts; node; node = g_list_next (node)) { - TomoeDictLoader *loader = node->data; - - if (g_type_module_use (G_TYPE_MODULE (loader))) { - TomoeDict *dict = NULL; - if (tomoe_module_match_name (loader->module, name)) { - dict = loader->instantiate (filename, editable); - } - g_type_module_unuse (G_TYPE_MODULE (loader)); - if (dict) - return dict; - } - } - - return NULL; -} - -/* -vi:ts=4:nowrap:ai:expandtab -*/ Index: tomoe/lib/tomoe-dict-loader.h diff -u tomoe/lib/tomoe-dict-loader.h:1.1 tomoe/lib/tomoe-dict-loader.h:removed --- tomoe/lib/tomoe-dict-loader.h:1.1 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-dict-loader.h Wed Nov 29 11:53:11 2006 @@ -1,50 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * Copyright (C) 2006 Kouhei Sutou <kou****@cozmi*****> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA - * - * $Id: tomoe-dict-loader.h,v 1.1 2006/11/28 15:07:02 kous Exp $ - */ - -/** - * @file tomoe-dict-loader.h - * @brief Provide a set of API to load dictionary. - */ - -#ifndef __TOMOE_DICT_LOADER_H__ -#define __TOMOE_DICT_LOADER_H__ - -#include <glib-object.h> - -G_BEGIN_DECLS - -#include "tomoe-module.h" -#include "tomoe-dict.h" - -void tomoe_dict_loader_load (const gchar *base_dir); -void tomoe_dict_loader_unload (void); -TomoeDict *tomoe_dict_loader_instantiate (const gchar *name, - const gchar *filename, - gboolean editable); - -G_END_DECLS - -#endif /* __TOMOE_DICT_LOADER_H__ */ - -/* -vi:ts=4:nowrap:ai:expandtab -*/ Index: tomoe/lib/tomoe-dict.c diff -u tomoe/lib/tomoe-dict.c:1.125 tomoe/lib/tomoe-dict.c:1.126 --- tomoe/lib/tomoe-dict.c:1.125 Wed Nov 29 09:32:35 2006 +++ tomoe/lib/tomoe-dict.c Wed Nov 29 11:53:11 2006 @@ -17,10 +17,30 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-dict.c,v 1.125 2006/11/29 00:32:35 ikezoe Exp $ + * $Id: tomoe-dict.c,v 1.126 2006/11/29 02:53:11 kous Exp $ */ #include "tomoe-dict.h" +#include "tomoe-module.h" + +static GList *dicts = NULL; + +void +tomoe_dict_load (const gchar *base_dir) +{ + if (!base_dir) + base_dir = DICTDIR; + + dicts = g_list_concat (tomoe_module_load_modules (base_dir), dicts); +} + +void +tomoe_dict_unload (void) +{ + g_list_foreach (dicts, (GFunc) g_object_unref, NULL); + g_list_free (dicts); + dicts = NULL; +} G_DEFINE_ABSTRACT_TYPE (TomoeDict, tomoe_dict, G_TYPE_OBJECT) @@ -39,6 +59,19 @@ { } +TomoeDict * +tomoe_dict_new (const gchar *name, const gchar *first_property, ...) +{ + GObject *dict; + va_list var_args; + + va_start (var_args, first_property); + dict = tomoe_module_instantiate (dicts, name, first_property, var_args); + va_end (var_args); + + return TOMOE_DICT (dict); +} + const gchar * tomoe_dict_get_name (TomoeDict *dict) { Index: tomoe/lib/tomoe-dict.h diff -u tomoe/lib/tomoe-dict.h:1.51 tomoe/lib/tomoe-dict.h:1.52 --- tomoe/lib/tomoe-dict.h:1.51 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-dict.h Wed Nov 29 11:53:11 2006 @@ -21,7 +21,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-dict.h,v 1.51 2006/11/28 15:07:02 kous Exp $ + * $Id: tomoe-dict.h,v 1.52 2006/11/29 02:53:11 kous Exp $ */ /** @@ -71,6 +71,13 @@ GType tomoe_dict_get_type (void) G_GNUC_CONST; +void tomoe_dict_load (const gchar *base_dir); +void tomoe_dict_unload (void); + +TomoeDict *tomoe_dict_new (const gchar *name, + const gchar *first_property, + ...); + /** * @brief Get the dictionary name. * @param dict - Pointer to the TomoeDict object. Index: tomoe/lib/tomoe-module.c diff -u tomoe/lib/tomoe-module.c:1.3 tomoe/lib/tomoe-module.c:1.4 --- tomoe/lib/tomoe-module.c:1.3 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-module.c Wed Nov 29 11:53:11 2006 @@ -17,16 +17,127 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-module.c,v 1.3 2006/11/28 15:07:02 kous Exp $ + * $Id: tomoe-module.c,v 1.4 2006/11/29 02:53:11 kous Exp $ */ #include <stdlib.h> #include <string.h> +#include <gmodule.h> + #include "tomoe-module.h" +#include "tomoe-module-impl.h" + +#define TOMOE_MODULE_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TOMOE_TYPE_MODULE, TomoeModulePrivate)) + +typedef struct _TomoeModulePrivate TomoeModulePrivate; +struct _TomoeModulePrivate +{ + GModule *library; + gchar *mod_path; + + TomoeModuleInitFunc init; + TomoeModuleExitFunc exit; + TomoeModuleInstantiateFunc instantiate; +}; + +G_DEFINE_TYPE (TomoeModule, tomoe_module, G_TYPE_TYPE_MODULE) + +static void finalize (GObject *object); +static gboolean load (GTypeModule *module); +static void unload (GTypeModule *module); + +static void _tomoe_module_show_error (GModule *module); +static GModule *_tomoe_module_open (const gchar *mod_path); +static void _tomoe_module_close (GModule *module); +static gboolean _tomoe_module_load_func (GModule *module, + const gchar *func_name, + gpointer *symbol); +static gboolean _tomoe_module_match_name (GModule *module, + const gchar *name); + +static void +tomoe_module_class_init (TomoeModuleClass *klass) +{ + GObjectClass *gobject_class; + GTypeModuleClass *type_module_class; + + gobject_class = G_OBJECT_CLASS (klass); + gobject_class->finalize = finalize; + + type_module_class = G_TYPE_MODULE_CLASS (klass); + type_module_class->load = load; + type_module_class->unload = unload; + + g_type_class_add_private (gobject_class, sizeof (TomoeModulePrivate)); +} + +static void +tomoe_module_init (TomoeModule *module) +{ + TomoeModulePrivate *priv = TOMOE_MODULE_GET_PRIVATE (module); + + priv->library = NULL; + priv->mod_path = NULL; +} + +static void +finalize (GObject *object) +{ + TomoeModulePrivate *priv = TOMOE_MODULE_GET_PRIVATE (object); + + g_free (priv->mod_path); + priv->mod_path = NULL; + + G_OBJECT_CLASS (tomoe_module_parent_class)->finalize (object); +} + +static gboolean +load (GTypeModule *module) +{ + TomoeModulePrivate *priv = TOMOE_MODULE_GET_PRIVATE (module); -void -tomoe_module_show_error (GModule *module) + priv->library = _tomoe_module_open (priv->mod_path); + if (!priv->library) + return FALSE; + + if (!_tomoe_module_load_func (priv->library, + G_STRINGIFY (TOMOE_MODULE_IMPL_INIT), + (gpointer )&priv->init) || + !_tomoe_module_load_func (priv->library, + G_STRINGIFY (TOMOE_MODULE_IMPL_EXIT), + (gpointer )&priv->exit) || + !_tomoe_module_load_func (priv->library, + G_STRINGIFY (TOMOE_MODULE_IMPL_INSTANTIATE), + (gpointer )&priv->instantiate)) { + _tomoe_module_close (priv->library); + priv->library = NULL; + return FALSE; + } + + priv->init (module); + + return TRUE; +} + +static void +unload (GTypeModule *module) +{ + TomoeModulePrivate *priv = TOMOE_MODULE_GET_PRIVATE (module); + + priv->exit (); + + _tomoe_module_close (priv->library); + priv->library = NULL; + + priv->init = NULL; + priv->exit = NULL; + priv->instantiate = NULL; +} + +static void +_tomoe_module_show_error (GModule *module) { gchar *message; @@ -45,83 +156,110 @@ g_free (message); } -GModule * -tomoe_module_open (const gchar *mod_path) +GObject * +tomoe_module_instantiate (GList *modules, const gchar *name, + const gchar *first_property, va_list var_args) +{ + GList *node; + + for (node = modules; node; node = g_list_next (node)) { + TomoeModule *module = node->data; + TomoeModulePrivate *priv; + + priv = TOMOE_MODULE_GET_PRIVATE (module); + if (g_type_module_use (G_TYPE_MODULE (module))) { + GObject *object = NULL; + if (_tomoe_module_match_name (priv->library, name)) { + object = priv->instantiate (first_property, var_args); + } + g_type_module_unuse (G_TYPE_MODULE (module)); + if (object) + return object; + } + } + + return NULL; +} + +static GModule * +_tomoe_module_open (const gchar *mod_path) { GModule *module; module = g_module_open (mod_path, G_MODULE_BIND_LAZY); if (!module) { - tomoe_module_show_error (NULL); + _tomoe_module_show_error (NULL); } return module; } -void -tomoe_module_close (GModule *module) +static void +_tomoe_module_close (GModule *module) { if (module && g_module_close (module)) { - tomoe_module_show_error (NULL); + _tomoe_module_show_error (NULL); } } -gboolean -tomoe_module_load_func (GModule *module, const gchar *func_name, - gpointer *symbol) +static gboolean +_tomoe_module_load_func (GModule *module, const gchar *func_name, + gpointer *symbol) { g_return_val_if_fail (module, FALSE); if (g_module_symbol (module, func_name, symbol)) { return TRUE; } else { - tomoe_module_show_error (module); + _tomoe_module_show_error (module); return FALSE; } } -static gpointer -tomoe_module_load (const gchar *base_dir, const gchar *name, - TomoeModuleInstantiateFunc instantiate) +static TomoeModule * +_tomoe_module_load (const gchar *base_dir, const gchar *name) { gchar *mod_path; - gpointer result = NULL; + TomoeModule *module = NULL; mod_path = g_module_build_path (base_dir, name); if (g_str_has_suffix (mod_path, G_MODULE_SUFFIX)) { - result = instantiate (mod_path); + TomoeModulePrivate *priv; + module = g_object_new (TOMOE_TYPE_MODULE, NULL); + priv = TOMOE_MODULE_GET_PRIVATE (module); + priv->mod_path = g_strdup (mod_path); + g_type_module_set_name (G_TYPE_MODULE (module), priv->mod_path); } g_free (mod_path); - return result; + return module; } GList * -tomoe_module_load_modules (const gchar *base_dir, - TomoeModuleInstantiateFunc instantiate) +tomoe_module_load_modules (const gchar *base_dir) { GDir *dir; - GList *results = NULL; + GList *modules = NULL; dir = g_dir_open (base_dir, 0, NULL); if (dir) { const gchar *entry; while ((entry = g_dir_read_name(dir))) { - gpointer result; - result = tomoe_module_load (base_dir, entry, instantiate); - if (result) - results = g_list_prepend (results, result); + TomoeModule *module; + module = _tomoe_module_load (base_dir, entry); + if (module) + modules = g_list_prepend (modules, module); } g_dir_close(dir); } - return results; + return modules; } -gboolean -tomoe_module_match_name (GModule *module, const gchar *name) +static gboolean +_tomoe_module_match_name (GModule *module, const gchar *name) { gboolean matched; gchar *module_base_name, *normalized_matched_name; Index: tomoe/lib/tomoe-module.h diff -u tomoe/lib/tomoe-module.h:1.4 tomoe/lib/tomoe-module.h:1.5 --- tomoe/lib/tomoe-module.h:1.4 Wed Nov 29 08:52:52 2006 +++ tomoe/lib/tomoe-module.h Wed Nov 29 11:53:11 2006 @@ -17,29 +17,45 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-module.h,v 1.4 2006/11/28 23:52:52 ikezoe Exp $ + * $Id: tomoe-module.h,v 1.5 2006/11/29 02:53:11 kous Exp $ */ #ifndef __TOMOE_MODULE_H__ #define __TOMOE_MODULE_H__ #include <glib-object.h> -#include <gmodule.h> G_BEGIN_DECLS -typedef gpointer (*TomoeModuleInstantiateFunc) (const gchar *mod_path); - -void tomoe_module_show_error (GModule *module); -GModule *tomoe_module_open (const gchar *mod_path); -void tomoe_module_close (GModule *module); -gboolean tomoe_module_load_func (GModule *module, - const gchar *func_name, - gpointer *symbol); -GList *tomoe_module_load_modules (const gchar *base_dir, - TomoeModuleInstantiateFunc instantiate); -gboolean tomoe_module_match_name (GModule *module, - const gchar *name); +#define TOMOE_TYPE_MODULE (tomoe_module_get_type ()) +#define TOMOE_MODULE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TOMOE_TYPE_MODULE, TomoeModule)) +#define TOMOE_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TOMOE_TYPE_MODULE, TomoeModuleClass)) +#define TOMOE_IS_MODULE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TOMOE_TYPE_MODULE)) +#define TOMOE_IS_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TOMOE_TYPE_MODULE)) +#define TOMOE_MODULE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), TOMOE_TYPE_MODULE, TomoeModuleClass)) + +typedef struct _TomoeModule TomoeModule; +typedef struct _TomoeModuleClass TomoeModuleClass; + +struct _TomoeModule +{ + GTypeModule object; +}; + +struct _TomoeModuleClass +{ + GTypeModuleClass parent_class; +}; + +GType tomoe_module_get_type (void) G_GNUC_CONST; + + +GList *tomoe_module_load_modules (const gchar *base_dir); + +GObject *tomoe_module_instantiate (GList *modules, + const gchar *name, + const gchar *first_property, + va_list var_args); G_END_DECLS Index: tomoe/lib/tomoe-recognizer-impl.h diff -u tomoe/lib/tomoe-recognizer-impl.h:1.14 tomoe/lib/tomoe-recognizer-impl.h:removed --- tomoe/lib/tomoe-recognizer-impl.h:1.14 Wed Nov 29 11:28:09 2006 +++ tomoe/lib/tomoe-recognizer-impl.h Wed Nov 29 11:53:11 2006 @@ -1,56 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * Copyright (C) 2006 Kouhei Sutou <kou****@cozmi*****> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA - * - * $Id: tomoe-recognizer-impl.h,v 1.14 2006/11/29 02:28:09 ikezoe Exp $ - */ - -/** - * @file tomoe-recognizer-impl.h - * @brief Provide a set of API to recognize handwriting. - */ - -#ifndef __TOMOE_RECOGNIZER_IMPL_H__ -#define __TOMOE_RECOGNIZER_IMPL_H__ - -#include <glib-object.h> - -G_BEGIN_DECLS - -#include "tomoe-recognizer.h" - -typedef void (*TomoeRecognizerInitFunc) (GTypeModule *module); -typedef void (*TomoeRecognizerExitFunc) (void); -typedef TomoeRecognizer *(*TomoeRecognizerInstantiateFunc) (void); - -#define TOMOE_RECOGNIZER_IMPL_INIT tomoe_recognizer_impl_init -#define TOMOE_RECOGNIZER_IMPL_EXIT tomoe_recognizer_impl_exit -#define TOMOE_RECOGNIZER_IMPL_INSTANTIATE tomoe_recognizer_impl_instantiate - - -void TOMOE_RECOGNIZER_IMPL_INIT (GTypeModule *module); -void TOMOE_RECOGNIZER_IMPL_EXIT (void); -GObject *TOMOE_RECOGNIZER_IMPL_INSTANTIATE (const gchar *first_property, va_list args); - -G_END_DECLS - -#endif /* __TOMOE_RECOGNIZER_H__ */ - -/* -vi:ts=4:nowrap:ai:expandtab -*/ Index: tomoe/lib/tomoe-recognizer-loader.c diff -u tomoe/lib/tomoe-recognizer-loader.c:1.3 tomoe/lib/tomoe-recognizer-loader.c:removed --- tomoe/lib/tomoe-recognizer-loader.c:1.3 Wed Nov 29 09:02:25 2006 +++ tomoe/lib/tomoe-recognizer-loader.c Wed Nov 29 11:53:11 2006 @@ -1,189 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * Copyright (C) 2006 Kouhei Sutou <kou****@cozmi*****> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA - * - * $Id: tomoe-recognizer-loader.c,v 1.3 2006/11/29 00:02:25 ikezoe Exp $ - */ - -#include <stdlib.h> - -#include "tomoe-recognizer-loader.h" -#include "tomoe-recognizer-impl.h" - -static GType tomoe_recognizer_loader_get_type (void); - -#define TOMOE_TYPE_RECOGNIZER_LOADER (tomoe_recognizer_loader_get_type ()) -#define TOMOE_RECOGNIZER_LOADER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TOMOE_TYPE_RECOGNIZER_LOADER, TomoeRecognizerLoader)) -#define TOMOE_IS_RECOGNIZER_LOADER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TOMOE_TYPE_RECOGNIZER_LOADER)) - -typedef struct _TomoeRecognizerLoader TomoeRecognizerLoader; -typedef struct _TomoeRecognizerLoaderClass TomoeRecognizerLoaderClass; - -struct _TomoeRecognizerLoader -{ - GTypeModule parent_instance; - - GModule *module; - - TomoeRecognizerInitFunc init; - TomoeRecognizerExitFunc exit; - TomoeRecognizerInstantiateFunc instantiate; - - gchar *mod_path; -}; - -struct _TomoeRecognizerLoaderClass -{ - GTypeModuleClass parent_class; -}; - -static GList *recognizers = NULL; -static GObjectClass *parent_class = NULL; - -static gboolean -load (GTypeModule *module) -{ - TomoeRecognizerLoader *loader = TOMOE_RECOGNIZER_LOADER (module); - - loader->module = tomoe_module_open (loader->mod_path); - if (!loader->module) - return FALSE; - - if (!tomoe_module_load_func (loader->module, - G_STRINGIFY (TOMOE_RECOGNIZER_IMPL_INIT), - (gpointer )&loader->init) || - !tomoe_module_load_func (loader->module, - G_STRINGIFY (TOMOE_RECOGNIZER_IMPL_EXIT), - (gpointer )&loader->exit) || - !tomoe_module_load_func (loader->module, - G_STRINGIFY (TOMOE_RECOGNIZER_IMPL_INSTANTIATE), - (gpointer )&loader->instantiate)) { - tomoe_module_close (loader->module); - loader->module = NULL; - return FALSE; - } - - loader->init (module); - - return TRUE; -} - -static void -unload (GTypeModule *module) -{ - TomoeRecognizerLoader *loader = TOMOE_RECOGNIZER_LOADER (module); - - loader->exit (); - - tomoe_module_close (loader->module); - loader->module = NULL; - - loader->init = NULL; - loader->exit = NULL; - loader->instantiate = NULL; -} - -static void -finalize (GObject *object) -{ - TomoeRecognizerLoader *loader = TOMOE_RECOGNIZER_LOADER (object); - - g_free (loader->mod_path); - loader->mod_path = NULL; - - parent_class->finalize (object); -} - -G_DEFINE_TYPE (TomoeRecognizerLoader, tomoe_recognizer_loader, - G_TYPE_TYPE_MODULE) - -static void -tomoe_recognizer_loader_class_init (TomoeRecognizerLoaderClass *class) -{ - GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class); - GObjectClass *gobject_class = G_OBJECT_CLASS (class); - - parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class)); - - module_class->load = load; - module_class->unload = unload; - - gobject_class->finalize = finalize; -} - -static void -tomoe_recognizer_loader_init (TomoeRecognizerLoader *object) -{ -} - -static gpointer -instantiate (const gchar *mod_path) -{ - TomoeRecognizerLoader *loader; - - loader = g_object_new (TOMOE_TYPE_RECOGNIZER_LOADER, NULL); - loader->mod_path = g_strdup (mod_path); - g_type_module_set_name (G_TYPE_MODULE (loader), loader->mod_path); - - return loader; -} - -void -tomoe_recognizer_loader_load (const gchar *base_dir) -{ - if (!base_dir) - base_dir = RECOGNIZERDIR; - - recognizers = g_list_concat (tomoe_module_load_modules (base_dir, - instantiate), - recognizers); -} - -void -tomoe_recognizer_loader_unload (void) -{ - g_list_foreach (recognizers, (GFunc) g_type_module_unuse, NULL); - g_list_free (recognizers); - recognizers = NULL; -} - -TomoeRecognizer * -tomoe_recognizer_loader_instantiate (const gchar *name) -{ - GList *node; - - for (node = recognizers; node; node = g_list_next (node)) { - TomoeRecognizerLoader *loader = node->data; - - if (g_type_module_use (G_TYPE_MODULE (loader))) { - TomoeRecognizer *recognizer = NULL; - if (tomoe_module_match_name (loader->module, name)) { - recognizer = loader->instantiate (); - } - g_type_module_unuse (G_TYPE_MODULE (loader)); - if (recognizer) - return recognizer; - } - } - - return NULL; -} - -/* -vi:ts=4:nowrap:ai:expandtab -*/ Index: tomoe/lib/tomoe-recognizer-loader.h diff -u tomoe/lib/tomoe-recognizer-loader.h:1.1 tomoe/lib/tomoe-recognizer-loader.h:removed --- tomoe/lib/tomoe-recognizer-loader.h:1.1 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-recognizer-loader.h Wed Nov 29 11:53:11 2006 @@ -1,48 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * Copyright (C) 2006 Kouhei Sutou <kou****@cozmi*****> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA - * - * $Id: tomoe-recognizer-loader.h,v 1.1 2006/11/28 15:07:02 kous Exp $ - */ - -/** - * @file tomoe-recognizer-loader.h - * @brief Provide a set of API to load recognizer. - */ - -#ifndef __TOMOE_RECOGNIZER_LOADER_H__ -#define __TOMOE_RECOGNIZER_LOADER_H__ - -#include <glib-object.h> - -G_BEGIN_DECLS - -#include "tomoe-module.h" -#include "tomoe-recognizer.h" - -void tomoe_recognizer_loader_load (const gchar *base_dir); -void tomoe_recognizer_loader_unload (void); -TomoeRecognizer *tomoe_recognizer_loader_instantiate (const gchar *name); - -G_END_DECLS - -#endif /* __TOMOE_RECOGNIZER_LOADER_H__ */ - -/* -vi:ts=4:nowrap:ai:expandtab -*/ Index: tomoe/lib/tomoe-recognizer.c diff -u tomoe/lib/tomoe-recognizer.c:1.17 tomoe/lib/tomoe-recognizer.c:1.18 --- tomoe/lib/tomoe-recognizer.c:1.17 Wed Nov 29 08:39:27 2006 +++ tomoe/lib/tomoe-recognizer.c Wed Nov 29 11:53:11 2006 @@ -18,13 +18,34 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-recognizer.c,v 1.17 2006/11/28 23:39:27 ikezoe Exp $ + * $Id: tomoe-recognizer.c,v 1.18 2006/11/29 02:53:11 kous Exp $ */ #include <stdlib.h> +#include "tomoe-module.h" #include "tomoe-recognizer.h" +static GList *recognizers = NULL; + +void +tomoe_recognizer_load (const gchar *base_dir) +{ + if (!base_dir) + base_dir = RECOGNIZERDIR; + + recognizers = g_list_concat (tomoe_module_load_modules (base_dir), + recognizers); +} + +void +tomoe_recognizer_unload (void) +{ + g_list_foreach (recognizers, (GFunc) g_object_unref, NULL); + g_list_free (recognizers); + recognizers = NULL; +} + G_DEFINE_ABSTRACT_TYPE (TomoeRecognizer, tomoe_recognizer, G_TYPE_OBJECT) static void @@ -38,6 +59,20 @@ { } +TomoeRecognizer * +tomoe_recognizer_new (const gchar *name, const gchar *first_property, ...) +{ + GObject *recognizer; + va_list var_args; + + va_start (var_args, first_property); + recognizer = tomoe_module_instantiate (recognizers, name, + first_property, var_args); + va_end (var_args); + + return TOMOE_RECOGNIZER (recognizer); +} + GList * tomoe_recognizer_search (TomoeRecognizer *recognizer, TomoeDict *dict, TomoeWriting *input) Index: tomoe/lib/tomoe-recognizer.h diff -u tomoe/lib/tomoe-recognizer.h:1.16 tomoe/lib/tomoe-recognizer.h:1.17 --- tomoe/lib/tomoe-recognizer.h:1.16 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-recognizer.h Wed Nov 29 11:53:11 2006 @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-recognizer.h,v 1.16 2006/11/28 15:07:02 kous Exp $ + * $Id: tomoe-recognizer.h,v 1.17 2006/11/29 02:53:11 kous Exp $ */ /** @@ -61,6 +61,14 @@ GType tomoe_recognizer_get_type (void) G_GNUC_CONST; + +void tomoe_recognizer_load (const gchar *base_dir); +void tomoe_recognizer_unload (void); + +TomoeRecognizer *tomoe_recognizer_new (const gchar *name, + const gchar *first_property, + ...); + /** * @brief Match strokes of tomoe_char with input. * @param recognizer - Pointer to the TomoeRecognizer object. Index: tomoe/lib/tomoe.c diff -u tomoe/lib/tomoe.c:1.46 tomoe/lib/tomoe.c:1.47 --- tomoe/lib/tomoe.c:1.46 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe.c Wed Nov 29 11:53:11 2006 @@ -20,7 +20,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe.c,v 1.46 2006/11/28 15:07:02 kous Exp $ + * $Id: tomoe.c,v 1.47 2006/11/29 02:53:11 kous Exp $ */ #include <stdio.h> @@ -41,8 +41,8 @@ initialized = TRUE; GTypeDebugFlags debug_flag = G_TYPE_DEBUG_NONE; g_type_init_with_debug_flags (debug_flag); - tomoe_dict_loader_load (NULL); - tomoe_recognizer_loader_load (NULL); + tomoe_dict_load (NULL); + tomoe_recognizer_load (NULL); } } @@ -54,8 +54,8 @@ initialized = FALSE; #warning FIXME: those causes memory warnings #if 0 - tomoe_dict_loader_unload (); - tomoe_recognizer_loader_unload (); + tomoe_dict_unload (); + tomoe_recognizer_unload (); #endif } } Index: tomoe/lib/tomoe.h diff -u tomoe/lib/tomoe.h:1.33 tomoe/lib/tomoe.h:1.34 --- tomoe/lib/tomoe.h:1.33 Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe.h Wed Nov 29 11:53:11 2006 @@ -20,7 +20,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe.h,v 1.33 2006/11/28 15:07:02 kous Exp $ + * $Id: tomoe.h,v 1.34 2006/11/29 02:53:11 kous Exp $ */ /** @file tomoe.h @@ -40,9 +40,6 @@ #include "tomoe-recognizer.h" #include "tomoe-writing.h" -#include "tomoe-dict-loader.h" -#include "tomoe-recognizer-loader.h" - /** * @brief Initialize tomoe library. */