~ubuntu-branches/ubuntu/precise/uim/precise

« back to all changes in this revision

Viewing changes to uim/uim-eb.c

  • Committer: Package Import Robot
  • Author(s): Ilya Barygin
  • Date: 2011-12-18 16:35:38 UTC
  • mfrom: (1.1.13) (15.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20111218163538-8ktir39z2mjpii8z
Tags: 1:1.7.1-3ubuntu1
* Merge from Debian testing (LP: #818199).
* Remaining changes:
  - debian/uim-qt.install: Fix plugin path for multiarch location.
* Dropped changes:
  - uim-applet-gnome removal (GNOME 3 applet is available)
  - 19_as-needed_compile_fix.dpatch (accepted into Debian package)
* translations.patch: add several files to POTFILE.in to prevent
  intltool-update failure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
  Copyright (c) 2003-2011 uim Project http://code.google.com/p/uim/
 
4
 
 
5
  All rights reserved.
 
6
 
 
7
  Redistribution and use in source and binary forms, with or without
 
8
  modification, are permitted provided that the following conditions
 
9
  are met:
 
10
 
 
11
  1. Redistributions of source code must retain the above copyright
 
12
     notice, this list of conditions and the following disclaimer.
 
13
  2. Redistributions in binary form must reproduce the above copyright
 
14
     notice, this list of conditions and the following disclaimer in the
 
15
     documentation and/or other materials provided with the distribution.
 
16
  3. Neither the name of authors nor the names of its contributors
 
17
     may be used to endorse or promote products derived from this software
 
18
     without specific prior written permission.
 
19
 
 
20
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
 
21
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
22
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
23
  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
 
24
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
25
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
26
  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
27
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
28
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
29
  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
30
  SUCH DAMAGE.
 
31
 
 
32
*/
 
33
 
 
34
/* FIXME! This is a ad-hoc solution to advance
 
35
   annotation related discussion. */
 
36
 
 
37
#include <config.h>
 
38
 
 
39
#include <stdio.h>
 
40
#include <stdlib.h>
 
41
#include <string.h>
 
42
#include <sys/types.h>
 
43
#include <iconv.h>
 
44
#include <gettext.h>
 
45
#include <eb/eb.h>
 
46
#include <eb/text.h>
 
47
#include <eb/font.h>
 
48
#include <eb/binary.h>
 
49
#include <eb/error.h>
 
50
 
 
51
#include "uim.h"
 
52
#include "uim-util.h"
 
53
#include "uim-notify.h"
 
54
 
 
55
#include "uim-eb.h"
 
56
 
 
57
 
 
58
#define MAX_HITS   10
 
59
#define MAX_TEXT   1000
 
60
#define MAX_LENGTH 10000
 
61
 
 
62
struct _uim_eb {
 
63
  EB_Book         book;
 
64
  EB_Subbook_Code subCodes[EB_MAX_SUBBOOKS];
 
65
  int             subCount;
 
66
};
 
67
 
 
68
static void go_text_eb (uim_eb *ueb,
 
69
                        EB_Position position,
 
70
                        char **str,
 
71
                        const char *enc);
 
72
 
 
73
static int
 
74
uim_eb_strappend(char **dest, const char *append, size_t append_len)
 
75
{
 
76
  if (*dest) {
 
77
    char *str;
 
78
    size_t dest_len = strlen(*dest);
 
79
    size_t len = dest_len + append_len;
 
80
 
 
81
    str = uim_realloc(*dest, len + 1);
 
82
    memcpy(&str[dest_len], append, append_len);
 
83
    str[len] = '\0';
 
84
    *dest = str;
 
85
  } else {
 
86
    char *str;
 
87
 
 
88
    str = uim_malloc(append_len + 1);
 
89
    memcpy(str, append, append_len);
 
90
    str[append_len] = '\0';
 
91
    *dest = str;
 
92
  }
 
93
  return 1;
 
94
}
 
95
 
 
96
void
 
97
uim_eb_open ()
 
98
{
 
99
  EB_Error_Code err;
 
100
 
 
101
  err = eb_initialize_library();
 
102
  if (err != EB_SUCCESS)
 
103
    uim_notify_fatal(_("eb: failed to initialize EB library : error = %s\n"),
 
104
             eb_error_message(err));
 
105
}
 
106
 
 
107
void
 
108
uim_eb_close (void)
 
109
{
 
110
  eb_finalize_library();
 
111
}
 
112
 
 
113
uim_eb *
 
114
uim_eb_new (const char *bookpath)
 
115
{
 
116
  uim_eb *ueb;
 
117
  EB_Error_Code err;
 
118
 
 
119
  ueb = uim_malloc(sizeof(uim_eb));
 
120
 
 
121
  eb_initialize_book(&ueb->book);
 
122
 
 
123
  err = eb_bind(&ueb->book, bookpath);
 
124
  if (err != EB_SUCCESS) {
 
125
    uim_notify_fatal(N_("eb: wrong bookpath"));
 
126
    free(ueb);
 
127
    return NULL;
 
128
  }
 
129
 
 
130
  err = eb_subbook_list(&ueb->book, ueb->subCodes, &ueb->subCount);
 
131
  if (err != EB_SUCCESS) {
 
132
    uim_notify_fatal(N_("eb: eb_subbook_list() failed\n"));
 
133
    free(ueb);
 
134
    return NULL;
 
135
  }
 
136
 
 
137
  return ueb;
 
138
}
 
139
 
 
140
void
 
141
uim_eb_destroy (uim_eb *ueb)
 
142
{
 
143
  if (ueb)
 
144
    eb_finalize_book(&ueb->book);
 
145
 
 
146
  free(ueb);
 
147
  ueb = NULL;
 
148
}
 
149
 
 
150
 
 
151
char *
 
152
uim_eb_search_text (uim_eb *ueb, const char *key, const char *enc)
 
153
{
 
154
  char *text;
 
155
  int i;
 
156
  char *str = NULL;
 
157
  iconv_t cd;
 
158
 
 
159
  /* FIXME! check return value */
 
160
 
 
161
  cd = (iconv_t)uim_iconv->create("EUC-JP", enc);
 
162
  text = uim_iconv->convert(cd, key);
 
163
  uim_iconv->release(cd);
 
164
 
 
165
  if (!text)
 
166
          return NULL;
 
167
 
 
168
  for (i = 0; i < ueb->subCount; i++) {
 
169
    EB_Hit hits[MAX_HITS];
 
170
    int hitCount;
 
171
    int j;
 
172
 
 
173
    /* specify subbook */
 
174
    if (eb_set_subbook(&ueb->book, ueb->subCodes[i]) != EB_SUCCESS) {
 
175
      uim_notify_fatal(N_("eb: eb_set_subbook() failed")); continue;
 
176
    }
 
177
 
 
178
    eb_search_word(&ueb->book, text);
 
179
    eb_hit_list(&ueb->book, MAX_HITS, hits, &hitCount);
 
180
    for (j = 0; j < hitCount; j++) {
 
181
      /*EB_Position headp = hits[j].heading;*/
 
182
      EB_Position textp = hits[j].text;
 
183
 
 
184
      go_text_eb(ueb, textp, &str, enc);
 
185
      uim_eb_strappend(&str, "\n", sizeof("\n"));
 
186
    }
 
187
  }
 
188
 
 
189
  free(text);
 
190
 
 
191
  return str;
 
192
}
 
193
 
 
194
 
 
195
static void
 
196
go_text_eb (uim_eb *ueb, EB_Position position, char **str, const char *enc)
 
197
{
 
198
  EB_Hookset hookset;
 
199
  char text[MAX_TEXT + 1];
 
200
  ssize_t text_length;
 
201
  ssize_t bytes;
 
202
  int i;
 
203
 
 
204
  if (eb_seek_text(&ueb->book, &position) != EB_SUCCESS) {
 
205
    uim_notify_fatal(N_("eb: eb_seek_text error occurs"));
 
206
    return;
 
207
  }
 
208
 
 
209
  eb_initialize_hookset(&hookset);
 
210
  for (i = 0; i < 1; i++) {
 
211
    char *local;
 
212
    iconv_t cd;
 
213
 
 
214
    if (eb_read_text(&ueb->book, NULL, &hookset,
 
215
                     NULL, MAX_TEXT, text, &text_length) != EB_SUCCESS) {
 
216
      bytes = 0;
 
217
      uim_notify_fatal(N_("eb_read_text : an error occurs"));
 
218
      return;
 
219
    }
 
220
 
 
221
    bytes += text_length;
 
222
    if (text_length < 1)
 
223
      break;
 
224
 
 
225
    /* FIXME! check return value */
 
226
    cd = (iconv_t)uim_iconv->create(enc, "EUC-JP");
 
227
    local = uim_iconv->convert(cd, text);
 
228
    uim_iconv->release(cd);
 
229
 
 
230
    uim_eb_strappend(str, local, strlen(local));
 
231
 
 
232
    free(local);
 
233
  }
 
234
  eb_finalize_hookset(&hookset);
 
235
}