sl@0: /* sl@0: * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "stringconv.h" sl@0: #include "logger.h" sl@0: sl@0: TUint8 *cstrFromUtf16(const TText *aUtf16, TInt aLength, TInt &outputBytes) sl@0: { sl@0: TPtrC16 src(aUtf16, aLength); sl@0: sl@0: TPtr8 nulldest(0,0); sl@0: sl@0: TInt len = CnvUtfConverter::ConvertFromUnicodeToUtf8(nulldest, src); sl@0: if(len<0) sl@0: { sl@0: dbg << Log::Indent() << "ConvertFromUnicodeToUtf8 failed" << Log::Endl(); sl@0: FatalError(); sl@0: } sl@0: sl@0: TUint8 *buf = new TUint8[len+1]; sl@0: TPtr8 dest(buf, len); sl@0: sl@0: (void) CnvUtfConverter::ConvertFromUnicodeToUtf8(dest, src); sl@0: buf[len] = 0; // Add NUL termination in case it is used with windows APIs sl@0: outputBytes = dest.Length(); sl@0: return buf; sl@0: } sl@0: sl@0: std::string stringFromUtf16(const TText *aUtf16, TInt aLength) sl@0: { sl@0: TInt outputBytes = 0; sl@0: TUint8 *outBuf = cstrFromUtf16(aUtf16, aLength, outputBytes); sl@0: sl@0: std::string str((const char *)outBuf, outputBytes); sl@0: delete [] outBuf; sl@0: return str; sl@0: } sl@0: sl@0: std::string stringFromUtf16(const TDesC &aUtf16) sl@0: { sl@0: return stringFromUtf16(aUtf16.Ptr(), aUtf16.Length()); sl@0: } sl@0: sl@0: TText *utf16FromUtf8(const TUint8 *aUtf8, TInt aLength, TInt &outputWords) sl@0: { sl@0: // Expand UTF-8 into internal UTF-16LE representation sl@0: sl@0: TPtrC8 src(aUtf8, aLength); sl@0: sl@0: TPtr16 nulldest(0,0); sl@0: sl@0: TInt len = CnvUtfConverter::ConvertToUnicodeFromUtf8(nulldest, src); sl@0: if(len<0) sl@0: { sl@0: dbg << Log::Indent() << "ConvertToUnicodeFromUtf8 failed" << Log::Endl(); sl@0: FatalError(); sl@0: } sl@0: sl@0: TUint16 *buf = new TUint16[len+1]; sl@0: TPtr16 dest(buf, len); sl@0: sl@0: (void) CnvUtfConverter::ConvertToUnicodeFromUtf8(dest, src); sl@0: sl@0: buf[len] = 0; // Add null termination in case it gets used with windows APIs sl@0: outputWords = dest.Length(); sl@0: return buf; sl@0: } sl@0: sl@0: sl@0: // End of file