~ubuntu-branches/ubuntu/lucid/mozvoikko/lucid-security

« back to all changes in this revision

Viewing changes to src/mozVoikko.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Heikki Mäntysaari
  • Date: 2008-08-28 20:26:55 UTC
  • Revision ID: james.westby@ubuntu.com-20080828202655-d32fy59yjger72f1
Tags: upstream-0.9.5
Import upstream version 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 *
 
4
 * Copyright (C) 2007 Andris Pavenis <andris.pavenis@iki.fi>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 *
 
20
 * ***** END OF LICENSE BLOCK ***** */
 
21
 
 
22
#include "mozVoikko.hxx"
 
23
#ifdef SYSTEM_LIBVOIKKO
 
24
#include <libvoikko/voikko.h>
 
25
#else
 
26
#include "voikko-dist.h"
 
27
#endif
 
28
#include "mozVoikkoSpell.hxx"
 
29
#include "mozVoikkoUtils.hxx"
 
30
 
 
31
#include <nsStringAPI.h>
 
32
#include <nsServiceManagerUtils.h>
 
33
#include <nsICategoryManager.h>
 
34
#include <nsISimpleEnumerator.h>
 
35
#include <nsIConsoleService.h>
 
36
#include <nsILocalFile.h>
 
37
#include <prlock.h>
 
38
 
 
39
#include <ctype.h>
 
40
#include <stdarg.h>
 
41
#include <stdlib.h>
 
42
#include <string.h>
 
43
 
 
44
namespace
 
45
{
 
46
 
 
47
    const char *encoding = "UTF-8";
 
48
 
 
49
    typedef const char * (*initvoikko_with_path_t)(int*, const char *, int, const char *);
 
50
 
 
51
    typedef const char * (*initvoikko_t)(int *, const char *, int);
 
52
 
 
53
    typedef int (*setsopt_t)(int, int, const char *);
 
54
 
 
55
    typedef int (*setbopt_t)(int, int, int);
 
56
 
 
57
    typedef int (*spell_t)(int, const char *);
 
58
 
 
59
    typedef char ** (*suggest_t)(int, const char *);
 
60
 
 
61
    typedef int (*terminate_voikko_t)(int);
 
62
        
 
63
    typedef void (*free_suggest_cstr_t)(char **);
 
64
 
 
65
    // Only one user can use voikko at the time, so we have only one copy and locking
 
66
    PRLock *voikko_lock = NULL;
 
67
 
 
68
    bool        libvoikko_init_tried = false;
 
69
 
 
70
    bool        libvoikko_initialized = false;
 
71
 
 
72
    PRLibrary *voikko_lib = NULL;
 
73
 
 
74
    int         voikko_users = 0;
 
75
 
 
76
    int         voikkohandle;
 
77
 
 
78
    // The initialization function of the loaded library
 
79
    initvoikko_with_path_t      init_func_;
 
80
 
 
81
    // The initialization function without explicit dictionary path
 
82
    initvoikko_t        init_system_func_;
 
83
 
 
84
    // The termination function of the loaded library
 
85
    terminate_voikko_t  terminate_func_;
 
86
 
 
87
    // The spell checking function of the loaded library
 
88
    spell_t     check_func_;
 
89
 
 
90
    // The suggestion generation function of the loaded library
 
91
    suggest_t   suggest_func_;
 
92
 
 
93
    // The procedure for releasing memory allocated by voikko_suggest_cstr().
 
94
    free_suggest_cstr_t free_suggest_cstr_func_;
 
95
 
 
96
    // The option setting functions of the loaded library
 
97
    setsopt_t   string_option_func_;
 
98
    setbopt_t   boolean_option_func_;
 
99
 
 
100
    // Preloaded libraries
 
101
    PreloadedLibraries *preloadedLibraries = NULL;
 
102
 
 
103
    struct VoikkoLock 
 
104
    {
 
105
        VoikkoLock(void)
 
106
        {
 
107
            PR_Lock(voikko_lock);
 
108
        }
 
109
 
 
110
        ~VoikkoLock()
 
111
        {
 
112
            PR_Unlock(voikko_lock);
 
113
        }
 
114
    };
 
115
 
 
116
    struct VoikkoPluginInit
 
117
    {
 
118
        VoikkoPluginInit(void)
 
119
        {
 
120
 
 
121
            voikko_lock = PR_NewLock();
 
122
            voikko_users = 0;
 
123
        }
 
124
 
 
125
        ~VoikkoPluginInit()
 
126
        {
 
127
            if (libvoikko_initialized)
 
128
            {
 
129
                (*terminate_func_)(voikkohandle);
 
130
            }
 
131
            else if (voikko_lib)
 
132
            {
 
133
                PR_UnloadLibrary (voikko_lib);
 
134
            }
 
135
 
 
136
            if (preloadedLibraries)
 
137
            {
 
138
                delete preloadedLibraries;
 
139
            }
 
140
 
 
141
            PR_DestroyLock(voikko_lock);
 
142
        }
 
143
    };
 
144
 
 
145
    VoikkoPluginInit voikko_init_0_;
 
146
 
 
147
    bool LibvoikkoInit(void)
 
148
    {
 
149
        VoikkoLock lock;
 
150
        if (libvoikko_init_tried)
 
151
            return libvoikko_initialized;
 
152
 
 
153
        libvoikko_init_tried = true;
 
154
 
 
155
        #ifndef SYSTEM_LIBVOIKKO
 
156
        nsresult rv;
 
157
        nsCOMPtr<nsIFile> libDir, dataDir;
 
158
        rv = getMozVoikkoBaseDirs(getter_AddRefs(libDir), getter_AddRefs(dataDir));
 
159
 
 
160
        if (NS_FAILED(rv))
 
161
        {
 
162
            logMessage("Failed to get directories where Voikko libraries and data are installed");
 
163
            return false;
 
164
        }
 
165
 
 
166
        nsCString libDirPath, dataDirPath;
 
167
        libDir->GetNativePath(libDirPath);
 
168
        dataDir->GetNativePath(dataDirPath);
 
169
 
 
170
        rv = dataDir->GetNativePath(dataDirPath);
 
171
        NS_ENSURE_SUCCESS(rv, false);
 
172
 
 
173
        logMessage("Using Voikko data directory %s", dataDirPath.get());
 
174
 
 
175
        preloadedLibraries = new PreloadedLibraries(libDir, preloadLibNames, numPreloadedLibs);
 
176
        if (!preloadedLibraries)
 
177
            return false;
 
178
 
 
179
        if (!*preloadedLibraries)
 
180
        {
 
181
            return false;
 
182
        }
 
183
        #endif
 
184
 
 
185
        voikko_lib = PR_LoadLibrary(libvoikkoName);
 
186
 
 
187
        if (voikko_lib == 0) {
 
188
            logMessage("%s is not available: %s", libvoikkoName, prGetErrorText().get());
 
189
            return false;
 
190
        }
 
191
 
 
192
        bool symbols_ok = LoadSymbol(voikko_lib, &init_func_, "voikko_init_with_path")
 
193
            && LoadSymbol(voikko_lib, &init_system_func_, "voikko_init")
 
194
            && LoadSymbol(voikko_lib, &terminate_func_, "voikko_terminate")
 
195
            && LoadSymbol(voikko_lib, &check_func_, "voikko_spell_cstr")
 
196
            && LoadSymbol(voikko_lib, &suggest_func_, "voikko_suggest_cstr")
 
197
            && LoadSymbol(voikko_lib, &string_option_func_, "voikko_set_string_option")
 
198
            && LoadSymbol(voikko_lib, &boolean_option_func_, "voikko_set_bool_option")
 
199
            && LoadSymbol(voikko_lib, &free_suggest_cstr_func_, "voikko_free_suggest_cstr");
 
200
 
 
201
        if (!symbols_ok) {
 
202
            logMessage("Failed to find at least one required symbol in %s.", libvoikkoName);
 
203
            PR_UnloadLibrary (voikko_lib);
 
204
            voikko_lib = NULL;
 
205
            return false;
 
206
        }
 
207
 
 
208
        #ifdef SYSTEM_LIBVOIKKO
 
209
        const char* error = init_system_func_(&voikkohandle, "fi_FI", 0);
 
210
        #else
 
211
        const char* error = init_func_(&voikkohandle, "fi_FI", 0, dataDirPath.get());
 
212
        #endif
 
213
        if (error != 0) {
 
214
            logMessage("Failed to initialize libvoikko: %s.", error);
 
215
            PR_UnloadLibrary (voikko_lib);
 
216
            voikko_lib = NULL;
 
217
            return false;
 
218
        }
 
219
 
 
220
        boolean_option_func_(voikkohandle, VOIKKO_OPT_IGNORE_DOT, 1);
 
221
        boolean_option_func_(voikkohandle, VOIKKO_OPT_IGNORE_NUMBERS, 1);
 
222
        boolean_option_func_(voikkohandle, VOIKKO_OPT_IGNORE_UPPERCASE, 1);
 
223
        #ifdef VOIKKO_OPT_ACCEPT_MISSING_HYPHENS
 
224
        boolean_option_func_(voikkohandle, VOIKKO_OPT_ACCEPT_MISSING_HYPHENS, 1);
 
225
        #endif
 
226
 
 
227
        int status = string_option_func_(voikkohandle, VOIKKO_OPT_ENCODING, encoding);
 
228
        if (!status) {
 
229
            logMessage("Failed to set encoding %s for libvoikko.", encoding);
 
230
            return false;
 
231
        }
 
232
        
 
233
        logMessage("%s is successfully initialized.", libvoikkoName);
 
234
 
 
235
        libvoikko_initialized = true;
 
236
        return true;
 
237
    }
 
238
 
 
239
    int check (const char * str)
 
240
    {
 
241
        int status;
 
242
        VoikkoLock lock;
 
243
        // Always return spellcheck failure if not initialized properly (should never happen...).
 
244
        if (!libvoikko_initialized) return 0;
 
245
 
 
246
        status = (*check_func_)(voikkohandle, str);
 
247
        return status ? 1 : 0;
 
248
    }
 
249
 
 
250
    char **get_suggestions (const char * word)
 
251
    {
 
252
        char **vsuggestions = NULL;
 
253
        VoikkoLock lock;
 
254
        if (!libvoikko_initialized) return NULL;
 
255
        vsuggestions = (*suggest_func_) (voikkohandle, word);
 
256
        return vsuggestions;
 
257
    }
 
258
}
 
259
 
 
260
MozVoikko::MozVoikko (void)
 
261
    : is_ok (false)
 
262
{
 
263
    if (LibvoikkoInit()) {
 
264
        is_ok = true;
 
265
    }
 
266
 
 
267
    initialized_ = true;
 
268
}
 
269
 
 
270
MozVoikko::~MozVoikko()
 
271
{
 
272
}
 
273
 
 
274
int MozVoikko::suggest (char *** slst, const char * word)
 
275
{
 
276
    int ns = 0;
 
277
  
 
278
    *slst = get_suggestions(word);
 
279
    if (*slst != NULL) 
 
280
    {
 
281
        while ((*slst)[ns] != NULL) ns++;
 
282
    }
 
283
  
 
284
    return ns;
 
285
}
 
286
 
 
287
void MozVoikko::freeSuggestions(char **slst)
 
288
{
 
289
    free_suggest_cstr_func_(slst);
 
290
}
 
291
 
 
292
int MozVoikko::spell (const char * str)
 
293
{
 
294
    int status;
 
295
 
 
296
    // Always return spellcheck failure if not initialized properly.
 
297
    if (!is_ok) return 0;
 
298
    status = check(str);
 
299
    return status ? 1 : 0;
 
300
}
 
301
 
 
302
char * MozVoikko::get_dic_encoding()
 
303
{
 
304
    return (char*) encoding;
 
305
}