Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855563
  • 博文数量: 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:34:55

/*****************************************************************************
* FUNCTION
*  app_ucs2_strchr
* DESCRIPTION
*  Searches a UCS2 encoded string for a given wide-character,
*  which may be the null character L'\0'.
* PARAMETERS
*  string        [IN]  UCS2 encoded string to search in.      
*  ch            [IN]  UCS2 encoded wide-character to search for.      
* RETURNS
*  returns pointer to the first occurrence of ch in string
*  returns NULL if ch does not occur in string
*****************************************************************************/
kal_int8 *app_ucs2_strchr(const kal_int8 *string,  kal_wchar ch)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *chr = (kal_int8*) string;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    do
    {
        if ((*(chr+1)<<8|(kal_uint8)(*chr)) == ch)
        {
            return chr;
        }
        chr += 2;
    } while (*chr || *(chr+1));

    return NULL;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_strrchr
* DESCRIPTION
*  Scan a UCS2 encoded string for the last occurrence of a character.
* PARAMETERS
*  string        [IN]  UCS2 encoded string to search in.      
*  ch            [IN]  UCS2 encoded wide-character to search for.      
* RETURNS
*  returns pointer to the last occurrence of ch in string
*  returns NULL if ch does not occur in string
*****************************************************************************/
kal_int8 *app_ucs2_strrchr(const kal_int8 * string, kal_wchar ch)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *cursor = (kal_int8 *)string;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/    
    while (*cursor || *(cursor+1))              /* find end of string */
    {
        cursor += 2;
    }
   
    /* search towards front */
    while ((kal_int8 *)string != cursor &&
           (*(cursor+1)<<8|(kal_uint8)(*cursor)) != ch)
    {
        cursor -= 2;
    }
   
    if ((*(cursor+1)<<8|(kal_uint8)(*cursor)) == ch)          /* found ?*/
    {
        return cursor;
    }
   
    return NULL;
}

/*****************************************************************************
* FUNCTION
*  app_ucs2_strupr
* DESCRIPTION
*  app_ucs2_strupr converts upper-case characters in a null-terminated
*  UCS2 encoded string their upper-case equivalents.  
* PARAMETERS
*  string    [IN]  UCS2 encoded string to change to upper case.      
* RETURNS
*  returns a pointer to the converted string. Because the modification is
*  done in place, the pointer returned is the same as the pointer passed
*  as the input argument.
*****************************************************************************/
kal_int8 *app_ucs2_strupr(kal_int8 *string)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *cursor;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    cursor=(kal_int8*)string;

    while(*cursor || *(cursor+1))
    {
        if ((*(cursor+1)<<8|(kal_uint8)(*cursor)) >= L'a' &&
            (*(cursor+1)<<8|(kal_uint8)(*cursor)) <= L'z' )
        {
            *cursor = *cursor - ('a' - 'A');
        }
        cursor += 2;
    }
   
    return string;
}

/*****************************************************************************
* FUNCTION
*  app_ucs2_strlwr
* DESCRIPTION
*  app_ucs2_strlwr converts lower-case characters in a null-terminated
*  UCS2 encoded string their lower-case equivalents.  
* PARAMETERS
*  string    [IN]  UCS2 encoded string to change to upper case.      
* RETURNS
*  returns a pointer to the converted string. Because the modification is
*  done in place, the pointer returned is the same as the pointer passed
*  as the input argument.
*****************************************************************************/
kal_int8 *app_ucs2_strlwr(kal_int8 *string)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *cursor;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    cursor=(kal_int8*)string;

    while(*cursor || *(cursor+1))
    {
        if ((*(cursor+1)<<8|(kal_uint8)(*cursor)) >= L'A' &&
            (*(cursor+1)<<8|(kal_uint8)(*cursor)) <= L'Z')
        {
            *cursor = *cursor -  'A' + 'a';
        }
        cursor += 2;
    }
   
    return string;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_stricmp
* DESCRIPTION
*  app_ucs2_stricmp perform a case-insensitive UCS2 encoded string
*  (wide-character) comparison.
* PARAMETERS
*  str_src   [IN]  UCS2 encoded destination string for
*                  left-hand side of comparison.
*  str_dst   [IN]  UCS2 encoded source string for right-hand
*                  side of comparison.
* RETURNS
*  returns -1 if str_src <  str_dst
*  returns  0 if str_src == str_dst
*  returns +1 if str_src >  str_dst
*****************************************************************************/
kal_int32 app_ucs2_stricmp(const kal_int8 *str_src, const kal_int8 *str_dst)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *src = (kal_int8 *)str_src;
    kal_int8 *dst = (kal_int8 *)str_dst;
    kal_wchar first = 0;
    kal_wchar last = 0;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    do {
        first = (((*(src+1)<<8|(kal_uint8)(*src)) <= L'Z') &&
                 ((*(src+1)<<8|(kal_uint8)(*src)) >= L'A'))
            ? (kal_wchar)(*src - 'A' + 'a')
            : (kal_wchar)*src;
        last = (((*(dst+1)<<8|(kal_uint8)(*dst)) <= L'Z') &&
                ((*(dst+1)<<8|(kal_uint8)(*dst)) >= L'A'))
            ? (kal_wchar)(*dst - 'A' + 'a')
            : (kal_wchar)*dst;
        src += 2;
        dst += 2;
    } while ( first && (first == last) );
   
   
    return (kal_int32)(first - last);
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_strnicmp
* DESCRIPTION
*  Compares two UCS2 encoded strings for lexical order without regard to case.
*  The comparison stops after:
*  (1) a difference between the strings is found;
*  (2) the end of the strings is reached;
*  (3) count characters have been compared.
* PARAMETERS
*  str_src   [IN]  UCS2 encoded destination string for
*                  left-hand side of comparison.
*  str_dst   [IN]  UCS2 encoded source string for right-hand
*                  side of comparison.
*  count     [IN]  Number of characters to compare.   
* RETURNS
*  returns -1 if str_src <  str_dst
*  returns  0 if str_src == str_dst
*  returns +1 if str_src >  str_dst
*****************************************************************************/
kal_int32 app_ucs2_strnicmp(const kal_int8 *str_src,
                            const kal_int8 *str_dst,
                            kal_uint32 count)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *src = (kal_int8 *)str_src;
    kal_int8 *dst = (kal_int8 *)str_dst;
    kal_wchar first = 0;
    kal_wchar last = 0;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (count)
    {
        do {
            first = (((*(src+1)<<8|(kal_uint8)(*src)) <= L'Z') &&
                     ((*(src+1)<<8|(kal_uint8)(*src)) >= L'A'))
                ? (kal_wchar)(*src - 'A' + 'a')
                : (kal_wchar)*src;
            last = (((*(dst+1)<<8|(kal_uint8)(*dst)) <= L'Z') &&
                    ((*(dst+1)<<8|(kal_uint8)(*dst)) >= L'A'))
                ? (kal_wchar)(*dst - 'A' + 'a')
                : (kal_wchar)*dst;
            src += 2;
            dst += 2;
        } while ( (--count) && first && (first == last) );
    }
   
    return (kal_int32)(first - last);
}


/*****************************************************************************
* FUNCTION
*  app_widechar_ansii_to_unicode
* 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_widechar_ansii_to_unicode(kal_wchar *pOutBuffer, kal_int8 *pInBuffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *start = pOutBuffer;   

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (*pInBuffer)
    {
        *(pOutBuffer++) =  (kal_wchar)(*(pInBuffer++));
    }

    *pOutBuffer = L'\0';
   
    return (kal_uint16)(pOutBuffer - start);
}


/*****************************************************************************
* FUNCTION
*  app_widechar_ansii_n_to_unicode
* 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]    size in bytes   
* RETURNS
*  Return the bytes to convert.
*****************************************************************************/
kal_uint16 app_widechar_ansii_n_to_unicode(kal_wchar *pOutBuffer, kal_int8 *pInBuffer, kal_uint32 len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *start = pOutBuffer;   

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while ((*pInBuffer) && (len))
    {
        *(pOutBuffer++) =  (kal_wchar)(*(pInBuffer++));
        --len;
    }

    *pOutBuffer = L'\0';
   
    return (kal_uint16)(pOutBuffer - start);
}


/*****************************************************************************
* FUNCTION
*  app_widechar_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.
* PARAMETERS
*  pOutBuffer      [OUT]   ANSII destination string  
*  pInBuffer       [IN]    UCS2 source string
* RETURNS
*  Return the bytes to convert.
*****************************************************************************/
kal_uint16 app_widechar_unicode_to_ansii(kal_int8 *pOutBuffer, kal_wchar *pInBuffer)
{
       /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *start = pOutBuffer;   
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (*pInBuffer)
    {
        *(pOutBuffer++) =  (kal_int8)(*(pInBuffer++));
    }
    
    *pOutBuffer = '\0';
   
    return (kal_uint16)(pOutBuffer - start);
}


/*****************************************************************************
* FUNCTION
*  app_widechar_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
*  little 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_widechar_unicode_n_to_ansii(kal_int8 *pOutBuffer, kal_wchar *pInBuffer, kal_uint32 len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int8 *start = pOutBuffer;   
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while ((*pInBuffer) && (len))
    {
        *(pOutBuffer++) =  (kal_int8)(*(pInBuffer++));
        len -= 2;
    }
    
    *pOutBuffer = '\0';
   
    return (kal_uint16)(pOutBuffer - start);
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcslen
* DESCRIPTION
*  Gets the number of characters of the given UCS2 encoded string(wide-character),
*  not including the final null wide-characters.
* PARAMETERS         
*  string    [IN]  UCS2 encoded string(wide-character) which length is to
*                  be computed.      
* RETURNS
*  The number of characters of a wide-character string.
*****************************************************************************/
kal_int32 app_ucs2_wcslen(const kal_wchar *string)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    const kal_wchar *str_tmp = string;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while(*str_tmp)
    {
        ++str_tmp;
    }
   
    return (kal_int32) (str_tmp - string);
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcscmp
* DESCRIPTION
*  Compares two UCS2 encoded strings(wide-character) and returns an integer to
*  indicate whether the destination string is less than the source string,
*  the two are equal, or whether the destination string is greater than the
*  source string.
* PARAMETERS         
*  str_src   [IN]  UCS2 encoded destination string(wide-character) for
*                  left-hand side of comparison.
*  str_dst   [IN]  UCS2 encoded source string(wide-character) for right-hand
*                  side of comparison.      
* RETURNS
*  returns <0 if str_src <  str_dst
*  returns  0 if str_src == str_dst
*  returns >0 if str_src >  str_dst
*****************************************************************************/
kal_int32 app_ucs2_wcscmp(const kal_wchar *str_src, const kal_wchar *str_dst)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while((*str_src == *str_dst) && *str_src)
    {
        ++str_src, ++str_dst;        
    }      

    return (kal_int32)(*str_src - *str_dst);
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcsncmp
* DESCRIPTION
*  Compares two UCS2 encoded strings(wide-character) for lexical order.
*  The comparison stops after:
*  (1) a difference between the strings is found;
*  (2) the end of the strings is reached;
*  (3) count characters have been compared (wide-character strings).
* PARAMETERS
*  str_src   [IN]  UCS2 encoded destination string(wide-character) for
*                  left-hand side of comparison.
*  str_dst   [IN]  UCS2 encoded source string(wide-character) for right-hand
*                  side of comparison.
*  count     [IN]  Number of characters to compare.   
* RETURNS
*  returns <0 if str_src <  str_dst
*  returns  0 if str_src == str_dst
*  returns >0 if str_src >  str_dst
*****************************************************************************/
kal_int32 app_ucs2_wcsncmp(const kal_wchar *str_src,
                           const kal_wchar *str_dst,
                           kal_uint32 count)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!count)
    {
        return 0;
    }   

    while ((--count) && (*str_src) && (*str_src == *str_dst))
    {
        ++str_src,++str_dst;        
    }   

    return (kal_int32)(*str_src - *str_dst);
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcscpy
* DESCRIPTION
*  Copies the UCS2 encoded source string(wide-character) into the destination
*  string; Assumes enough space in the destination string.
* PARAMETERS
*  str_dst   [OUT]  UCS2 encoded destination string(wide-character) over which
*                   the source string is to be copied.
*  str_src   [IN]   UCS2 encoded source string(wide-character) to be copied
*                   over the destination string.
* RETURNS
*  A pointer to the destination string.
*****************************************************************************/
kal_wchar *app_ucs2_wcscpy(kal_wchar *str_dst, const kal_wchar *str_src)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *str_tmp = str_dst;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    *str_tmp = *str_src;
   
    while(*str_src)
    {
        *(++str_tmp) = *(++str_src); /* Copy str_src over str_dst */
    }
   
    return str_dst;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcsncpy
* DESCRIPTION
*  Copies count characters from the source string to the destination string.  
*  Note:
*  Unlike ANSI C standard library function wcsncpy. If count is less than the
*  length of source, NULL wide-characters also is put onto the end of the
*  copied string. But if count is greater than the length of sources, str_dst
*  is not padded with null characters to length count (wide-characters).
* PARAMETERS
*  str_dst   [OUT]  UCS2 encoded destination string(wide-character) over which
*                   the source string is to be copied.
*  str_src   [IN]   UCS2 encoded source string(wide-character) to be copied
*                   over the destination string.
*  count     [IN]   max number of characters to copy.
* RETURNS
*  A pointer to the destination string.
*****************************************************************************/
kal_wchar *app_ucs2_wcsncpy(kal_wchar *str_dst, const kal_wchar *str_src, kal_uint32 count)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *str_tmp = str_dst;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (count && *str_src)
    {   
        *str_tmp = *str_src; /* copy string */
        ++str_tmp, ++str_src;
        --count;
    }   
   
    *str_tmp = L'\0';   
   
    return str_dst;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcscat
* DESCRIPTION
*  Concatenates the source string onto the end of the destination string.
*  Assumes enough space in the destination string.
* PARAMETERS
*  str_dst   [OUT]  UCS2 encoded destination string(wide-character) over which
*                   "str_src" is to be copied.      
*  str_src   [IN]   UCS2 encoded source string(wide-character) to be copied
*                   over "str_dst".      
* RETURNS
*  A pointer to the destination string.
*****************************************************************************/
kal_wchar *app_ucs2_wcscat(kal_wchar *str_dst, const kal_wchar *str_src)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *str_tmp = str_dst;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/   
    while(*str_tmp)
    {
        ++str_tmp;            /* find end of str_dst */
    }
   
    while(*str_src)
    {
        *str_tmp= *str_src;   /* Copy str_src to end of str_dst */
        ++str_tmp, ++str_src;
    }

    *str_tmp = L'\0';

    return  str_dst;          /* return str_dst */
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcsncat
* DESCRIPTION
*  Appends at most count characters of the source string onto the
*  end of destination string, and ALWAYS terminates with a null character.
*  If count is greater than the length of source string, the length of source
*  string is used instead.  (Like app_ucs2_wcsncpy, this routine does not pad out
*  to count characters).
* PARAMETERS
*  str_dst   [OUT]  UCS2 encoded destination string(wide-character) to be appended.        
*  str_src   [IN]   UCS2 encoded source string(wide-character) to be appended
*                   to the end of destination string.      
*  count     [IN]   Number of characters to append.      
* RETURNS
*  A pointer to the destination string.
*****************************************************************************/
kal_wchar *app_ucs2_wcsncat(kal_wchar *str_dst, const kal_wchar *str_src, kal_uint32 count)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *str_tmp = str_dst;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while(*str_tmp)
    {
        ++str_tmp;               /* find end of str_dst */
    }

    while (count && *str_src)
    {   
        *str_tmp = *str_src;     /* copy string */
        ++str_tmp, ++str_src;
        --count;
    }

    *str_tmp = L'\0';
   
    return str_dst;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcsstr
* DESCRIPTION
*  Find a substring.
* PARAMETERS
*  string        [IN]  UCS2 encoded string(wide-character) to search.      
*  str_char_set  [IN]  UCS2 encoded string(wide-character) to search for.      
* RETURNS
*  Returns a pointer to the first occurrence of str_char_set in string, or
*  NULL if str_char_set does not appear in string. If str_char_set points to
*  a string of zero length, the function returns string.
*****************************************************************************/
kal_wchar *app_ucs2_wcsstr(const kal_wchar *string, const kal_wchar *str_char_set)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *str_tmp = (kal_wchar *) string;
    kal_wchar *cursor1, *cursor2;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (*str_tmp)
    {
        cursor1 = str_tmp;
        cursor2 = (kal_wchar *) str_char_set;

        while ((*cursor1)&&(*cursor2)&&!(*cursor1 - *cursor2))
        {
            cursor1++, cursor2++;
        }

        if (!*cursor2)
        {
            return str_tmp;
        }

        ++str_tmp;
    }

    return NULL;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcschr
* DESCRIPTION
*  Searches a UCS2 encoded string(wide-character) for a given wide-character,
*  which may be the null character L'\0'.
* PARAMETERS
*  string        [IN]  UCS2 encoded string(wide-character) to search in.      
*  ch            [IN]  UCS2 encoded wide-character to search for.      
* RETURNS
*  returns pointer to the first occurrence of ch in string
*  returns NULL if ch does not occur in string
*****************************************************************************/
kal_wchar *app_ucs2_wcschr(const kal_wchar *string,  kal_wchar ch)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (*string && *string != ch)
    {
        ++string;
    }
   
    if (*string == ch)
    {
        return (kal_wchar *)string;
    }
   
    return NULL;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcsrchr
* DESCRIPTION
*  Scan a UCS2 encoded string(wide-character) for the last occurrence of a
*  character.
* PARAMETERS
*  string        [IN]  UCS2 encoded string(wide-character) to search in.      
*  ch            [IN]  UCS2 encoded wide-character to search for.      
* RETURNS
*  returns pointer to the last occurrence of ch in string
*  returns NULL if ch does not occur in string
*****************************************************************************/
kal_wchar *app_ucs2_wcsrchr(const kal_wchar * string, kal_wchar ch)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *start = (kal_wchar *)string;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/    
    while (*string)                /* find end of string */
    {
        ++string;
    }
   
    /* search towards front */
    while (string != start && *string != ch)
    {
        --string;
    }
   
    if (*string == ch)             /* found ? */
    {
        return (kal_wchar *)string;
    }
   
    return NULL;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcslwr
* DESCRIPTION
*  app_ucs2_wcsrchr converts upper-case characters in a null-terminated
*  UCS2 encoded string(wide-character) to their lower-case equivalents.  
* PARAMETERS
*  string    [IN]  UCS2 encoded string(wide-character) to change to lower
*                  case.      
* RETURNS
*  returns a pointer to the converted string. Because the modification is
*  done in place, the pointer returned is the same as the pointer passed
*  as the input argument.
*****************************************************************************/
kal_wchar *app_ucs2_wcslwr(kal_wchar *string)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *cursor;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (cursor=string; *cursor; ++cursor)
    {    
        if ((*cursor >= (kal_wchar)L'A') && (*cursor <= (kal_wchar)L'Z') )
        {    
            *cursor = *cursor - L'A' + L'a';
        }
    }
    return string;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcsupr
* DESCRIPTION
*  app_ucs2_wcsrchr converts upper-case characters in a null-terminated
*  UCS2 encoded string(wide-character) to their upper-case equivalents.  
* PARAMETERS
*  string    [IN]  UCS2 encoded string(wide-character) to change to upper
*                  case.      
* RETURNS
*  returns a pointer to the converted string. Because the modification is
*  done in place, the pointer returned is the same as the pointer passed
*  as the input argument.
*****************************************************************************/
kal_wchar *app_ucs2_wcsupr(kal_wchar *string)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar *cursor;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (cursor=string; *cursor; cursor++)
    {
        if (*cursor >= (kal_wchar)L'a' && *cursor <= (kal_wchar)L'z')
        {
            *cursor = *cursor - (L'a' - L'A');
        }
    }
   
    return string;
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcsicmp
* DESCRIPTION
*  app_ucs2_wcsicmp perform a case-insensitive UCS2 encoded string
*  (wide-character) comparison.
* PARAMETERS
*  str_src   [IN]  UCS2 encoded destination string(wide-character) for
*                  left-hand side of comparison.
*  str_dst   [IN]  UCS2 encoded source string(wide-character) for right-hand
*                  side of comparison.      
* RETURNS
*  returns <0 if str_src <  str_dst
*  returns  0 if str_src == str_dst
*  returns >0 if str_src >  str_dst
*****************************************************************************/
kal_int32 app_ucs2_wcsicmp(const kal_wchar *str_src, const kal_wchar *str_dst)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar first, last;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    do {
        first = ((*str_dst <= L'Z') && (*str_dst >= L'A'))
            ? *str_dst + L'a' - L'A'
            : *str_dst;
        last = ((*str_src <= L'Z') && (*str_src >= L'A'))
            ? *str_src + L'a' - L'A'
            : *str_src;
        str_dst++;
        str_src++;
    } while (first && (first == last));
   
    return (kal_int32)(first - last);
}


/*****************************************************************************
* FUNCTION
*  app_ucs2_wcsnicmp
* DESCRIPTION
*  Compares two UCS2 encoded strings(wide-character) for lexical order
*  without regard to case.
*  The comparison stops after:
*  (1) a difference between the strings is found;
*  (2) the end of the strings is reached;
*  (3) count characters have been compared (wide-character strings).
* PARAMETERS
*  str_src   [IN]  UCS2 encoded destination string(wide-character) for
*                  left-hand side of comparison.
*  str_dst   [IN]  UCS2 encoded source string(wide-character) for right-hand
*                  side of comparison.
*  count     [IN]  Number of characters to compare.   
* RETURNS
*  returns -1 if str_src <  str_dst
*  returns  0 if str_src == str_dst
*  returns +1 if str_src >  str_dst
*****************************************************************************/
kal_int32 app_ucs2_wcsnicmp(const kal_wchar *str_src,
                            const kal_wchar *str_dst,
                            kal_uint32 count)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_wchar first = 0;
    kal_wchar last = 0;
   
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (count)
    {
        do {
            first = ((*str_src <= L'Z') && (*str_src >= L'A'))
                ? *str_src - L'A' + L'a'
                : *str_src;
            last = ((*str_dst <= L'Z') && (*str_dst >= L'A'))
                ? *str_dst - L'A' + L'a'
                : *str_dst;
            str_src++;
            str_dst++;
        } while ( (--count) && first && (first == last) );
    }
   
    return (kal_int32)(first - last);
}
阅读(1833) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~