utilitylibraries/libutils/src/descriptor8tochar.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Contains the source for Descriptor8 to char * conversions
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19  
       
    20 #include "libutils.h"
       
    21   
       
    22 
       
    23 	
       
    24  /**
       
    25    * Converts a descriptor of type TBuf8 to char*
       
    26    * @param aSrc is the descriptor of type TBuf8 that is to be converted to char*
       
    27    * @param aDes is a char* that will hold the result after conversion. Care should be taken to 
       
    28    * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
       
    29    * being allocated to the char* is being made known using n_size
       
    30    * @param n_size is the size of the char*. This should have a minimum value equal to size of the 
       
    31    * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
       
    32    */
       
    33 
       
    34 EXPORT_C int Tbuf8ToChar(TDes8& aSrc, char* aDes, int& n_size)
       
    35 {	
       
    36     unsigned int ilen = aSrc.Length();
       
    37     
       
    38     if (0 == ilen)
       
    39     {
       
    40     	return EDescriptorNoData;
       
    41     }
       
    42 	else if(!aDes)
       
    43 	{
       
    44 		return EInvalidPointer;
       
    45 	}
       
    46 	else if (n_size < (ilen+1))
       
    47 	{
       
    48 		n_size = ilen + 1;
       
    49 		return EInvalidSize;
       
    50 	}
       
    51 
       
    52 	memcpy(aDes , (const char *)aSrc.Ptr(), ilen);
       
    53 	
       
    54 	aDes[ilen] = '\0';
       
    55 		
       
    56 	return ESuccess;
       
    57 }
       
    58 
       
    59  /**
       
    60     * Converts a descriptor of type TBufC8 to char*
       
    61    * @param aSrc is the descriptor of type TBufC8 that is to be converted to char*
       
    62    * @param aDes is a char* that will hold the result after conversion. Care should be taken to 
       
    63    * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
       
    64    * being allocated to the char* is being made known using n_size
       
    65    * @param n_size is the size of the char*. This should have a minimum value equal to size of the 
       
    66    * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
       
    67    */
       
    68 
       
    69 EXPORT_C int Tbufc8ToChar(TDesC8& aSrc, char* aDes, int& n_size)
       
    70 {	
       
    71     unsigned int ilen = aSrc.Length();
       
    72     
       
    73     if (0 == ilen)
       
    74     {
       
    75     	return EDescriptorNoData;
       
    76     }
       
    77     else if(!aDes)
       
    78     {
       
    79     	return EInvalidPointer;
       
    80     }
       
    81     else if(n_size < ilen+1)
       
    82     {
       
    83     	n_size = ilen;
       
    84     	return EInvalidSize;
       
    85     }
       
    86     	
       
    87 	memcpy(aDes, aSrc.Ptr(), ilen);
       
    88 	aDes[ilen] = '\0';
       
    89         
       
    90     return ESuccess;
       
    91 }
       
    92 
       
    93 /**
       
    94    * Converts a descriptor of type TLitC8 to char*
       
    95    * @param aSrc is the descriptor of type TLitC8 that is to be converted to char*
       
    96    * @param aDes is a char* that will hold the result after conversion. Care should be taken to 
       
    97    * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
       
    98    * being allocated to the char* is being made known using n_size
       
    99    * @param n_size is the size of the char*. This should have a minimum value equal to size of the 
       
   100    * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
       
   101    */
       
   102 	
       
   103 EXPORT_C int Tlitc8ToChar(const TDesC8& aSrc, char* aDes, int& n_size)
       
   104 {   
       
   105     unsigned int ilen = aSrc.Length();
       
   106     
       
   107     if (0 == ilen )
       
   108     {
       
   109     	return EDescriptorNoData;
       
   110     }     
       
   111     else if(!aDes)
       
   112     {
       
   113     	return EInvalidPointer;
       
   114     }
       
   115     else if (n_size < ilen )
       
   116     {
       
   117     	n_size = ilen + 1;
       
   118     	return EInvalidSize;
       
   119     }
       
   120     
       
   121     memcpy(aDes, (char *)aSrc.Ptr(), ilen);
       
   122 	aDes[ilen] = '\0';
       
   123 	
       
   124 	return ESuccess;	 
       
   125 }
       
   126 
       
   127 
       
   128  /**
       
   129    * Converts a descriptor of type TPtr8 to character stream
       
   130    *
       
   131    * @param aSrc is the descriptor to be converted , aDes is the 
       
   132    * reference to the character sream where the result of conversion 
       
   133    * is stored , n_size specifies the conversion size of the string 
       
   134    * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
       
   135    *  -4 is EInvalidPointer)
       
   136    */
       
   137 EXPORT_C int Tptr8ToChar(const TDes8& aSrc, char* aDes, int& n_size)
       
   138 {
       
   139     unsigned int ilen = aSrc.Length();
       
   140 	
       
   141 	if (0 == ilen)
       
   142     {
       
   143     	return EDescriptorNoData;	
       
   144     }
       
   145 	else if ( !aDes )
       
   146     {
       
   147         return EInvalidPointer;
       
   148     }
       
   149     else if (n_size < ilen)
       
   150     {
       
   151     	n_size = ilen + 1;
       
   152     	return EInvalidSize;
       
   153     }
       
   154     
       
   155 	memcpy(aDes , (const char *)aSrc.Ptr(), ilen);
       
   156 	aDes[ilen] = '\0';
       
   157     
       
   158     return ESuccess;
       
   159 }
       
   160 
       
   161  /**
       
   162    * Converts a descriptor of type TPtrC8 to character stream
       
   163    *
       
   164    * @param aSrc is the descriptor to be converted , aDes is the 
       
   165    * reference to the character sream where the result of conversion 
       
   166    * is stored , n_size specifies the conversion size of the string 
       
   167    * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
       
   168    *  -4 is EInvalidPointer)
       
   169    */
       
   170 EXPORT_C int Tptrc8ToCharp(TPtrC8& aSrc, char* aDes, int& n_size)
       
   171 {
       
   172     unsigned int ilen = aSrc.Length();
       
   173 	if (0 == ilen)
       
   174     {
       
   175     	return EDescriptorNoData;	
       
   176     }
       
   177 	else if ( !aDes )
       
   178     {
       
   179         return EInvalidPointer;
       
   180     }
       
   181     else if (n_size < ilen)
       
   182     {
       
   183     	n_size = ilen + 1;
       
   184     	return EInvalidSize;
       
   185     }
       
   186 	
       
   187 	memcpy(aDes , aSrc.Ptr(), ilen);
       
   188 	aDes[ilen] = '\0';
       
   189     
       
   190     return ESuccess;
       
   191 }
       
   192 
       
   193  /**
       
   194    * Converts a descriptor of type RBuf8 to character stream
       
   195    *
       
   196    * @param aSrc is the descriptor to be converted , aDes is the 
       
   197    * reference to the character sream where the result of conversion 
       
   198    * is stored , n_size specifies the conversion size of the string 
       
   199    * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
       
   200    *  -4 is EInvalidPointer , -5 is EDescriptorNoData)
       
   201    */
       
   202    
       
   203 EXPORT_C int Rbuf8ToChar(TDes8& aSrc, char* aDes, int& n_size)
       
   204 {
       
   205     unsigned int ilen = aSrc.Length();
       
   206     
       
   207     if (0 == ilen)
       
   208     {
       
   209     	return EDescriptorNoData;	
       
   210     } 
       
   211 	else if ( !aDes )
       
   212     {
       
   213         return EInvalidPointer;
       
   214     }
       
   215     else if (n_size < ilen)
       
   216     {
       
   217     	n_size = ilen + 1;
       
   218     	return EInvalidSize;
       
   219     }
       
   220     
       
   221 	memcpy (aDes,(char *)aSrc.Ptr(), ilen);
       
   222 	aDes[ilen] = '\0';
       
   223 
       
   224 	return ESuccess;
       
   225 }
       
   226 
       
   227  /**
       
   228    * Converts a descriptor of type HBufC8 to character stream
       
   229    *
       
   230    * @param aSrc is the descriptor to be converted , aDes is the 
       
   231    * reference to the character sream where the result of conversion 
       
   232    * is stored , n_size specifies the conversion size of the string 
       
   233    * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
       
   234    *  -4 is EInvalidPointer , -5 is EDescriptorNoData)
       
   235    */
       
   236 
       
   237 EXPORT_C int Hbufc8ToChar(HBufC8 *aSrc, char* aDes, int& n_size)
       
   238 {
       
   239     unsigned int ilen = 0;
       
   240     
       
   241     if ( !aDes || !aSrc)
       
   242     {
       
   243         return EInvalidPointer;
       
   244     }
       
   245     else 
       
   246     {
       
   247         ilen = aSrc->Length();
       
   248         if (0 == ilen)
       
   249         {
       
   250         	return EDescriptorNoData;		
       
   251         }
       
   252         else if (n_size < ilen)
       
   253     	{
       
   254     		n_size = ilen + 1;
       
   255     		return EInvalidSize;
       
   256     	}  	
       
   257     } 	
       
   258     
       
   259 	memcpy (aDes,(char *)aSrc->Ptr(), ilen);
       
   260 	aDes[ilen] = '\0';
       
   261 
       
   262 	return ESuccess;
       
   263 }