utilitylibraries/libutils/src/chartodescriptor8.cpp
changeset 0 e4d67989cc36
child 34 5fae379060a7
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 char * to Descriptor8 conversions
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "libutils.h"
       
    21   
       
    22 
       
    23 
       
    24 
       
    25 /**
       
    26    * Converts a character stream to TBuf8
       
    27    * @param aSrc is char* which is to be converted to TBuf8
       
    28    * @param aDes is the pointer to the descriptor of type TBuf8 which will hold
       
    29    * the result of conversion.aDes needs to be allocated with sufficient amount 
       
    30    * of memory before being passed to function. This Descriptor should have 
       
    31    * a allocation that is equal to or greater than char*
       
    32    */
       
    33    
       
    34 EXPORT_C  int CharToTbuf8 (const char* aSrc, TDes8& aDes)
       
    35 {
       
    36 	if (!aSrc)
       
    37 	{
       
    38 	    return EInvalidPointer;	
       
    39 	}
       
    40 	else 
       
    41 	{
       
    42 	    if (strlen (aSrc) > aDes.MaxLength())
       
    43 	    {
       
    44 	    	return EInsufficientMemory;	
       
    45 	    }	
       
    46 	}
       
    47 	
       
    48 	aDes.Copy ((const TUint8*)aSrc);
       
    49 		
       
    50 	return ESuccess;
       
    51 }
       
    52 
       
    53  /**
       
    54    * Converts a character stream to HBufC8\\
       
    55    * @param aSrc is char* which is to be converted to HBufC8
       
    56    * @param aDes is the pointer to the descriptor of type HBufC8 which will hold
       
    57    * the result of conversion.aDes needs to be allocated with sufficient amount 
       
    58    * of memory before being passed to function. This Descriptor should have 
       
    59    * a allocation that is equal to or greater than char*
       
    60    */
       
    61    
       
    62 EXPORT_C int CharToHbufc8(const char* aSrc, HBufC8* aDes)
       
    63 {
       
    64     unsigned int ilendes = 0;
       
    65 	if ( !aSrc || !aDes )
       
    66 	{
       
    67 		return EInvalidPointer;
       
    68 	}
       
    69 	else 
       
    70 	{
       
    71 	    ilendes = aDes->Length();
       
    72 	    if(0 == ilendes)
       
    73 	    {
       
    74 	    	return EUseNewMaxL;
       
    75 	    }
       
    76 	    else if (strlen (aSrc) > ilendes)
       
    77 	    {
       
    78 	    	return EInsufficientMemory;	
       
    79 	    }	
       
    80 	}
       
    81 		
       
    82 	*aDes = (const TUint8*)aSrc;
       
    83 	return ESuccess;
       
    84 	
       
    85 }
       
    86 
       
    87  /**
       
    88    * Converts a character stream to Tptr8
       
    89    * @param aSrc is char* which is to be converted to TPtr8
       
    90    * @param aDes is the pointer to the descriptor of type TPtr8 which will hold
       
    91    * the result of conversion.aDes needs to be allocated with sufficient amount 
       
    92    * of memory before being passed to function. This Descriptor should have 
       
    93    * a allocation that is equal to or greater than char*
       
    94    */
       
    95    
       
    96 EXPORT_C int CharpToTptr8( const char* aSrc, TPtr8& aDes )
       
    97 {
       
    98     unsigned int ilen = 0, ilendes = 0;
       
    99 	if (!aSrc)
       
   100 	{
       
   101 		return EInvalidPointer;
       
   102 	}
       
   103 	
       
   104 	ilen = strlen(aSrc);
       
   105 	ilendes = aDes.MaxLength();
       
   106 	
       
   107 	if (ilendes < ilen)
       
   108 	{
       
   109 		return EInsufficientMemory;
       
   110 	}
       
   111 	
       
   112 	aDes.Set((TUint8 *)(aSrc), ilen, ilendes);
       
   113 	
       
   114 	return ESuccess;
       
   115 }
       
   116 
       
   117  /**
       
   118    * Converts a character stream to TPtrc8
       
   119    * @param aSrc is char* which is to be converted to TPtrc8
       
   120    * @param aDes is the pointer to the descriptor of type TPtrc8 which will hold
       
   121    * the result of conversion.aDes needs to be allocated with sufficient amount 
       
   122    * of memory before being passed to function. This Descriptor should have 
       
   123    * a allocation that is equal to or greater than char*
       
   124    */
       
   125    
       
   126 EXPORT_C int CharpToTptrc8(const char* aSrc, TPtrC8& aDes)
       
   127 {    
       
   128 	if ( !aSrc )
       
   129 	{
       
   130 		return EInvalidPointer;
       
   131 	}
       
   132 	
       
   133 	aDes.Set((TUint8 *)(aSrc), strlen(aSrc));
       
   134 	
       
   135 	return ESuccess;
       
   136 }
       
   137 
       
   138  /**
       
   139    * Converts a character stream to RBuf8
       
   140    * @param aSrc is char* which is to be converted to RBuf8
       
   141    * @param aDes is the pointer to the descriptor of type RBuf8 which will hold
       
   142    * the result of conversion.aDes needs to be allocated with sufficient amount 
       
   143    * of memory before being passed to function. This Descriptor should have 
       
   144    * a allocation that is equal to or greater than char*
       
   145    */
       
   146 
       
   147 EXPORT_C int CharToRbuf8(const char* aSrc, RBuf8& aDes)
       
   148 {   
       
   149     int retval = ESuccess, ilen = 0;
       
   150     
       
   151     if ( !aSrc )
       
   152     {
       
   153     	return EInvalidPointer;
       
   154     }
       
   155     
       
   156     ilen = strlen(aSrc);
       
   157     
       
   158     if (KErrNone == aDes.Create(ilen))
       
   159     {
       
   160     	aDes.Copy((const unsigned char *)aSrc, ilen);
       
   161     }
       
   162 	else
       
   163 	{
       
   164 		retval = EInsufficientSystemMemory;
       
   165 	}
       
   166 	
       
   167 	return retval;	
       
   168 }