1 // OVOFHanConvertZhTwJp.cpp: Traditional Chinese->Japan Kanji Output Filter
2 //
3 // Copyright (c) 2004-2006 The OpenVanilla Project (http://openvanilla.org)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 // 3. Neither the name of OpenVanilla nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30
31 #undef OV_DEBUG
32 #include <stdlib.h>
33 #include <string.h>
34 #include <OpenVanilla/OpenVanilla.h>
35 #include <OpenVanilla/OVLibrary.h>
36 #include <OpenVanilla/OVUtility.h>
37
38 // externally defined in ZhTwToJpKanji.c
39 extern size_t ZhTwToJpKanjiTableSize;
40 extern unsigned short ZhTwToJpKanjiTable[];
41
42 // Linear search.
ZhTwToJpKanji(unsigned short c)43 unsigned short ZhTwToJpKanji(unsigned short c) {
44 extern size_t ZhTwToJpKanjiTableSize;
45 extern unsigned short ZhTwToJpKanjiTable[];
46 unsigned int i;
47 for ( i = 0; i < ZhTwToJpKanjiTableSize; i += 2 ) {
48 if(ZhTwToJpKanjiTable[i] == c) {
49 return ZhTwToJpKanjiTable[i+1];
50 }
51 }
52 return c;
53 }
54
55 // OVOF interface
56 class OVOFHanConvertZhTwJp : public OVOutputFilter {
57 public:
OVOFHanConvertZhTwJp()58 OVOFHanConvertZhTwJp() {
59 u16buf=NULL;
60 }
61
initialize(OVDictionary * moduleCfg,OVService * srv,const char * modulePath)62 virtual int initialize(OVDictionary *moduleCfg, OVService *srv,
63 const char *modulePath) {
64 return 1;
65 }
66
identifier()67 virtual const char *identifier() { return "OVOFHanConvertZhTwJp"; }
localizedName(const char * locale)68 virtual const char *localizedName(const char *locale) {
69 if (!strcasecmp(locale, "zh_TW")) return "繁體中文轉日文漢字";
70 if (!strcasecmp(locale, "zh_CN")) return "繁体中文转日文汉字";
71 return "Traditional Chinese to Japanese Kanji";
72 }
process(const char * src,OVService * srv)73 virtual const char *process (const char *src, OVService *srv) {
74 if (u16buf) {
75 free(u16buf);
76 u16buf=NULL;
77 }
78
79 unsigned short *u16p;
80 int l;
81 l=srv->UTF8ToUTF16(src, &u16p);
82
83 if (!l) return src;
84 u16buf=(unsigned short*)calloc(1,l*sizeof(unsigned short));
85 memcpy(u16buf, u16p, l*sizeof(unsigned short));
86
87 for (int i=0; i<l; i++) {
88 unsigned short c=ZhTwToJpKanji(u16buf[i]);
89 if (c) u16buf[i]=c;
90 }
91 return srv->UTF16ToUTF8(u16buf, l);
92 }
93
94 protected:
95 unsigned short *u16buf;
96 };
97
98 OV_SINGLE_MODULE_WRAPPER(OVOFHanConvertZhTwJp);
99
100