Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855559
  • 博文数量: 102
  • 博客积分: 7086
  • 博客等级: 少将
  • 技术积分: 2245
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:01
文章分类

全部博文(102)

文章存档

2012年(2)

2011年(1)

2010年(21)

2009年(31)

2008年(47)

我的朋友

分类: C/C++

2008-06-27 11:33:50

#include
#include
#include

typedef unsigned char           kal_uint8;
typedef signed char             kal_int8;
typedef char                    kal_char;
typedef unsigned short          kal_wchar;

typedef unsigned short int      kal_uint16;
typedef signed short int        kal_int16;

typedef unsigned int            kal_uint32;
typedef signed int              kal_int32;


typedef enum
{
    KAL_FALSE,
    KAL_TRUE
} kal_bool;


#ifndef NULL
#define NULL    (void *)0x0000
#endif


#define ENCODING_LENGTH       2

/*****************************************************************************
* FUNCTION
*  app_unicode_to_ucs2encoding
* DESCRIPTION
*  convert unicode to UCS2 encoding
* PARAMETERS
*  unicode         [IN]        Value to be encoded
*  charLength      [OUT]         
*  arrOut          [OUT]         
*  array           [OUT]       Of kal_uint8
* RETURNS
*  kal_uint8 -> Status
*****************************************************************************/
kal_uint8 app_unicode_to_ucs2encoding(kal_wchar unicode, kal_uint8 *charLength, kal_uint8 *arrOut)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_uint8 status = 1;
    kal_uint8 index = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (arrOut != 0)
    {

        if (unicode < 256)
        {
            arrOut[index++] = *((kal_uint8*) (&unicode));
            arrOut[index] = 0;

        }
        else
        {
            arrOut[index++] = *((kal_uint8*) (&unicode));
            arrOut[index] = *(((kal_uint8*) (&unicode)) + 1);

        }
        *charLength = 2;
    }
    else
    {

        status = 0;
    }

#ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
    if (arrOut != 0)
    {

        if (unicode < 256)
        {
            arrOut[index++] = 0;    /* *((kal_uint8*)(&unicode)); */
            arrOut[index] = *((kal_uint8*) (&unicode));

        }
        else
        {
            arrOut[index++] = *(((kal_uint8*) (&unicode)) + 1);        /* *((kal_uint8*)(&unicode)); */
            arrOut[index] = *((kal_uint8*) (&unicode)); /* *(((kal_uint8*)(&unicode)) + 1); */

        }
        *charLength = 2;
    }
    else
    {

        status = 0;
    }
#endif /* __FOR_TESTING */

    return status;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2encoding_to_unicode
* DESCRIPTION
*  convert UCS2 encoded scheme to unicode
* PARAMETERS
*  pUnicode        [OUT]       Equivalent  
*  arr             [IN]        array containing  UCS2 encoded characters
* RETURNS
*  kal_uint8 -> Status
*****************************************************************************/
kal_uint8 app_ucs2encoding_to_unicode(kal_wchar *pUnicode, kal_uint8 *arr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_uint8 index = 0;
    kal_uint8 status = 1;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if ((arr != NULL) && (pUnicode != NULL))
    {

        *((kal_uint8*) (pUnicode)) = arr[index++];
        *(((kal_uint8*) (pUnicode)) + 1) = arr[index];
    }
    else
    {

        status = 0;
    }

#ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
    if ((arr != NULL) && (pUnicode != NULL))
    {

        *((kal_uint8*) (pUnicode)) = arr[index];            /* arr[index++]; */
        *(((kal_uint8*) (pUnicode)) + 1) = arr[index++];    /* arr[index]; */
    }
    else
    {

        status = 0;
    }
#endif /* __FOR_TESTING */
    return status;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_strlen
* DESCRIPTION
*  The function is used for query UCS2 string length in character not in bytes.
  * PARAMETERS
*  arrOut    [IN]  UCS2 string     
* RETURNS
*  Return UCS2 string length in character not in bytes
*****************************************************************************/
kal_int32 app_ucs2_strlen(const kal_int8 *arrOut)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_int32 nCount = 0;
    kal_int32 nLength = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* Check for NULL character only at the odd no. of bytes
       assuming forst byte start from zero */
    if (arrOut)
    {
        while (arrOut[nCount] != 0 || arrOut[nCount + 1] != 0)
        {
            ++nLength;
            nCount += 2;
        }
    }
    return nLength; /* One is added to count 0th byte */
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_strcmp
* DESCRIPTION
*  The function is used for compare UCS2 strings.
* PARAMETERS
*  string1     [IN]   UCS2 string1
*  string2     [IN]   UCS2 string2
* RETURNS
*  <0 - string1 less than string2
*   0 - string1 identical to string2
*  >0 - string1 greater than string2
*****************************************************************************/
kal_int32 app_ucs2_strcmp(const kal_int8 *string1, const kal_int8 *string2)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while ((*string1 == *string2) && (*(string1 + 1) == *(string2 + 1)))
    {

        if ((*string1 == 0) && (*(string1 + 1) == 0))
        {
            return 0;
        }

        string1 += 2;
        string2 += 2;

    }   /* End of while */

    /* The return value indicates the lexicographic relation of string1 to string2 */
    /* ie  < 0 , > 0 and 0 */
    if (*string1 == *string2 && *(string1 + 1) != *(string2 + 1))
    {
        return (*(string1 + 1) - *(string2 + 1));
    }
    else
    {
        return (*string1 - *string2);

    }

}

/*****************************************************************************
* FUNCTION
*  app_ucs2_unconditional_strcmp
* DESCRIPTION
*  compares two strings no conditional about uppercase and lowercase.
* PARAMETERS
*  string1     [IN]        > first String
*  string2     [IN]        > Second String
* RETURNS
*  
*****************************************************************************/
kal_int32 app_ucs2_unconditional_strcmp(const kal_int8 *string1, const kal_int8 *string2)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while ((*string1 == *string2) ||
           ((*string1 + 0x20) == *string2) ||
           (*string1 == (*string2 + 0x20)) &&
           (*(string1 + 1) == *(string2 + 1)))
    {

        if ((*string1 == 0) && (*(string1 + 1) == 0))
        {
            return 0;
        }

        string1 += 2;
        string2 += 2;

    }   /* End of while */

    /* The return value indicates the lexicographic relation of string1 to string2 */
    /* ie  < 0 , > 0 and 0 */
    if (*string1 == *string2 && *(string1 + 1) != *(string2 + 1))
    {
        return (*(string1 + 1) - *(string2 + 1));
    }
    else
    {
        return (*string1 - *string2);

    }
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_strcpy
* DESCRIPTION
*  The function is used for copy UCS2 string.
* PARAMETERS
*  strDestination  [OUT]   UCS2 destination string
*  strSource       [IN]    UCS2 source string
* RETURNS
*  Return the destination string.
*****************************************************************************/
kal_int8 *app_ucs2_strcpy(kal_int8 *strDestination, const kal_int8 *strSource)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_wchar count = 1;
    kal_int8 *temp = strDestination;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (strSource == NULL)
    {
        if (strDestination)
        {
            *(strDestination + count - 1) = '\0';
            *(strDestination + count) = '\0';
        }
        return temp;

    }

    if (strDestination == NULL || strSource == NULL)
    {
        return NULL;
    }
    while (!((*(strSource + count) == 0) && (*(strSource + count - 1) == 0)))
    {

        *(strDestination + count - 1) = *(strSource + count - 1);
        *(strDestination + count) = *(strSource + count);
        count += 2;
    }

    *(strDestination + count - 1) = '\0';
    *(strDestination + count) = '\0';

    return temp;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_strncmp
* DESCRIPTION
*  The function is used for compare UCS2 characters of two strings. Size is
*  the character number not the byte numbers.
* PARAMETERS
*  string1     [IN]  UCS2 string1
*  string2     [IN]  UCS2 string2
*  size        [IN]  Number in character (not byte)      
* RETURNS
*  <0 - string1 substring less than string2 substring
*   0 - string1 substring identical to string2 substring
*  >0 - string1 substring greater than string2 substring
*****************************************************************************/
/* MTK Added by Tim for solve a potential wrong answer when string1 and string2 are the same and less than "size" */
kal_int32 app_ucs2_strncmp(const kal_int8 *string1, const kal_int8 *string2, kal_uint32 size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint32 count = 0;
    kal_wchar nStr1;
    kal_wchar nStr2;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    size = size << 1;   /* User is passing no of charcters not bytes */

    while (count < size)
    {
        nStr1 = (string1[1] << 8) | (kal_uint8)string1[0];
        nStr2 = (string2[1] << 8) | (kal_uint8)string2[0];
        if (nStr1 == 0 || nStr2 == 0 || nStr1 != nStr2)
        {
            return nStr1 - nStr2;
        }
        string1 += 2;
        string2 += 2;
        count += 2;
    }
    return 0;
}

/* End MTK: Tim */


/*****************************************************************************
* FUNCTION
*  app_ucs2_strncpy
* DESCRIPTION
*  The function is used for copy UCS2 characters of one string to another.
*  If size is less than the length of strSource, a null character is still
*  appended automatically to strDestination. If size is greater than the
*  length of strSource, strDestination is padding with null characters up
*  to length size. The function behavior is a little bit different from strncpy.
* PARAMETERS
*  strDestination  [OUT]  UCS2 destination string
*  strSource       [IN]   UCS2 source string
*  size            [IN]   Size in character (not byte)
* RETURNS
*  Return the destination string.
*****************************************************************************/
kal_int8 *app_ucs2_strncpy(kal_int8 *strDestination, const kal_int8 *strSource, kal_uint32 size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_wchar count = 1;
    kal_uint32 count1 = 0;
    kal_int8 *temp = strDestination;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    size = size * 2;

    /* MMI_ASSERT(!(strDestination == NULL)); */

    while (!((*(strSource + count) == 0) && (*(strSource + count - 1) == 0)) && (count1 < size))
    {

        *(strDestination + count - 1) = *(strSource + count - 1);
        *(strDestination + count) = *(strSource + count);
        count += 2;
        count1 += 2;
    }

    *(strDestination + count - 1) = '\0';
    *(strDestination + count) = '\0';

    return temp;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_strcat
* DESCRIPTION
*  The function is used for append strSource to strDestination and terminate
*  the resulting string with a null character.
* PARAMETERS
*  strDestination   [OUT]  UCS2 destination string      
*  strSource        [IN]   UCS2 source string     
* RETURNS
*  Return the destination string
*****************************************************************************/
kal_int8 *app_ucs2_strcat(kal_int8 *strDestination, const kal_int8 *strSource)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_int8 *dest = strDestination;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    dest = dest + app_ucs2_strlen(strDestination) * ENCODING_LENGTH;

    app_ucs2_strcpy(dest, strSource);
    return strDestination;

}


/*****************************************************************************
* FUNCTION
*  app_ucs2_strncat
* DESCRIPTION
*  The function is used for append the first size characters of strSource
*  to strDestination and terminate the resulting string with a null character.
* PARAMETERS
*  strDestination      [OUT]   UCS2 destination string      
*  strSource           [IN]    UCS2 source string   
*  size                [IN]    Size in character (not byte)   
* RETURNS
*  Return the destination string
*****************************************************************************/
kal_int8 *app_ucs2_strncat(kal_int8 *strDestination, const kal_int8 *strSource, kal_uint32 size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_int8 *dest = strDestination;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    dest = dest + app_ucs2_strlen(strDestination) * ENCODING_LENGTH;

    app_ucs2_strncpy(dest, strSource, size);
    return strDestination;

}


/*****************************************************************************
* FUNCTION
*  app_ucs2str_appendchar
* DESCRIPTION
*  
*  
*  User has to ensure that enough space is
*  available in destination
* PARAMETERS
*  strDestination      [OUT]         
*  ch                  [IN]        
* RETURNS
*  kal_int8
*****************************************************************************/
kal_int8 *app_ucs2str_appendchar(kal_int8 *strDestination, kal_wchar ch)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *dest = strDestination;
    kal_wchar buffer[2];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    dest = dest + app_ucs2_strlen(strDestination) * ENCODING_LENGTH;
    buffer[0] = ch;
    buffer[1] = 0;

    app_ucs2_strcpy(dest, (const kal_int8*)buffer);
    return strDestination;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2str_n_appendchar
* DESCRIPTION
*  
*  
*  User has to ensure that enough space is
*  available in destination
* PARAMETERS
*  strDestination      [OUT]         
*  ch                  [IN]        
*  size                [IN]        
* RETURNS
*  kal_int8
*****************************************************************************/
kal_int8 *app_ucs2str_n_appendchar(kal_int8 *strDestination, kal_wchar ch, kal_uint32 size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *dest = strDestination;
    kal_wchar buffer[2];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    dest = dest + app_ucs2_strlen(strDestination) * ENCODING_LENGTH;
    buffer[0] = ch;
    buffer[1] = 0;

    app_ucs2_strncpy(dest, (const kal_int8*)buffer, size);
    return strDestination;
}


/*****************************************************************************
* FUNCTION
*  app_ansii_to_unicodestring
* DESCRIPTION
*  The function is used for convert ANSII string to UCS2 string. The caller
*  need to make sure the pOutBuffer  size must have enough space or the function
*  causes the memory corruption. The function will add the terminated character
*  at the end of pOutBuffer array. The byte order of UCS2 character(output param)
*  is little endian.
* PARAMETERS
*  pOutBuffer      [OUT]  UCS2 destination string   
*  pInBuffer       [IN]   ANSII source string  
* RETURNS
*  Return the bytes to convert.
*****************************************************************************/
kal_uint16 app_ansii_to_unicodestring(kal_int8 *pOutBuffer, kal_int8 *pInBuffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_int16 count = -1;
    kal_uint8 charLen = 0;
    kal_uint8 arrOut[2];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (*pInBuffer != '\0')
    {

        app_unicode_to_ucs2encoding((kal_wchar) (*((kal_uint8 *)pInBuffer)), &charLen, arrOut);

        // #ifdef MMI_ON_WIN32
        pOutBuffer[++count] = arrOut[0];
        pOutBuffer[++count] = arrOut[1];
        pInBuffer++;
        // #endif

    #ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
        pOutBuffer[++count] = arrOut[1];    /* arrOut[0]; */
        pOutBuffer[++count] = arrOut[0];    /* arrOut[1]; */
        pInBuffer++;
    #endif /* __FOR_TESTING */

    }

    pOutBuffer[++count] = '\0';
    pOutBuffer[++count] = '\0';
    return count + 1;
}


/*****************************************************************************
* FUNCTION
*  app_ansii_n_to_unicodestring
* DESCRIPTION
*  The function is used for convert the characters of ANSII string to UCS2
*  string. The caller need to make sure the pOutBuffer size is greater than
*  len and the function doesn't add the terminated character at the end of
*  the pOutBuffer array. The byte order of UCS2 character(output param) is
*  little endian.
* PARAMETERS
*  pOutBuffer      [OUT]   UCS2 destination string.      
*  pInBuffer       [IN]    ANSII source string     
*  len             [IN]    Length in bytes   
* RETURNS
*  Return the bytes to convert.
*****************************************************************************/
kal_uint16 app_ansii_n_to_unicodestring(kal_int8 *pOutBuffer, kal_int8 *pInBuffer, kal_uint32 len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_int16 count = -1;
    kal_uint8 charLen = 0;
    kal_uint8 arrOut[2];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (len)
    {

        app_unicode_to_ucs2encoding((kal_wchar) (*((kal_uint8 *)pInBuffer)), &charLen, arrOut);

        // #ifdef MMI_ON_WIN32
        pOutBuffer[++count] = arrOut[0];
        pOutBuffer[++count] = arrOut[1];
        pInBuffer++;
        // #endif

    #ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
        pOutBuffer[++count] = arrOut[1];    /* arrOut[0]; */
        pOutBuffer[++count] = arrOut[0];    /* arrOut[1]; */
        pInBuffer++;
    #endif /* __FOR_TESTING */

        len--;

    }

    return count + 1;
}


/*****************************************************************************
* FUNCTION
*  app_ansii_to_unicodestring_ex
* DESCRIPTION
*  The function is used for convert ANSII string to UCS2 string. The caller
*  need to make sure the pOutBuffer  size must have enough space or the function
*  causes the memory corruption. The function will add the terminated character
*  at the end of pOutBuffer array. The byte order of UCS2 character(output param)
*  is little endian.
*  Unlike app_ansii_to_unicodestring, it will ignore the controls in ANSII string.
* PARAMETERS
*  pOutBuffer      [OUT]  UCS2 destination string   
*  pInBuffer       [IN]   ANSII source string  
* RETURNS
*  Return the bytes to convert.
*****************************************************************************/
kal_uint16 app_ansii_to_unicodestring_ex(kal_int8 *pOutBuffer, kal_int8 *pInBuffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_int16 count = -1;
    kal_uint8 charLen = 0;
    kal_uint8 arrOut[2];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (*pInBuffer != '\0')
    {
        if ((*pInBuffer > 0x00)&&(*pInBuffer < 0x20)||(*pInBuffer == 0x07F))
        {
            pInBuffer++;
            continue;
        }

        app_unicode_to_ucs2encoding((kal_wchar) (*((kal_uint8 *)pInBuffer)), &charLen, arrOut);
        
        pOutBuffer[++count] = arrOut[0];
        pOutBuffer[++count] = arrOut[1];
        pInBuffer++;
        
    }

    pOutBuffer[++count] = '\0';
    pOutBuffer[++count] = '\0';
    return count + 1;
}


/*****************************************************************************
* FUNCTION
*  app_ansii_n_to_unicodestring_ex
* DESCRIPTION
*  The function is used for convert the characters of ANSII string to UCS2
*  string. The caller need to make sure the pOutBuffer size is greater than
*  len and the function doesn't add the terminated character at the end of
*  the pOutBuffer array. The byte order of UCS2 character(output param) is
*  little endian.
*  Unlike app_ansii_n_to_unicodestring, it will ignore the controls in ANSII string.
* PARAMETERS
*  pOutBuffer      [OUT]   UCS2 destination string.      
*  pInBuffer       [IN]    ANSII source string     
*  len             [IN]    Length in bytes   
* RETURNS
*  Return the bytes to convert.
*****************************************************************************/
kal_uint16 app_ansii_n_to_unicodestring_ex(kal_int8 *pOutBuffer, kal_int8 *pInBuffer, kal_uint32 len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_int16 count = -1;
    kal_uint8 charLen = 0;
    kal_uint8 arrOut[2];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (len)
    {
        if ((*pInBuffer > 0x00)&&(*pInBuffer < 0x20)||(*pInBuffer == 0x07F))
        {
            pInBuffer++;
            len--;            
            continue;
        }

        app_unicode_to_ucs2encoding((kal_wchar) (*((kal_uint8 *)pInBuffer)), &charLen, arrOut);

        pOutBuffer[++count] = arrOut[0];
        pOutBuffer[++count] = arrOut[1];

        if (*pInBuffer == '\0')
        {
            break;
        }
        else
        {
            pInBuffer++;
        }
        
        len--;

    }

    return count + 1;
}


/*****************************************************************************
* FUNCTION
*  app_unicode_to_ansii
* DESCRIPTION
*  The function is used for convert UCS2 string to ANSII string.
*  The caller need to make sure the pOutBuffer  size must have enough space
*  or the function causes the memory corruption. The function will add the
*  terminated character at the end of pOutBuffer array. The byte order of
*  UCS2 character(input param) is little endian.
* PARAMETERS
*  pOutBuffer      [OUT]   ANSII destination string  
*  pInBuffer       [IN]    UCS2 source string
* RETURNS
*  Return the bytes to convert.
*****************************************************************************/
kal_uint16 app_unicode_to_ansii(kal_int8 *pOutBuffer, kal_int8 *pInBuffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_uint16 count = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (!((*pInBuffer == 0) && (*(pInBuffer + 1) == 0)))
    {
        *pOutBuffer = *(pInBuffer);

    #ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
        *pOutBuffer = *(pInBuffer + 1);
    #endif
        pInBuffer += 2;
        pOutBuffer++;
        count++;
    }

    *pOutBuffer = 0;
    return count;
}


/*****************************************************************************
* FUNCTION
*  app_unicode_n_to_ansii
* DESCRIPTION
*  The function is used for convert the characters of UCS2 string to ANSII
*  string. The caller need to make sure the pOutBuffer size is greater than
*  len and the function doesn't add the terminated character at the end of
*  the pOutBuffer array. The byte order of UCS2 character(input param) is l
*  ittle endian.
* PARAMETERS
*  pOutBuffer      [OUT]    ANSII destination string.     
*  pInBuffer       [IN]     UCS2 source string.   
*  len             [IN]     Length in bytes.  
* RETURNS
*  Return the bytes to convert.
*****************************************************************************/
kal_uint16 app_unicode_n_to_ansii(kal_int8 *pOutBuffer, kal_int8 *pInBuffer, kal_uint32 len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_uint16 count = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while ((len) && (!((*pInBuffer == 0) && (*(pInBuffer + 1) == 0))))
    {
        *pOutBuffer = *(pInBuffer);

    #ifdef __FOR_TESTING    /* MMI_ON_HARDWARE_P */
        *pOutBuffer = *(pInBuffer + 1);
    #endif
        pInBuffer += 2;
        pOutBuffer++;
        count++;
        len -= 2;
    }

    return count;
}


/*****************************************************************************
* FUNCTION
*  app_unicode_to_utf8_len
* DESCRIPTION
*  Calculate the byte number that convert Unicode encode to UTF8 encode
* PARAMETERS
*  ucs2            [IN]         
* RETURNS
*  kal_int32
*****************************************************************************/
kal_int32 app_unicode_to_utf8_len(kal_wchar ucs2)
{
    if (ucs2 < (kal_wchar) 0x80)
        return 1;
    else if (ucs2 < (kal_wchar) 0x800)
        return 2;
    else
        return 3;
}



/*****************************************************************************
* FUNCTION
*  app_stricmp
* DESCRIPTION
*  Case-insensitive string comparison.
*  app_stricmp() compares string s to string t case-insensitively;
* PARAMETERS
*  s            [IN]         
*  t            [IN]         
* RETURNS
*  return <0 if s0 if s>t
*****************************************************************************/
kal_int32 app_stricmp(kal_char *s, kal_char *t)
{
    for ( ; tolower(*s) == tolower(*t); s++, t++)
        if (*s == '\0')
            return 0;
    return tolower(*s) - tolower(*t);
}


/*****************************************************************************
* FUNCTION
*  app_strnicmp
* DESCRIPTION
*  Case-insensitive string comparison for up to n characters.
* PARAMETERS
*  s            [IN]         
*  t            [IN]         
*  n            [IN]    number of compared characters
* RETURNS
*  return <0 if s0 if s>t.
*****************************************************************************/
kal_int32 app_strnicmp(kal_char *s, kal_char *t, int n)
{
    if (n < 0)
        return n;

    for ( ; --n >= 0 && tolower(*s) == tolower(*t); s++, t++)
        if (*s == '\0')
            return 0;

    if (n < 0)
        return 0;
    else
        return tolower(*s) - tolower(*t);
}


/*****************************************************************************
* FUNCTION
*  app_strtolower
* DESCRIPTION
*  Convert a string to all-lower-case string
* PARAMETERS
*  s            [IN/OUT]         
* RETURNS
*  return pointer to the character string passed in.
*****************************************************************************/
kal_char *app_strtolower(kal_char *s)
{
    kal_char *orig = s;

    for ( ; *s != '\0'; s++)
        *s = tolower(*s);
    return orig;
}


/*****************************************************************************
* FUNCTION
*  app_log2
* DESCRIPTION
*  Find the log base 2 of an integer. The log base 2 of an integer is
*  the same as the position of the highest bit set.
* PARAMETERS
*  i            [IN]
* RETURNS
*  the log base 2 of an integer
*****************************************************************************/
kal_int32 app_log2(kal_uint32 i)
{
    int r = 0;

    while (i >>= 1)
        r++;
    return r;

}



/*****************************************************************************
* FUNCTION
*  app_log10
* DESCRIPTION
*  Find the log base 10 of an integer.
*  log10(i) = log2(i) / log2(10) = log2(i) * (1/log2(10)
*  ~= log2(v) * 1233/4096 = log2(v) * 1233 >> 12
*  Adding one is needed because the log2() rounds down.
*  Finally, since the value t is only an approximation, that may be off
*  by one, hte exact value is found by substracting the vlaue of
*  v < power_of_10[t].
*  
* PARAMETERS
*  i            [IN]
* RETURNS
*  the log base 10 of an integer
*****************************************************************************/
kal_int32 app_log10(kal_uint32 i)
{
    kal_int32 t;
    kal_uint32 power_of_10[] = {
        1, 10, 100, 1000, 10000, 100000,
        1000000, 10000000, 100000000, 1000000000};

    t = (app_log2(i) + 1) * 1233 >> 12;
    return (t - (i < power_of_10[t]));
}


/*****************************************************************************
* FUNCTION
*  app_digits
* DESCRIPTION
*  return the digits in decimal format of an unsigned integer
* PARAMETERS
*  i            [IN]
* RETURNS
*  the digits of a unsigned integer in decimal format.
*****************************************************************************/
kal_int32 app_intdigits(kal_uint32 i)
{
    return app_log10(i) + 1;
}

/*****************************************************************************
* FUNCTION
*  app_ucs2_tolower
* DESCRIPTION
*  Convert a character to all-lower-case character
* PARAMETERS
*  ch        [IN] UCS2 encoded character.
* RETURNS
*  return the converted character.
*****************************************************************************/
kal_wchar app_ucs2_tolower(kal_wchar ch)
{
   if( ch >=L'A' && ch <= L'Z' )
      return (kal_wchar)( ch + ('a' - 'A') );
   return ch;
}

/*****************************************************************************
* FUNCTION
*  app_ucs2_strtolower
* DESCRIPTION
*  Convert a string to all-lower-case string
* PARAMETERS
*  s          [IN/OUT]   UCS2 encoded string      
* RETURNS
*  return pointer to the character string passed in.
*****************************************************************************/
kal_wchar *app_ucs2_strtolower(kal_wchar *s)
{
    kal_wchar *orig = s;

    for ( ; *s != L'\0'; s++)
        *s = app_ucs2_tolower(*s);
    return orig;
}

/*****************************************************************************
* FUNCTION
*  app_ucs2_strstr
* DESCRIPTION
*  
* PARAMETERS
*  a       [IN]        
*  p       [IN]        
* RETURNS
*  
*****************************************************************************/
kal_wchar *app_ucs2_strstr(const kal_wchar *a, const kal_wchar *p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int i, m, n;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (p == NULL || a == NULL)
    {
        return NULL;
    }

    m = app_ucs2_wcslen(a);
    n = app_ucs2_wcslen(p);

    for (i = 0; i < m; i++)
    {
        int i_tmp = i;
        int j = 0;

        while (*(a + i) == *(p + j))
        {
            i++;
            j++;

            if (j == n)
            {
                return (kal_wchar*) (a + i_tmp);
            }
            else if (i >= m)
            {
                return NULL;
            }
        }
        i = i_tmp;
    }
    return NULL;
}
阅读(1972) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~