charconvfw/Charconv/ongoing/Source/foreign/plugins/UCS2.CPP
changeset 32 8b9155204a54
equal deleted inserted replaced
31:b9ad20498fb4 32:8b9155204a54
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:      
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 #include <e32std.h>
       
    26 #include <charconv.h>
       
    27 #include <convgeneratedcpp.h>
       
    28 #include <UCS2.H>
       
    29 #include <ecom/implementationproxy.h>
       
    30 #include "charactersetconverter.h"
       
    31 
       
    32 class CUCS2ConverterImpl : public CCharacterSetConverterPluginInterface
       
    33 	{
       
    34 
       
    35 public:
       
    36 	virtual const TDesC8& ReplacementForUnconvertibleUnicodeCharacters();
       
    37 
       
    38 	virtual TInt ConvertFromUnicode(
       
    39 		CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, 
       
    40 		const TDesC8& aReplacementForUnconvertibleUnicodeCharacters, 
       
    41 		TDes8& aForeign, 
       
    42 		const TDesC16& aUnicode, 
       
    43 		CCnvCharacterSetConverter::TArrayOfAscendingIndices& aIndicesOfUnconvertibleCharacters);
       
    44 
       
    45 	virtual TInt ConvertToUnicode(
       
    46 		CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, 
       
    47 		TDes16& aUnicode, 
       
    48 		const TDesC8& aForeign, 
       
    49 		TInt& aState, 
       
    50 		TInt& aNumberOfUnconvertibleCharacters, 
       
    51 		TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter);
       
    52 
       
    53 	virtual TBool IsInThisCharacterSetL(
       
    54 		TBool& aSetToTrue, 
       
    55 		TInt& aConfidenceLevel, 
       
    56 		const TDesC8& aSample);
       
    57 
       
    58 	static CUCS2ConverterImpl* NewL();
       
    59 	virtual ~CUCS2ConverterImpl();
       
    60 
       
    61 private:
       
    62 	CUCS2ConverterImpl();
       
    63 	void ConstructL();
       
    64 
       
    65 	};
       
    66 
       
    67 
       
    68 
       
    69 // The following code has been copied and modified from the plugin computer generated code
       
    70 // that is generated from the charconv/data/???.cpl && /???.txt files
       
    71 // *** code begins ***
       
    72 #define ARRAY_LENGTH(aArray) (sizeof(aArray)/sizeof((aArray)[0]))
       
    73 
       
    74 #pragma warning (disable: 4049) // compiler limit : terminating line number emission
       
    75 
       
    76 _LIT8(KLit8ReplacementForUnconvertibleUnicodeCharacters, "\xff\xfd");
       
    77 
       
    78 GLDEF_C const TDesC8& ReplacementForUnconvertibleUnicodeCharacters_internal()
       
    79 	{
       
    80 	return KLit8ReplacementForUnconvertibleUnicodeCharacters;
       
    81 	}
       
    82 
       
    83 GLDEF_D const SCnvConversionData conversionData=
       
    84 	{
       
    85 	SCnvConversionData::EUnspecified,
       
    86 	{NULL,NULL},
       
    87 	{NULL,NULL},
       
    88 	{NULL,NULL},
       
    89 	NULL,
       
    90 	NULL
       
    91 	};
       
    92 
       
    93 
       
    94 const TInt    KByteOrderMark = 0xfeff;
       
    95 const TInt 	  KStateOffset = 0x1000;
       
    96 
       
    97 #ifndef EKA2
       
    98 GLDEF_C TInt E32Dll(TDllReason)
       
    99 	{
       
   100 	return KErrNone;
       
   101 	}
       
   102 #endif//EKA2
       
   103 
       
   104 const TDesC8& CUCS2ConverterImpl::ReplacementForUnconvertibleUnicodeCharacters()
       
   105 	{
       
   106 	return ReplacementForUnconvertibleUnicodeCharacters_internal();
       
   107 	}
       
   108 
       
   109 
       
   110 /**
       
   111  * Takes a 16 bit UCS2 descriptor with or without BOM and translates it to an
       
   112  * eight bit descriptor in Big Endian format.
       
   113  *
       
   114  * Note aDefaultEndiannessOfForeignCharacters is not used by this converter
       
   115  *
       
   116  * @param CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters
       
   117  * @param const TDesC8&
       
   118  * @param TDes8& aForeign
       
   119  * @param const TDesC16& aUnicode
       
   120  * @param CCnvCharacterSetConverter::TArrayOfAscendingIndices&
       
   121  *
       
   122  * returns number of converted characters
       
   123 */
       
   124 TInt CUCS2ConverterImpl::ConvertFromUnicode(CCnvCharacterSetConverter::TEndianness /*aDefaultEndiannessOfForeignCharacters*/, 
       
   125 								 const TDesC8& /*aReplacementForUnconvertibleUnicodeCharacters*/, 
       
   126 								 TDes8& aForeign, 
       
   127 								 const TDesC16& aUnicode, 
       
   128 								 CCnvCharacterSetConverter::TArrayOfAscendingIndices& /*aIndicesOfUnconvertibleCharacters*/)
       
   129 	{
       
   130 	TInt numberOfUnicodeCharacters =0;
       
   131 	TInt nextChar;
       
   132 	
       
   133 	// start at the begining of the output buffer
       
   134 	aForeign.Zero();
       
   135 	
       
   136 	// while there is unicode data to convert and space in the output buffer
       
   137 	while ( (aForeign.Length() + 1 < aForeign.MaxLength()) && (numberOfUnicodeCharacters < aUnicode.Length()) )
       
   138 		{
       
   139 		nextChar = aUnicode[numberOfUnicodeCharacters];
       
   140 
       
   141 		// Note - this always converts to EBigEndian 
       
   142 		aForeign.Append((nextChar & 0xff00) >> 8);
       
   143 		aForeign.Append(nextChar & 0xff );
       
   144 			
       
   145 		numberOfUnicodeCharacters++;
       
   146 		}
       
   147 		
       
   148 	// returns the number of unconverted characters left at the end of the input descriptor  
       
   149 	return aUnicode.Length() - numberOfUnicodeCharacters;
       
   150 	}
       
   151 
       
   152 /**
       
   153  * Takes an 8 bit descriptor with or without a BOM and translates it to unicode 
       
   154  * Input endiness is determined by Byte Order Markers (BOM) in the source text.
       
   155  * If no BOM is present aDefaultEndiannessOfForeignCharacters is used.
       
   156  *
       
   157  * When the data is too large to fit in the output buffer, the endiness is saved in the state
       
   158  * variable between conversions
       
   159  *
       
   160  * @param aDefaultEndiannessOfForeignCharacters Default endiness if no BOMs present in the source
       
   161  * @param aUnicode Contains the converted text in the Unicode character set
       
   162  * @param aForeign The non-Unicode source text to be converted
       
   163  * @param aState Not used by this converter
       
   164  * @param aNumberOfUnconvertibleCharacters Contains the number of bytes which could not be converted to unicode
       
   165  * @param aIndexOfFirstByteOfFirstUnconvertibleCharacter The index of the first unconvertable byte or -1 if all converted.
       
   166  *
       
   167  * @return aNumberOfUnconvertibleCharacters The number of unconverted bytes left at the end of the input 
       
   168  * descriptor (e.g. because the output descriptor is not long enough to hold all the text), or one of the 
       
   169  * error values defined in TError. 
       
   170  * @internalTechnology 
       
   171  */
       
   172 TInt CUCS2ConverterImpl::ConvertToUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, 
       
   173 						   TDes16& aUnicode,	
       
   174 						   const TDesC8& aForeign,
       
   175 						   TInt& aState,
       
   176 						   TInt& aNumberOfUnconvertibleCharacters, 
       
   177 						   TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter) 
       
   178 	{
       
   179 	TInt numberOfBytesConverted = 0;
       
   180 	TInt numberOfUnicodeCharacters =0;
       
   181 	TChar nextChar;
       
   182 	
       
   183 	// work out what byte order to use
       
   184 	CCnvCharacterSetConverter::TEndianness byteOrderMark;
       
   185 	if ( aState==CCnvCharacterSetConverter::KStateDefault )
       
   186 		{
       
   187 		// this is the first call so use the default or BOM for byte order
       
   188 		byteOrderMark = aDefaultEndiannessOfForeignCharacters;
       
   189 		}
       
   190 	else
       
   191 		{
       
   192 		// this is not the first call so use the saved byte order
       
   193 		byteOrderMark = STATIC_CAST( CCnvCharacterSetConverter::TEndianness, aState - KStateOffset );
       
   194 		}
       
   195 		
       
   196 	if ( aForeign.Length() < 2)
       
   197 		{ // too small to do anything with		
       
   198 		return -1;
       
   199 		}
       
   200 	// If the state is KStateDefault (this is the first call) check for BOM markers
       
   201 	else if (aState==CCnvCharacterSetConverter::KStateDefault)
       
   202 		{
       
   203 		// is there a Little Endian BOM
       
   204 		if (aForeign[0]==0xff && aForeign[1]==0xfe )
       
   205 			{ 
       
   206 			byteOrderMark = CCnvCharacterSetConverter::ELittleEndian; 
       
   207 			}
       
   208 		else if (aForeign[0]==0xfe && aForeign[1]==0xff )
       
   209 			{
       
   210 			byteOrderMark = CCnvCharacterSetConverter::EBigEndian; 
       
   211 			}
       
   212 		// remember the detected state
       
   213 		aState = byteOrderMark + KStateOffset;
       
   214 		}
       
   215 
       
   216 	// start at begining of the output buffer provided
       
   217 	aUnicode.Zero();
       
   218 	
       
   219 	// while there is at least 2 bytes of data to convert and space in the output buffer
       
   220 	while ( (numberOfBytesConverted+1 < aForeign.Size()) && (numberOfUnicodeCharacters < aUnicode.MaxLength()) )
       
   221 		{
       
   222 		if (byteOrderMark == CCnvCharacterSetConverter::ELittleEndian )
       
   223 			{
       
   224 			// ELittleEndian 0x??00
       
   225 			nextChar = aForeign[numberOfBytesConverted] + ( aForeign[numberOfBytesConverted+1] << 8);
       
   226 			}
       
   227 		else
       
   228 			{
       
   229 			// EBigEndian 0x00??
       
   230 			nextChar = ( aForeign[numberOfBytesConverted] <<8 ) + aForeign[numberOfBytesConverted+1];
       
   231 			}
       
   232 			
       
   233 		// save the unicode character extracted	unless it's a BOM
       
   234 		if ( nextChar != KByteOrderMark )
       
   235 			{
       
   236 			aUnicode.Append( nextChar );
       
   237 			numberOfUnicodeCharacters++;	
       
   238 			}
       
   239 			
       
   240 		numberOfBytesConverted+=2;
       
   241 		}
       
   242 	
       
   243 	// there are no uncovertable characters with UCS2,
       
   244 	aNumberOfUnconvertibleCharacters = 0;
       
   245 	// a negative value indicates that all characters converted
       
   246 	aIndexOfFirstByteOfFirstUnconvertibleCharacter = -1;
       
   247 			
       
   248 	// returns the number of unconverted bytes left at the end of the input descriptor 
       
   249 	// Note there could be 1 byte left over if an odd number of bytes provided for conversion
       
   250 	return aForeign.Size() - numberOfBytesConverted;
       
   251 	}
       
   252 
       
   253 
       
   254 /**
       
   255  * This converter does not support autodetect so always returns a confidence value of 0.
       
   256  * @internalTechnology 
       
   257  */
       
   258 TBool CUCS2ConverterImpl::IsInThisCharacterSetL(TBool& aSetToTrue, TInt& aConfidenceLevel, const TDesC8&)
       
   259 	{
       
   260 	aSetToTrue=ETrue;
       
   261 	aConfidenceLevel=0;
       
   262 	return EFalse;
       
   263 	}
       
   264 
       
   265 CUCS2ConverterImpl* CUCS2ConverterImpl::NewL()
       
   266 	{
       
   267 	CUCS2ConverterImpl* self = new(ELeave) CUCS2ConverterImpl();
       
   268 	CleanupStack::PushL(self);
       
   269 	self->ConstructL();
       
   270 	CleanupStack::Pop(self);
       
   271 	return self;
       
   272 	}
       
   273 
       
   274 CUCS2ConverterImpl::~CUCS2ConverterImpl()
       
   275 	{
       
   276 	}
       
   277 
       
   278 CUCS2ConverterImpl::CUCS2ConverterImpl()
       
   279 	{
       
   280 	}
       
   281 
       
   282 void CUCS2ConverterImpl::ConstructL()
       
   283 	{
       
   284 	}
       
   285 
       
   286 const TImplementationProxy ImplementationTable[] = 
       
   287 	{
       
   288 		IMPLEMENTATION_PROXY_ENTRY(0x101FF492,	CUCS2ConverterImpl::NewL)
       
   289 	};
       
   290 
       
   291 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
   292 	{
       
   293 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
   294 
       
   295 	return ImplementationTable;
       
   296 	}
       
   297 
       
   298