-
Notifications
You must be signed in to change notification settings - Fork 0
/
eplkup_gaiji.c
358 lines (326 loc) · 9.89 KB
/
eplkup_gaiji.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*------------------------------------------------------------------------
-- Copyright (C) 2011-2014 Christopher Brochtrup
--
-- This file is part of eplkup.
--
-- eplkup is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- eplkup is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with eplkup. If not, see <http://www.gnu.org/licenses/>.
--
------------------------------------------------------------------------*/
#include <stdlib.h>
#include <string.h>
#include "eplkup_gaiji.h"
#include "eplkup_utils.h"
#include "eplkup_data.h"
/*------------------------------------------------------------------------
-- Name: is_gaiji_width_char
--
-- Description:
-- Does the provided character represent a gaiji width (narrow/wide)?
--
-- Parameters:
-- (IN) c - The character to check.
--
-- Returns:
-- 0 = Character is not a gaiji width character.
-- 1 = Character is a gaiji width character.
--
------------------------------------------------------------------------*/
int is_gaiji_width_char(char c)
{
return ((c == 'n') || (c == 'w'));
} /* is_gaiji_width_char */
/*------------------------------------------------------------------------
-- Name: get_gaiji_table
--
-- Description:
-- Get the appropriate gaiji table based on the dictionary name and
-- gaiji width (narrow/wide).
--
-- Parameters:
-- (IN) dic_name - Name of the dictionary.
-- (IN) width - Gaiji width. 'n' = narrow. 'w' = wide.
-- (OUT) gaiji_table - Pointer to the found table. NULL if table not found.
-- (OUT) max_elem - Number of elements in the table. NULL if table not found.
--
-- Returns:
-- None.
--
------------------------------------------------------------------------*/
void get_gaiji_table(const char *dic_name, char width, Gaiji_table_type **gaiji_table, int *max_elem)
{
if(str_eq_i(dic_name, "wadai5"))
{
if(width == 'n')
{
*gaiji_table = gaiji_table_narrow_wadai5;
*max_elem = NUM_NARROW_WADAI5_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_wadai5;
*max_elem = NUM_WIDE_WADAI5_ITEMS;
}
}
else if(str_eq_i(dic_name, "kojien"))
{
if(width == 'n')
{
*gaiji_table = gaiji_table_narrow_kojien;
*max_elem = NUM_NARROW_KOJIEN_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_kojien;
*max_elem = NUM_WIDE_KOJIEN_ITEMS;
}
}
else if(str_eq_i(dic_name, "chujiten"))
{
if(width == 'n')
{
*gaiji_table = gaiji_table_narrow_chujiten;
*max_elem = NUM_NARROW_CHUJITEN_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_chujiten;
*max_elem = NUM_WIDE_CHUJITEN_ITEMS;
}
}
else if(str_eq_i(dic_name, "genius"))
{
if(width == 'n')
{
*gaiji_table = gaiji_table_narrow_genius;
*max_elem = NUM_NARROW_GENIUS_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_genius;
*max_elem = NUM_WIDE_GENIUS_ITEMS;
}
}
else if(str_eq_i(dic_name, "daijirin"))
{
if(width == 'n')
{
*gaiji_table = gaiji_table_narrow_daijirin;
*max_elem = NUM_NARROW_DAIJIRIN_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_daijirin;
*max_elem = NUM_WIDE_DAIJIRIN_ITEMS;
}
}
else if(str_eq_i(dic_name, "meikyou"))
{
if(width == 'n')
{
*gaiji_table = gaiji_table_narrow_meikyou;
*max_elem = NUM_NARROW_MEIKYOU_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_meikyou;
*max_elem = NUM_WIDE_MEIKYOU_ITEMS;
}
}
else if(str_eq_i(dic_name, "meikyojj"))
{
if(width == 'n')
{
*gaiji_table = gaiji_table_narrow_meikyojj;
*max_elem = NUM_NARROW_MEIKYOJJ_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_meikyojj;
*max_elem = NUM_WIDE_MEIKYOJJ_ITEMS;
}
}
else if(str_eq_i(dic_name, "daijisen"))
{
if(width == 'n')
{
*gaiji_table = gaiji_table_narrow_daijisen;
*max_elem = NUM_NARROW_DAIJISEN_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_daijisen;
*max_elem = NUM_WIDE_DAIJISEN_ITEMS;
}
}
else if(str_eq_i(dic_name, "snmkg99"))
{
if(width == 'n')
{
*gaiji_table = NULL;
//*gaiji_table = gaiji_table_narrow_snmkg99;
*max_elem = NUM_NARROW_SNMKG99_ITEMS;
}
else
{
*gaiji_table = gaiji_table_wide_snmkg99;
*max_elem = NUM_WIDE_SNMKG99_ITEMS;
}
}
else
{
*gaiji_table = NULL;
}
} /* get_gaiji_table */
/*------------------------------------------------------------------------
-- Name: compare_gaiji
--
-- Description:
-- Comparison routine for the binary search in get_gaiji_replacment_elem.
--
-- Parameters:
-- (IN) key - The item we are searching for.
-- (IN) elem - The element of the array we are searching in.
--
-- Returns:
-- <0: key < elem
-- =0: key == elem
-- >0: key > elem
--
------------------------------------------------------------------------*/
int compare_gaiji (const void *key, const void *elem)
{
return *(unsigned short*)key - (unsigned short)((Gaiji_table_type*)elem)->code;
} /* compare_gaiji */
/*------------------------------------------------------------------------
-- Name: get_gaiji_replacment_elem
--
-- Description:
-- Get the gaiji table element for the given gaiji code and dictionary.
--
-- Parameters:
-- (IN) dic_name - Name of the dictionary
-- (IN) width - Character representing the width of the gaiji.
-- 'n' = narrow. 'w' = wide.
-- (IN) code - The gaiji code.
--
-- Returns:
-- The gaiji table element or NULL if element could not be found.
--
------------------------------------------------------------------------*/
Gaiji_table_type * get_gaiji_replacment_elem(const char *dic_name, char width, unsigned short code)
{
int max_elem = 0;
Gaiji_table_type *gaiji_elem = NULL;
Gaiji_table_type *gaiji_table = NULL;
/* Get the gaiji table to use */
get_gaiji_table(dic_name, width, &gaiji_table, &max_elem);
/* Was an appropriate gaiji table found? If so, find the particular gaiji replacement to use. */
if(gaiji_table != NULL)
{
gaiji_elem = (Gaiji_table_type *)bsearch(&code, gaiji_table, max_elem, sizeof(Gaiji_table_type), compare_gaiji);
}
return gaiji_elem;
} /* get_gaiji_replacment_elem */
/*------------------------------------------------------------------------
-- Name: get_gaiji_replacement_text
--
-- Description:
-- Get the UTF-8 gaiji replacement text for the given gaiji code and dictionary.
--
-- Parameters:
-- (OUT) dest - Storage for the gaiji replacement text. Must be
-- at least MAX_BYTES_UTF8_REPLACEMENT + 1 bytes.
-- (IN) dic_name - Name of the dictionary
-- (IN) width - Character representing the width of the gaiji.
-- 'n' = narrow. 'w' = wide.
-- (IN) c1 - 1st character of the gaiji code.
-- (IN) c2 - 2nd character of the gaiji code.
-- (IN) c3 - 3rd character of the gaiji code.
-- (IN) c4 - 4th character of the gaiji code.
--
-- Returns:
-- The gaiji replacement text.
--
------------------------------------------------------------------------*/
char * get_gaiji_replacement_text(char *dest, const char *dic_name, char width, char c1, char c2, char c3, char c4)
{
int i = 0;
Gaiji_table_type *gaiji = NULL;
char hex_str[4] = { c1, c2, c3, c4 };
unsigned short code = (unsigned short)strtoul(hex_str, NULL, 16);
/* Get the element from the appropriate gaiji table to use */
gaiji = get_gaiji_replacment_elem(dic_name, width, code);
/* Was a gaiji replacement found? If so, copy the replacement into dest. */
if(gaiji != NULL)
{
for(i = 0; i < gaiji->num_bytes; i++)
{
dest[i] = gaiji->replacement[i];
}
dest[i] = '\0';
}
else
{
/* If the gaiji replacement could not be found, default to "?" */
dest[0] = '?';
dest[1] = '\0';
}
return dest;
} /* get_gaiji_replacement_text */
/*------------------------------------------------------------------------
-- Name: replace_gaiji_with_utf8
--
-- Description:
-- Replace all gaiji that have UTF-8 equivalents with those equivalents.
--
-- The gaiji in the source string should be replresented as follows:
-- {#[w or n][hex_gaiji_code]x4} (use this printf format: {#n%04X} or {#w%04X})
--
-- Parameters:
-- (OUT) dest - Storage for the replaced text.
-- (IN) src - The source text that contains the gaiji codes to replace.
--
-- Returns:
-- The replaced text.
--
------------------------------------------------------------------------*/
char * replace_gaiji_with_utf8(char *dest, char *src)
{
int i = 0;
int dest_idx = 0;
int src_len = strlen(src);
char replacement[MAX_BYTES_UTF8_REPLACEMENT + 1]; /* Storage for the replacement character(s) */
dest[0] = '\0';
for(i = 0; i < src_len; i++)
{
/* Is a gaiji code encountered? */
if(((i + 7 < src_len) && src[i] == '{') && (src[i + 1] == '#') && is_gaiji_width_char(src[i + 2])
&& is_hex(src[i + 3]) && is_hex(src[i + 4]) && is_hex(src[i + 5]) && is_hex(src[i + 6]) && (src[i + 7] == '}'))
{
/* Get the replacement character(s) for the gaiji code */
get_gaiji_replacement_text(replacement, subbook_directory, src[i + 2], src[i + 3], src[i + 4], src[i + 5], src[i + 6]);
/* Add the replacement character(s) to dest */
dest[dest_idx] = '\0';
strcat(dest, replacement);
dest_idx += strlen(replacement);
i += 7;
}
else /* No gaiji code encountered */
{
dest[dest_idx++] = src[i];
}
}
dest[dest_idx] = '\0';
return dest;
} /* replace_gaiji_with_utf8 */