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 string to Descriptor16 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 string to a descriptor of type TBuf16 sl@0: * sl@0: * @param aSrc is the string to be converted , aDes is the sl@0: * reference to the descriptor where the result of conversion sl@0: * is stored,n_size specifies the conversion size of the char array sl@0: * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, sl@0: * -3 is EStringNoData, -9 is EInsufficientSystemMemory) sl@0: */ sl@0: EXPORT_C int StringToTbuf16(string& aSrc, TDes16& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: const char* charString = aSrc.c_str(); sl@0: int ilen = 0; sl@0: int minusone = -1; sl@0: sl@0: if('\0' == charString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: sl@0: ilen = strlen(charString); sl@0: sl@0: if (ilen > aDes.MaxLength()) sl@0: { sl@0: return EInsufficientMemory; sl@0: } sl@0: sl@0: wchar_t* WcharString = new wchar_t[ilen]; sl@0: sl@0: if(!WcharString) sl@0: { sl@0: return EInsufficientSystemMemory; sl@0: } sl@0: sl@0: if(minusone == mbstowcs(WcharString, (const char*)charString, ilen)) sl@0: { sl@0: retval = EInvalidMBSSequence; sl@0: } sl@0: else sl@0: { sl@0: WcharString[ilen] = L'\0'; sl@0: aDes.Copy((unsigned short*)WcharString,ilen); sl@0: } sl@0: sl@0: delete []WcharString; sl@0: return retval; sl@0: } sl@0: sl@0: /** sl@0: * Converts a string to a descriptor of type Tptr16 sl@0: * sl@0: * @param aSrc is the string 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 StringToTptr16 (string& aSrc, wchar_t *wPtr, TPtr16& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: int ilen = 0, ideslen = 0; sl@0: const char* charString = aSrc.c_str(); sl@0: int minusone = -1; sl@0: sl@0: if('\0' == charString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: else if(!wPtr) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: sl@0: ilen = strlen(charString); sl@0: ideslen = aDes.MaxLength(); sl@0: sl@0: if (ilen > ideslen) sl@0: { sl@0: return EInsufficientMemory; sl@0: } sl@0: sl@0: if(minusone != mbstowcs(wPtr, (const char*)charString, ilen)) sl@0: { sl@0: aDes.Set((unsigned short *)wPtr, ilen, ideslen); sl@0: } sl@0: else sl@0: { sl@0: retval = EInvalidMBSSequence; sl@0: } sl@0: sl@0: return retval; sl@0: } sl@0: sl@0: /** sl@0: * Converts a string to a descriptor of type Tptrc16 sl@0: * sl@0: * @param aSrc is the string 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 StringToTptrc16(string& aSrc, wchar_t* wptr, TPtrC16& aDes) sl@0: { sl@0: unsigned int ilen = 0; sl@0: int retval = ESuccess; sl@0: const char* charString = aSrc.c_str(); sl@0: int minusone = -1; sl@0: sl@0: if (!wptr) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: sl@0: if('\0' == charString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: sl@0: ilen = strlen(charString); sl@0: sl@0: if(minusone != mbstowcs(wptr, (const char*)charString, ilen )) sl@0: { sl@0: aDes.Set((TUint16 *)(wptr), ilen); sl@0: } sl@0: else sl@0: { sl@0: retval = EInvalidMBSSequence; sl@0: } sl@0: sl@0: return retval; sl@0: } sl@0: sl@0: /** sl@0: * Converts a string to a descriptor of type HBufc16 sl@0: * sl@0: * @param aSrc is the string 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, -6 is EUseNewMaxL, sl@0: * -9 is EInsufficientSystemMemory) sl@0: */ sl@0: sl@0: EXPORT_C int StringToHbufc16(string& aSrc , HBufC16* aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int ilen = 0, ilendes = 0; sl@0: const char* charString = aSrc.c_str(); sl@0: int minusone = -1; sl@0: sl@0: if('\0' == charString[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: ilen = strlen(charString); 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 < ilen) sl@0: { sl@0: return EInsufficientMemory; sl@0: } sl@0: sl@0: wchar_t* WcharString = new wchar_t[ilen]; sl@0: sl@0: if(!WcharString) sl@0: { sl@0: return EInsufficientSystemMemory; sl@0: } sl@0: sl@0: if(minusone == mbstowcs(WcharString, (const char*)charString, ilen)) sl@0: { sl@0: retval = EInvalidMBSSequence; sl@0: } sl@0: else sl@0: { sl@0: TPtrC16 temp((unsigned short*)WcharString, ilen); sl@0: *aDes = temp; sl@0: } sl@0: sl@0: delete[] WcharString; sl@0: return retval; sl@0: } sl@0: sl@0: /** sl@0: * Converts a string to a descriptor of type RBufc16 sl@0: * sl@0: * @param aSrc is the string 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, -7 is EInvalidMBSSequence, -9 is EInsufficientSystemMemory) sl@0: */ sl@0: sl@0: EXPORT_C int StringToRbuf16(const string& aSrc, RBuf16& aDes) sl@0: { sl@0: int retval = ESuccess; sl@0: unsigned int ilen = 0; sl@0: int minusone = -1; sl@0: sl@0: const char* charString = aSrc.c_str(); sl@0: sl@0: if('\0' == charString[0]) sl@0: { sl@0: return EStringNoData; sl@0: } sl@0: sl@0: ilen = strlen(charString); sl@0: sl@0: wchar_t* buf = new wchar_t[ilen]; sl@0: sl@0: if (!buf) sl@0: { sl@0: return EInsufficientMemory; sl@0: } sl@0: sl@0: if(minusone != mbstowcs(buf, charString, ilen)) sl@0: { sl@0: aDes.Copy((const unsigned short *)buf, ilen); sl@0: } sl@0: else sl@0: { sl@0: retval = EInvalidMBSSequence; sl@0: } sl@0: sl@0: delete []buf; sl@0: return retval; sl@0: }