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 "GbkUnicode.h"        /* GBK to Unicode mapping table */
00045 #include "Converter.h"
00046 
00047 #define False 0
00048 #define True 1
00049 
00050 typedef unsigned char *MString;
00051 typedef unsigned char *XPointer;
00052 
00053 static int isgb(unsigned char s1, unsigned s2)
00054 {
00055        if(s1 >= 0xA0 && s1 <= 0xf7 ){
00056               if(s2 < 0xA1 ||  s2 > 0xfe)
00057                      return False;
00058               else
00059                      return True;
00060        } else {
00061               return False;
00062        }
00063 }
00064 
00065 static int isgbk(unsigned char s1, unsigned s2)
00066 {
00067        if(s1 >= 0x81 && s1 <= 0xfe ){
00068               if(s2 < 0x40 || s2 == 0x7f || s2 > 0xfe)
00069                      return False;
00070               else
00071                      return True;
00072        } else {
00073               return False;
00074        }
00075 }
00076 
00077 zh_CN_gbktoUTF_16(MString *from, int *from_len, MString *to, int *to_len)
00078 {
00079        XPointer from_str=*from,  to_str=*to;
00080        int from_left, to_left, ret;
00081        int i,j;
00082        int n= *from_len;
00083        int count=0;
00084        int hanzi_num = 0;
00085        unsigned long   gbk_val;        /* GBK value */
00086        int             unidx;          /* Unicode index */
00087        unsigned long   uni_val;        /* Unicode */
00088 
00089        from_left= *from_len;
00090        to_left = *to_len;
00091 
00092 #ifdef DEBUG
00093        printf("Calling the  zh_CN_gbktoUTF_16_built_in, from_len = %d,to_len=%d\n",*from_len,*to_len);
00094 #endif
00095 
00096        j = 0;
00097        for(i = 0; i < *from_len; i ++) {
00098               if ((unsigned char)from_str[i] <= 0x7f) {
00099                      to_str[j++] = from_str[i] >> 8;
00100                      to_str[j++] = from_str[i] & 0xff;
00101                      count = count +2;
00102               }
00103               else if (isgbk((unsigned char)from_str[i],(unsigned char)from_str[i+1])) {
00104                      gbk_val = ( ( (unsigned char)from_str[i] & 0xff ) << 8 )
00105                          + ( (unsigned char)from_str[i+1] & 0xff);
00106                      unidx = search_unicode(gbk_val, gbk_unicode_tab, GBKMAX);
00107                      if (unidx >= 0) uni_val = gbk_unicode_tab[unidx].unicode;
00108                      else uni_val = -1;
00109                      to_str[j++] = uni_val >> 8;
00110                      to_str[j++] = uni_val & 0xff;
00111                      i++;
00112                      count = count +2;
00113                      hanzi_num ++;
00114               }
00115        }
00116        *to_len = to_left - count;
00117 
00118 #ifdef DEBUG
00119        printf("MBSTOWCSfrom=%s,from_left=%d,wstr=%x,to_lef=%d\n",from_str,from_left,to_str[0],to_left);
00120        for(i=0;i<j;i++) printf(" %0x",to_str[i]);
00121        printf("\n");
00122 #endif
00123 
00124        return hanzi_num;
00125 }
00126 
00127 void zh_str_to_utf16 (unsigned char* src, unsigned short* dst, int * caret) {
00128 
00129        unsigned char from_str[1024];
00130        unsigned char to_str[1024];
00131        unsigned char *from =from_str;
00132        unsigned char *to=to_str;
00133        int from_len,to_len =1024;
00134        unsigned short high, low;
00135        int i, j;
00136        int hanzi_num;
00137 
00138        from = from_str, to=to_str;
00139        strcpy(from, src);
00140        from_len = strlen(from);
00141        to_len = 1024;
00142        hanzi_num = zh_CN_gbktoUTF_16(&from, &from_len, &to, &to_len);
00143        *caret -= hanzi_num;
00144 
00145        if(to_len == 1024) dst[0] = 0;
00146        else {
00147               for(i = 0, j = 0; i < 1024 - to_len;) {
00148                      high = (unsigned short)to_str[i];
00149                      low  = (unsigned short)to_str[i+1];
00150                      dst[j] = high * 256 + low;
00151                      j ++; 
00152                      i += 2;
00153               }
00154               dst[j] = 0;
00155        }
00156 }
00157 
00158 static KeySym getX11KeySym(long awtKey)
00159 {
00160        int i;
00161        for (i = 0; keymapTable[i].awtKey != 0; i++) {
00162               if (keymapTable[i].awtKey == awtKey) {
00163                      return keymapTable[i].x11Key;
00164               }
00165        }
00166        return 0;
00167 }
00168 
00169 modifyEvent(long *keyCode, unsigned short *keyChar, long *State)
00170 {
00171        KeySym keysym = 0;
00172        long state = 0;
00173 
00174        switch(*keyCode) {
00175        case java_awt_event_KeyEvent_VK_ENTER:
00176        case java_awt_event_KeyEvent_VK_BACK_SPACE:
00177        case java_awt_event_KeyEvent_VK_TAB:
00178        case java_awt_event_KeyEvent_VK_ESCAPE:
00179        case java_awt_event_KeyEvent_VK_DELETE:
00180               keysym = getX11KeySym(*keyCode);
00181               break;
00182        default:
00183               if (*keyChar != 0 && *keyChar < 256) {
00184                      keysym = *keyChar;
00185               } else {
00186                      keysym = getX11KeySym(*keyCode);
00187               }
00188               break;
00189        }
00190        if (keysym >= 'A' && keysym <= 'Z') {
00191               state |= ShiftMask;
00192        }
00193        if (*State & java_awt_event_InputEvent_SHIFT_MASK) {
00194               state |= ShiftMask;
00195        }
00196        if (*State & java_awt_event_InputEvent_CTRL_MASK) {
00197               state |= ControlMask;
00198        }
00199        if (*State & java_awt_event_InputEvent_META_MASK) {
00200               state |= awt_MetaMask;
00201        }
00202        if (*State & java_awt_event_InputEvent_ALT_MASK) {
00203               state |= awt_AltMask;
00204        }
00205 
00206        *keyCode = keysym;
00207        if((*keyChar < 0x20) || (*keyChar > 0x7E))
00208               *keyChar = 0xFF;
00209        if(state != 0) *State = state;
00210 
00211 #ifdef DEBUG
00212        printf("After convert code: %x, char %x, state %x\n", *keyCode, *keyChar, *State);
00213 #endif
00214 }
00215 
00216 int search_unicode(unsigned long x, gbk_unicode v[], int n)
00217 {
00218        int low, high, mid;
00219 
00220        low = 0;
00221        high = n - 1;
00222        while (low <= high) {
00223               mid = (low + high) / 2;
00224               if (x < v[mid].gbkcode) high = mid - 1;
00225               else if (x > v[mid].gbkcode) low = mid + 1;
00226               else return mid; /* found match */
00227 
00228        }
00229        return (-1);    /* no match */
00230 }
00231