1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003 Dom Lachowicz, Raphael Finkel
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.1 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 Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, Dom Lachowicz
21 * gives permission to link the code of this program with
22 * non-LGPL Spelling Provider libraries (eg: a MSFT Office
23 * spell checker backend) and distribute linked combinations including
24 * the two. You must obey the GNU Lesser General Public License in all
25 * respects for all of the code used other than said providers. If you modify
26 * this file, you may extend this exception to your version of the
27 * file, but you are not obligated to do so. If you do not wish to
28 * do so, delete this exception statement from your version.
42 #include "enchant-provider.h"
44 #include <uspell/utf8convert.h>
45 #include <uspell/uniprops.h>
46 #include <uspell/uspell.h>
48 ENCHANT_PLUGIN_DECLARE("Uspell")
50 static const size_t MAXALTERNATIVE = 20; // we won't return more than this number of suggestions
51 static const size_t MAXCHARS = 100; // maximum number of bytes of utf8 or chars of UCS4 in a word
54 uspell_checker_get_dictionary_dirs (EnchantBroker * broker)
59 GSList *config_dirs, *iter;
61 config_dirs = enchant_get_user_config_dirs ();
63 for (iter = config_dirs; iter; iter = iter->next)
65 dirs = g_slist_append (dirs, g_build_filename ((const gchar *)iter->data,
69 g_slist_foreach (config_dirs, (GFunc)g_free, NULL);
70 g_slist_free (config_dirs);
74 const gchar* const * system_data_dirs = g_get_system_data_dirs ();
75 const gchar* const * iter;
77 for (iter = system_data_dirs; *iter; iter++)
79 dirs = g_slist_append (dirs, g_build_filename (*iter, "uspell", "dicts", NULL));
83 /* until I work out how to link the modules against enchant in MacOSX - fjf
85 #ifndef XP_TARGET_COCOA
86 char * uspell_prefix = NULL;
88 /* Look for explicitly set registry values */
89 uspell_prefix = enchant_get_registry_value ("Uspell", "Data_Dir");
91 dirs = g_slist_append (dirs, uspell_prefix);
93 /* Dynamically locate library and search for modules relative to it. */
94 char * enchant_prefix = enchant_get_prefix_dir();
97 uspell_prefix = g_build_filename(enchant_prefix, "share", "enchant", "uspell", NULL);
98 g_free(enchant_prefix);
99 dirs = g_slist_append (dirs, uspell_prefix);
103 #ifdef ENCHANT_USPELL_DICT_DIR
104 dirs = g_slist_append (dirs, g_strdup (ENCHANT_USPELL_DICT_DIR));
108 GSList *config_dirs, *iter;
110 config_dirs = enchant_get_dirs_from_param (broker, "enchant.uspell.dictionary.path");
112 for (iter = config_dirs; iter; iter = iter->next)
114 dirs = g_slist_append (dirs, g_strdup ((const gchar *)iter->data));
117 g_slist_foreach (config_dirs, (GFunc)g_free, NULL);
118 g_slist_free (config_dirs);
125 uspell_dict_check (EnchantDict * me, const char *const word, size_t len)
128 wide_t buf1[MAXCHARS], buf2[MAXCHARS], *curBuf, *otherBuf, *tmpBuf;
129 utf8_t myWord[MAXCHARS];
133 return 1; // too long; can't be right
134 memcpy(reinterpret_cast<char *>(myWord), word, len);
138 manager = reinterpret_cast<uSpell *>(me->user_data);
140 length = utf8_wide(curBuf, myWord, MAXCHARS);
141 if (manager->isSpelledRight(curBuf, length)) {
142 return 0; // correct the first time
144 if (manager->theFlags & uSpell::upperLower) {
145 toUpper(otherBuf, curBuf, length);
146 if (manager->isSpelledRight(otherBuf, length)) {
147 manager->acceptWord(myWord);
148 return 0; // correct if converted to all upper case
154 if (manager->theFlags & uSpell::hasComposition) {
155 unPrecompose(otherBuf, &length, curBuf, length);
156 if (manager->isSpelledRight(otherBuf, length)) {
157 manager->acceptWord(myWord);
158 return 0; // correct if precomposed characters expanded, all upper
164 if (manager->theFlags & uSpell::hasCompounds) {
165 if (manager->isSpelledRightMultiple(curBuf, length)) {
166 manager->acceptWord(myWord);
167 return 0; // correct as two words. Not right for all languages.
174 uspell_dict_suggest (EnchantDict * me, const char *const word,
175 size_t len, size_t * out_n_suggs)
178 utf8_t myWord[MAXCHARS];
180 char **sugg_arr = NULL;
182 wide_t buf[MAXCHARS];
187 if (len >= MAXCHARS) // no suggestions; the word is outlandish
189 memcpy(reinterpret_cast<char *>(myWord), word, len);
191 manager = reinterpret_cast<uSpell *>(me->user_data);
193 list = reinterpret_cast<utf8_t **>(
194 calloc(sizeof(char *), MAXALTERNATIVE));
195 length = utf8_wide(buf, myWord, MAXCHARS);
196 *out_n_suggs = manager->showAlternatives(buf, length,
197 list, MAXALTERNATIVE);
201 sugg_arr = g_new0 (char *, *out_n_suggs + 1);
202 for (i = 0; i < *out_n_suggs; i++)
207 g_strdup (reinterpret_cast<const gchar *>(sugg));
213 } // uspell_dict_suggest
216 uspell_dict_add_to_session (EnchantDict * me, const char *const word,
220 wide_t buf[MAXCHARS];
221 utf8_t myWord[MAXCHARS];
224 manager = reinterpret_cast<uSpell *>(me->user_data);
226 manager->acceptWord((const utf8_t *)word);
228 return; // too long; can't reasonably convert
229 // see if we want to acceptWord(uppercase(myWord))
230 if (!(manager->theFlags & uSpell::upperLower)) return; // non-case language
231 length = utf8_wide(buf, (const utf8_t *)word, MAXCHARS);
232 for (index = 0; index < length; index++) {
233 if (g_unichar_isupper(buf[index])) return; // case-sensitive word
234 buf[index] = g_unichar_toupper(buf[index]);
236 wide_utf8(myWord, MAXCHARS, buf, length);
237 manager->acceptWord(myWord);
238 } // uspell_dict_add_to_session
241 const char * language_tag;
242 const char * corresponding_uspell_file_name;
246 static const Mapping mapping [] = {
248 {"he_IL", "hebrew", 0},
249 {"yi", "yiddish", uSpell::hasComposition},
250 {"en_US", "american", uSpell::upperLower},
253 static const size_t n_mappings = (sizeof(mapping)/sizeof(mapping[0]));
256 s_buildHashNames (std::vector<std::string> & names, EnchantBroker * broker, const char * tag)
262 for (mapIndex = 0; mapIndex < n_mappings; mapIndex++) {
263 if (!strcmp(tag, mapping[mapIndex].language_tag))
267 if (mapIndex < n_mappings) {
270 char * dict = g_strdup_printf ("%s.uspell.dat",
271 mapping[mapIndex].corresponding_uspell_file_name);
273 dirs = uspell_checker_get_dictionary_dirs (broker);
275 for (iter = dirs; iter; iter = iter->next)
279 tmp = g_build_filename ((const char *)iter->data, dict, NULL);
280 names.push_back (tmp);
284 g_slist_foreach (dirs, (GFunc)g_free, NULL);
290 uspell_request_dict (const char * base, const char * mapping, const int flags)
292 char *fileName, *transName, *filePart, *transPart;
299 filePart = g_strconcat(mapping, ".uspell.dat", NULL);
300 transPart = g_strconcat(mapping, ".uspell.trans", NULL);
301 fileName = g_build_filename (base, filePart, NULL);
302 transName = g_build_filename (base, transPart, NULL);
307 manager = new uSpell(fileName, transName, flags);
320 uspell_request_manager (const char * dir, size_t mapIndex)
322 uSpell * manager = NULL;
324 manager = uspell_request_dict (dir,
325 mapping[mapIndex].corresponding_uspell_file_name,
326 mapping[mapIndex].language_flags);
328 if (!manager) return NULL;
330 // look for a supplementary private dictionary
331 const char *config_dir = g_get_user_config_dir();
333 gchar * auxFileName, * transPart;
334 transPart = g_strconcat (mapping[mapIndex].language_tag, ".dic", NULL);
335 auxFileName = g_build_filename (config_dir, transPart, NULL);
338 (void) manager->assimilateFile (auxFileName);
339 g_free (auxFileName);
347 ENCHANT_MODULE_EXPORT (EnchantProvider *)
348 init_enchant_provider (void);
351 uspell_provider_request_dict (EnchantProvider * me, const char *const tag)
353 EnchantDict *dict = NULL;
354 uSpell *manager = NULL;
360 for (mapIndex = 0; mapIndex < n_mappings && !found; mapIndex++) {
361 if (!strcmp(tag, mapping[mapIndex].language_tag))
368 dirs = uspell_checker_get_dictionary_dirs (me->owner);
370 for (iter = dirs; iter && !manager; iter = iter->next)
372 manager = uspell_request_manager ((const char *)iter->data, mapIndex);
375 g_slist_foreach (dirs, (GFunc)g_free, NULL);
381 dict = g_new0 (EnchantDict, 1);
382 dict->user_data = manager;
383 dict->check = uspell_dict_check;
384 dict->suggest = uspell_dict_suggest;
385 dict->add_to_session = uspell_dict_add_to_session;
386 // don't use personal, session - let higher level implement that
392 uspell_provider_dictionary_exists (struct str_enchant_provider * me,
393 const char *const tag)
395 std::vector <std::string> names;
397 s_buildHashNames (names, me->owner, tag);
398 for (size_t i = 0; i < names.size(); i++) {
399 if (g_file_test (names[i].c_str(), G_FILE_TEST_EXISTS))
407 uspell_provider_dispose_dict (EnchantProvider * me, EnchantDict * dict)
409 uSpell *manager = reinterpret_cast<uSpell *>(dict->user_data);
416 uspell_provider_list_dictionaries (EnchantProvider * me,
417 size_t * out_n_dicts)
422 for (i = 0; i < n_mappings; i++)
423 if (uspell_provider_dictionary_exists (me, mapping[i].language_tag))
430 char ** out_dicts = g_new0 (char *, nb + 1);
431 for (i = 0; i < n_mappings; i++)
432 if (uspell_provider_dictionary_exists (me, mapping[i].language_tag))
433 out_dicts[i] = g_strdup (mapping[i].language_tag);
439 uspell_provider_free_string_list (EnchantProvider * me, char **str_list)
441 g_strfreev (str_list);
445 uspell_provider_dispose (EnchantProvider * me)
451 uspell_provider_identify (EnchantProvider * me)
457 uspell_provider_describe (EnchantProvider * me)
459 return "Uspell Provider";
463 init_enchant_provider (void)
465 EnchantProvider *provider;
467 provider = g_new0 (EnchantProvider, 1);
468 provider->dispose = uspell_provider_dispose;
469 provider->request_dict = uspell_provider_request_dict;
470 provider->dispose_dict = uspell_provider_dispose_dict;
471 provider->dictionary_exists = uspell_provider_dictionary_exists;
472 provider->identify = uspell_provider_identify;
473 provider->describe = uspell_provider_describe;
474 provider->list_dicts = uspell_provider_list_dictionaries;
475 provider->free_string_list = uspell_provider_free_string_list;