Back to index

im-sdk  12.3.91
Jword.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 /*
00043 **  Operations on JWORD.
00044 */
00045 
00046 #include "PYIM.h"
00047 
00048 VOID    GetAsciiFromJword(JWORD* pwJwordArray, CHAR* szAsciiArray, JINT nLen);
00049 JINT    JwordValidLen(JWORD* pwJwordArray, JINT nMaxLen);
00050 JINT    JwordHanziLen(JWORD* pwJwordArray, JINT nMaxLen);
00051 JINT    JwordStrStrReplace(JWORD* pwDst, JWORD* pwSrc1, JWORD* pwSrc2, JINT nLenSrc2);
00052 JWORD*  StrToJword(CHAR* szStr);
00053 VOID    JwordInfo(JWORD* pwJwordArray, JINT nMaxLen);
00054 VOID    Jword2Uchar(JWORD* pwJword, UCHAR* szUch, JINT nMaxLen);
00055 UCHAR*  RecovDyz2244(UCHAR *szDyz2244);
00056 JINT    JwordNCmp(JWORD* pwSrc1, JWORD* pwSrc2, JINT nNum);
00057 VOID    JwordNCpy(JWORD* pwDst, JWORD* pwSrc, JINT nLen);
00058 JINT    IsGbkkkHz(JWORD wHz);
00059 
00060 /*
00061 **  Convert CHAR* to JWORD*. Zero was appended.
00062 */
00063 JWORD* StrToJword(CHAR* szStr)
00064 {
00065        JINT    i, nLen;
00066 static  JWORD*  pwRet = NULL;      
00067        
00068        nLen = strlen(szStr);
00069        
00070        free(pwRet);
00071        pwRet = (JWORD*)malloc((nLen + MALIGN) * sizeof(JWORD));
00072        if(pwRet == NULL)
00073        {
00074               fprintf (stderr, "Failed to alloc Memory in StrToJword().\n");
00075               return NULL;
00076        }
00077        memset(pwRet, '\0', (nLen + MALIGN) * sizeof(JWORD));
00078 
00079        for (i = 0; i < nLen; i++)
00080               pwRet[i]  = (JWORD)szStr[i];
00081        
00082        return pwRet;
00083 }
00084 
00085 /*
00086 **  List JWORD Array Information.
00087 **  For Debug Only.
00088 */
00089 VOID JwordInfo(JWORD* pwJwordArray, JINT nMaxLen)
00090 {
00091        JINT    i, j, nLen;
00092        JWORD   wTmp;
00093        UCHAR   szStr[1024];
00094        
00095        nLen = JwordValidLen(pwJwordArray, nMaxLen);
00096 
00097        memset(szStr, '\0', 1024);
00098 
00099        i = j = 0;
00100        for (i = 0; i < nLen; i++)
00101        {
00102               wTmp = *(pwJwordArray + i);
00103               if ( (wTmp & 0xFF00) > 0)
00104               {
00105                      *(szStr + j) = (UCHAR)(( wTmp & 0xFF00) >> 8);
00106                      j++;
00107                      
00108                      *(szStr + j) = (UCHAR)(wTmp & 0x00FF);
00109                      j++;
00110               }
00111               else if ((wTmp < 0x0080) && (wTmp != 0x0009))
00112               {
00113                      *(szStr + j)  = (UCHAR)(wTmp & 0x00FF);
00114                      j++;
00115               }
00116        }
00117        
00118        fprintf (stderr, "%d  %s\n", j, szStr);
00119 /*     fprintf (stderr, "%d  %s\n", j, RecovDyz2244(szStr));   */
00120 }
00121 
00122 
00123 /*
00124 **  Search pwDst for the first occurance of pwSrc1, and replace it with
00125 **  pwSrc2. The length of pwSrc2 is indicated in argument nLenSrc2.
00126 **
00127 **  If pwSrc1 is not found in pwDst, False is return.
00128 **  Call function must assure pwDst big enough to contain all the 
00129 **  replaced pwSrc2.
00130 **
00131 **  NOTICE: This function is not designed for general usage!!!!
00132 **          Because there are assumptions about the length of pwDst,
00133 **          pwSrc1, pwSrc2!!!! 
00134 */
00135 JINT JwordStrStrReplace(JWORD* pwDst, JWORD* pwSrc1, JWORD* pwSrc2, JINT nLenSrc2)
00136 {
00137        JINT   i, j, k, nLenDst, nLenSrc1, nFoundFlag, nFirstAddr;
00138        
00139        nFoundFlag = 0;
00140        nLenDst    = JwordValidLen(pwDst, 256);
00141        nLenSrc1   = JwordValidLen(pwSrc1, 80);
00142        
00143        /*
00144        **  Find the startup offset in PwDst.
00145        **  THE FOLLOWING CODE TO BE TESTED TO REPLACE ABOVE CODE!! MXL 97-12-06
00146        */
00147        i = j = k = 0;
00148        for(i = 0; (i < nLenDst) && (j < nLenSrc1); i++)
00149        {
00150               if (pwDst[i] == pwSrc1[j])
00151                      j++;
00152               else
00153                      j = 0;
00154        }
00155 
00156        if (j == nLenSrc1)
00157        {
00158 #ifdef _DEBUG
00159               /* If there is a SPACE char in the end of found pwSrc1 in pwDst. */
00160               if ( pwDst[i] == (JWORD)' ')
00161               {
00162                      fprintf (stderr, "JwordStrStrReplace():  THERE IS A SPACE!\n");
00163                      nLenSrc1 += 1;
00164               }
00165 #endif
00166 
00167               /* 
00168               **  NOTES: Don't change the sequence of the following exchange and 
00169               **  the FOR sentence sequence 
00170               */
00171               nFirstAddr = i - j;
00172               if (nLenSrc1 >= nLenSrc2)
00173               {
00174                      for (i = nFirstAddr; i < (nFirstAddr + nLenSrc2); i++)
00175                             pwDst[i]  = pwSrc2[i - nFirstAddr];
00176                      for (i = (nFirstAddr + nLenSrc2); i < (nLenDst - (nLenSrc1 - nLenSrc2)); i++)
00177                             pwDst[i]  = pwDst[i + (nLenSrc1 - nLenSrc2)];
00178                      for (i = (nLenDst - (nLenSrc1 - nLenSrc2)); i < 256; i++)
00179                             pwDst[i]  = 0x0000;
00180               }
00181               else if (nLenSrc1 < nLenSrc2)
00182               {
00183                      for (i = (nLenDst + (nLenSrc2 - nLenSrc1)); i < 256; i++)
00184                             pwDst[i]  = 0x0000;
00185                      for (i = (nFirstAddr + nLenSrc2); i < (nLenDst + nLenSrc2 - nLenSrc1); i++)
00186                             pwDst[i]  = pwDst[i - (nLenSrc2 - nLenSrc1)];
00187                      for (i = nFirstAddr; i < (nFirstAddr + nLenSrc2); i++)
00188                             pwDst[i]  = pwSrc2[i - nFirstAddr];
00189               }
00190               return TRUE;
00191        }
00192        else
00193        {
00194 #ifdef _DEBUG
00195               fprintf(stderr, "Cannot find string to be replaced in pwdst. JwordStrStrReplace().\n");
00196 #endif
00197               return FALSE;
00198        }
00199 }
00200 
00201 
00202 /*
00203 **  Get the valid length of JWORD array in 16 bits. 
00204 **  Valid means contains really data.
00205 */
00206 JINT JwordValidLen(JWORD* pwJwordArray, JINT nMaxLen)
00207 {
00208        JINT   i, nValidLen;
00209        
00210        i = 0;
00211        nValidLen = 0;
00212        while ((i < nMaxLen) && (*(pwJwordArray + i) != 0))
00213        {
00214               i++;
00215               nValidLen++;
00216        }
00217               
00218        return nValidLen;
00219 }
00220 
00221 
00222 /*
00223 **  Get the Hanzi Number in Jword Array.
00224 **  Notice:   Hanzi is in GBK2244.
00225 **
00226 **  MXL: 97-12-27: TO be rewroten????
00227 */
00228 JINT JwordHanziLen(JWORD* pwJwordArray, JINT nMaxLen)
00229 {
00230        JINT   i, nValidLen;
00231        
00232        i = 0;
00233        nValidLen = 0;
00234        while ((i < nMaxLen) && (pwJwordArray[i] != 0))
00235        {
00236               i++;
00237               if (pwJwordArray[i] != 0x0009)
00238                      nValidLen++;
00239        }
00240               
00241        return nValidLen;
00242 }
00243 
00244 
00245 /*
00246 **  Assume szAsciiArray is big enough to contain all ASCII characters in pwJwordArray.
00247 **  End part of szAsciiArray was filled with '\0'
00248 */
00249 VOID GetAsciiFromJword(JWORD* pwJwordArray, CHAR* szAsciiArray, JINT nLen)
00250 {
00251        JINT   i, j;
00252        
00253        i = j = 0;
00254        for (i = 0; i < nLen; i++)
00255        {
00256               if ( *(pwJwordArray + i) < 0x0080)
00257               {
00258                      *(szAsciiArray + j) = (CHAR)(*(pwJwordArray + i));
00259                      j++;
00260               }
00261        }
00262        
00263        for ( ; j < nLen; j++)
00264               *(szAsciiArray + j) = '\0';
00265 }
00266 
00267 
00268 /*
00269 **  Convert a Jword array to UCHAR string. Max Length of pwJword is
00270 **  given in parameter nMaxLen. Caller must assure that size of szUch 
00271 **  is 2 * nMaxLen.
00272 **
00273 **  Notice: nMaxLen is in JWORD, not in BYTE!!
00274 */
00275 VOID Jword2Uchar(JWORD* pwJword, UCHAR* szUch, JINT nMaxLen)
00276 {
00277        JINT     i, j;
00278        
00279        j = 0;
00280        for(i = 0; i < nMaxLen; i++)
00281        {
00282               if(pwJword[i] >= 0x0100)           /* OR 0x8140: First char defined in GBK */
00283               {
00284                      szUch[j++] = (UCHAR)((pwJword[i] & 0xFF00) >> 8);
00285                      szUch[j++] = (UCHAR)(pwJword[i] & 0x00FF);
00286               }
00287               else
00288                      szUch[j++] = (UCHAR)(pwJword[i] & 0x00FF);
00289        }
00290 }
00291 
00292 
00293 /*
00294 **  This Function similar to strncmp() in standard ANSI library.
00295 **  RETURN:  0        pwSrc1 == pwSrc2
00296 **          -1        pwSrc1 <  pwSrc2
00297 **           1        pwSrc1 >  pwSrc2
00298 **
00299 **  Notice: because Jword hava no StringZero characteristics,
00300 **          SO, must give a proper nNum value!!!
00301 **          nNum is in JWORD, not in BYTE!!
00302 */
00303 JINT JwordNCmp(JWORD* pwSrc1, JWORD* pwSrc2, JINT nNum)
00304 {
00305        JINT    i;
00306        JINT    nRet;
00307 
00308        nRet = 0;
00309        for(i = 0; (i < nNum) && (nRet == 0); i++)
00310        {
00311               if(pwSrc1[i] > pwSrc2[i])
00312               {
00313                      nRet = 1;
00314                      break;
00315               }
00316               else if(pwSrc1[i] < pwSrc2[i])
00317               {
00318                      nRet = -1;
00319                      break;
00320               }
00321        }
00322 
00323        return nRet;
00324 }
00325 
00326 
00327 VOID JwordNCpy(JWORD* pwDst, JWORD* pwSrc, JINT nLen)
00328 {
00329        JINT    i;
00330        
00331        for (i = 0; i < nLen; i++)
00332               pwDst[i] = pwSrc[i];
00333 }
00334 
00335 /*
00336 **  Is it Gbkkk Hanzi? 
00337 **  This implementatation is not Fully_correct Now!!
00338 */
00339 JINT IsGbkkkHz(JWORD wHz)
00340 {
00341        JINT    nRet;
00342        JINT    nHi, nLow;
00343     
00344        nHi  = (wHz & 0xFF00) >> 8;
00345        nLow = wHz & 0x00FF;
00346     
00347        nRet = TRUE;
00348        if ((wHz >= 0x2001) && (wHz <= 0x2244))
00349               nRet = FALSE;
00350        else if ((nHi >= 0xB0) && (nHi <= 0xF7) && (nLow >= 0xA1) && (nLow <= 0xFE))
00351               nRet = FALSE;
00352               
00353        return nRet;
00354 }
00355 
00356 
00357