Back to index

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