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 Descriptor8 to char * 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 descriptor of type TBuf8 to char* sl@0: * @param aSrc is the descriptor of type TBuf8 that is to be converted to char* sl@0: * @param aDes is a char* that will hold the result after conversion. Care should be taken to sl@0: * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is sl@0: * being allocated to the char* is being made known using n_size sl@0: * @param n_size is the size of the char*. This should have a minimum value equal to size of the sl@0: * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code sl@0: */ sl@0: sl@0: EXPORT_C int Tbuf8ToChar(TDes8& aSrc, char* aDes, int& n_size) sl@0: { sl@0: unsigned int ilen = aSrc.Length(); sl@0: sl@0: if (0 == ilen) sl@0: { sl@0: return EDescriptorNoData; sl@0: } sl@0: else if(!aDes) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else if (n_size < (ilen+1)) sl@0: { sl@0: n_size = ilen + 1; sl@0: return EInvalidSize; sl@0: } sl@0: sl@0: memcpy(aDes , (const char *)aSrc.Ptr(), ilen); sl@0: sl@0: aDes[ilen] = '\0'; sl@0: sl@0: return ESuccess; sl@0: } sl@0: sl@0: /** sl@0: * Converts a descriptor of type TBufC8 to char* sl@0: * @param aSrc is the descriptor of type TBufC8 that is to be converted to char* sl@0: * @param aDes is a char* that will hold the result after conversion. Care should be taken to sl@0: * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is sl@0: * being allocated to the char* is being made known using n_size sl@0: * @param n_size is the size of the char*. This should have a minimum value equal to size of the sl@0: * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code sl@0: */ sl@0: sl@0: EXPORT_C int Tbufc8ToChar(TDesC8& aSrc, char* aDes, int& n_size) sl@0: { sl@0: unsigned int ilen = aSrc.Length(); sl@0: sl@0: if (0 == ilen) sl@0: { sl@0: return EDescriptorNoData; sl@0: } sl@0: else if(!aDes) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else if(n_size < ilen+1) sl@0: { sl@0: n_size = ilen; sl@0: return EInvalidSize; sl@0: } sl@0: sl@0: memcpy(aDes, aSrc.Ptr(), ilen); sl@0: aDes[ilen] = '\0'; sl@0: sl@0: return ESuccess; sl@0: } sl@0: sl@0: /** sl@0: * Converts a descriptor of type TLitC8 to char* sl@0: * @param aSrc is the descriptor of type TLitC8 that is to be converted to char* sl@0: * @param aDes is a char* that will hold the result after conversion. Care should be taken to sl@0: * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is sl@0: * being allocated to the char* is being made known using n_size sl@0: * @param n_size is the size of the char*. This should have a minimum value equal to size of the sl@0: * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code sl@0: */ sl@0: sl@0: EXPORT_C int Tlitc8ToChar(const TDesC8& aSrc, char* aDes, int& n_size) sl@0: { sl@0: unsigned int ilen = aSrc.Length(); sl@0: sl@0: if (0 == ilen ) sl@0: { sl@0: return EDescriptorNoData; sl@0: } sl@0: else if(!aDes) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else if (n_size < ilen ) sl@0: { sl@0: n_size = ilen + 1; sl@0: return EInvalidSize; sl@0: } sl@0: sl@0: memcpy(aDes, (char *)aSrc.Ptr(), ilen); sl@0: aDes[ilen] = '\0'; sl@0: sl@0: return ESuccess; sl@0: } sl@0: sl@0: sl@0: /** sl@0: * Converts a descriptor of type TPtr8 to character stream sl@0: * sl@0: * @param aSrc is the descriptor to be converted , aDes is the sl@0: * reference to the character sream where the result of conversion sl@0: * is stored , n_size specifies the conversion size of the string sl@0: * @return Status code (0 is ESuccess, -2 is EInvalidSize , sl@0: * -4 is EInvalidPointer) sl@0: */ sl@0: EXPORT_C int Tptr8ToChar(const TDes8& aSrc, char* aDes, int& n_size) sl@0: { sl@0: unsigned int ilen = aSrc.Length(); sl@0: sl@0: if (0 == ilen) sl@0: { sl@0: return EDescriptorNoData; sl@0: } sl@0: else if ( !aDes ) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else if (n_size < ilen) sl@0: { sl@0: n_size = ilen + 1; sl@0: return EInvalidSize; sl@0: } sl@0: sl@0: memcpy(aDes , (const char *)aSrc.Ptr(), ilen); sl@0: aDes[ilen] = '\0'; sl@0: sl@0: return ESuccess; sl@0: } sl@0: sl@0: /** sl@0: * Converts a descriptor of type TPtrC8 to character stream sl@0: * sl@0: * @param aSrc is the descriptor to be converted , aDes is the sl@0: * reference to the character sream where the result of conversion sl@0: * is stored , n_size specifies the conversion size of the string sl@0: * @return Status code (0 is ESuccess, -2 is EInvalidSize , sl@0: * -4 is EInvalidPointer) sl@0: */ sl@0: EXPORT_C int Tptrc8ToCharp(TPtrC8& aSrc, char* aDes, int& n_size) sl@0: { sl@0: unsigned int ilen = aSrc.Length(); sl@0: if (0 == ilen) sl@0: { sl@0: return EDescriptorNoData; sl@0: } sl@0: else if ( !aDes ) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else if (n_size < ilen) sl@0: { sl@0: n_size = ilen + 1; sl@0: return EInvalidSize; sl@0: } sl@0: sl@0: memcpy(aDes , aSrc.Ptr(), ilen); sl@0: aDes[ilen] = '\0'; sl@0: sl@0: return ESuccess; sl@0: } sl@0: sl@0: /** sl@0: * Converts a descriptor of type RBuf8 to character stream sl@0: * sl@0: * @param aSrc is the descriptor to be converted , aDes is the sl@0: * reference to the character sream where the result of conversion sl@0: * is stored , n_size specifies the conversion size of the string sl@0: * @return Status code (0 is ESuccess, -2 is EInvalidSize , sl@0: * -4 is EInvalidPointer , -5 is EDescriptorNoData) sl@0: */ sl@0: sl@0: EXPORT_C int Rbuf8ToChar(TDes8& aSrc, char* aDes, int& n_size) sl@0: { sl@0: unsigned int ilen = aSrc.Length(); sl@0: sl@0: if (0 == ilen) sl@0: { sl@0: return EDescriptorNoData; sl@0: } sl@0: else if ( !aDes ) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else if (n_size < ilen) sl@0: { sl@0: n_size = ilen + 1; sl@0: return EInvalidSize; sl@0: } sl@0: sl@0: memcpy (aDes,(char *)aSrc.Ptr(), ilen); sl@0: aDes[ilen] = '\0'; sl@0: sl@0: return ESuccess; sl@0: } sl@0: sl@0: /** sl@0: * Converts a descriptor of type HBufC8 to character stream sl@0: * sl@0: * @param aSrc is the descriptor to be converted , aDes is the sl@0: * reference to the character sream where the result of conversion sl@0: * is stored , n_size specifies the conversion size of the string sl@0: * @return Status code (0 is ESuccess, -2 is EInvalidSize , sl@0: * -4 is EInvalidPointer , -5 is EDescriptorNoData) sl@0: */ sl@0: sl@0: EXPORT_C int Hbufc8ToChar(HBufC8 *aSrc, char* aDes, int& n_size) sl@0: { sl@0: unsigned int ilen = 0; sl@0: sl@0: if ( !aDes || !aSrc) sl@0: { sl@0: return EInvalidPointer; sl@0: } sl@0: else sl@0: { sl@0: ilen = aSrc->Length(); sl@0: if (0 == ilen) sl@0: { sl@0: return EDescriptorNoData; sl@0: } sl@0: else if (n_size < ilen) sl@0: { sl@0: n_size = ilen + 1; sl@0: return EInvalidSize; sl@0: } sl@0: } sl@0: sl@0: memcpy (aDes,(char *)aSrc->Ptr(), ilen); sl@0: aDes[ilen] = '\0'; sl@0: sl@0: return ESuccess; sl@0: }