sl@0: /* sl@0: * Copyright (c) 2008 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 "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: Contains the source for wchar * to Descriptor8 conversions sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "libutils.h" sl@0: sl@0: sl@0: sl@0: /** sl@0: * Converts a wchar to a descriptor of type TBuf8 sl@0: * sl@0: * @param aSrc is the wchar to be converted , aDes is the sl@0: * reference to the descriptor where the result of conversion sl@0: * is stored sl@0: * @return Status code (0 is ESuccess,-1 is EInsufficientMemory, sl@0: * -3 is EStringNoData, -4 is EInvalidPointer ) sl@0: */ sl@0: sl@0: EXPORT_C int WcharToTbuf8(const wchar_t* aSrc, TDes8& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int ilen = 0; sl@0: int minusone = -1; sl@0: if ( !aSrc ) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else sl@0: { sl@0: ilen = wcslen(aSrc); sl@0: if (ilen*2 > aDes.MaxLength()) sl@0: { sl@0: return EInsufficientMemory; sl@0: } sl@0: } sl@0: sl@0: char *CharString = new char[ilen*2]; sl@0: sl@0: if (!CharString) sl@0: { sl@0: return EInsufficientSystemMemory; sl@0: } sl@0: sl@0: if(minusone == wcstombs(CharString, (const wchar_t*)aSrc, ilen*2)) sl@0: { sl@0: retval = EInvalidWCSSequence; sl@0: } sl@0: else sl@0: { sl@0: aDes.Copy((unsigned char *)CharString, ilen*2); sl@0: } sl@0: sl@0: delete []CharString; sl@0: return retval; sl@0: } sl@0: sl@0: /** sl@0: * Converts a wchar to a descriptor of type HBufc8 sl@0: * sl@0: * @param aSrc is the Wstring to be converted, aDes is the sl@0: * reference to the descriptor where the result of conversion sl@0: * is stored sl@0: * @return Status code (0 is ESuccess, -1 is EInsufficientMemory ) sl@0: */ sl@0: sl@0: EXPORT_C int WcharToHbufc8(const wchar_t* aSrc, HBufC8* aDes) sl@0: { sl@0: unsigned int wlen = 0, ilendes = 0; sl@0: int retval = ESuccess; sl@0: int minusone = -1; sl@0: if ( !aSrc || !aDes) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else sl@0: { sl@0: wlen = wcslen(aSrc); sl@0: ilendes = aDes->Length(); sl@0: if(0 == ilendes) sl@0: { sl@0: return EUseNewMaxL; sl@0: } sl@0: else if (wlen > ilendes) sl@0: { sl@0: return EInsufficientMemory; sl@0: } sl@0: } sl@0: sl@0: char *CharString = new char[wlen*2]; sl@0: sl@0: if (!CharString) sl@0: { sl@0: return EInsufficientSystemMemory; sl@0: } sl@0: sl@0: sl@0: if(minusone == wcstombs(CharString, aSrc, wlen*2)) sl@0: { sl@0: retval = EInvalidWCSSequence; sl@0: } sl@0: else sl@0: { sl@0: (*aDes) = (const TUint8 *)CharString; sl@0: } sl@0: sl@0: delete []CharString; sl@0: return retval; sl@0: } sl@0: sl@0: /** sl@0: * Converts a wchar to a descriptor of type TPtr8 sl@0: * sl@0: * @param aSrc is the wchar to be converted , aDes is the sl@0: * reference to the descriptor where the result of conversion sl@0: * is stored sl@0: * @return Status code (0 is ESuccess,-1 is EInsufficientMemory, sl@0: * -3 is EStringNoData, -4 is EInvalidPointer ) sl@0: */ sl@0: sl@0: EXPORT_C int WcharpToTptr8(const wchar_t* aSrc, char* cPtr, TPtr8& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int wlen = 0, ilendes = 0; sl@0: int minusone = -1; sl@0: if ( !aSrc ) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else sl@0: { sl@0: wlen = wcslen(aSrc); sl@0: ilendes = aDes.MaxLength(); sl@0: if (ilendes < wlen ) sl@0: { sl@0: return EInsufficientMemory; sl@0: } sl@0: } sl@0: sl@0: if(minusone != wcstombs(cPtr, (const wchar_t*)aSrc, wlen)) sl@0: { sl@0: aDes.Set((TUint8*)cPtr, wlen, ilendes); sl@0: } sl@0: else sl@0: { sl@0: retval = EInvalidWCSSequence; sl@0: } sl@0: sl@0: return retval; sl@0: } sl@0: sl@0: /** sl@0: * Converts a wchar to a descriptor of type TPtrC8 sl@0: * sl@0: * @param aSrc is the wchar to be converted , aDes is the sl@0: * reference to the descriptor where the result of conversion sl@0: * is stored sl@0: * @return Status code (0 is ESuccess,-1 is EInsufficientMemory, sl@0: * -4 is EInvalidPointer ) sl@0: */ sl@0: sl@0: EXPORT_C int WcharpToTptrc8(const wchar_t* aSrc, char* cPtr, TPtrC8& aDes) sl@0: { sl@0: sl@0: int retval = ESuccess; sl@0: unsigned int wlen = 0 ; sl@0: int minusone = -1; sl@0: sl@0: if ( !aSrc || !cPtr) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: sl@0: wlen = wcslen(aSrc); sl@0: sl@0: if(minusone != wcstombs(cPtr, (const wchar_t*)aSrc, wlen)) sl@0: { sl@0: aDes.Set((TUint8*)cPtr,wlen); sl@0: } sl@0: else sl@0: { sl@0: retval = EInvalidWCSSequence; sl@0: } sl@0: sl@0: return retval; sl@0: } sl@0: sl@0: sl@0: /** sl@0: * Converts a wchar to a descriptor of type RBuf8 sl@0: * sl@0: * @param aSrc is the wchar to be converted , aDes is the sl@0: * reference to the descriptor where the result of conversion sl@0: * is stored sl@0: * @return Status code (0 is ESuccess ,-1 is EInsufficientMemory, -4 is EInvalidPointer ) sl@0: */ sl@0: sl@0: EXPORT_C int WcharToRbuf8(const wchar_t* aSrc, RBuf8& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int wlen = 0; sl@0: char* buf = NULL; sl@0: int minusone = -1; sl@0: sl@0: if (!aSrc) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: sl@0: wlen = wcslen(aSrc); sl@0: sl@0: buf = new char[wlen*2]; sl@0: if (!buf) sl@0: { sl@0: return EInsufficientSystemMemory; sl@0: } sl@0: sl@0: if(minusone != wcstombs(buf, (const wchar_t*)aSrc, wlen*2)) sl@0: { sl@0: aDes.Copy((const unsigned char *)buf, wlen*2); sl@0: } sl@0: else sl@0: { sl@0: retval = EInvalidWCSSequence; sl@0: } sl@0: sl@0: delete []buf; sl@0: return retval; sl@0: }