1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/utilitylibraries/libutils/src/descriptor8tochar.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,263 @@
1.4 +/*
1.5 +* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description: Contains the source for Descriptor8 to char * conversions
1.18 + *
1.19 +*/
1.20 +
1.21 +
1.22 +
1.23 +#include "libutils.h"
1.24 +
1.25 +
1.26 +
1.27 + /**
1.28 + * Converts a descriptor of type TBuf8 to char*
1.29 + * @param aSrc is the descriptor of type TBuf8 that is to be converted to char*
1.30 + * @param aDes is a char* that will hold the result after conversion. Care should be taken to
1.31 + * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
1.32 + * being allocated to the char* is being made known using n_size
1.33 + * @param n_size is the size of the char*. This should have a minimum value equal to size of the
1.34 + * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
1.35 + */
1.36 +
1.37 +EXPORT_C int Tbuf8ToChar(TDes8& aSrc, char* aDes, int& n_size)
1.38 +{
1.39 + unsigned int ilen = aSrc.Length();
1.40 +
1.41 + if (0 == ilen)
1.42 + {
1.43 + return EDescriptorNoData;
1.44 + }
1.45 + else if(!aDes)
1.46 + {
1.47 + return EInvalidPointer;
1.48 + }
1.49 + else if (n_size < (ilen+1))
1.50 + {
1.51 + n_size = ilen + 1;
1.52 + return EInvalidSize;
1.53 + }
1.54 +
1.55 + memcpy(aDes , (const char *)aSrc.Ptr(), ilen);
1.56 +
1.57 + aDes[ilen] = '\0';
1.58 +
1.59 + return ESuccess;
1.60 +}
1.61 +
1.62 + /**
1.63 + * Converts a descriptor of type TBufC8 to char*
1.64 + * @param aSrc is the descriptor of type TBufC8 that is to be converted to char*
1.65 + * @param aDes is a char* that will hold the result after conversion. Care should be taken to
1.66 + * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
1.67 + * being allocated to the char* is being made known using n_size
1.68 + * @param n_size is the size of the char*. This should have a minimum value equal to size of the
1.69 + * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
1.70 + */
1.71 +
1.72 +EXPORT_C int Tbufc8ToChar(TDesC8& aSrc, char* aDes, int& n_size)
1.73 +{
1.74 + unsigned int ilen = aSrc.Length();
1.75 +
1.76 + if (0 == ilen)
1.77 + {
1.78 + return EDescriptorNoData;
1.79 + }
1.80 + else if(!aDes)
1.81 + {
1.82 + return EInvalidPointer;
1.83 + }
1.84 + else if(n_size < ilen+1)
1.85 + {
1.86 + n_size = ilen;
1.87 + return EInvalidSize;
1.88 + }
1.89 +
1.90 + memcpy(aDes, aSrc.Ptr(), ilen);
1.91 + aDes[ilen] = '\0';
1.92 +
1.93 + return ESuccess;
1.94 +}
1.95 +
1.96 +/**
1.97 + * Converts a descriptor of type TLitC8 to char*
1.98 + * @param aSrc is the descriptor of type TLitC8 that is to be converted to char*
1.99 + * @param aDes is a char* that will hold the result after conversion. Care should be taken to
1.100 + * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
1.101 + * being allocated to the char* is being made known using n_size
1.102 + * @param n_size is the size of the char*. This should have a minimum value equal to size of the
1.103 + * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
1.104 + */
1.105 +
1.106 +EXPORT_C int Tlitc8ToChar(const TDesC8& aSrc, char* aDes, int& n_size)
1.107 +{
1.108 + unsigned int ilen = aSrc.Length();
1.109 +
1.110 + if (0 == ilen )
1.111 + {
1.112 + return EDescriptorNoData;
1.113 + }
1.114 + else if(!aDes)
1.115 + {
1.116 + return EInvalidPointer;
1.117 + }
1.118 + else if (n_size < ilen )
1.119 + {
1.120 + n_size = ilen + 1;
1.121 + return EInvalidSize;
1.122 + }
1.123 +
1.124 + memcpy(aDes, (char *)aSrc.Ptr(), ilen);
1.125 + aDes[ilen] = '\0';
1.126 +
1.127 + return ESuccess;
1.128 +}
1.129 +
1.130 +
1.131 + /**
1.132 + * Converts a descriptor of type TPtr8 to character stream
1.133 + *
1.134 + * @param aSrc is the descriptor to be converted , aDes is the
1.135 + * reference to the character sream where the result of conversion
1.136 + * is stored , n_size specifies the conversion size of the string
1.137 + * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
1.138 + * -4 is EInvalidPointer)
1.139 + */
1.140 +EXPORT_C int Tptr8ToChar(const TDes8& aSrc, char* aDes, int& n_size)
1.141 +{
1.142 + unsigned int ilen = aSrc.Length();
1.143 +
1.144 + if (0 == ilen)
1.145 + {
1.146 + return EDescriptorNoData;
1.147 + }
1.148 + else if ( !aDes )
1.149 + {
1.150 + return EInvalidPointer;
1.151 + }
1.152 + else if (n_size < ilen)
1.153 + {
1.154 + n_size = ilen + 1;
1.155 + return EInvalidSize;
1.156 + }
1.157 +
1.158 + memcpy(aDes , (const char *)aSrc.Ptr(), ilen);
1.159 + aDes[ilen] = '\0';
1.160 +
1.161 + return ESuccess;
1.162 +}
1.163 +
1.164 + /**
1.165 + * Converts a descriptor of type TPtrC8 to character stream
1.166 + *
1.167 + * @param aSrc is the descriptor to be converted , aDes is the
1.168 + * reference to the character sream where the result of conversion
1.169 + * is stored , n_size specifies the conversion size of the string
1.170 + * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
1.171 + * -4 is EInvalidPointer)
1.172 + */
1.173 +EXPORT_C int Tptrc8ToCharp(TPtrC8& aSrc, char* aDes, int& n_size)
1.174 +{
1.175 + unsigned int ilen = aSrc.Length();
1.176 + if (0 == ilen)
1.177 + {
1.178 + return EDescriptorNoData;
1.179 + }
1.180 + else if ( !aDes )
1.181 + {
1.182 + return EInvalidPointer;
1.183 + }
1.184 + else if (n_size < ilen)
1.185 + {
1.186 + n_size = ilen + 1;
1.187 + return EInvalidSize;
1.188 + }
1.189 +
1.190 + memcpy(aDes , aSrc.Ptr(), ilen);
1.191 + aDes[ilen] = '\0';
1.192 +
1.193 + return ESuccess;
1.194 +}
1.195 +
1.196 + /**
1.197 + * Converts a descriptor of type RBuf8 to character stream
1.198 + *
1.199 + * @param aSrc is the descriptor to be converted , aDes is the
1.200 + * reference to the character sream where the result of conversion
1.201 + * is stored , n_size specifies the conversion size of the string
1.202 + * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
1.203 + * -4 is EInvalidPointer , -5 is EDescriptorNoData)
1.204 + */
1.205 +
1.206 +EXPORT_C int Rbuf8ToChar(TDes8& aSrc, char* aDes, int& n_size)
1.207 +{
1.208 + unsigned int ilen = aSrc.Length();
1.209 +
1.210 + if (0 == ilen)
1.211 + {
1.212 + return EDescriptorNoData;
1.213 + }
1.214 + else if ( !aDes )
1.215 + {
1.216 + return EInvalidPointer;
1.217 + }
1.218 + else if (n_size < ilen)
1.219 + {
1.220 + n_size = ilen + 1;
1.221 + return EInvalidSize;
1.222 + }
1.223 +
1.224 + memcpy (aDes,(char *)aSrc.Ptr(), ilen);
1.225 + aDes[ilen] = '\0';
1.226 +
1.227 + return ESuccess;
1.228 +}
1.229 +
1.230 + /**
1.231 + * Converts a descriptor of type HBufC8 to character stream
1.232 + *
1.233 + * @param aSrc is the descriptor to be converted , aDes is the
1.234 + * reference to the character sream where the result of conversion
1.235 + * is stored , n_size specifies the conversion size of the string
1.236 + * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
1.237 + * -4 is EInvalidPointer , -5 is EDescriptorNoData)
1.238 + */
1.239 +
1.240 +EXPORT_C int Hbufc8ToChar(HBufC8 *aSrc, char* aDes, int& n_size)
1.241 +{
1.242 + unsigned int ilen = 0;
1.243 +
1.244 + if ( !aDes || !aSrc)
1.245 + {
1.246 + return EInvalidPointer;
1.247 + }
1.248 + else
1.249 + {
1.250 + ilen = aSrc->Length();
1.251 + if (0 == ilen)
1.252 + {
1.253 + return EDescriptorNoData;
1.254 + }
1.255 + else if (n_size < ilen)
1.256 + {
1.257 + n_size = ilen + 1;
1.258 + return EInvalidSize;
1.259 + }
1.260 + }
1.261 +
1.262 + memcpy (aDes,(char *)aSrc->Ptr(), ilen);
1.263 + aDes[ilen] = '\0';
1.264 +
1.265 + return ESuccess;
1.266 +}