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 wstring to Descriptor8 conversions sl@0: * 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 wstring to a descriptor of type TBufc8 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: * -3 is EStringNoData, -4 is EInvalidPointer, -8 is EInvalidWCSSequence sl@0: * -9 is EInsufficientSystemMemory) sl@0: */ sl@0: sl@0: EXPORT_C int WstringToTbuf8(wstring& aSrc, TDes8& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int ilen = 0; sl@0: const wchar_t* wcharString = aSrc.c_str(); sl@0: int minusone = -1; sl@0: sl@0: if (L'\0' == wcharString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: sl@0: ilen = wcslen(wcharString); sl@0: sl@0: if (ilen*2 > aDes.MaxLength()) sl@0: { sl@0: return EInsufficientMemory; 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*)wcharString, 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 wstring to a descriptor of type TPtr8 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: * -3 is EStringNoData, -4 is EInvalidPointer ) sl@0: */ sl@0: sl@0: EXPORT_C int WstringToTptr8(wstring& aSrc, char* cPtr, TPtr8& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int wlen = 0, ilendes = 0; sl@0: const wchar_t* wcharString = aSrc.c_str(); sl@0: int minusone = -1; sl@0: sl@0: if (L'\0' == wcharString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: sl@0: wlen = wcslen(wcharString); sl@0: ilendes = aDes.MaxLength(); sl@0: sl@0: if (ilendes < 2*wlen) sl@0: { sl@0: return EInsufficientMemory; sl@0: } sl@0: sl@0: if(minusone != wcstombs(cPtr, wcharString, wlen*2)) sl@0: { sl@0: aDes.Set((unsigned char *)cPtr, wlen*2, 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 wstring to a descriptor of type TPtrC8 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: * -3 is EStringNoData, -4 is EInvalidPointer ) sl@0: */ sl@0: sl@0: EXPORT_C int WstringToTptrc8(wstring& aSrc, char* cPtr, TPtrC8& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int wlen = 0; sl@0: const wchar_t* wcharString = aSrc.c_str(); sl@0: int minusone = -1; sl@0: sl@0: if (L'\0' == wcharString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: else if ( !cPtr ) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: sl@0: wlen = wcslen(wcharString); sl@0: sl@0: if(minusone != wcstombs(cPtr, (const wchar_t*)wcharString, wlen*2 )) sl@0: { sl@0: aDes.Set((TUint8 *)(cPtr), wlen*2); 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 wstring 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: * -3 is EStringNoData ) sl@0: */ sl@0: sl@0: EXPORT_C int WstringToHbufc8(wstring& aSrc, HBufC8* aDes) sl@0: { sl@0: unsigned int wlen = 0 , ilendes = 0; sl@0: int retval = ESuccess; sl@0: const wchar_t* wcharString = aSrc.c_str(); sl@0: int minusone = -1; sl@0: sl@0: if (L'\0' == wcharString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: else if (!aDes) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: sl@0: wlen = wcslen(wcharString); sl@0: ilendes = aDes->Length(); sl@0: sl@0: if (0 == ilendes) sl@0: { sl@0: return EUseNewMaxL; sl@0: } sl@0: else if (ilendes < wlen) sl@0: { sl@0: return EInsufficientMemory; 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, (const wchar_t *)wcharString, wlen*2)) sl@0: { sl@0: retval = EInvalidWCSSequence; sl@0: } sl@0: else sl@0: { sl@0: *aDes = (TUint8 *)CharString; sl@0: } sl@0: sl@0: delete []CharString; sl@0: return retval; sl@0: } sl@0: sl@0: /** sl@0: * Converts a wstring to a descriptor of type RBuf8 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 -3 is EStringNoData ) sl@0: */ sl@0: sl@0: EXPORT_C int WstringToRbuf8(const wstring& aSrc, RBuf8& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int wlen = 0; sl@0: const wchar_t* wcharString = aSrc.c_str(); sl@0: int minusone = -1; sl@0: sl@0: if (L'\0' == wcharString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: sl@0: wlen = wcslen(aSrc.c_str()); sl@0: sl@0: char* buf = new char[wlen*2]; sl@0: sl@0: if (!buf) sl@0: { sl@0: return EInsufficientSystemMemory; sl@0: } sl@0: sl@0: if(minusone != wcstombs(buf, (const wchar_t*)wcharString, 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: }