1 /*
2 
3   Copyright (c) 2003-2013 uim Project https://github.com/uim/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 #include <config.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <iconv.h>
41 #include <gettext.h>
42 #include <eb/eb.h>
43 #include <eb/text.h>
44 #include <eb/font.h>
45 #include <eb/binary.h>
46 #include <eb/error.h>
47 
48 #include "uim.h"
49 #include "uim-util.h"
50 #include "uim-notify.h"
51 
52 #include "uim-eb.h"
53 
54 
55 #define MAX_HITS   10
56 #define MAX_TEXT   1000
57 #define MAX_LENGTH 10000
58 
59 struct _uim_eb {
60   EB_Book         book;
61   EB_Subbook_Code subCodes[EB_MAX_SUBBOOKS];
62   int             subCount;
63 };
64 
65 static void go_text_eb (uim_eb *ueb,
66 			EB_Position position,
67 			char **str,
68 			const char *enc);
69 
70 static int
uim_eb_strappend(char ** dest,const char * append,size_t append_len)71 uim_eb_strappend(char **dest, const char *append, size_t append_len)
72 {
73   if (*dest) {
74     char *str;
75     size_t dest_len = strlen(*dest);
76     size_t len = dest_len + append_len;
77 
78     str = uim_realloc(*dest, len + 1);
79     memcpy(&str[dest_len], append, append_len);
80     str[len] = '\0';
81     *dest = str;
82   } else {
83     char *str;
84 
85     str = uim_malloc(append_len + 1);
86     memcpy(str, append, append_len);
87     str[append_len] = '\0';
88     *dest = str;
89   }
90   return 1;
91 }
92 
93 void
uim_eb_open()94 uim_eb_open ()
95 {
96   EB_Error_Code err;
97 
98   err = eb_initialize_library();
99   if (err != EB_SUCCESS)
100     uim_notify_fatal(_("eb: failed to initialize EB library : error = %s\n"),
101 	     eb_error_message(err));
102 }
103 
104 void
uim_eb_close(void)105 uim_eb_close (void)
106 {
107   eb_finalize_library();
108 }
109 
110 uim_eb *
uim_eb_new(const char * bookpath)111 uim_eb_new (const char *bookpath)
112 {
113   uim_eb *ueb;
114   EB_Error_Code err;
115 
116   ueb = uim_malloc(sizeof(uim_eb));
117 
118   eb_initialize_book(&ueb->book);
119 
120   err = eb_bind(&ueb->book, bookpath);
121   if (err != EB_SUCCESS) {
122     uim_notify_fatal(N_("eb: wrong bookpath"));
123     free(ueb);
124     return NULL;
125   }
126 
127   err = eb_subbook_list(&ueb->book, ueb->subCodes, &ueb->subCount);
128   if (err != EB_SUCCESS) {
129     uim_notify_fatal(N_("eb: eb_subbook_list() failed\n"));
130     free(ueb);
131     return NULL;
132   }
133 
134   return ueb;
135 }
136 
137 void
uim_eb_destroy(uim_eb * ueb)138 uim_eb_destroy (uim_eb *ueb)
139 {
140   if (ueb)
141     eb_finalize_book(&ueb->book);
142 
143   free(ueb);
144   ueb = NULL;
145 }
146 
147 
148 char *
uim_eb_search_text(uim_eb * ueb,const char * key,const char * enc)149 uim_eb_search_text (uim_eb *ueb, const char *key, const char *enc)
150 {
151   char *text;
152   int i;
153   char *str = NULL;
154   iconv_t cd;
155 
156   /* FIXME! check return value */
157 
158   cd = (iconv_t)uim_iconv->create("EUC-JP", enc);
159   text = uim_iconv->convert(cd, key);
160   uim_iconv->release(cd);
161 
162   if (!text)
163 	  return NULL;
164 
165   for (i = 0; i < ueb->subCount; i++) {
166     EB_Hit hits[MAX_HITS];
167     int hitCount;
168     int j;
169 
170     /* specify subbook */
171     if (eb_set_subbook(&ueb->book, ueb->subCodes[i]) != EB_SUCCESS) {
172       uim_notify_fatal(N_("eb: eb_set_subbook() failed")); continue;
173     }
174 
175     eb_search_word(&ueb->book, text);
176     eb_hit_list(&ueb->book, MAX_HITS, hits, &hitCount);
177     for (j = 0; j < hitCount; j++) {
178       /*EB_Position headp = hits[j].heading;*/
179       EB_Position textp = hits[j].text;
180 
181       go_text_eb(ueb, textp, &str, enc);
182       uim_eb_strappend(&str, "\n", sizeof("\n"));
183     }
184   }
185 
186   free(text);
187 
188   return str;
189 }
190 
191 
192 static void
go_text_eb(uim_eb * ueb,EB_Position position,char ** str,const char * enc)193 go_text_eb (uim_eb *ueb, EB_Position position, char **str, const char *enc)
194 {
195   EB_Hookset hookset;
196   char text[MAX_TEXT + 1];
197   ssize_t text_length;
198   ssize_t bytes;
199   int i;
200 
201   if (eb_seek_text(&ueb->book, &position) != EB_SUCCESS) {
202     uim_notify_fatal(N_("eb: eb_seek_text error occurs"));
203     return;
204   }
205 
206   eb_initialize_hookset(&hookset);
207   for (i = 0; i < 1; i++) {
208     char *local;
209     iconv_t cd;
210 
211     if (eb_read_text(&ueb->book, NULL, &hookset,
212 		     NULL, MAX_TEXT, text, &text_length) != EB_SUCCESS) {
213       bytes = 0;
214       uim_notify_fatal(N_("eb_read_text : an error occurs"));
215       return;
216     }
217 
218     bytes += text_length;
219     if (text_length < 1)
220       break;
221 
222     /* FIXME! check return value */
223     cd = (iconv_t)uim_iconv->create(enc, "EUC-JP");
224     local = uim_iconv->convert(cd, text);
225     uim_iconv->release(cd);
226 
227     uim_eb_strappend(str, local, strlen(local));
228 
229     free(local);
230   }
231   eb_finalize_hookset(&hookset);
232 }
233