现在的位置: 首页 > 综合 > 正文

有关字符串的算法集合

2013年08月04日 ⁄ 综合 ⁄ 共 45491字 ⁄ 字号 评论关闭

/*****************************************************************************
 * DESCRIPTION:
 *     Conversion unsinged int to string.
 *
 * RETURN VALUE:
 *     uVal[In]: unsinged int value.
 *     str[Out]: Point of the out string buffer.
 *
 * PARAMETER:
 *     void
 *
 * REMARK:
 *
 *****************************************************************************/
void unsingedTostring(unsigned int uVal, CHAR *str)
{
    unsigned int i,j;
    S8 itoaArray[20];

    i = uVal;
    j = 0;
    do
    {
        itoaArray[j] = (CHAR) (i % 10) + 0x30;
        j++;
        i = i / 10;
    } while (i!=0);

    str[j] = 0;
    while (j!=0)
    {
        j--;
        *str = itoaArray[j];
        str++;
    }
}

/****************************************************************************
 *  Function:           ustr_Strlen( U16* ucs2Str )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16
ustr_Strlen( const U16* str )
{
        const U16* ustr = str;
 
        while( *ustr++ );
 
        return( (U16)(ustr - str - 1) );
}
 
/****************************************************************************
 *  Function:           ustr_Strcpy( U16* dst, U16* src )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_Strcpy( U16* dst, const U16* src )
{
    U16* cp = dst;
 
    while( *cp++ = *src++ )
        ;               /* Copy src over dst */
 
    return( dst );
}
 
/****************************************************************************
 *  Function:           ustr_Strncpy( U16* dest, const U16* src, U16 count )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_Strncpy( U16* dst, const U16* src, U16 count )
{
    U16* start = dst;
 
    while( count && (*dst++ = *src++) )    /* copy string */
        count--;
 
    if( count )                              /* pad out with zeroes */
        while(--count)
            *dst++ = '/0';
 
    return(start);
}
 
/****************************************************************************
 *  Function:           ustr_Strcat ( U16* dst, const U16* src )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_Strcat ( U16* dst, const U16* src )
{
    U16 * cp = dst;
 
    while( *cp )
        cp++;                   /* find end of dst */
 
    while( *cp++ = *src++ ) ;   /* Copy src to end of dst */
 
    return( dst );              /* return dst */
}
 
/****************************************************************************
 *  Function:           ustr_Strncat ( U16* front, const U16* back, U16 count )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_Strncat ( U16* dst, const U16* src, U16 count )
{
    U16 *start = dst;
 
    while (*dst++);
 
    dst--;
 
    while( count-- )
        if( !(*dst++ = *src++) )
            return(start);
 
    *dst = 0;
    return(start);
}
 
/****************************************************************************
 *  Function:           ustr_Strcmp( const U16 * src, const U16 * dst )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
S16
ustr_Strcmp( const U16 * string1, const U16 * string2 )
{
 S16 ret = 0 ;

 while( !( ret = ( S16 )( *string1 - *string2 ) ) && ( *string1 ) && ( *string2 ) )
 {
  ++string1;
  ++string2;
 }

 if( ret < 0 )
 {
  ret = -1 ;
 }

 if( ret > 0 )
 {
  ret = 1 ;
 }

 return( ret );
}
 
/****************************************************************************
 *  Function:           ustr_Strncmp ( const U16* first, const U16* last, U16 count )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
S16
ustr_Strncmp ( const U16* string1, const U16* string2, U16 count )
{
    if( !count )
    {
        return( 0 );
    }

    while( ( --count ) && ( *string1 ) && ( *string2 ) && ( *string1 == *string2 ) )
    {
        string1++;
        string2++;
    }

    return( ( S16 )( *string1 - *string2 ) );
}

/* BQC 2004/03/15 Tony Yang, Add for supporting UCS2 file system { */

/****************************************************************************
 *  Function:           ustr_Strstr( const U16 * string, U16 charSet )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16 *
ustr_Strstr( const U16 * string, const U16 * charSet )
{
    const U16 * pu16Temp = string;
   
    if( *string == 0x0000 || *charSet == 0x0000 )
    {
        return NULL;
    }
    else
    {
        while( *string != 0x0000 )
        {
            if( *string == *charSet )
            {
                pu16Temp = string;
               
                for( ; *string == *charSet && *charSet != 0x0000 && *string != 0x0000; string++, charSet++ )
                    ;
               
                if( *charSet == 0x0000 )
                {
                    return ( U16 * )pu16Temp;
                }
                else
                {
                    string = pu16Temp + 1;
                }
            }
            else
            {
                string++;
            }
        }
    }
   
    return NULL;
}

U16 * ustr_Strstrc( const U16 * string, U16 charSet )
{
    const U16 * pu16Temp = string;
   
    while( ( *pu16Temp != 0x0000 ) && ( *pu16Temp != charSet ) )
    {
        pu16Temp++;
    }
   
    return ( U16 * )( ( *pu16Temp != 0x0000 ) ? pu16Temp : NULL );
}

/****************************************************************************
 *  Function:           ustr_Uppercase( U16 * string )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
S16
ustr_Uppercase( U16 * string )
{
    S16 s16Ret = 1;
   
    while( s16Ret > 0 && *string != 0x0000 )
    {
        if( ( *string >= 0x0041 ) && ( *string <= 0x005A ) )
        {
            string++;
        }
        else if ( ( *string >= 0x0061 ) && ( *string <= 0x007A ) )
        {
            *string = *string - 0x0020;
           
            string++;
        }
        else
        {
            s16Ret = 0;
        }
    }
   
    return s16Ret;
}

/* BQC 2004/03/15 Tony Yang, Add for supporting UCS2 file system } */
 
/****************************************************************************
 *  Function:           uisdigit(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is a digit.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisdigit( U16 ch )
{
        return (U8)(ch >= '0' && ch <= '9');
}
 
/****************************************************************************
 *  Function:           uisprint( U16 ch )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is undisplay char.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisprint( U16 ch )
{
        if ((ch >= 0x00 && ch <= 0x1F)
                && (ch != 0x09)         /* 0x09 is Tab */
                && (ch != 0x0A)         /* 0x0A is LF */
                && (ch != 0x0D))        /* 0x0D is CR */
        {
                return 0;
        }
        else
        {
                return 1;
        }
 
}
/****************************************************************************
 *  Function:           uisalpha( U16 ch )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is an alpha.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisalpha( U16 ch )
{
        return (U8)((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'));
}
 
/****************************************************************************
 *  Function:           uislower( U16 ch )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is an alpha.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uislower( U16 ch )
{
        return (U8)( ch>='a' && ch<='z' );
}
 
/****************************************************************************
 *  Function:           uisupper( U16 ch )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is an alpha.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisupper( U16 ch )
{
        return (U8)( ch>='A' && ch<='Z' );
}
 
/****************************************************************************
 *  Function:           uisspace(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is a space.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisspace(U16 ch)
{
        return (U8)(ch == 0x20);
}
 
/****************************************************************************
 *  Function:           uisnbsp(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is a no breaking space.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisnbsp( U16 ch )
{
        return (U8)(ch == 0xA0);
}
 
/****************************************************************************
 *  Function:           uisbreak(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is CR or LF.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisbreak(U16 ch)
{
        return (U8)(ch == 0x0A || ch == 0x0D);
}
 
/****************************************************************************
 *  Function:           uisCR( U16 ucs2Code )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given unicode expresses CR.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisCR( U16 ch )
{
    return (U8)( ch == 0x0d );
}
 
/****************************************************************************
 *  Function:           uisLF( U16 ucs2Code )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given unicode expresses LF.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisLF( U16 ch )
{
    return (U8)( ch == 0x0a );
}
 
/****************************************************************************
 *  Function:           uistab(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is Tab key.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uistab(U16 ch)
{
        return (U8)(ch == 0x09);
 
}       /* end of ustr_IsTab() */
 
/****************************************************************************
 *  Function:           utoupper(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        make the small letter to the capital letter.
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16
utoupper(U16 ch)
{
        if (ch >= 'a' && ch <= 'z')
                ch -= 'a'-'A';
        return ch;
}
 
/****************************************************************************
 *  Function:           utolower(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        change the capital letter to the small letter.
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16
utolower(U16 ch)
{
        if (ch >= 'A' && ch <= 'Z')
                ch += 'a'-'A';
        return ch;
 
}
 
/****************************************************************************
 *  Function:           ustr_AsciiToUCS2( U16* pUnicode, const U8* pAscii )
 *--------------------------------------------------------------------------
 *  Description:        convert Ascii code to unicode
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_AsciiToUCS2( U16* pUnicode, const U8* pAscii )
{
        U16 *pUni = pUnicode;
 
        while( *pUnicode++ = *pAscii++ );
 
        return pUni;
 
}       /* end of ustr_AsciiToUCS2() */
 
/****************************************************************************
 *  Function:           ustr_UCS2ToAscii( U8* pAscii, const U16* pUnicode )
 *--------------------------------------------------------------------------
 *  Description:        convert unicode to Ascii code
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8*
ustr_UCS2ToAscii( U8* pAscii, const U16* pUnicode )
{
        U8 *pAscCode = pAscii;
 
        while( !(*pUnicode & 0xFF00) && (*pAscii++ = (U8)(*pUnicode++)) )
                ;
 
        *pAscii = 0;
 
        return pAscCode;
 
}       /* end of ustr_AsciiToUCS2() */
 
 
/****************************************************************************
 *  Function:           ustr_GSMToUCS2(U16* pUnicode, U16 gsmCode )
 *--------------------------------------------------------------------------
 *  Description:        change the 03.38 gsm code to the standard unicode
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
ustr_GSMToUCS2( U16* pUnicode, U16 gsmCode )
{
        if( (gsmCode & 0xff00) != 0x1B00 )
        {
                /* if the gsm code is not extended, we can directly
                   get out the unicode from the normal codepage accords
                   to the gsm code. */
                *pUnicode = ustrNormGsmCodePage[gsmCode];
                return 1;
        }
        else
        {
                /* to extended gsm code, we must look for the corresponding
                   unicode from the extended codepage. */
                U8 i;
                gsmCode &= 0x00FF;
                for(i=0; i<NUM_OF_EXTENDED_GSMCODE; i++)
                {
                        if (ustrExtGsmCodePage[i].extGsmCode == gsmCode)
                        {
                                *pUnicode = ustrExtGsmCodePage[i].unicode;
                                return 1;
                        }
                }
        }
 
        return 0;
 
}       /* end of ustr_GSMToUCS2() */
 
/****************************************************************************
 *  Function:           ustr_UCS2ToGSM( U16* pGsmCode, U16 unicode )
 *--------------------------------------------------------------------------
 *  Description:        change unicode to the gsm 03.38 code.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
ustr_UCS2ToGSM( U16* pGsmCode, U16 unicode )
{
        U8 retCode;
        U16     i;
 
        retCode = 0;
 
        if ((unicode >= 0x0020 && unicode <= 0x0023)
                || (unicode >= 0x0025 && unicode <= 0x003F)
                || (unicode >= 0x0041 && unicode <= 0x005A)
                || (unicode >= 0x0061 && unicode <= 0x007A))
        {
                /* in these condictions, the unicode and gsm
                   code is as same. */
                *pGsmCode = (U16)(unicode & 0xFF);
                retCode   = 1;
        }
        else
        {
                for(i=0; i<NUM_OF_NORMAL_GSMCODE && !retCode ; i++)
                {
                        if (ustrNormGsmCodePage[i] == unicode)
                        {
                                *pGsmCode = i;
                                retCode   = 1;
                        }
                }
        }
 
        if( !retCode )
        {
                /* to extended gsm code, we must look for the corresponding
                   gsm code for the unicode from the extended codepage. */
                for( i=0; i<NUM_OF_EXTENDED_GSMCODE && !retCode ; i++ )
                {
                        if (ustrExtGsmCodePage[i].unicode == unicode)
                        {
                                *pGsmCode = ustrExtGsmCodePage[i].extGsmCode;
                                *pGsmCode |= 0x1B00;
                                retCode   =  1;
                        }
                }
        }
 
        return retCode;
 
}       /* end of ustr_UCS2ToGSM() */
 
/****************************************************************************
 *  Function:           ustr_GSMStrToUCS2Str( U8* pUnicode, U8 *gsmCode,
 *                                                                                U16 srcLen , U16 *dstStrLen)
 *--------------------------------------------------------------------------
 *  Description:        change 03.38 gsm code string to standard unicode string
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8 ustr_GSMStrToUCS2Str( U16* pu16Unicode, U8 *gsmCode, U16 srcLen, U16 *dstStrLen)
{
        U16 i;
        U8 success = 1;
        U16 code;
    U16 dstCode;
    U16* pSrcCode = pu16Unicode;
        *dstStrLen = 0;
 
        for(i=0; i<srcLen; i++, gsmCode++)
        {
                if(*gsmCode == 0x1b)
                {
                        code = (U16)(*(gsmCode+1) + (*gsmCode << 8));
 
                        if( ustr_GSMToUCS2(&dstCode, code) )
                        {
                                i++;
                                gsmCode++;
                                *pu16Unicode++ = dstCode;
                                continue;
                        }
                        else
                        {
                                code = *gsmCode;
                        }
                }
                else
                {
                        code = *gsmCode;
                }
 
                if(ustr_GSMToUCS2(&dstCode, code))
        {
            *pu16Unicode++ = dstCode;
        }
        else
        {
            success = 0;
        }
        }
    *pu16Unicode  = 0;
    *dstStrLen = (U16)(pu16Unicode - pSrcCode);
        return success;
 
}       /* end of ustr_GSMStrToUCS2Str() */
 
/****************************************************************************
 *  Function:           ustr_UCS2StrToGSMStr( U8* pGsmCode, U8* unicode,
 *                                                                                U16 srcStrLen, U16 *dstLen )
 *--------------------------------------------------------------------------
 *  Description:        change unicode string to the gsm 03.38 string.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8 ustr_UCS2StrToGSMStr( U8* pGsmCode, U16* pu16Unicode, U16 srcStrLen, U16 *dstLen )
{
        U8 success = 1;
        U16 i;
        U16 code;
    U8* pSrcCode = pGsmCode;
 
        for(i=0; i<srcStrLen; i++, pu16Unicode++)
        {
                if(ustr_UCS2ToGSM(&code, *pu16Unicode))
                {
                        if(code & 0x1b00)
                        {
                                *pGsmCode++ = 0x1b;
                        }
                *pGsmCode++ = (U8) (code & 0xFF);
                }
                else
                {
                        success = 0;
                }
        }
    *dstLen = (U16)(pGsmCode - pSrcCode);
        *pGsmCode = 0;
        return success;
 
}       /* end of ustr_USC2StrToGSMStr() */
 
/***************************************************************************
 *  Function:           ustr_UCS2ToBcd( )
 *--------------------------------------------------------------------------
 *  Description:        Convert unicode to bcd code.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  bcdFmt            | * |   |   |  bcd code format.
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              |   | * |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          | * |   |   |  the pointer of string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustr_UCS2ToBcd( ustr_BcdFormatE bcdFmt, U16* pUnicode, U8* pBcd )
{
    U16 strLen;
 
    strLen = ustr_Strlen( pUnicode );
 
    switch( bcdFmt )
    {
        case BCD_UNPACKED:
        ustrUCS2ToBcdUp( pUnicode, strLen, pBcd );
        break;
 
    case BCD_PACKED_LSB_FIRST:
        ustrUCS2ToBcdLf( pUnicode, strLen, pBcd );
        break;
 
    case BCD_PACKED_MSB_FIRST:
        default:
        ustrUCS2ToBcdMf( pUnicode, strLen, pBcd );
        break;
    }
 
}   /* End Of ustr_UCS2ToBcd() */
 
/***************************************************************************
 *  Function:           ustr_BcdToUCS2( )
 *--------------------------------------------------------------------------
 *  Description:        Convert bcd code to unicode.
 *
 *  Return Value:       none
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  bcdFmt            | * |   |   |  bcd code format.
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  bcdLen            | * |   |   |  bcd code length.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          |   | * |   |  the pointer of output string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustr_BcdToUCS2( ustr_BcdFormatE bcdFmt, U8 *pBcd, U16* pUnicode)
{
    /* NOTE: LSB first 1234 = 0x0021 0x0043, bcdLen = 4               */
    /*                 123  = 0x0021 0x00f3, bcdLen = 3               */
    /*       MSB first 1234 = 0x0012 0x0034, bcdLen = 4               */
    /*                 123  = 0x0012 0x003f, bcdLen = 3               */
    /*       unpacked  1234 = 0x0001 0x0002 0x0003 0x0004, bcdLen = 4 */
        U16 bcdLen;
 
        bcdLen = ustr_GetBcdLen( bcdFmt, pBcd );
 
    switch( bcdFmt )
    {
        case BCD_UNPACKED:
        ustrBcdUpToUCS2( pBcd, bcdLen, pUnicode );
        break;
 
    case BCD_PACKED_LSB_FIRST:
        ustrBcdLfToUCS2( pBcd, bcdLen, pUnicode );
        break;
 
    case BCD_PACKED_MSB_FIRST:
        default:
        ustrBcdMfToUCS2( pBcd, bcdLen, pUnicode );
        break;
    }
 
}   /* End Of ustr_BcdToUCS2() */
 
/***************************************************************************
 *  Function  :         ustr_GetBcdLen( )
 *--------------------------------------------------------------------------
 *  Description:        Get bcd number length.
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *  bcdFmt            | * |   |   |  bcd code format.
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16
ustr_GetBcdLen( ustr_BcdFormatE bcdFmt, U8 *pBcd )
{
    /* NOTE: LSB first 1234 = 0x21 0x43, bcdLen = 4           */
    /*                 123  = 0x21 0xf3, bcdLen = 3           */
    /*       MSB first 1234 = 0x12 0x34, bcdLen = 4           */
    /*                 123  = 0x12 0x3f, bcdLen = 3           */
    /*       unpacked  1234 = 0x01 0x02 0x03 0x04, bcdLen = 4 */
 
        U16 i;
    U16 bcdLen;
 
    switch( bcdFmt )
    {
        case BCD_UNPACKED:
        bcdLen = 0;
        for( i = 0; pBcd[i] != 0xff && pBcd[i] != 0x0f; i++)
                        ;
        bcdLen = i;
        break;
 
    case BCD_PACKED_LSB_FIRST:
        bcdLen = 0;
        for( i = 0; 1; i++ )
        {
            if( ( (pBcd[i/2] >> (((i+1)&1)?0:4)) & 0x0f ) == 0x0f )
            {
                                /* NOTE: BCD is end by 0x0f */
                break;
            }
            bcdLen ++;
        }
        bcdLen = i;
        break;
 
        case BCD_PACKED_MSB_FIRST:
        default:
        bcdLen = 0;
        for( i = 0; 1; i++ )
        {
            if( ( (pBcd[i/2] >> ((i&1)?0:4)) & 0x0f ) == 0x0f )
            {
               break;
            }
            bcdLen ++;
        }
        bcdLen = i;
        break;
    }
 
    return bcdLen;
 
}   /* End Of ustr_GetBcdLen() */
 
/***************************************************************************
 *  Function:           ustrBcdLfToUCS2( )
 *--------------------------------------------------------------------------
 *  Description:        Convert LSB format bcd code to unicode.
 *
 *  Return Value:       none
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  bcdLen            | * |   |   |  bcd code length.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          |   | * |   |  the pointer of output string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustrBcdLfToUCS2( U8 *pBcd, U16 bcdLen, U16* pUnicode )
{
        /*BCD format - LSB first (1234 = 0x0021, 0x0043)*/
    U16 i;
    U16 unicode;
        U8  bcdCode;
 
    for( i = 0; i < bcdLen; i++ )
    {
        bcdCode = (U8)((pBcd[i/2] >> (((i+1) & 1) ? 0 : 4)) & 0x0F);
        if( bcdCode == DIALBCD_FILLER )
        {
            break;
        }
 
        unicode = (U16)((bcdCode == DIALBCD_STAR) ? '*':
                        (bcdCode == DIALBCD_HASH) ? '#':
                        (bcdCode == DIALBCD_PAUSE)? 'P':
                        (bcdCode == DIALBCD_WILD) ? 'w': (bcdCode + '0'));
 
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*' ||
               unicode == '#' || (unicode == 'P' || unicode == 'p') ||
               unicode == 'W' || unicode == 'w' ) )
                {
                        unicode = 0;
                }
 
        pUnicode[i] = unicode;
    }
 
    pUnicode[i] = 0;
 
}  /*-- End of ustrBcdLfToUCS2( ) --*/
 
/***************************************************************************
 *  Function:           ustrBcdMfToUCS2( )
 *--------------------------------------------------------------------------
 *  Description:        Convert MSB format bcd code to unicode.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  bcdLen            | * |   |   |  bcd code length.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          |   | * |   |  the pointer of output string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustrBcdMfToUCS2( U8 *pBcd, U16 bcdLen, U16* pUnicode )
{
        /*BCD format - MSB first (1234 = 0x0012 0x0034)*/
    U16 i;
    U16 unicode;
        U8  bcdCode;
 
    for( i = 0;i < bcdLen; i++ )
    {
        bcdCode = (U8)((pBcd[i/2] >> ((i & 1) ? 0 : 4)) & 0x0F);
        if( bcdCode == 0x0f )
                {
                        break;
                }
 
        unicode = (U16)((bcdCode == DIALBCD_STAR) ? '*':
                        (bcdCode == DIALBCD_HASH) ? '#':
                        (bcdCode == DIALBCD_PAUSE)? 'P':
                        (bcdCode == DIALBCD_WILD) ? 'w': (bcdCode + '0'));
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
           unicode = 0;
                }
 
        pUnicode[i] = unicode;
    }
 
    pUnicode[i] = 0;
 
}  /*-- End of ustrBcdMfToUCS2( ) --*/
 
/***************************************************************************
 *  Function:           ustrBcdUpToUCS2( )
 *--------------------------------------------------------------------------
 *  Description:        Convert unpacked format bcd code to unicode.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  bcdLen            | * |   |   |  bcd code length.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          |   | * |   |  the pointer of output string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustrBcdUpToUCS2( U8 *pBcd, U16 bcdLen, U16* pUnicode)
{
        /*BCD format - unpacked (1 digit per byte)*/
    U16 i;
    U16 unicode;
        U8  bcdCode;
 
    for( i = 0; i < bcdLen; i++ )
    {
        bcdCode = pBcd[i];
        unicode = (U16)((bcdCode == DIALBCD_STAR) ? '*':
                        (bcdCode == DIALBCD_HASH) ? '#':
                        (bcdCode == DIALBCD_PAUSE)? 'P':
                        (bcdCode == DIALBCD_WILD) ? 'w': (bcdCode + '0'));
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
           unicode = 0;
                }
 
        pUnicode[i] = unicode;
    }
 
    pUnicode[i] = 0;
 
}  /*-- End of ustrBcdUpToUCS2( ) --*/
 
/***************************************************************************
 *  Function:           ustrUCS2ToBcdMf()
 *--------------------------------------------------------------------------
 *  Description:        Convert unicode to MSB format bcd code.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
void
ustrUCS2ToBcdMf( U16* pUnicode, U16 uniLen, U8 *pBcd )
{
    U8  bcdCode;
    U16 unicode;
    U16 i;
 
    for( i = 0; i < uniLen; i++)
    {
                if( (unicode = pUnicode[i]) & 0xFF00 )
                {
                        /* invalid unicode */
                        break;
                }
 
        if (unicode == '+')
                {
                        unicode = 'W';
                }
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
            break;
                }
 
        switch( unicode )
        {
        case '*':
            bcdCode = DIALBCD_STAR;
            break;
        case '#':
            bcdCode = DIALBCD_HASH;
            break;
        case 'P':
        case 'p':
            bcdCode = DIALBCD_PAUSE;
            break;
        case 'W':
        case 'w':
            bcdCode = DIALBCD_WILD;
            break;
        default:
            bcdCode = (U8)(unicode - '0');
            break;
        }
        pBcd[i/2] = (U8)(((i & 1) ? pBcd[i/2] : 0) + (bcdCode << ((i+1 & 1) ? 4 : 0)));
    }
    if( i & 1 )
    {
       pBcd[ (i-1) / 2 ] |= 0x0f;
    }
 
}   /* End Of ustrUCS2ToBcdMf() */
 
/***************************************************************************
 *  Function:           ustrUCS2ToBcdLf()
 *--------------------------------------------------------------------------
 *  Description:        Convert unicode to LSB format bcd code.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
void
ustrUCS2ToBcdLf( U16* pUnicode, U16 uniLen, U8 *pBcd )
{
    U8  bcdCode;
    U16 unicode;
    U16 i;
 
    for( i = 0; i < uniLen; i++ )
    {
                if( (unicode = pUnicode[i]) & 0xFF00 )
                {
                        /* invalid unicode */
                        break;
                }
 
        if (unicode == '+')
                {
                        unicode = 'W';
                }
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
                        break;
                }
 
        switch( unicode )
        {
            case '*':
                bcdCode = DIALBCD_STAR;
                break;
            case '#':
                bcdCode = DIALBCD_HASH;
                break;
            case 'P':
            case 'p':
                bcdCode = DIALBCD_PAUSE;
                break;
            case 'W':
            case 'w':
                bcdCode = DIALBCD_WILD;
                break;
            default:
                bcdCode = (U8)(unicode - '0');
                break;
        }
        pBcd[i/2] = (U8)(((i & 1) ? pBcd[i/2] : 0) + (bcdCode << ((i & 1) ? 4 : 0)));
    }
    if( i & 1 )
    {
       pBcd[ (i-1) / 2 ] |= 0xf0;
    }
 
}   /* End Of ustrUCS2ToBcdLf() */
 
/***************************************************************************
 *  Function:           ustrUCS2ToBcdUp()
 *--------------------------------------------------------------------------
 *  Description:        Convert unicode to unpacked format bcd code.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
void
ustrUCS2ToBcdUp( U16* pUnicode, U16 uniLen, U8 *pBcd )
{
    U8  bcdCode;
    U16 unicode;
    U16 i;
 
    for( i = 0; i < uniLen; i++)
    {
                if( (unicode = pUnicode[i]) & 0xFF00 )
                {
                        /* invalid unicode */
                        break;
                }
 
        if (unicode == '+')
                {
                        unicode = 'W';
                }
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
            break;
                }
 
        switch( unicode )
        {
            case '*':
                bcdCode = DIALBCD_STAR;
                break;
            case '#':
                bcdCode = DIALBCD_HASH;
                break;
            case 'P':
            case 'p':
                bcdCode = DIALBCD_PAUSE;
                break;
            case 'W':
            case 'w':
                bcdCode = DIALBCD_WILD;
                break;
            default:
                bcdCode = (U8)(unicode - '0');
                break;
        }
        pBcd[i] = bcdCode;
    }
 
}   /* End Of ustrUCS2ToBcdUp() */
 
/***************************************************************************
 *  Function:           ustr_StrcatUCS2StrGSMStr()
 *--------------------------------------------------------------------------
 *  Description:        Strcat one UCS2 string and one GSM string to UCS2String.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
 
U16 ustr_StrcatUCS2StrGSMStr(U16 *frontStr, U16 frontStrLen, U8 *backStr, U16 backStrLen)
{
        U16 i;
        U16 txtIndex;
        txtIndex = frontStrLen;
        for(i= 0; i < backStrLen; i++,backStr++)
        {
                U16 GSMChar, UCS2Char;
                GSMChar = *backStr;
                if (GSMChar == 0x1b)
                {
                        GSMChar = (U16)(*(backStr+1) + (*backStr << 8));
                        i++;
                        backStr++;
                }
                if(ustr_GSMToUCS2(&UCS2Char, GSMChar))
                {
                        frontStr[txtIndex++] = UCS2Char;
                }
                else
                {
                        return 0;
                }
        }
        frontStr[txtIndex] = 0;
        return txtIndex;
}
 
/***************************************************************************
 *  Function:           ustr_StrcatGSMStrUCS2Str()
 *--------------------------------------------------------------------------
 *  Description:        Strcat GSM string and UCS2 string to UCS2String.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
U16 ustr_StrcatGSMStrUCS2Str(U8 *frontStr, U16 frontStrLen, U8 *backStr, U16 backStrLen)
{
        /* assume memory is enough, copy GSM string to the end of string */
        U16 i;
        U8 *bufStr;
        U16 *pu16BufStr;
        U16 txtIndex = 0;
 
    pu16BufStr = (U16*)frontStr;
        memcpy(&frontStr[frontStrLen*2], frontStr, frontStrLen);
        bufStr = &frontStr[frontStrLen*2];
 
        for(i= 0; i < frontStrLen; i++,bufStr++)
        {
                U16 GSMChar, UCS2Char;
                GSMChar = *bufStr;
                if (GSMChar == 0x1b)
                {
                        GSMChar = (U16)(*(bufStr+1) + (*bufStr << 8));
                        i++;
                        bufStr++;
                }
                if(ustr_GSMToUCS2(&UCS2Char, GSMChar))
                {
                        pu16BufStr[txtIndex++] = UCS2Char;
                }
                else
        

【上篇】
【下篇】

抱歉!评论已关闭.