コード例 #1
0
TInt CISO2022KRImplementation::ConvertFromUnicode(
    CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, 
    const TDesC8& aReplacementForUnconvertibleUnicodeCharacters, 
    TDes8& aForeign, 
    const TDesC16& aUnicode, 
    CCnvCharacterSetConverter::TArrayOfAscendingIndices& aIndicesOfUnconvertibleCharacters)
	{
    TInt ret;
    TInt currPos = 3;
    TUint outputConversionFlags = 0;
    TUint inputConversionFlags = CCnvCharacterSetConverter::EInputConversionFlagAppend;
    TISO2022FromUniState currState = EISO2022Initialize;
    TUint8 shiftByte = 0;
    TPtr8 shiftBytePtr(NULL, 0);

    aForeign.SetLength(0);

    /* Start with escape sequence */
    aForeign.Append( KLit8EscapeSequence );

    ret = CCnvCharacterSetConverter::DoConvertFromUnicode( CnvCp949Table::ConversionData(),
                                                           aDefaultEndiannessOfForeignCharacters,
                                                           aReplacementForUnconvertibleUnicodeCharacters,
                                                           aForeign,
                                                           aUnicode,
                                                           aIndicesOfUnconvertibleCharacters,
                                                           outputConversionFlags, 
                                                           inputConversionFlags );
    /* Append shift in and out bytes as needed */
    while( currPos < aForeign.Length() )
        {
        TUint8 *currChar = (TUint8 *)aForeign.Mid(currPos).Ptr();
        if( *currChar > KMaxAscii )
            { /* KSC character */
            if( currState != EISO2022KSC )
                { /* Insert shift out byte */
                shiftByte = SHIFT_OUT_BYTE;
                currState = EISO2022KSC;
                }

            /* Clear the 8th bit */
            *currChar = (*currChar & ~(0x80));
            }
        else
            { /* ASCII character */
            if( currState != EISO2022Ascii )
                { /* Insert shift in byte */
                shiftByte = SHIFT_IN_BYTE;
                currState = EISO2022Ascii;
                }
            }

        if( shiftByte )
            {
            if( (aForeign.Length() + 1) > aForeign.MaxLength() )
                { /* Make room for shift byte */
                if( aForeign[ (aForeign.Length() - 1) ] > KMaxAscii )
                    { /* Drop a dual byte KSC character */
                    aForeign.SetLength( aForeign.Length() - 2 );
                    }
                else
                    { /* Drop a single byte ASCII character */
                    aForeign.SetLength( aForeign.Length() - 1 );
                    }
                    /* Increase unconverted amount */
                    ret++;
                /* TBD, propably should try to fix aIndicesOfUnconvertibleCharacters
                        if possible */
                }
                shiftBytePtr.Set( &shiftByte, 1, 1 );
                aForeign.Insert( currPos, shiftBytePtr );
                currPos++;
                shiftByte = 0;
            }

        /* Skip current character */
        currPos++;
        }

    return ret;
    }
コード例 #2
0
ファイル: utils.cpp プロジェクト: cdaffara/symbiandump-os1
TInt ParseMixedBinaryAsciiDataL(TDes8& aTextToConvert)
/**
Parses aTextToConvert based on the following rules:
'\\' (double backslash) is used to denote a single '\'
(single backslash)
'\xnn' denote a byte of binary data where nn is in hex-decimal.
The '\xnn' in aTextToConvert is replaced by the binary byte
that it represents.

For example: If aTextToConvert contains "abc\\def\xFF",
after parsing, it will contain "abc\def?" where ? = 0xFF.

@param aTextToConvert Modifiable buffer which will be parsed. 

@return KErrNone if aTextToConvert is in valid
        EAdditionalParamDataFormatMixedBinaryAndAscii format.
        KErrArgument if aTextToConvert is in an incorrect format.

@panic KErrNoMemory if there is not enough memory to do the parsing.
*/
	{
	// Pointer to unparsed portion of additionalParamDataBuffer
	HBufC8* resultBuffer = HBufC8::NewLC(aTextToConvert.Length());

	__ASSERT_ALWAYS(resultBuffer, PanicClient(KErrNoMemory));
	
	TPtr8 result(resultBuffer->Des());
	
	// Position of backslash
	TInt pos = 0;

	while ((pos = aTextToConvert.Locate('\\')) != KErrNotFound)
		{
		// Check that the backslash is followed by at least one more character
		if ((pos+1) >= aTextToConvert.Length())
			{
			return KErrArgument;
			}

		TUint8 modifier = aTextToConvert[pos+1];

		// Parse depending on character after the backslash
		switch (modifier)
			{
		case '\\':
			// Next character after the '\' is another '\'.
			// Replace it with a single '\' and move
			// on.
			result.Append(aTextToConvert.Left(pos+1));
			aTextToConvert.Delete(0, pos+2);
			break;
		case 'x':
			// Next character is an 'x' so check that there are three 
			// characters after the backslash (one for the x and two
			// characters of HEX.
			if ((pos+3) >= aTextToConvert.Length()) 
				{
				return KErrArgument;
				}
			// Convert those to HEX and replace '\xNN' with this.
			result.Append(aTextToConvert.Left(pos));
			TUint8 hexAsInt;
			if (AsciiHexToNum(aTextToConvert.MidTPtr(pos+2,2), hexAsInt) != KErrNone)
				{
				return KErrArgument;
				}
			// Append the raw byte to the result
			result.SetLength(result.Length()+1);
			result[result.Length()-1] = hexAsInt;
			aTextToConvert.Delete(0, pos+4);
			break;		
			}
		} // End while
	aTextToConvert.Insert(0, result);
	
	CleanupStack::PopAndDestroy(resultBuffer);
	return KErrNone;
	}