UCS2.c
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:39k
源码类别:

MTK

开发平台:

C/C++

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *   UCS2.c
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   MAUI
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *   UCS2 utility
  48.  *
  49.  * Author:
  50.  * -------
  51.  * -------
  52.  *  
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * removed!
  58.  *
  59.  * removed!
  60.  * removed!
  61.  * removed!
  62.  *
  63.  * removed!
  64.  * removed!
  65.  * removed!
  66.  *
  67.  * removed!
  68.  * removed!
  69.  * removed!
  70.  *
  71.  * removed!
  72.  * removed!
  73.  * removed!
  74.  *
  75.  * removed!
  76.  * removed!
  77.  * removed!
  78.  *
  79.  * removed!
  80.  * removed!
  81.  * removed!
  82.  *
  83.  *------------------------------------------------------------------------------
  84.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  85.  *============================================================================
  86.  ****************************************************************************/
  87. /**
  88.  * Copyright Notice
  89.  * ?2002 - 2003, Pixtel Communications, Inc., 1489 43rd Ave. W.,
  90.  * Vancouver, B.C. V6M 4K8 Canada. All Rights Reserved.
  91.  *  (It is illegal to remove this copyright notice from this software or any
  92.  *  portion of it)
  93.  */
  94. /**********************************************************************************
  95.    Filename:      UCS2.c
  96.    Author:        Manish
  97.    Date Created:  Feb 5-2003
  98.    Contains:      
  99. **********************************************************************************/
  100. #include "PixtelDataTypes.h"
  101. #include "Ucs2dcl.h"
  102. #include "Ucs2prot.h"
  103. #include "Unicodexdcl.h"
  104. #include "string.h"
  105. #include "DebugInitDef.h"
  106. #ifndef MMI_ON_WIN32
  107. #include "app_str.h"
  108. #endif
  109. /*****************************************************************************
  110.  * FUNCTION
  111.  *  UnicodeToUCS2Encoding
  112.  * DESCRIPTION
  113.  *  convert unicode to UCS2 encoding
  114.  * PARAMETERS
  115.  *  unicode         [IN]        Value to be encoded
  116.  *  charLength      [OUT]         
  117.  *  arrOut          [OUT]         
  118.  * RETURNS
  119.  *  U8 -> Status
  120.  *****************************************************************************/
  121. U8 UnicodeToUCS2Encoding(U16 unicode, U8 *charLength, U8 *arrOut)
  122. {
  123. #ifndef MMI_ON_WIN32
  124.     /*----------------------------------------------------------------*/
  125.     /* Local Variables                                                */
  126.     /*----------------------------------------------------------------*/
  127.     /*----------------------------------------------------------------*/
  128.     /* Code Body                                                      */
  129.     /*----------------------------------------------------------------*/
  130.     /* On the Target */
  131.     return (U8)app_unicode_to_ucs2encoding(
  132.                 (kal_uint16)unicode, 
  133.                 (kal_uint8*)charLength,
  134.                 (kal_uint8*)arrOut);
  135. #else
  136.     /*----------------------------------------------------------------*/
  137.     /* Local Variables                                                */
  138.     /*----------------------------------------------------------------*/
  139.     U8 status = ST_SUCCESS;
  140.     U8 index = 0;
  141.     /*----------------------------------------------------------------*/
  142.     /* Code Body                                                      */
  143.     /*----------------------------------------------------------------*/
  144.     if (arrOut != 0)
  145.     {
  146.         if (unicode < 256)
  147.         {
  148.             arrOut[index++] = *((U8*) (&unicode));
  149.             arrOut[index] = 0;
  150.         }
  151.         else
  152.         {
  153.             arrOut[index++] = *((U8*) (&unicode));
  154.             arrOut[index] = *(((U8*) (&unicode)) + 1);
  155.         }
  156.         *charLength = 2;
  157.     }
  158.     else
  159.     {
  160.         status = ST_FAILURE;
  161.     }
  162.     return status;
  163. #endif
  164. }
  165. /*****************************************************************************
  166.  * FUNCTION
  167.  *  UCS2EncodingToUnicode
  168.  * DESCRIPTION
  169.  *  convert UCS2 encoded scheme to unicode
  170.  * PARAMETERS
  171.  *  pUnicode        [IN]        array containing  UCS2 encoded characters
  172.  *  arr             [IN]        unicode equivalent
  173.  * RETURNS
  174.  *  U8 -> Status
  175.  *****************************************************************************/
  176. U8 UCS2EncodingToUnicode(PU16 pUnicode, PU8 arr)
  177. {
  178. #ifndef MMI_ON_WIN32
  179.     /*----------------------------------------------------------------*/
  180.     /* Local Variables                                                */
  181.     /*----------------------------------------------------------------*/
  182.     /*----------------------------------------------------------------*/
  183.     /* Code Body                                                      */
  184.     /*----------------------------------------------------------------*/
  185.     /* On the Target */
  186.     return (U8)app_ucs2encoding_to_unicode((kal_uint16 *)pUnicode, (kal_uint8*)arr);
  187. #else
  188.     /*----------------------------------------------------------------*/
  189.     /* Local Variables                                                */
  190.     /*----------------------------------------------------------------*/
  191.     U8 index = 0;
  192.     U8 status = ST_SUCCESS;
  193.     /*----------------------------------------------------------------*/
  194.     /* Code Body                                                      */
  195.     /*----------------------------------------------------------------*/
  196.     if ((arr != NULL) && (pUnicode != NULL))
  197.     {
  198.         *((U8*) (pUnicode)) = arr[index++];
  199.         *(((U8*) (pUnicode)) + 1) = arr[index];
  200.     }
  201.     else
  202.     {
  203.         status = ST_FAILURE;
  204.     }
  205.     return status;
  206. #endif
  207. }
  208. /*****************************************************************************
  209.  * FUNCTION
  210.  *  UCS2Strlen
  211.  * DESCRIPTION
  212.  *  Gives the length of UCS2 encoded string
  213.  * PARAMETERS
  214.  *  arrOut      [IN]        array containing  UCS2 encoded characters
  215.  * RETURNS
  216.  *  U16 -> Status
  217.  *****************************************************************************/
  218. S32 UCS2Strlen(const S8 *arrOut)
  219. {
  220. #ifndef MMI_ON_WIN32
  221.     /*----------------------------------------------------------------*/
  222.     /* Local Variables                                                */
  223.     /*----------------------------------------------------------------*/
  224.     /*----------------------------------------------------------------*/
  225.     /* Code Body                                                      */
  226.     /*----------------------------------------------------------------*/
  227.     /* On the Target */
  228.     return (S32)app_ucs2_strlen((const kal_int8 *)arrOut);
  229. #else
  230.     /*----------------------------------------------------------------*/
  231.     /* Local Variables                                                */
  232.     /*----------------------------------------------------------------*/
  233.     S32 nCount = 0;
  234.     S32 nLength = 0;
  235.     /*----------------------------------------------------------------*/
  236.     /* Code Body                                                      */
  237.     /*----------------------------------------------------------------*/
  238.     /* Check for NULL character only at the odd no. of bytes 
  239.        assuming forst byte start from zero */
  240.     if (arrOut)
  241.     {
  242.         while (arrOut[nCount] != 0 || arrOut[nCount + 1] != 0)
  243.         {
  244.             ++nLength;
  245.             nCount += 2;
  246.         }
  247.     }
  248.     return nLength; /* One is added to count 0th byte */
  249. #endif
  250. }
  251. /*****************************************************************************
  252.  * FUNCTION
  253.  *  UCS2Strcpy
  254.  * DESCRIPTION
  255.  *  copies the one UCS2 encoded string to other
  256.  * PARAMETERS
  257.  *  strDestination      [OUT]       StrDest-> Destination array
  258.  *  strSource           [IN]        
  259.  * RETURNS
  260.  *  PS8 -> pointer to destination string or NULL
  261.  *****************************************************************************/
  262. S8 *UCS2Strcpy(S8 *strDestination, const S8 *strSource)
  263. {
  264. #ifndef MMI_ON_WIN32
  265.     /*----------------------------------------------------------------*/
  266.     /* Local Variables                                                */
  267.     /*----------------------------------------------------------------*/
  268.     /*----------------------------------------------------------------*/
  269.     /* Code Body                                                      */
  270.     /*----------------------------------------------------------------*/
  271.     /* On the Target */
  272.     return (S8*)app_ucs2_strcpy((kal_int8*)strDestination, (const kal_int8*)strSource);
  273. #else
  274.     /*----------------------------------------------------------------*/
  275.     /* Local Variables                                                */
  276.     /*----------------------------------------------------------------*/
  277.     U16 count = 1;
  278.     S8 *temp = strDestination;
  279.     /*----------------------------------------------------------------*/
  280.     /* Code Body                                                      */
  281.     /*----------------------------------------------------------------*/
  282.     if (strSource == NULL)
  283.     {
  284.         if (strDestination)
  285.         {
  286.             *(strDestination + count - 1) = '';
  287.             *(strDestination + count) = '';
  288.         }
  289.         return temp;
  290.     }
  291.     if (strDestination == NULL || strSource == NULL)
  292.     {
  293.         return NULL;
  294.     }
  295.     while (!((*(strSource + count) == 0) && (*(strSource + count - 1) == 0)))
  296.     {
  297.         *(strDestination + count - 1) = *(strSource + count - 1);
  298.         *(strDestination + count) = *(strSource + count);
  299.         count += 2;
  300.     }
  301.     *(strDestination + count - 1) = '';
  302.     *(strDestination + count) = '';
  303.     return temp;
  304. #endif
  305. }
  306. /*****************************************************************************
  307.  * FUNCTION
  308.  *  UCS2Strcmp
  309.  * DESCRIPTION
  310.  *  compares two strings
  311.  * PARAMETERS
  312.  *  string1     [IN]        > first String
  313.  *  string2     [OUT]       > Second String
  314.  * RETURNS
  315.  *  
  316.  *****************************************************************************/
  317. S32 UCS2Strcmp(const S8 *string1, const S8 *string2)
  318. {
  319. #ifndef MMI_ON_WIN32
  320.     /*----------------------------------------------------------------*/
  321.     /* Local Variables                                                */
  322.     /*----------------------------------------------------------------*/
  323.     /*----------------------------------------------------------------*/
  324.     /* Code Body                                                      */
  325.     /*----------------------------------------------------------------*/
  326.     /* On the Target */
  327.     return (S32)app_ucs2_strcmp((const kal_int8*)string1, (const kal_int8*)string2);
  328. #else
  329.     /*----------------------------------------------------------------*/
  330.     /* Local Variables                                                */
  331.     /*----------------------------------------------------------------*/
  332.     /*----------------------------------------------------------------*/
  333.     /* Code Body                                                      */
  334.     /*----------------------------------------------------------------*/
  335.     while ((*string1 == *string2) && (*(string1 + 1) == *(string2 + 1)))
  336.     {
  337.         if ((*string1 == 0) && (*(string1 + 1) == 0))
  338.         {
  339.             return 0;
  340.         }
  341.         string1 += 2;
  342.         string2 += 2;
  343.     }   /* End of while */
  344.     /* The return value indicates the lexicographic relation of string1 to string2 */
  345.     /* ie  < 0 , > 0 and 0 */
  346.     if (*string1 == *string2 && *(string1 + 1) != *(string2 + 1))
  347.     {
  348.         return (*(string1 + 1) - *(string2 + 1));
  349.     }
  350.     else
  351.     {
  352.         return (*string1 - *string2);
  353.     }
  354. #endif
  355. }
  356. /*****************************************************************************
  357.  * FUNCTION
  358.  *  UCS2Strncmp
  359.  * DESCRIPTION
  360.  *  compares two strings
  361.  *  
  362.  *  In size pass no of characters not bytes
  363.  * PARAMETERS
  364.  *  string1     [IN]        > first String
  365.  *  string2     [OUT]       > Second String
  366.  *  size        [IN]        
  367.  * RETURNS
  368.  *  
  369.  *****************************************************************************/
  370. /* MTK Added by Tim for solve a potential wrong answer when string1 and string2 are the same and less than "size" */
  371. S32 UCS2Strncmp(const S8 *string1, const S8 *string2, U32 size)
  372. {
  373. #ifndef MMI_ON_WIN32
  374.     /*----------------------------------------------------------------*/
  375.     /* Local Variables                                                */
  376.     /*----------------------------------------------------------------*/
  377.     /*----------------------------------------------------------------*/
  378.     /* Code Body                                                      */
  379.     /*----------------------------------------------------------------*/
  380.     /* On the Target */
  381.     return (S32)app_ucs2_strncmp(
  382.                     (const kal_int8*)string1,
  383.                     (const kal_int8*)string2,
  384.                     (kal_uint32)size);
  385. #else
  386.     /*----------------------------------------------------------------*/
  387.     /* Local Variables                                                */
  388.     /*----------------------------------------------------------------*/
  389.     U32 count = 0;
  390.     U16 nStr1;
  391.     U16 nStr2;
  392.     /*----------------------------------------------------------------*/
  393.     /* Code Body                                                      */
  394.     /*----------------------------------------------------------------*/
  395.     size = size << 1;   /* User is passing no of charcters not bytes */
  396.     while (count < size)
  397.     {
  398.         nStr1 = (string1[1] << 8) | string1[0];
  399.         nStr2 = (string2[1] << 8) | string2[0];
  400.         if (nStr1 == 0 || nStr2 == 0 || nStr1 != nStr2)
  401.         {
  402.             return nStr1 - nStr2;
  403.         }
  404.         string1 += 2;
  405.         string2 += 2;
  406.         count += 2;
  407.     }
  408.     return 0;
  409. #endif
  410. }
  411. /*****************************************************************************
  412.  * FUNCTION
  413.  *  UCS2Strncpy
  414.  * DESCRIPTION
  415.  *  copies the one UCS2 encoded string to other
  416.  *  
  417.  *  In size pass no of characters not bytes
  418.  * PARAMETERS
  419.  *  strDestination      [OUT]       StrDest-> Destination array
  420.  *  strSource           [IN]        
  421.  *  size                [IN]        
  422.  * RETURNS
  423.  *  PS8 -> pointer to destination string or NULL
  424.  *****************************************************************************/
  425. S8 *UCS2Strncpy(S8 *strDestination, const S8 *strSource, U32 size)
  426. {
  427. #ifndef MMI_ON_WIN32
  428.     /*----------------------------------------------------------------*/
  429.     /* Local Variables                                                */
  430.     /*----------------------------------------------------------------*/
  431.     /*----------------------------------------------------------------*/
  432.     /* Code Body                                                      */
  433.     /*----------------------------------------------------------------*/
  434.     /* On the Target */
  435.     return (S8*)app_ucs2_strncpy(
  436.                     (kal_int8*)strDestination,
  437.                     (const kal_int8*)strSource,
  438.                     (kal_uint32)size);
  439. #else
  440.     /*----------------------------------------------------------------*/
  441.     /* Local Variables                                                */
  442.     /*----------------------------------------------------------------*/
  443.     U16 count = 1;
  444.     U32 count1 = 0;
  445.     S8 *temp = strDestination;
  446.     /*----------------------------------------------------------------*/
  447.     /* Code Body                                                      */
  448.     /*----------------------------------------------------------------*/
  449.     size = size * 2;
  450.     MMI_ASSERT(!(strDestination == NULL));
  451.     while (!((*(strSource + count) == 0) && (*(strSource + count - 1) == 0)) && (count1 < size))
  452.     {
  453.         *(strDestination + count - 1) = *(strSource + count - 1);
  454.         *(strDestination + count) = *(strSource + count);
  455.         count += 2;
  456.         count1 += 2;
  457.     }
  458.     *(strDestination + count - 1) = '';
  459.     *(strDestination + count) = '';
  460.     return temp;
  461. #endif
  462. }
  463. /*****************************************************************************
  464.  * FUNCTION
  465.  *  UCS2Strcat
  466.  * DESCRIPTION
  467.  *  
  468.  *  
  469.  *  User has to ensure that enough space is
  470.  *  available in destination
  471.  * PARAMETERS
  472.  *  strDestination      [OUT]         
  473.  *  strSource           [IN]        
  474.  * RETURNS
  475.  *  PS8
  476.  *****************************************************************************/
  477. S8 *UCS2Strcat(S8 *strDestination, const S8 *strSource)
  478. {
  479. #ifndef MMI_ON_WIN32
  480.     /*----------------------------------------------------------------*/
  481.     /* Local Variables                                                */
  482.     /*----------------------------------------------------------------*/
  483.     /*----------------------------------------------------------------*/
  484.     /* Code Body                                                      */
  485.     /*----------------------------------------------------------------*/
  486.     /* On the Target */
  487.     return (S8*)app_ucs2_strcat((kal_int8*)strDestination, (const kal_int8*)strSource);
  488. #else
  489.     /*----------------------------------------------------------------*/
  490.     /* Local Variables                                                */
  491.     /*----------------------------------------------------------------*/
  492.     S8 *dest = strDestination;
  493.     /*----------------------------------------------------------------*/
  494.     /* Code Body                                                      */
  495.     /*----------------------------------------------------------------*/
  496.     dest = dest + UCS2Strlen(strDestination) * ENCODING_LENGTH;
  497.     UCS2Strcpy(dest, strSource);
  498.     return strDestination;
  499. #endif
  500. }
  501. /*****************************************************************************
  502.  * FUNCTION
  503.  *  UCS2Strncat
  504.  * DESCRIPTION
  505.  *  
  506.  *  
  507.  *  User has to ensure that enough space is
  508.  *  available in destination
  509.  * PARAMETERS
  510.  *  strDestination      [OUT]         
  511.  *  strSource           [IN]        
  512.  *  size                [IN]        
  513.  * RETURNS
  514.  *  PS8
  515.  *****************************************************************************/
  516. S8 *UCS2Strncat(S8 *strDestination, const S8 *strSource, U32 size)
  517. {
  518. #ifndef MMI_ON_WIN32
  519.     /*----------------------------------------------------------------*/
  520.     /* Local Variables                                                */
  521.     /*----------------------------------------------------------------*/
  522.     /*----------------------------------------------------------------*/
  523.     /* Code Body                                                      */
  524.     /*----------------------------------------------------------------*/
  525.     /* On the Target */
  526.     return (S8*)app_ucs2_strncat(
  527.                     (kal_int8*)strDestination, 
  528.                     (const kal_int8*)strSource,
  529.                     (kal_uint32)size);
  530. #else
  531.     /*----------------------------------------------------------------*/
  532.     /* Local Variables                                                */
  533.     /*----------------------------------------------------------------*/
  534.     S8 *dest = strDestination;
  535.     /*----------------------------------------------------------------*/
  536.     /* Code Body                                                      */
  537.     /*----------------------------------------------------------------*/
  538.     dest = dest + UCS2Strlen(strDestination) * ENCODING_LENGTH;
  539.     UCS2Strncpy(dest, strSource, size);
  540.     return strDestination;
  541. #endif
  542. }
  543. /*****************************************************************************
  544.  * FUNCTION
  545.  *  UCS2StrAppendChar
  546.  * DESCRIPTION
  547.  *  
  548.  *  
  549.  *  User has to ensure that enough space is
  550.  *  available in destination
  551.  * PARAMETERS
  552.  *  strDestination      [OUT]         
  553.  *  ch                  [IN]        
  554.  * RETURNS
  555.  *  PS8
  556.  *****************************************************************************/
  557. S8 *UCS2StrAppendChar(S8 *strDestination, U16 ch)
  558. {
  559. #ifndef MMI_ON_WIN32
  560.     /*----------------------------------------------------------------*/
  561.     /* Local Variables                                                */
  562.     /*----------------------------------------------------------------*/
  563.     /*----------------------------------------------------------------*/
  564.     /* Code Body                                                      */
  565.     /*----------------------------------------------------------------*/
  566.     /* On the Target */
  567.     return (S8*)app_ucs2str_appendchar((kal_int8*)strDestination, (kal_uint16) ch);
  568. #else
  569.     /*----------------------------------------------------------------*/
  570.     /* Local Variables                                                */
  571.     /*----------------------------------------------------------------*/
  572.     S8 *dest = strDestination;
  573.     U16 buffer[2];
  574.     /*----------------------------------------------------------------*/
  575.     /* Code Body                                                      */
  576.     /*----------------------------------------------------------------*/
  577.     dest = dest + UCS2Strlen(strDestination) * ENCODING_LENGTH;
  578.     buffer[0] = ch;
  579.     buffer[1] = 0;
  580.     UCS2Strcpy(dest, (const S8*)buffer);
  581.     return strDestination;
  582. #endif
  583. }
  584. /*****************************************************************************
  585.  * FUNCTION
  586.  *  UCS2StrNAppendChar
  587.  * DESCRIPTION
  588.  *  
  589.  *  
  590.  *  User has to ensure that enough space is
  591.  *  available in destination
  592.  * PARAMETERS
  593.  *  strDestination      [OUT]         
  594.  *  ch                  [IN]        
  595.  *  size                [IN]        
  596.  * RETURNS
  597.  *  PS8
  598.  *****************************************************************************/
  599. S8 *UCS2StrNAppendChar(S8 *strDestination, U16 ch, U32 size)
  600. {
  601. #ifndef MMI_ON_WIN32
  602.     /*----------------------------------------------------------------*/
  603.     /* Local Variables                                                */
  604.     /*----------------------------------------------------------------*/
  605.     /*----------------------------------------------------------------*/
  606.     /* Code Body                                                      */
  607.     /*----------------------------------------------------------------*/
  608.     /* On the Target */
  609.     return (S8*)app_ucs2str_n_appendchar(
  610.                     (kal_int8*)strDestination,
  611.                     (kal_uint16) ch,
  612.                     (kal_uint32)size);
  613. #else
  614.     /*----------------------------------------------------------------*/
  615.     /* Local Variables                                                */
  616.     /*----------------------------------------------------------------*/
  617.     S8 *dest = strDestination;
  618.     U16 buffer[2];
  619.     /*----------------------------------------------------------------*/
  620.     /* Code Body                                                      */
  621.     /*----------------------------------------------------------------*/
  622.     dest = dest + UCS2Strlen(strDestination) * ENCODING_LENGTH;
  623.     buffer[0] = ch;
  624.     buffer[1] = 0;
  625.     UCS2Strncpy(dest, (const S8*)buffer, size);
  626.     return strDestination;
  627. #endif
  628. }
  629. /*****************************************************************************
  630.  * FUNCTION
  631.  *  UCS2Strchr
  632.  * DESCRIPTION
  633.  *  find the next chr 'c'
  634.  * IMPACT
  635.  *  will not affect input strSrc, but return a pointer to point out the place.
  636.  * PARAMETERS
  637.  *  strSrc      [IN]        
  638.  *  c           [IN]        
  639.  * RETURNS
  640.  *  
  641.  *****************************************************************************/
  642. S8 *UCS2Strchr(const S8 *strSrc, U16 c)
  643. {
  644.     /*----------------------------------------------------------------*/
  645.     /* Local Variables                                                */
  646.     /*----------------------------------------------------------------*/
  647.     U16 *chr = (U16*) strSrc;
  648.     /*----------------------------------------------------------------*/
  649.     /* Code Body                                                      */
  650.     /*----------------------------------------------------------------*/
  651.     do
  652.     {
  653.         if (*chr == c)
  654.         {
  655.             return (S8*) chr;
  656.         }
  657.     } while (*(++chr));
  658.     return NULL;
  659. }
  660. /*****************************************************************************
  661.  * FUNCTION
  662.  *  UCS2StrTrimLeft
  663.  * DESCRIPTION
  664.  *  skip over the TrimChars
  665.  * PARAMETERS
  666.  *  strSrc          [IN]        
  667.  *  TrimChars       [IN]        
  668.  * RETURNS
  669.  *  
  670.  *****************************************************************************/
  671. S8 *UCS2StrTrimLeft(const S8 *strSrc, const S8 *TrimChars)
  672. {
  673.     /*----------------------------------------------------------------*/
  674.     /* Local Variables                                                */
  675.     /*----------------------------------------------------------------*/
  676.     U16 *chr = (U16*) strSrc;
  677.     S32 trim_length = UCS2Strlen(TrimChars);
  678.     /*----------------------------------------------------------------*/
  679.     /* Code Body                                                      */
  680.     /*----------------------------------------------------------------*/
  681.     while (*chr)
  682.     {
  683.         S32 i = 0;
  684.         for (; i < trim_length; i++)
  685.         {
  686.             if (*chr == ((U16*) TrimChars)[i])
  687.             {
  688.                 chr++;
  689.                 break;
  690.             }
  691.         }
  692.         if (i == trim_length)
  693.         {
  694.             return (S8*) chr;
  695.         }
  696.     }
  697.     return NULL;
  698. }
  699. /*****************************************************************************
  700.  * FUNCTION
  701.  *  UCS2StrGetStr
  702.  * DESCRIPTION
  703.  *  search for the break_chars and store how many chars have been read over to reach
  704.  *  the desired break_char in read_length
  705.  * PARAMETERS
  706.  *  strSrc          [IN]        
  707.  *  break_chars     [IN]        
  708.  *  read_length     [?]         
  709.  *  direction       [IN]        1: sesarch forward ; -1: search backward.
  710.  * RETURNS
  711.  *  1: found the break_chars ;  -1: searched over the whole strSrc and do not fing the break_chars
  712.  *****************************************************************************/
  713. S32 UCS2StrGetStr(const S8 *strSrc, const S8 *break_chars, S32 *read_length, S16 direction)
  714. {
  715.     /*----------------------------------------------------------------*/
  716.     /* Local Variables                                                */
  717.     /*----------------------------------------------------------------*/
  718.     U16 *src = (U16*) strSrc;
  719.     S32 x = 0;
  720.     S32 break_chars_length = UCS2Strlen(break_chars);
  721.     /*----------------------------------------------------------------*/
  722.     /* Code Body                                                      */
  723.     /*----------------------------------------------------------------*/
  724.     while (*src)
  725.     {
  726.         for (x = 0; x < break_chars_length; x++)
  727.         {
  728.             if (*src == ((U16*) break_chars)[x])
  729.             {
  730.                 if (direction > 0)
  731.                 {
  732.                     *read_length = src - (U16*) strSrc;
  733.                 }
  734.                 else
  735.                 {
  736.                     *read_length = (U16*) strSrc - src;
  737.                 }
  738.                 return 1;
  739.             }
  740.         }
  741.         src += direction;
  742.     }
  743.     return -1;
  744. }
  745. /*****************************************************************************
  746.  * FUNCTION
  747.  *  UCS2StrGetNumInt
  748.  * DESCRIPTION
  749.  *  to parse over a int number, including positive and negative signs (+,-).
  750.  * PARAMETERS
  751.  *  strSrc          [IN]        
  752.  *  out_num         [?]         Is the parsed int number
  753.  *  read_length     [?]         
  754.  * RETURNS
  755.  *  1: parse successfully ; -1: parse number fail.
  756.  *****************************************************************************/
  757. S32 UCS2StrGetNumInt(const S8 *strSrc, S32 *out_num, S32 *read_length)
  758. {
  759.     /*----------------------------------------------------------------*/
  760.     /* Local Variables                                                */
  761.     /*----------------------------------------------------------------*/
  762. #define     GETNUM(wchar_p) (*wchar_p - L'0')
  763.     U16 *src = (U16*) strSrc;
  764.     BOOL is_negative = FALSE;
  765.     S32 num = 0;
  766.     /*----------------------------------------------------------------*/
  767.     /* Code Body                                                      */
  768.     /*----------------------------------------------------------------*/
  769.     if (*src == L'-')
  770.     {
  771.         is_negative = TRUE;
  772.         src++;
  773.     }
  774.     if (*src == L'+')
  775.     {
  776.         src++;
  777.     }
  778.     if (GETNUM(src) < 0 || GETNUM(src) > 9)
  779.     {
  780.         return -1;
  781.     }
  782.     while (*src)
  783.     {
  784.         if (GETNUM(src) < 0 || GETNUM(src) > 9)
  785.         {
  786.             break;
  787.         }
  788.         num = num * 10 + GETNUM(src);
  789.         src++;
  790.     }
  791.     *read_length = src - (U16*) strSrc;
  792.     if (is_negative)
  793.     {
  794.         *out_num = -num;
  795.     }
  796.     else
  797.     {
  798.         *out_num = num;
  799.     }
  800.     return 1;
  801. }
  802. /*****************************************************************************
  803.  * FUNCTION
  804.  *  AnsiiToUnicodeString
  805.  * DESCRIPTION
  806.  *  Converts Ansii encode string to unicode
  807.  *  
  808.  *  Caller has to ensure that pOutBuffer
  809.  *  should be as large
  810.  * PARAMETERS
  811.  *  pOutBuffer      [OUT]     
  812.  *  pInBuffer       [IN]     
  813.  * RETURNS
  814.  *  U16
  815.  *****************************************************************************/
  816. U16 AnsiiToUnicodeString(S8 *pOutBuffer, S8 *pInBuffer)
  817. {
  818. #ifndef MMI_ON_WIN32
  819.     /*----------------------------------------------------------------*/
  820.     /* Local Variables                                                */
  821.     /*----------------------------------------------------------------*/
  822.     /*----------------------------------------------------------------*/
  823.     /* Code Body                                                      */
  824.     /*----------------------------------------------------------------*/
  825.     /* On the Target */
  826.     return (U16)app_ansii_to_unicodestring((kal_int8*)pOutBuffer, (kal_int8*)pInBuffer);
  827. #else
  828.     /*----------------------------------------------------------------*/
  829.     /* Local Variables                                                */
  830.     /*----------------------------------------------------------------*/
  831.     S16 count = -1;
  832.     U8 charLen = 0;
  833.     U8 arrOut[2];
  834.     /*----------------------------------------------------------------*/
  835.     /* Code Body                                                      */
  836.     /*----------------------------------------------------------------*/
  837.     while (*pInBuffer != '')
  838.     {
  839.         UnicodeToUCS2Encoding((U16) * ((PU8) pInBuffer), &charLen, arrOut);
  840.         // #ifdef MMI_ON_WIN32
  841.         pOutBuffer[++count] = arrOut[0];
  842.         pOutBuffer[++count] = arrOut[1];
  843.         pInBuffer++;
  844.         // #endif
  845.     #ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
  846.         pOutBuffer[++count] = arrOut[1];    /* arrOut[0]; */
  847.         pOutBuffer[++count] = arrOut[0];    /* arrOut[1]; */
  848.         pInBuffer++;
  849.     #endif /* __FOR_TESTING */ 
  850.     }
  851.     pOutBuffer[++count] = '';
  852.     pOutBuffer[++count] = '';
  853.     return count + 1;
  854. #endif
  855. }
  856. /*****************************************************************************
  857.  * FUNCTION
  858.  *  AnsiiNToUnicodeString
  859.  * DESCRIPTION
  860.  *  Converts N character Ansii encode string to unicode
  861.  *  
  862.  *  Caller has to ensure that pOutBuffer
  863.  *  should be as large
  864.  * PARAMETERS
  865.  *  pOutBuffer      [OUT]         
  866.  *  pInBuffer       [IN]         
  867.  *  len             [IN]        
  868.  * RETURNS
  869.  *  U16
  870.  *****************************************************************************/
  871. U16 AnsiiNToUnicodeString(S8 *pOutBuffer, S8 *pInBuffer, U32 len)
  872. {
  873. #ifndef MMI_ON_WIN32
  874.     /*----------------------------------------------------------------*/
  875.     /* Local Variables                                                */
  876.     /*----------------------------------------------------------------*/
  877.     /*----------------------------------------------------------------*/
  878.     /* Code Body                                                      */
  879.     /*----------------------------------------------------------------*/
  880.     /* On the Target */
  881.     return (U16)app_ansii_n_to_unicodestring(
  882.                     (kal_int8*)pOutBuffer,
  883.                     (kal_int8*)pInBuffer,
  884.                     (kal_uint32)len);
  885. #else
  886.     /*----------------------------------------------------------------*/
  887.     /* Local Variables                                                */
  888.     /*----------------------------------------------------------------*/
  889.     S16 count = -1;
  890.     U8 charLen = 0;
  891.     U8 arrOut[2];
  892.     /*----------------------------------------------------------------*/
  893.     /* Code Body                                                      */
  894.     /*----------------------------------------------------------------*/
  895.     while (len)
  896.     {
  897.         UnicodeToUCS2Encoding((U16) * ((PU8) pInBuffer), &charLen, arrOut);
  898.         // #ifdef MMI_ON_WIN32
  899.         pOutBuffer[++count] = arrOut[0];
  900.         pOutBuffer[++count] = arrOut[1];
  901.         if (*pInBuffer == '')
  902.         {
  903.             break;
  904.         }
  905.         else
  906.         {
  907.             pInBuffer++;
  908.         }
  909.         // #endif
  910.     #ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
  911.         pOutBuffer[++count] = arrOut[1];    /* arrOut[0]; */
  912.         pOutBuffer[++count] = arrOut[0];    /* arrOut[1]; */
  913.         pInBuffer++;
  914.     #endif /* __FOR_TESTING */ 
  915.         len--;
  916.     }
  917.     return count + 1;
  918. #endif
  919. }
  920. /*****************************************************************************
  921.  * FUNCTION
  922.  *  UnicodeToAnsii
  923.  * DESCRIPTION
  924.  *  Converts Unicode encode string to Ascii
  925.  *  
  926.  *  Caller has to ensure that pOutBuffer
  927.  *  should be  large enough
  928.  * PARAMETERS
  929.  *  pOutBuffer      [OUT]     
  930.  *  pInBuffer       [IN]     
  931.  * RETURNS
  932.  *  U16
  933.  *****************************************************************************/
  934. U16 UnicodeToAnsii(S8 *pOutBuffer, S8 *pInBuffer)
  935. {
  936. #ifndef MMI_ON_WIN32
  937.     /*----------------------------------------------------------------*/
  938.     /* Local Variables                                                */
  939.     /*----------------------------------------------------------------*/
  940.     /*----------------------------------------------------------------*/
  941.     /* Code Body                                                      */
  942.     /*----------------------------------------------------------------*/
  943.     /* On the Target */
  944.     return (U16)app_unicode_to_ansii((kal_int8*)pOutBuffer,(kal_int8*)pInBuffer);
  945. #else
  946.     /*----------------------------------------------------------------*/
  947.     /* Local Variables                                                */
  948.     /*----------------------------------------------------------------*/
  949.     U16 count = 0;
  950.     /*----------------------------------------------------------------*/
  951.     /* Code Body                                                      */
  952.     /*----------------------------------------------------------------*/
  953.     while (!((*pInBuffer == 0) && (*(pInBuffer + 1) == 0)))
  954.     {
  955.         *pOutBuffer = *(pInBuffer);
  956.     #ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
  957.         *pOutBuffer = *(pInBuffer + 1);
  958.     #endif 
  959.         pInBuffer += 2;
  960.         pOutBuffer++;
  961.         count++;
  962.     }
  963.     *pOutBuffer = 0;
  964.     return count;
  965. #endif
  966. }
  967. /*****************************************************************************
  968.  * FUNCTION
  969.  *  UnicodeNToAnsii
  970.  * DESCRIPTION
  971.  *  Converts N character Unicode encode string to Ascii
  972.  *  
  973.  *  Caller has to ensure that pOutBuffer
  974.  *  should be  large enough
  975.  * PARAMETERS
  976.  *  pOutBuffer      [OUT]         
  977.  *  pInBuffer       [IN]         
  978.  *  len             [IN]        
  979.  * RETURNS
  980.  *  U16
  981.  *****************************************************************************/
  982. U16 UnicodeNToAnsii(S8 *pOutBuffer, S8 *pInBuffer, U32 len)
  983. {
  984. #ifndef MMI_ON_WIN32
  985.     /*----------------------------------------------------------------*/
  986.     /* Local Variables                                                */
  987.     /*----------------------------------------------------------------*/
  988.     /*----------------------------------------------------------------*/
  989.     /* Code Body                                                      */
  990.     /*----------------------------------------------------------------*/
  991.     /* On the Target */
  992.     return (U16)app_unicode_n_to_ansii(
  993.                     (kal_int8*)pOutBuffer,
  994.                     (kal_int8*)pInBuffer,
  995.                     (kal_uint32)len);
  996. #else
  997.     /*----------------------------------------------------------------*/
  998.     /* Local Variables                                                */
  999.     /*----------------------------------------------------------------*/
  1000.     U16 count = 0;
  1001.     /*----------------------------------------------------------------*/
  1002.     /* Code Body                                                      */
  1003.     /*----------------------------------------------------------------*/
  1004.     while ((len) && (!((*pInBuffer == 0) && (*(pInBuffer + 1) == 0))))
  1005.     {
  1006.         *pOutBuffer = *(pInBuffer);
  1007.     #ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
  1008.         *pOutBuffer = *(pInBuffer + 1);
  1009.     #endif 
  1010.         pInBuffer += 2;
  1011.         pOutBuffer++;
  1012.         count++;
  1013.         len -= 2;
  1014.     }
  1015.     return count;
  1016. #endif
  1017. }