os/textandloc/charconvfw/charconvplugins/src/shared/gbk_shared.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/charconvfw/charconvplugins/src/shared/gbk_shared.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 +* Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include <e32std.h>
    1.23 +#include <charconv.h>
    1.24 +#include <convdata.h>
    1.25 +#include <convgeneratedcpp.h>
    1.26 +#include "gb2312.h"
    1.27 +#include "gbk.h"
    1.28 +
    1.29 +#define ARRAY_LENGTH(aArray) (sizeof(aArray)/sizeof((aArray)[0]))
    1.30 +
    1.31 +const TInt KNumberOfBytesOfWorkingMemory=(21+21+2+1)*sizeof(SCnvConversionData::SOneDirectionData::SRange); // GB2312.CTL defines 21 foreign-to-Unicode ranges and 21 Unicode-to-foreign ranges, and GBK.CTL defines 2 foreign-to-Unicode ranges and 1 Unicode-to-foreign range
    1.32 +
    1.33 +_LIT(KLitPanicText, "GBK_SHARED");
    1.34 +
    1.35 +enum TPanic
    1.36 +	{
    1.37 +	EPanicTooManyBytesOfWorkingMemoryUsed1=1,
    1.38 +	EPanicTooManyBytesOfWorkingMemoryUsed2
    1.39 +	};
    1.40 +
    1.41 +LOCAL_D const SCnvConversionData::SVariableByteData::SRange foreignVariableByteDataRanges[]=
    1.42 +	{
    1.43 +		{
    1.44 +		0x00,
    1.45 +		0x7f,
    1.46 +		0,
    1.47 +		0
    1.48 +		},
    1.49 +		{
    1.50 +		0x80,
    1.51 +		0xff,
    1.52 +		1,
    1.53 +		0
    1.54 +		}
    1.55 +	};
    1.56 +
    1.57 +LOCAL_C void Panic(TPanic aPanic)
    1.58 +	{
    1.59 +	User::Panic(KLitPanicText, aPanic);
    1.60 +	}
    1.61 +
    1.62 +LOCAL_C void SetUpCompleteGbkConversionData(SCnvConversionData& aCompleteGbkConversionData, TUint8* aWorkingMemory)
    1.63 +	{
    1.64 +	const SCnvConversionData& gb2312ConversionData=CnvGb2312::ConversionData();
    1.65 +	const SCnvConversionData& supplementaryGbkConversionData=conversionData;
    1.66 +	// create a SCnvConversionData that is the combination of gb2312ConversionData and supplementaryGbkConversionData
    1.67 +	aCompleteGbkConversionData.iEndiannessOfForeignCharacters=SCnvConversionData::EFixedBigEndian;
    1.68 +	aCompleteGbkConversionData.iForeignVariableByteData.iNumberOfRanges=ARRAY_LENGTH(foreignVariableByteDataRanges);
    1.69 +	aCompleteGbkConversionData.iForeignVariableByteData.iRangeArray=foreignVariableByteDataRanges;
    1.70 +	TInt numberOfBytesOfWorkingMemoryUsed=0;
    1.71 +	// set up the foreign-to-Unicode data
    1.72 +	const TInt numberOfForeignToUnicodeDataRanges=gb2312ConversionData.iForeignToUnicodeData.iNumberOfRanges+supplementaryGbkConversionData.iForeignToUnicodeData.iNumberOfRanges;
    1.73 +	aCompleteGbkConversionData.iForeignToUnicodeData.iNumberOfRanges=numberOfForeignToUnicodeDataRanges;
    1.74 +	SCnvConversionData::SOneDirectionData::SRange* foreignToUnicodeDataRangeArray=REINTERPRET_CAST(SCnvConversionData::SOneDirectionData::SRange*, aWorkingMemory+numberOfBytesOfWorkingMemoryUsed);
    1.75 +	numberOfBytesOfWorkingMemoryUsed+=(numberOfForeignToUnicodeDataRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    1.76 +	__ASSERT_ALWAYS(numberOfBytesOfWorkingMemoryUsed<=KNumberOfBytesOfWorkingMemory, Panic(EPanicTooManyBytesOfWorkingMemoryUsed1));
    1.77 +	aCompleteGbkConversionData.iForeignToUnicodeData.iRangeArray=foreignToUnicodeDataRangeArray;
    1.78 +	Mem::Copy(foreignToUnicodeDataRangeArray, gb2312ConversionData.iForeignToUnicodeData.iRangeArray, gb2312ConversionData.iForeignToUnicodeData.iNumberOfRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    1.79 +	Mem::Copy(foreignToUnicodeDataRangeArray+gb2312ConversionData.iForeignToUnicodeData.iNumberOfRanges, supplementaryGbkConversionData.iForeignToUnicodeData.iRangeArray, supplementaryGbkConversionData.iForeignToUnicodeData.iNumberOfRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    1.80 +	// set up the Unicode-to-foreign data
    1.81 +	const TInt numberOfUnicodeToForeignDataRanges=gb2312ConversionData.iUnicodeToForeignData.iNumberOfRanges+supplementaryGbkConversionData.iUnicodeToForeignData.iNumberOfRanges;
    1.82 +	aCompleteGbkConversionData.iUnicodeToForeignData.iNumberOfRanges=numberOfUnicodeToForeignDataRanges;
    1.83 +	SCnvConversionData::SOneDirectionData::SRange* unicodeToForeignDataRangeArray=REINTERPRET_CAST(SCnvConversionData::SOneDirectionData::SRange*, aWorkingMemory+numberOfBytesOfWorkingMemoryUsed);
    1.84 +	numberOfBytesOfWorkingMemoryUsed+=(numberOfUnicodeToForeignDataRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    1.85 +	__ASSERT_ALWAYS(numberOfBytesOfWorkingMemoryUsed<=KNumberOfBytesOfWorkingMemory, Panic(EPanicTooManyBytesOfWorkingMemoryUsed2));
    1.86 +	aCompleteGbkConversionData.iUnicodeToForeignData.iRangeArray=unicodeToForeignDataRangeArray;
    1.87 +	Mem::Copy(unicodeToForeignDataRangeArray, gb2312ConversionData.iUnicodeToForeignData.iRangeArray, gb2312ConversionData.iUnicodeToForeignData.iNumberOfRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    1.88 +	Mem::Copy(unicodeToForeignDataRangeArray+gb2312ConversionData.iUnicodeToForeignData.iNumberOfRanges, supplementaryGbkConversionData.iUnicodeToForeignData.iRangeArray, supplementaryGbkConversionData.iUnicodeToForeignData.iNumberOfRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    1.89 +	}
    1.90 +
    1.91 +EXPORT_C const TDesC8& CnvGbk::ReplacementForUnconvertibleUnicodeCharacters()
    1.92 +	{
    1.93 +	return ReplacementForUnconvertibleUnicodeCharacters_internal();
    1.94 +	}
    1.95 +
    1.96 +EXPORT_C const SCnvConversionData& CnvGbk::ConversionData()
    1.97 +	{
    1.98 +	return conversionData;
    1.99 +	}
   1.100 +
   1.101 +EXPORT_C TInt CnvGbk::ConvertFromUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, const TDesC8& aReplacementForUnconvertibleUnicodeCharacters, TDes8& aForeign, const TDesC16& aUnicode, CCnvCharacterSetConverter::TArrayOfAscendingIndices& aIndicesOfUnconvertibleCharacters)
   1.102 +	{
   1.103 +	TUint notUsed;
   1.104 +	return ConvertFromUnicode(aDefaultEndiannessOfForeignCharacters, aReplacementForUnconvertibleUnicodeCharacters, aForeign, aUnicode, aIndicesOfUnconvertibleCharacters, notUsed, 0);
   1.105 +	}
   1.106 +
   1.107 +EXPORT_C TInt CnvGbk::ConvertFromUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, const TDesC8& aReplacementForUnconvertibleUnicodeCharacters, TDes8& aForeign, const TDesC16& aUnicode, CCnvCharacterSetConverter::TArrayOfAscendingIndices& aIndicesOfUnconvertibleCharacters, TUint& aOutputConversionFlags, TUint aInputConversionFlags)
   1.108 +	{
   1.109 +	SCnvConversionData completeGbkConversionData;
   1.110 +	TUint8 workingMemory[KNumberOfBytesOfWorkingMemory]; // 900 bytes on the stack is just about tolerable (no other functions called from here will make any significant use of the stack)
   1.111 +	SetUpCompleteGbkConversionData(completeGbkConversionData, workingMemory);
   1.112 +	return CCnvCharacterSetConverter::DoConvertFromUnicode(completeGbkConversionData, aDefaultEndiannessOfForeignCharacters, aReplacementForUnconvertibleUnicodeCharacters, aForeign, aUnicode, aIndicesOfUnconvertibleCharacters, aOutputConversionFlags, aInputConversionFlags);
   1.113 +	}
   1.114 +
   1.115 +EXPORT_C TInt CnvGbk::ConvertToUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, TDes16& aUnicode, const TDesC8& aForeign, TInt& aNumberOfUnconvertibleCharacters, TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter)
   1.116 +	{
   1.117 +	TUint notUsed;
   1.118 +	return ConvertToUnicode(aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign, aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter, notUsed, 0);
   1.119 +	}
   1.120 +
   1.121 +EXPORT_C TInt CnvGbk::ConvertToUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, TDes16& aUnicode, const TDesC8& aForeign, TInt& aNumberOfUnconvertibleCharacters, TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter, TUint& aOutputConversionFlags, TUint aInputConversionFlags)
   1.122 +	{
   1.123 +	SCnvConversionData completeGbkConversionData;
   1.124 +	TUint8 workingMemory[KNumberOfBytesOfWorkingMemory]; // 900 bytes on the stack is just about tolerable (no other functions called from here will make any significant use of the stack)
   1.125 +	SetUpCompleteGbkConversionData(completeGbkConversionData, workingMemory);
   1.126 +	return CCnvCharacterSetConverter::DoConvertToUnicode(completeGbkConversionData, aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign, aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter, aOutputConversionFlags, aInputConversionFlags);
   1.127 +	}
   1.128 +
   1.129 +EXPORT_C TBool CnvGbk::IsCharGBBased(TInt& aConfidenceLevel, const TDesC8& aSample)
   1.130 +	{
   1.131 +	return CnvGb2312::IsCharGBBased(aConfidenceLevel, aSample);
   1.132 +	}