Back to index

im-sdk  12.3.91
Converter.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include "gbk_unicode.h"        /* GBK to Unicode mapping table */
00045 #include "Converter.h"
00046 
00047 #define False 0
00048 #define True 1
00049 /*
00050 #define DEBUG
00051 */
00052 
00053 typedef unsigned char *MString;
00054 typedef unsigned char *XPointer;
00055 
00056 static int isgb(unsigned char s1, unsigned s2)
00057 {
00058     /* consider the first byte */
00059     if(s1 >= 0xA0 && s1 <= 0xf7 ){
00060       if(s2 < 0xA1 ||  s2 > 0xfe)
00061         return False;
00062       else
00063         return True;
00064     }else{
00065         return False;
00066     }
00067 }
00068 
00069 static int isgbk(unsigned char s1, unsigned s2)
00070 {
00071     /* consider the first byte */
00072     if(s1 >= 0x81 && s1 <= 0xfe ){
00073       if(s2 < 0x40 || s2 == 0x7f || s2 > 0xfe)
00074         return False;
00075       else
00076         return True;
00077     }else{
00078         return False;
00079     }
00080 }
00081 
00082 zh_CN_euctoUTF_16(MString *from, int *from_len, MString *to, int *to_len)
00083 {
00084     XPointer from_str=*from,  to_str=*to;
00085     int from_left, to_left, ret;
00086     int i,j;
00087     int n= *from_len;
00088     int count=0;
00089     int hanzi_num = 0;
00090     unsigned long   gb_val;        /* GB2312 value */
00091     int             unidx;          /* Unicode index */
00092     unsigned long   uni_val;        /* Unicode */
00093 
00094     from_left= *from_len;
00095     to_left = *to_len;
00096 
00097 #ifdef DEBUG
00098     printf("Calling the  zh_CN_euctoUTF_16_built_in, from_len = %d,to_len=%d\n",*from_len,*to_len);
00099 #endif
00100 
00101     j = 0;
00102     for(i = 0; i < *from_len; i ++) {
00103         if ((unsigned char)from_str[i] <= 0x7f) {
00104             to_str[j++] = from_str[i] >> 8;
00105             to_str[j++] = from_str[i] & 0xff;
00106             count = count +2;
00107         }
00108         else if (isgb((unsigned char)from_str[i],(unsigned char)from_str[i+1])) {
00109            gb_val = ( ( (unsigned char)from_str[i] & 0xff ) << 8 ) 
00110               + ( (unsigned char)from_str[i+1] & 0xff);
00111            unidx = search_unicode(gb_val, gbk_unicode_tab, GBKMAX);
00112            if (unidx >= 0) uni_val = gbk_unicode_tab[unidx].unicode;
00113            else uni_val = -1;
00114             to_str[j++] = uni_val >> 8;
00115             to_str[j++] = uni_val & 0xff;
00116             i++;
00117             count = count +2;
00118            hanzi_num ++;
00119         }
00120     }
00121     *to_len = to_left - count;
00122 
00123 #ifdef DEBUG
00124     printf("MBSTOWCSfrom=%s,from_left=%d,wstr=%x,to_lef=%d\n",from_str,from_left,to_str[0],to_left);
00125     for(i=0;i<*from_len;i++) printf(" %x ", from_str[i]);
00126     printf("\n");
00127     for(i=0;i<j;i++) printf(" %0x",to_str[i]);
00128     printf("\n");
00129 #endif
00130 
00131     return hanzi_num;
00132 }
00133 
00134 zh_CN_gbktoUTF_16(MString *from, int *from_len, MString *to, int *to_len)
00135 {
00136     XPointer from_str=*from,  to_str=*to;
00137     int from_left, to_left, ret;
00138     int i,j;
00139     int n= *from_len;
00140     int count=0;
00141     int hanzi_num = 0;
00142     unsigned long   gbk_val;        /* GBK value */
00143     int             unidx;          /* Unicode index */
00144     unsigned long   uni_val;        /* Unicode */
00145 
00146     from_left= *from_len;
00147     to_left = *to_len;
00148 
00149 #ifdef DEBUG
00150     printf("Calling the  zh_CN_gbktoUTF_16_built_in, from_len = %d,to_len=%d\n",*from_len,*to_len);
00151 #endif
00152 
00153     j = 0;
00154     for(i = 0; i < *from_len; i ++) {
00155         if ((unsigned char)from_str[i] <= 0x7f) {
00156             to_str[j++] = from_str[i] >> 8;
00157             to_str[j++] = from_str[i] & 0xff;
00158            count = count +2;
00159         }
00160         else if (isgbk((unsigned char)from_str[i],(unsigned char)from_str[i+1])) {
00161            gbk_val = ( ( (unsigned char)from_str[i] & 0xff ) << 8 ) 
00162               + ( (unsigned char)from_str[i+1] & 0xff);
00163            unidx = search_unicode(gbk_val, gbk_unicode_tab, GBKMAX);
00164            if (unidx >= 0) uni_val = gbk_unicode_tab[unidx].unicode;
00165            else uni_val = -1;
00166             to_str[j++] = uni_val >> 8;
00167             to_str[j++] = uni_val & 0xff;
00168             i++;
00169             count = count +2;
00170            hanzi_num ++;
00171         }
00172     }
00173     *to_len = to_left - count;
00174 
00175 #ifdef DEBUG
00176     printf("MBSTOWCSfrom=%s,from_left=%d,wstr=%x,to_lef=%d\n",from_str,from_left,to_str[0],to_left);
00177     for(i=0;i<j;i++) printf(" %0x",to_str[i]);
00178     printf("\n");
00179 #endif
00180 
00181     return hanzi_num;
00182 }
00183 
00184 void zh_str_to_utf16 (unsigned char* src, unsigned short* dst, int * caret) {
00185 
00186     unsigned char from_str[1024];
00187     unsigned char to_str[1024];
00188     unsigned char *from =from_str;
00189     unsigned char *to=to_str;
00190     int from_len,to_len =1024;
00191     unsigned short high, low;
00192     int i, j;
00193     int hanzi_num;
00194 
00195     from = from_str, to=to_str;
00196     strcpy(from, src);
00197     from_len = strlen(from);
00198     to_len = 1024;
00199     hanzi_num = zh_CN_gbktoUTF_16(&from, &from_len, &to, &to_len);
00200     *caret -= hanzi_num;
00201 
00202     if(to_len == 1024) dst[0] = 0; 
00203     else {
00204         for(i = 0, j = 0; i < 1024 - to_len;) {
00205             high = (unsigned short)to_str[i];
00206             low  = (unsigned short)to_str[i+1];
00207             dst[j] = high * 256 + low;
00208             j ++; i += 2;
00209         }
00210         dst[j] = 0;
00211     }
00212 }
00213 
00214 static KeySym getX11KeySym(long awtKey)
00215 {
00216     int i;
00217     for (i = 0; keymapTable[i].awtKey != 0; i++) {
00218         if (keymapTable[i].awtKey == awtKey) {
00219             return keymapTable[i].x11Key;
00220         }
00221     }
00222     return 0;
00223 }
00224 
00225 modifyEvent(long *keyCode, unsigned short *keyChar, long *State)
00226 {
00227     KeySym keysym = 0;
00228     long state = 0;
00229 
00230     switch(*keyCode) {
00231       case java_awt_event_KeyEvent_VK_ENTER:
00232       case java_awt_event_KeyEvent_VK_BACK_SPACE:
00233       case java_awt_event_KeyEvent_VK_TAB:
00234       case java_awt_event_KeyEvent_VK_ESCAPE:
00235       case java_awt_event_KeyEvent_VK_DELETE:
00236           keysym = getX11KeySym(*keyCode);
00237           break;
00238       default:
00239           if (*keyChar != 0 && *keyChar < 256) {
00240               keysym = *keyChar;
00241           } else {
00242               keysym = getX11KeySym(*keyCode);
00243           }
00244           break;
00245     }
00246     if (keysym >= 'A' && keysym <= 'Z') {
00247         state |= ShiftMask;
00248     }
00249     if (*State & java_awt_event_InputEvent_SHIFT_MASK) {
00250         state |= ShiftMask;
00251     }
00252     if (*State & java_awt_event_InputEvent_CTRL_MASK) {
00253         state |= ControlMask;
00254     }
00255     if (*State & java_awt_event_InputEvent_META_MASK) {
00256         state |= awt_MetaMask;
00257     }
00258     if (*State & java_awt_event_InputEvent_ALT_MASK) {
00259         state |= awt_AltMask;
00260     }
00261 
00262     *keyCode = keysym;
00263     if((*keyChar < 0x20) || (*keyChar > 0x7E))
00264        *keyChar = 0xFF;
00265     if(state != 0) *State = state;
00266 
00267 #ifdef DEBUG
00268     printf("After convert code: %x, char %x, state %x\n", *keyCode, *keyChar, *State);
00269 #endif
00270 }
00271 
00272 int search_unicode(unsigned long x, gbk_unicode v[], int n)
00273 {
00274     int low, high, mid;
00275 
00276     low = 0;
00277     high = n - 1;
00278     while (low <= high) {
00279         mid = (low + high) / 2;
00280         if (x < v[mid].gbkcode) high = mid - 1;
00281         else if (x > v[mid].gbkcode) low = mid + 1;
00282         else return mid; /* found match */ 
00283     }
00284     return (-1);    /* no match */
00285 }
00286