~ubuntu-branches/ubuntu/hardy/uim/hardy

« back to all changes in this revision

Viewing changes to gtk/uim-eb.c

  • Committer: Bazaar Package Importer
  • Author(s): Masahito Omote
  • Date: 2005-12-04 13:10:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051204131042-ktzc8b17zi7a3cw8
Tags: 1:0.4.9.1-1
* New upstream release
* libuim0-nox, libuim-nox-dev, and libuim0-dbg-nox is now obsolete.
  Because libuim0 does not depends on X11. They now become dummy package,
  therefore you can safely remove them.
* Add --enable-debug in configure again.
* debian/patches/08_fix_privilage_escalation_CVE_2005_3149: disabled.
* Fix Error on purge because update-uim-config is not found.
  (closes: Bug#339345)
* uim-qt: New package for Qt utilities for uim. qt-immodule does not
  contained yet because of Debian's Qt3 does not support immodule and
  because uim does not recognize libqt4-dev's headers properly. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
  Copyright (c) 2003,2004 uim Project http://uim.freedesktop.org/
 
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 <stdio.h>
 
38
#include <stdlib.h>
 
39
#include <string.h>
 
40
#include <eb/eb.h>
 
41
#include <eb/text.h>
 
42
#include <eb/font.h>
 
43
#include <eb/binary.h>
 
44
#include <eb/error.h>
 
45
 
 
46
#include "uim-eb.h"
 
47
 
 
48
#define MAX_HITS   10
 
49
#define MAX_TEXT   1000
 
50
#define MAX_LENGTH 10000
 
51
 
 
52
struct _uim_eb {
 
53
  EB_Book         book;
 
54
  EB_Subbook_Code subCodes[EB_MAX_SUBBOOKS];
 
55
  int             subCount;
 
56
};
 
57
 
 
58
static void go_text_eb (uim_eb *ueb,
 
59
                        EB_Position position,
 
60
                        GString *str);
 
61
 
 
62
static unsigned int eb_ref_count = 0;
 
63
 
 
64
uim_eb *
 
65
uim_eb_new (const char *bookpath)
 
66
{
 
67
  uim_eb *ueb;
 
68
  EB_Error_Code err;
 
69
 
 
70
  ueb = malloc(sizeof(uim_eb));
 
71
  eb_ref_count++;
 
72
 
 
73
  err = eb_initialize_library();
 
74
  if (err != EB_SUCCESS)
 
75
    fprintf(stderr, "failed to initialize EB library : error = %s\n",
 
76
            eb_error_message(err));
 
77
 
 
78
  eb_initialize_book(&ueb->book);
 
79
 
 
80
  err = eb_bind(&ueb->book, bookpath);
 
81
  if (err != EB_SUCCESS) {
 
82
    fprintf(stderr, "wrong bookpath\n");
 
83
    free(ueb);
 
84
    return NULL;
 
85
  }
 
86
 
 
87
  err = eb_subbook_list(&ueb->book, ueb->subCodes, &ueb->subCount);
 
88
  if (err != EB_SUCCESS) {
 
89
    g_printerr("eb_subbook_list() failed\n");
 
90
    free(ueb);
 
91
    return NULL;
 
92
  }
 
93
 
 
94
  return ueb;
 
95
}
 
96
 
 
97
 
 
98
void
 
99
uim_eb_destroy (uim_eb *ueb)
 
100
{
 
101
  if (!ueb)
 
102
    return;
 
103
 
 
104
  eb_finalize_book(&ueb->book);
 
105
 
 
106
  eb_ref_count--;
 
107
  if (eb_ref_count == 0)
 
108
    eb_finalize_library();
 
109
}
 
110
 
 
111
 
 
112
char *
 
113
uim_eb_search_text (uim_eb *ueb, const gchar *text_utf8)
 
114
{
 
115
  gchar *text;
 
116
  int i;
 
117
  gsize bytes_read, bytes_written;
 
118
  GString *str;
 
119
 
 
120
  /* FIXME! check return value */
 
121
  text = g_convert(text_utf8, strlen(text_utf8),
 
122
                   "EUC-JP", "UTF-8",
 
123
                   &bytes_read, &bytes_written,
 
124
                   NULL);
 
125
  g_return_val_if_fail(text, FALSE);
 
126
 
 
127
  str = g_string_new("");
 
128
 
 
129
  for (i = 0; i < ueb->subCount; i++) {
 
130
    EB_Hit hits[MAX_HITS];
 
131
    int hitCount;
 
132
    int j;
 
133
 
 
134
    /* specify subbook */
 
135
    if (eb_set_subbook(&ueb->book, ueb->subCodes[i]) != EB_SUCCESS) {
 
136
      g_print("eb_set_subbook() failed\n"); continue;
 
137
    }
 
138
 
 
139
    eb_search_word(&ueb->book, text);
 
140
    eb_hit_list(&ueb->book, MAX_HITS, hits, &hitCount);
 
141
    for (j = 0; j < hitCount; j++) {
 
142
      /*EB_Position headp = hits[j].heading;*/
 
143
      EB_Position textp = hits[j].text;
 
144
 
 
145
      go_text_eb(ueb, textp, str);
 
146
      g_string_append(str, "\n");
 
147
    }
 
148
  }
 
149
 
 
150
  g_free(text);
 
151
 
 
152
  return g_string_free(str, FALSE);
 
153
}
 
154
 
 
155
 
 
156
static void
 
157
go_text_eb (uim_eb *ueb, EB_Position position, GString *str)
 
158
{
 
159
  EB_Hookset hookset;
 
160
  char text[MAX_TEXT + 1];
 
161
  int text_length;
 
162
  int bytes;
 
163
  int i;
 
164
 
 
165
  if (eb_seek_text(&ueb->book, &position) != EB_SUCCESS) {
 
166
    g_print("eb_seek_text error occurs");
 
167
    return;
 
168
  }
 
169
 
 
170
  eb_initialize_hookset(&hookset);
 
171
  for (i = 0; i < 1; i++) {
 
172
    gchar *text_utf8;
 
173
    gsize bytes_read, bytes_written;
 
174
 
 
175
    if (eb_read_text(&ueb->book, NULL, &hookset,
 
176
                     NULL, MAX_TEXT, text, &text_length) != EB_SUCCESS) {
 
177
      bytes = 0;
 
178
      g_print("eb_read_text : an error occurs.\n");
 
179
      return;
 
180
    }
 
181
 
 
182
    bytes += text_length;
 
183
    if (text_length < 1)
 
184
      break;
 
185
 
 
186
    /* FIXME! check return value */
 
187
    text_utf8 = g_convert(text, strlen(text),
 
188
                          "UTF-8", "EUC-JP",
 
189
                          &bytes_read, &bytes_written,
 
190
                          NULL);
 
191
    g_string_append(str, text_utf8);
 
192
    g_free(text_utf8);
 
193
  }
 
194
  eb_finalize_hookset(&hookset);
 
195
}