Kouhei Sutou
kous****@users*****
2006年 11月 29日 (水) 00:07:02 JST
Index: tomoe/lib/Makefile.am diff -u tomoe/lib/Makefile.am:1.41 tomoe/lib/Makefile.am:1.42 --- tomoe/lib/Makefile.am:1.41 Tue Nov 28 17:11:31 2006 +++ tomoe/lib/Makefile.am Wed Nov 29 00:07:02 2006 @@ -38,6 +38,7 @@ tomoe-context.h \ tomoe-config.h \ tomoe-dict.h \ + tomoe-dict-impl.h \ tomoe-module.h \ tomoe-query.h \ tomoe-reading.h \ @@ -74,10 +75,12 @@ 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.42 tomoe/lib/tomoe-config.c:1.43 --- tomoe/lib/tomoe-config.c:1.42 Tue Nov 28 17:11:31 2006 +++ tomoe/lib/tomoe-config.c Wed Nov 29 00:07:02 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.42 2006/11/28 08:11:31 kous Exp $ + * $Id: tomoe-config.c,v 1.43 2006/11/28 15:07:02 kous Exp $ */ #ifdef HAVE_CONFIG_H @@ -31,6 +31,8 @@ #include <glib/gi18n.h> #include "tomoe-config.h" +#include "tomoe-dict-loader.h" +#include "tomoe-config.h" #include "glib-utils.h" #define TOMOE_CONFIG_GET_PRIVATE(obj) \ @@ -274,11 +276,11 @@ if (_tomoe_dict_key_file_get_boolean_value (key_file, dict_name, "user", TRUE)) { - dict = tomoe_dict_new (filename, TRUE, NULL, NULL); + dict = tomoe_dict_loader_instantiate ("xml", filename, TRUE); } else { gchar *dict_filename; dict_filename = g_build_filename (TOMOEDATADIR, filename, NULL); - dict = tomoe_dict_new (dict_filename, FALSE, NULL, NULL); + dict = tomoe_dict_loader_instantiate ("xml", filename, TRUE); g_free (dict_filename); } @@ -345,7 +347,7 @@ continue; } - dict = tomoe_dict_new (path, FALSE, NULL, NULL); + dict = tomoe_dict_loader_instantiate ("xml", path, FALSE); 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.39 tomoe/lib/tomoe-context.c:1.40 --- tomoe/lib/tomoe-context.c:1.39 Tue Nov 28 13:10:01 2006 +++ tomoe/lib/tomoe-context.c Wed Nov 29 00:07:02 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.39 2006/11/28 04:10:01 kous Exp $ + * $Id: tomoe-context.c,v 1.40 2006/11/28 15:07:02 kous Exp $ */ #include "tomoe-dict.h" -#include "tomoe-recognizer.h" +#include "tomoe-recognizer-loader.h" #include "tomoe-context.h" #include "tomoe-config.h" #include "tomoe-shelf.h" @@ -114,22 +114,6 @@ priv->config = cfg; } -void -tomoe_context_load_recognizer (TomoeContext *ctx, - const gchar *base_dir, - const gchar *name) -{ - TomoeContextPrivate *priv; - - g_return_if_fail (TOMOE_IS_CONTEXT(ctx)); - - priv = TOMOE_CONTEXT_GET_PRIVATE(ctx); - if (priv->recognizer) - g_object_unref (priv->recognizer); - - priv->recognizer = tomoe_recognizer_new (base_dir, name); -} - static gint _candidate_compare_func (gconstpointer a, gconstpointer b) { @@ -158,7 +142,7 @@ if (!names) return matched; if (!priv->recognizer) - priv->recognizer = tomoe_recognizer_new (NULL, NULL); + priv->recognizer = tomoe_recognizer_loader_instantiate ("simple"); 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.1 tomoe/lib/tomoe-dict-impl.h:1.2 --- tomoe/lib/tomoe-dict-impl.h:1.1 Tue Nov 28 17:50:15 2006 +++ tomoe/lib/tomoe-dict-impl.h Wed Nov 29 00:07:02 2006 @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-dict-impl.h,v 1.1 2006/11/28 08:50:15 kous Exp $ + * $Id: tomoe-dict-impl.h,v 1.2 2006/11/28 15:07:02 kous Exp $ */ /** @@ -34,47 +34,20 @@ #include "tomoe-dict.h" -typedef gpointer (*TomoeDictNewFunc) (GHashTable *hash); -typedef gpointer (*TomoeDictFreeFunc) (gpointer context); -typedef gchar *(*TomoeDictGetNameFunc) (gpointer context); -typedef gboolean (*TomoeDictRegisterCharFunc) (gpointer context, - TomoeChar *chr); -typedef gboolean (*TomoeDictUnregisterCharFunc) (gpointer context, - const gchar *utf8); -typedef TomoeChar *(*TomoeDictGetCharFunc) (gpointer context, - const gchar *utf8); -typedef GList *(*TomoeDictSearchFunc) (gpointer context, - TomoeQuery *query); - -typedef void (*TomoeDictLoadFunc) (gpointer context, - const gchar *filename, - gboolean editable); - -#define TOMOE_DICT_IMPL_NEW tomoe_dict_impl_new -#define TOMOE_DICT_IMPL_FREE tomoe_dict_impl_free -#define TOMOE_DICT_IMPL_GET_NAME tomoe_dict_impl_get_name -#define TOMOE_DICT_IMPL_REGISTER_CHAR tomoe_dict_impl_register_char -#define TOMOE_DICT_IMPL_UNREGISTER_CHAR tomoe_dict_impl_unregister_char -#define TOMOE_DICT_IMPL_GET_CHAR tomoe_dict_impl_get_char -#define TOMOE_DICT_IMPL_SEARCH tomoe_dict_impl_search - -#define TOMOE_DICT_IMPL_LOAD tomoe_dict_impl_load - -gpointer TOMOE_DICT_IMPL_NEW (void); -void TOMOE_DICT_IMPL_FREE (gpointer context); -const gchar *TOMOE_DICT_IMPL_GET_NAME (gpointer context); -gboolean TOMOE_DICT_IMPL_REGISTER_CHAR (gpointer context, - TomoeChar *chr); -gboolean TOMOE_DICT_IMPL_UNREGISTER_CHAR (gpointer context, - const gchar *utf8); -TomoeChar *TOMOE_DICT_IMPL_GET_CHAR (gpointer context, - const gchar *utf8); -GList *TOMOE_DICT_IMPL_SEARCH (gpointer context, - TomoeQuery *query); - -void TOMOE_DICT_IMPL_LOAD (gpointer context, - const gchar *filename, - gboolean editable); +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); +TomoeDict *TOMOE_DICT_IMPL_INSTANTIATE (const gchar *filename, + gboolean editable); G_END_DECLS Index: tomoe/lib/tomoe-dict-loader.c diff -u /dev/null tomoe/lib/tomoe-dict-loader.c:1.1 --- /dev/null Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-dict-loader.c Wed Nov 29 00:07:02 2006 @@ -0,0 +1,189 @@ +/* -*- 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.1 2006/11/28 15:07:02 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_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; + 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 /dev/null tomoe/lib/tomoe-dict-loader.h:1.1 --- /dev/null Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-dict-loader.h Wed Nov 29 00:07:02 2006 @@ -0,0 +1,50 @@ +/* -*- 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.123 tomoe/lib/tomoe-dict.c:1.124 --- tomoe/lib/tomoe-dict.c:1.123 Tue Nov 28 17:11:31 2006 +++ tomoe/lib/tomoe-dict.c Wed Nov 29 00:07:02 2006 @@ -17,13 +17,12 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-dict.c,v 1.123 2006/11/28 08:11:31 kous Exp $ + * $Id: tomoe-dict.c,v 1.124 2006/11/28 15:07:02 kous Exp $ */ #include "tomoe-dict.h" -#include "tomoe-dict-impl.h" -G_DEFINE_TYPE (TomoeDict, tomoe_dict, TOMOE_TYPE_MODULE) +G_DEFINE_ABSTRACT_TYPE (TomoeDict, tomoe_dict, G_TYPE_OBJECT) static void tomoe_dict_class_init (TomoeDictClass *klass) @@ -35,170 +34,74 @@ { } -static void -tomoe_dict_load (TomoeDict *dict, const gchar *filename, gboolean editable) -{ - TomoeModule *module; - TomoeDictLoadFunc load_func; - TomoeDictLoadFunc *load_func_p; - gpointer *p; - - g_return_if_fail (TOMOE_IS_DICT (dict)); - module = TOMOE_MODULE (dict); - load_func_p = &load_func; - p = (gpointer *)load_func_p; - if (tomoe_module_load_func (module, - G_STRINGIFY(TOMOE_DICT_IMPL_LOAD), - p)) { - load_func (tomoe_module_get_context (module), filename, editable); - } else { - tomoe_module_show_error (module); - } -} - -TomoeDict * -tomoe_dict_new (const char* filename, gboolean editable, - const gchar *base_dir, const gchar *name) -{ - TomoeDict *dict; - - if (!filename && !*filename) return NULL; - - dict = g_object_new (TOMOE_TYPE_DICT, - "default_base_dir", DICTDIR, - "new_func_name", - G_STRINGIFY(TOMOE_DICT_IMPL_NEW), - "free_func_name", - G_STRINGIFY(TOMOE_DICT_IMPL_FREE), - NULL); - if (!tomoe_module_find_module (TOMOE_MODULE (dict), base_dir, name)) { - g_object_unref (dict); - g_warning ("can't find module for dictionary"); - return NULL; - } - tomoe_dict_load (dict, filename, editable); - - return dict; -} - const gchar * tomoe_dict_get_name (TomoeDict *dict) { - TomoeModule *module; - const gchar *result = NULL; - TomoeDictGetNameFunc get_name_func; - TomoeDictGetNameFunc *get_name_func_p; - gpointer *p; - - g_return_val_if_fail (TOMOE_IS_DICT (dict), result); - module = TOMOE_MODULE (dict); - get_name_func_p = &get_name_func; - p = (gpointer *)get_name_func_p; - if (tomoe_module_load_func (module, - G_STRINGIFY(TOMOE_DICT_IMPL_GET_NAME), - p)) { - result = get_name_func (tomoe_module_get_context (module)); - } else { - tomoe_module_show_error (module); - } + TomoeDictClass *klass; - return result; + g_return_val_if_fail (TOMOE_IS_DICT (dict), NULL); + + klass = TOMOE_DICT_GET_CLASS (dict); + if (klass->get_name) + return klass->get_name (dict); + else + return NULL; } gboolean tomoe_dict_register_char (TomoeDict *dict, TomoeChar *chr) { - TomoeModule *module; - gboolean result = FALSE; - TomoeDictRegisterCharFunc register_char_func; - TomoeDictRegisterCharFunc *register_char_func_p; - gpointer *p; - - g_return_val_if_fail (TOMOE_IS_DICT (dict), result); - module = TOMOE_MODULE (dict); - register_char_func_p = ®ister_char_func; - p = (gpointer *)register_char_func_p; - if (tomoe_module_load_func (module, - G_STRINGIFY(TOMOE_DICT_IMPL_REGISTER_CHAR), - p)) { - result = register_char_func (tomoe_module_get_context (module), chr); - } else { - tomoe_module_show_error (module); - } + TomoeDictClass *klass; - return result; + g_return_val_if_fail (TOMOE_IS_DICT (dict), FALSE); + + klass = TOMOE_DICT_GET_CLASS (dict); + if (klass->register_char) + return klass->register_char (dict, chr); + else + return FALSE; } gboolean tomoe_dict_unregister_char (TomoeDict *dict, const gchar *utf8) { - TomoeModule *module; - gboolean result = FALSE; - TomoeDictUnregisterCharFunc unregister_char_func; - TomoeDictUnregisterCharFunc *unregister_char_func_p; - gpointer *p; - - g_return_val_if_fail (TOMOE_IS_DICT (dict), result); - module = TOMOE_MODULE (dict); - unregister_char_func_p = &unregister_char_func; - p = (gpointer *)unregister_char_func_p; - if (tomoe_module_load_func (module, - G_STRINGIFY(TOMOE_DICT_IMPL_UNREGISTER_CHAR), - p)) { - result = unregister_char_func (tomoe_module_get_context (module), utf8); - } else { - tomoe_module_show_error (module); - } + TomoeDictClass *klass; + + g_return_val_if_fail (TOMOE_IS_DICT (dict), FALSE); - return result; + klass = TOMOE_DICT_GET_CLASS (dict); + if (klass->unregister_char) + return klass->unregister_char (dict, utf8); + else + return FALSE; } TomoeChar * tomoe_dict_get_char (TomoeDict *dict, const gchar *utf8) { - TomoeModule *module; - TomoeChar *result = NULL; - TomoeDictGetCharFunc get_char_func; - TomoeDictGetCharFunc *get_char_func_p; - gpointer *p; + TomoeDictClass *klass; g_return_val_if_fail (TOMOE_IS_DICT (dict), NULL); - module = TOMOE_MODULE (dict); - get_char_func_p = &get_char_func; - p = (gpointer *)get_char_func_p; - if (tomoe_module_load_func (module, - G_STRINGIFY(TOMOE_DICT_IMPL_GET_CHAR), - p)) { - result = get_char_func (tomoe_module_get_context (module), utf8); - } else { - tomoe_module_show_error (module); - } - return result; + klass = TOMOE_DICT_GET_CLASS (dict); + if (klass->get_char) + return klass->get_char (dict, utf8); + else + return FALSE; } GList * tomoe_dict_search (TomoeDict *dict, TomoeQuery *query) { - TomoeModule *module; - GList *result = NULL; - TomoeDictSearchFunc search_func; - TomoeDictSearchFunc *search_func_p; - gpointer *p; + TomoeDictClass *klass; g_return_val_if_fail (TOMOE_IS_DICT (dict), NULL); - module = TOMOE_MODULE (dict); - search_func_p = &search_func; - p = (gpointer *)search_func_p; - if (tomoe_module_load_func (module, - G_STRINGIFY(TOMOE_DICT_IMPL_SEARCH), - p)) { - result = search_func (tomoe_module_get_context (module), query); - } else { - tomoe_module_show_error (module); - } - return result; + klass = TOMOE_DICT_GET_CLASS (dict); + if (klass->search) + return klass->search (dict, query); + else + return NULL; } /* Index: tomoe/lib/tomoe-dict.h diff -u tomoe/lib/tomoe-dict.h:1.50 tomoe/lib/tomoe-dict.h:1.51 --- tomoe/lib/tomoe-dict.h:1.50 Tue Nov 28 17:11:31 2006 +++ tomoe/lib/tomoe-dict.h Wed Nov 29 00:07:02 2006 @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * Copyright (C) 2000 - 2004 Hiroyuki Komatsu <komat****@taiya*****> * Copyright (C) 2004 Hiroaki Nakamura <hnaka****@good-*****> @@ -21,7 +21,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-dict.h,v 1.50 2006/11/28 08:11:31 kous Exp $ + * $Id: tomoe-dict.h,v 1.51 2006/11/28 15:07:02 kous Exp $ */ /** @@ -36,7 +36,6 @@ G_BEGIN_DECLS -#include "tomoe-module.h" #include "tomoe-char.h" #include "tomoe-query.h" @@ -52,28 +51,27 @@ struct _TomoeDict { - TomoeModule object; + GObject object; }; struct _TomoeDictClass { - TomoeModuleClass parent_class; + GObjectClass parent_class; + + const gchar *(*get_name) (TomoeDict *dict); + gboolean (*register_char) (TomoeDict *dict, + TomoeChar *chr); + gboolean (*unregister_char) (TomoeDict *dict, + const gchar *utf8); + TomoeChar *(*get_char) (TomoeDict *dict, + const gchar *utf8); + GList *(*search) (TomoeDict *dict, + TomoeQuery *query); }; GType tomoe_dict_get_type (void) G_GNUC_CONST; /** - * @brief Create a dictionary from a file. - * @param filename - Name of dictionary file to load. - * @param editable - Editability of the dictionary. - * @return Pointer to newly allocated TomoeDict object. - */ -TomoeDict *tomoe_dict_new (const gchar *filename, - gboolean editable, - const gchar *base_dir, - const gchar *name); - -/** * @brief Get the dictionary name. * @param dict - Pointer to the TomoeDict object. * @return Name of the dictionary. Index: tomoe/lib/tomoe-module.c diff -u tomoe/lib/tomoe-module.c:1.2 tomoe/lib/tomoe-module.c:1.3 --- tomoe/lib/tomoe-module.c:1.2 Tue Nov 28 17:11:31 2006 +++ tomoe/lib/tomoe-module.c Wed Nov 29 00:07:02 2006 @@ -17,147 +17,25 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-module.c,v 1.2 2006/11/28 08:11:31 kous Exp $ + * $Id: tomoe-module.c,v 1.3 2006/11/28 15:07:02 kous Exp $ */ #include <stdlib.h> +#include <string.h> -#include <gmodule.h> -#include <glib/gi18n.h> #include "tomoe-module.h" -#define TOMOE_MODULE_GET_PRIVATE(obj) \ - (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TOMOE_TYPE_MODULE, TomoeModulePrivate)) - -typedef struct _TomoeModulePrivate TomoeModulePrivate; -struct _TomoeModulePrivate -{ - GModule *module; - void *context; - - gchar *default_base_dir; - gchar *new_func_name; - gchar *free_func_name; -}; - -enum -{ - PROP_0, - PROP_NAME, - PROP_BASE_DIR, - PROP_DEFAULT_BASE_DIR, - PROP_NEW_FUNC_NAME, - PROP_FREE_FUNC_NAME -}; - -G_DEFINE_TYPE (TomoeModule, tomoe_module, G_TYPE_OBJECT) - -static void dispose (GObject *object); -static void set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec); - -static void -tomoe_module_class_init (TomoeModuleClass *klass) -{ - GObjectClass *gobject_class; - GParamSpec *spec; - - gobject_class = G_OBJECT_CLASS (klass); - - gobject_class->dispose = dispose; - gobject_class->set_property = set_property; - - spec = g_param_spec_string ("default_base_dir", - N_("Default base directory"), - N_("Default base directory for loading or " - "searching module."), - NULL, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_property (gobject_class, PROP_DEFAULT_BASE_DIR, - spec); - - spec = g_param_spec_string ("new_func_name", - N_("New function name"), - N_("New function name of the module."), - NULL, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_property (gobject_class, PROP_NEW_FUNC_NAME, spec); - - spec = g_param_spec_string ("free_func_name", - N_("Free function name"), - N_("Free function name of the module."), - NULL, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_property (gobject_class, PROP_FREE_FUNC_NAME, spec); - - g_type_class_add_private (gobject_class, sizeof (TomoeModulePrivate)); -} - -static void -tomoe_module_init (TomoeModule *module) -{ - TomoeModulePrivate *priv = TOMOE_MODULE_GET_PRIVATE (module); - - priv->module = NULL; - priv->context = NULL; -} - -static void -set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - TomoeModulePrivate *priv; - - priv = TOMOE_MODULE_GET_PRIVATE (object); - switch (prop_id) { - case PROP_DEFAULT_BASE_DIR: - g_free (priv->default_base_dir); - priv->default_base_dir = g_value_dup_string (value); - break; - case PROP_NEW_FUNC_NAME: - g_free (priv->new_func_name); - priv->new_func_name = g_value_dup_string (value); - break; - case PROP_FREE_FUNC_NAME: - g_free (priv->free_func_name); - priv->free_func_name = g_value_dup_string (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); - break; - } -} - -gpointer -tomoe_module_get_context (TomoeModule *module) -{ - TomoeModulePrivate *priv; - - g_return_val_if_fail (TOMOE_IS_MODULE (module), NULL); - priv = TOMOE_MODULE_GET_PRIVATE (module); - return priv->context; -} - - void -tomoe_module_show_error (TomoeModule *module) +tomoe_module_show_error (GModule *module) { - TomoeModulePrivate *priv; gchar *message; - g_return_if_fail (TOMOE_IS_MODULE (module)); - priv = TOMOE_MODULE_GET_PRIVATE (module); - if (!g_module_error ()) return; message = g_locale_to_utf8 (g_module_error(), -1, NULL, NULL, NULL); - if (priv->module) { + if (module) { gchar *name; - name = g_strdup (g_module_name (priv->module)); + name = g_strdup (g_module_name (module)); g_warning ("%s: %s", name, message); g_free (name); } else { @@ -167,140 +45,96 @@ g_free (message); } -gboolean -tomoe_module_load_func (TomoeModule *module, const gchar *func_name, - gpointer *symbol) +GModule * +tomoe_module_open (const gchar *mod_path) { - TomoeModulePrivate *priv; + GModule *module; - g_return_val_if_fail (TOMOE_IS_MODULE (module), FALSE); - priv = TOMOE_MODULE_GET_PRIVATE (module); + module = g_module_open (mod_path, G_MODULE_BIND_LAZY); + if (!module) { + tomoe_module_show_error (NULL); + } - g_return_val_if_fail (priv->module, FALSE); - if (g_module_symbol (priv->module, func_name, symbol)) { - return TRUE; - } else { - tomoe_module_show_error (module); - return FALSE; + return module; +} + +void +tomoe_module_close (GModule *module) +{ + if (module && g_module_close (module)) { + tomoe_module_show_error (NULL); } } -static void -tomoe_module_close_module (TomoeModule *module) +gboolean +tomoe_module_load_func (GModule *module, const gchar *func_name, + gpointer *symbol) { - TomoeModulePrivate *priv; - TomoeModuleFreeFunc free_func; - TomoeModuleFreeFunc *free_func_p; - gpointer *p; - - g_return_if_fail (TOMOE_IS_MODULE (module)); - priv = TOMOE_MODULE_GET_PRIVATE (module); - - g_return_if_fail (priv->module); - - free_func_p = &free_func; - p = (gpointer *)free_func_p; - if (tomoe_module_load_func (module, priv->free_func_name, p)) - free_func (priv->context); - else - tomoe_module_show_error (module); + g_return_val_if_fail (module, FALSE); - if (!g_module_close (priv->module)) + if (g_module_symbol (module, func_name, symbol)) { + return TRUE; + } else { tomoe_module_show_error (module); - priv->module = NULL; - priv->context = NULL; + return FALSE; + } } -static gboolean -tomoe_module_load(TomoeModule *module, const gchar *base_dir, const gchar *name) +static gpointer +tomoe_module_load (const gchar *base_dir, const gchar *name, + TomoeModuleInstantiateFunc instantiate) { - TomoeModulePrivate *priv; - gboolean success = FALSE; gchar *mod_path; - - g_return_val_if_fail (TOMOE_IS_MODULE (module), FALSE); - priv = TOMOE_MODULE_GET_PRIVATE (module); + gpointer result = NULL; mod_path = g_module_build_path (base_dir, name); - priv->module = g_module_open (mod_path, G_MODULE_BIND_LAZY); - priv->context = NULL; - - if (priv->module) { - TomoeModuleNewFunc new_func; - TomoeModuleNewFunc *new_func_p; - gpointer *p; - - new_func_p = &new_func; - p = (gpointer *)new_func_p; - if (tomoe_module_load_func (module, priv->new_func_name, p)) { - priv->context = new_func (); - success = TRUE; - } else { - tomoe_module_close_module (module); - } - } else { - tomoe_module_show_error (NULL); + if (g_str_has_suffix (mod_path, G_MODULE_SUFFIX)) { + result = instantiate (mod_path); } - g_free (mod_path); - return success; + return result; } -gboolean -tomoe_module_find_module(TomoeModule *module, - const gchar *base_dir, const gchar *name) +GList * +tomoe_module_load_modules (const gchar *base_dir, + TomoeModuleInstantiateFunc instantiate) { - TomoeModulePrivate *priv; GDir *dir; + GList *results = NULL; - g_return_val_if_fail (TOMOE_IS_MODULE (module), FALSE); - priv = TOMOE_MODULE_GET_PRIVATE (module); - - if (!base_dir) - base_dir = priv->default_base_dir; - - if (name) { - tomoe_module_load (module, base_dir, name); - } else { - dir = g_dir_open (base_dir, 0, NULL); - if (dir) { - const gchar *entry; - - while ((entry = g_dir_read_name(dir))) { - if (tomoe_module_load (module, base_dir, entry)) - break; - } - - g_dir_close(dir); + 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); } + + g_dir_close(dir); } - return priv->module ? TRUE : FALSE; + return results; } -static void -dispose (GObject *object) +gboolean +tomoe_module_match_name (GModule *module, const gchar *name) { - TomoeModule *module; - module = TOMOE_MODULE (object); - TomoeModulePrivate *priv; + gboolean matched; + gchar *module_base_name, *normalized_matched_name; - priv = TOMOE_MODULE_GET_PRIVATE (module); + module_base_name = g_path_get_basename (g_module_name (module)); + normalized_matched_name = g_module_build_path (NULL, name); - if (priv->module) { - tomoe_module_close_module (module); - } + matched = (0 == strcmp (module_base_name, normalized_matched_name)); - g_free (priv->default_base_dir); - g_free (priv->new_func_name); - g_free (priv->free_func_name); - - priv->default_base_dir = NULL; - priv->new_func_name = NULL; - priv->free_func_name = NULL; + g_free (module_base_name); + g_free (normalized_matched_name); - G_OBJECT_CLASS (tomoe_module_parent_class)->dispose (object); + return matched; } /* Index: tomoe/lib/tomoe-module.h diff -u tomoe/lib/tomoe-module.h:1.2 tomoe/lib/tomoe-module.h:1.3 --- tomoe/lib/tomoe-module.h:1.2 Tue Nov 28 17:11:31 2006 +++ tomoe/lib/tomoe-module.h Wed Nov 29 00:07:02 2006 @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-module.h,v 1.2 2006/11/28 08:11:31 kous Exp $ + * $Id: tomoe-module.h,v 1.3 2006/11/28 15:07:02 kous Exp $ */ /** @@ -29,42 +29,22 @@ #define __TOMOE_MODULE_H__ #include <glib-object.h> +#include <gmodule.h> G_BEGIN_DECLS -#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 -{ - GObject object; -}; - -struct _TomoeModuleClass -{ - GObjectClass parent_class; -}; - -typedef gpointer (*TomoeModuleNewFunc) (void); -typedef void (*TomoeModuleFreeFunc) (gpointer context); - -GType tomoe_module_get_type (void) G_GNUC_CONST; - -gpointer tomoe_module_get_context (TomoeModule *module); -gboolean tomoe_module_find_module (TomoeModule *module, - const gchar *base_dir, - const gchar *name); -void tomoe_module_show_error (TomoeModule *module); -gboolean tomoe_module_load_func (TomoeModule *module, - const gchar *func_name, - gpointer *symbol); +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); G_END_DECLS Index: tomoe/lib/tomoe-recognizer-impl.h diff -u tomoe/lib/tomoe-recognizer-impl.h:1.11 tomoe/lib/tomoe-recognizer-impl.h:1.12 --- tomoe/lib/tomoe-recognizer-impl.h:1.11 Tue Nov 28 16:14:40 2006 +++ tomoe/lib/tomoe-recognizer-impl.h Wed Nov 29 00:07:02 2006 @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-recognizer-impl.h,v 1.11 2006/11/28 07:14:40 kous Exp $ + * $Id: tomoe-recognizer-impl.h,v 1.12 2006/11/28 15:07:02 kous Exp $ */ /** @@ -32,25 +32,20 @@ G_BEGIN_DECLS -#include "tomoe-dict.h" -#include "tomoe-writing.h" +#include "tomoe-recognizer.h" -typedef gpointer (*TomoeRecognizerNewFunc) (void); -typedef void (*TomoeRecognizerFreeFunc) (gpointer context); -typedef GList *(*TomoeRecognizerSearchFunc) (gpointer context, - TomoeDict *dict, - TomoeWriting *input); - -#define TOMOE_RECOGNIZER_IMPL_NEW tomoe_recognizer_impl_new -#define TOMOE_RECOGNIZER_IMPL_FREE tomoe_recognizer_impl_free -#define TOMOE_RECOGNIZER_IMPL_SEARCH tomoe_recognizer_impl_search - - -gpointer TOMOE_RECOGNIZER_IMPL_NEW (void); -void TOMOE_RECOGNIZER_IMPL_FREE (gpointer context); -GList *TOMOE_RECOGNIZER_IMPL_SEARCH (gpointer context, - TomoeDict *dict, - TomoeWriting *input); +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); +TomoeRecognizer *TOMOE_RECOGNIZER_IMPL_INSTANTIATE (void); G_END_DECLS Index: tomoe/lib/tomoe-recognizer-loader.c diff -u /dev/null tomoe/lib/tomoe-recognizer-loader.c:1.1 --- /dev/null Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-recognizer-loader.c Wed Nov 29 00:07:02 2006 @@ -0,0 +1,190 @@ +/* -*- 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.1 2006/11/28 15:07:02 kous 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_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; + 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 /dev/null tomoe/lib/tomoe-recognizer-loader.h:1.1 --- /dev/null Wed Nov 29 00:07:02 2006 +++ tomoe/lib/tomoe-recognizer-loader.h Wed Nov 29 00:07:02 2006 @@ -0,0 +1,48 @@ +/* -*- 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.15 tomoe/lib/tomoe-recognizer.c:1.16 --- tomoe/lib/tomoe-recognizer.c:1.15 Tue Nov 28 16:12:03 2006 +++ tomoe/lib/tomoe-recognizer.c Wed Nov 29 00:07:02 2006 @@ -1,3 +1,4 @@ + /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * Copyright (C) 2006 Kouhei Sutou <kou****@cozmi*****> @@ -17,16 +18,14 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-recognizer.c,v 1.15 2006/11/28 07:12:03 kous Exp $ + * $Id: tomoe-recognizer.c,v 1.16 2006/11/28 15:07:02 kous Exp $ */ #include <stdlib.h> -#include <gmodule.h> #include "tomoe-recognizer.h" -#include "tomoe-recognizer-impl.h" -G_DEFINE_TYPE (TomoeRecognizer, tomoe_recognizer, TOMOE_TYPE_MODULE) +G_DEFINE_ABSTRACT_TYPE (TomoeRecognizer, tomoe_recognizer, G_TYPE_OBJECT) static void tomoe_recognizer_class_init (TomoeRecognizerClass *klass) @@ -38,45 +37,19 @@ { } -TomoeRecognizer * -tomoe_recognizer_new (const gchar *base_dir, const gchar *name) -{ - TomoeRecognizer *recognizer; - - recognizer = g_object_new (TOMOE_TYPE_RECOGNIZER, - "default_base_dir", RECOGNIZERDIR, - "new_func_name", - G_STRINGIFY(TOMOE_RECOGNIZER_IMPL_NEW), - "free_func_name", - G_STRINGIFY(TOMOE_RECOGNIZER_IMPL_FREE), - NULL); - tomoe_module_find_module (TOMOE_MODULE (recognizer), base_dir, name); - return recognizer; -} - GList * -tomoe_recognizer_search (const TomoeRecognizer *recognizer, +tomoe_recognizer_search (TomoeRecognizer *recognizer, TomoeDict *dict, TomoeWriting *input) { - TomoeModule *module; - GList *result = NULL; - TomoeRecognizerSearchFunc search_func; - TomoeRecognizerSearchFunc *search_func_p; - gpointer *p; + TomoeRecognizerClass *klass; g_return_val_if_fail (TOMOE_IS_RECOGNIZER (recognizer), NULL); - module = TOMOE_MODULE (recognizer); - search_func_p = &search_func; - p = (gpointer *)search_func_p; - if (tomoe_module_load_func (module, - G_STRINGIFY(TOMOE_RECOGNIZER_IMPL_SEARCH), - p)) { - result = search_func (tomoe_module_get_context (module), dict, input); - } else { - tomoe_module_show_error (module); - } - return result; + klass = TOMOE_RECOGNIZER_GET_CLASS (recognizer); + if (klass->search) + return klass->search (recognizer, dict, input); + else + return NULL; } /* Index: tomoe/lib/tomoe-recognizer.h diff -u tomoe/lib/tomoe-recognizer.h:1.15 tomoe/lib/tomoe-recognizer.h:1.16 --- tomoe/lib/tomoe-recognizer.h:1.15 Tue Nov 28 16:12:03 2006 +++ tomoe/lib/tomoe-recognizer.h Wed Nov 29 00:07:02 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.15 2006/11/28 07:12:03 kous Exp $ + * $Id: tomoe-recognizer.h,v 1.16 2006/11/28 15:07:02 kous Exp $ */ /** @@ -32,7 +32,6 @@ G_BEGIN_DECLS -#include "tomoe-module.h" #include "tomoe-dict.h" #include "tomoe-writing.h" @@ -48,17 +47,19 @@ struct _TomoeRecognizer { - TomoeModule object; + GObject object; }; struct _TomoeRecognizerClass { - TomoeModuleClass parent_class; + GObjectClass parent_class; + + GList *(*search) (TomoeRecognizer *recognizer, + TomoeDict *dict, + TomoeWriting *input); }; GType tomoe_recognizer_get_type (void) G_GNUC_CONST; -TomoeRecognizer *tomoe_recognizer_new (const gchar *base_dir, - const gchar *name); /** * @brief Match strokes of tomoe_char with input. @@ -67,9 +68,9 @@ * @param input - Pointer to tomoe_glyph matchkey. * @return The array of TomoeCandidate. */ -GList *tomoe_recognizer_search (const TomoeRecognizer *recognizer, - TomoeDict *dict, - TomoeWriting *inputs); +GList *tomoe_recognizer_search (TomoeRecognizer *recognizer, + TomoeDict *dict, + TomoeWriting *input); G_END_DECLS Index: tomoe/lib/tomoe.c diff -u tomoe/lib/tomoe.c:1.45 tomoe/lib/tomoe.c:1.46 --- tomoe/lib/tomoe.c:1.45 Sat Nov 25 16:01:29 2006 +++ tomoe/lib/tomoe.c Wed Nov 29 00:07:02 2006 @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * Copyright (C) 2000 - 2004 Hiroyuki Komatsu <komat****@taiya*****> * Copyright (C) 2004 Hiroaki Nakamura <hnaka****@good-*****> @@ -20,7 +20,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe.c,v 1.45 2006/11/25 07:01:29 makeinu Exp $ + * $Id: tomoe.c,v 1.46 2006/11/28 15:07:02 kous Exp $ */ #include <stdio.h> @@ -28,29 +28,36 @@ #include <string.h> #include <glib/garray.h> #include "tomoe.h" -#include "tomoe-dict.h" -#include "tomoe-context.h" -#include "tomoe-config.h" -/* - * Initialize tomoe +static gboolean initialized = FALSE; + +/* + * Initialize tomoe */ void tomoe_init (void) { - static gboolean initialized = FALSE; - if (!initialized) { + 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); } } /* finalize tomoe */ -void +void tomoe_quit (void) { - /* TODO unfreed tomoe objects check */ + if (initialized) { + initialized = FALSE; +#warning FIXME: those causes memory warnings +#if 0 + tomoe_dict_loader_unload (); + tomoe_recognizer_loader_unload (); +#endif + } } /* Index: tomoe/lib/tomoe.h diff -u tomoe/lib/tomoe.h:1.32 tomoe/lib/tomoe.h:1.33 --- tomoe/lib/tomoe.h:1.32 Fri Nov 24 14:49:51 2006 +++ tomoe/lib/tomoe.h Wed Nov 29 00:07:02 2006 @@ -20,7 +20,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe.h,v 1.32 2006/11/24 05:49:51 kous Exp $ + * $Id: tomoe.h,v 1.33 2006/11/28 15:07:02 kous Exp $ */ /** @file tomoe.h @@ -40,6 +40,9 @@ #include "tomoe-recognizer.h" #include "tomoe-writing.h" +#include "tomoe-dict-loader.h" +#include "tomoe-recognizer-loader.h" + /** * @brief Initialize tomoe library. */