os/ossrv/utilitylibraries/libutils/src/chartodescriptor8.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/utilitylibraries/libutils/src/chartodescriptor8.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,168 @@
     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 char * to Descriptor8 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 +/**
    1.29 +   * Converts a character stream to TBuf8
    1.30 +   * @param aSrc is char* which is to be converted to TBuf8
    1.31 +   * @param aDes is the pointer to the descriptor of type TBuf8 which will hold
    1.32 +   * the result of conversion.aDes needs to be allocated with sufficient amount 
    1.33 +   * of memory before being passed to function. This Descriptor should have 
    1.34 +   * a allocation that is equal to or greater than char*
    1.35 +   *
    1.36 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
    1.37 +   */
    1.38 +   
    1.39 +EXPORT_C  int CharToTbuf8 (const char* aSrc, TDes8& aDes)
    1.40 +{
    1.41 +	if (!aSrc)
    1.42 +	{
    1.43 +	    return EInvalidPointer;	
    1.44 +	}
    1.45 +	else 
    1.46 +	{
    1.47 +	    if (strlen (aSrc) > aDes.MaxLength())
    1.48 +	    {
    1.49 +	    	return EInsufficientMemory;	
    1.50 +	    }	
    1.51 +	}
    1.52 +	
    1.53 +	aDes.Copy ((const TUint8*)aSrc);
    1.54 +		
    1.55 +	return ESuccess;
    1.56 +}
    1.57 +
    1.58 + /**
    1.59 +   * Converts a character stream to HBufC8\\
    1.60 +   * @param aSrc is char* which is to be converted to HBufC8
    1.61 +   * @param aDes is the pointer to the descriptor of type HBufC8 which will hold
    1.62 +   * the result of conversion.aDes needs to be allocated with sufficient amount 
    1.63 +   * of memory before being passed to function. This Descriptor should have 
    1.64 +   * a allocation that is equal to or greater than char*
    1.65 +   *
    1.66 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -6 is EUseNewMaxL)
    1.67 +   */
    1.68 +   
    1.69 +EXPORT_C int CharToHbufc8(const char* aSrc, HBufC8* aDes)
    1.70 +{
    1.71 +    unsigned int ilendes = 0;
    1.72 +	if ( !aSrc || !aDes )
    1.73 +	{
    1.74 +		return EInvalidPointer;
    1.75 +	}
    1.76 +	else 
    1.77 +	{
    1.78 +	    ilendes = aDes->Length();
    1.79 +	    if(0 == ilendes)
    1.80 +	    {
    1.81 +	    	return EUseNewMaxL;
    1.82 +	    }
    1.83 +	    else if (strlen (aSrc) > ilendes)
    1.84 +	    {
    1.85 +	    	return EInsufficientMemory;	
    1.86 +	    }	
    1.87 +	}
    1.88 +		
    1.89 +	*aDes = (const TUint8*)aSrc;
    1.90 +	return ESuccess;
    1.91 +	
    1.92 +}
    1.93 +
    1.94 + /**
    1.95 +   * Converts a character stream to Tptr8
    1.96 +   * @param aSrc is char* which is to be converted to TPtr8
    1.97 +   * @param aDes is the pointer to the descriptor of type TPtr8 which will hold
    1.98 +   * the result of conversion.aDes needs to be allocated with sufficient amount 
    1.99 +   * of memory before being passed to function. This Descriptor should have 
   1.100 +   * a allocation that is equal to or greater than char*
   1.101 +   *
   1.102 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
   1.103 +   */
   1.104 +   
   1.105 +EXPORT_C int CharpToTptr8( const char* aSrc, TPtr8& aDes )
   1.106 +{
   1.107 +    unsigned int ilen = 0, ilendes = 0;
   1.108 +	if (!aSrc)
   1.109 +	{
   1.110 +		return EInvalidPointer;
   1.111 +	}
   1.112 +	
   1.113 +	ilen = strlen(aSrc);
   1.114 +	ilendes = aDes.MaxLength();
   1.115 +	
   1.116 +	if (ilendes < ilen)
   1.117 +	{
   1.118 +		return EInsufficientMemory;
   1.119 +	}
   1.120 +	
   1.121 +	aDes.Set((TUint8 *)(aSrc), ilen, ilendes);
   1.122 +	
   1.123 +	return ESuccess;
   1.124 +}
   1.125 +
   1.126 + /**
   1.127 +   * Converts a character stream to TPtrc8
   1.128 +   * @param aSrc is char* which is to be converted to TPtrc8
   1.129 +   * @param aDes is the pointer to the descriptor of type TPtrc8 which will hold
   1.130 +   * the result of conversion.aDes needs to be allocated with sufficient amount 
   1.131 +   * of memory before being passed to function. This Descriptor should have 
   1.132 +   * a allocation that is equal to or greater than char*
   1.133 +   *
   1.134 +   * @return Status code (0 is ESuccess, -4 is EInvalidPointer)  
   1.135 +   */
   1.136 +   
   1.137 +EXPORT_C int CharpToTptrc8(const char* aSrc, TPtrC8& aDes)
   1.138 +{    
   1.139 +	if ( !aSrc )
   1.140 +	{
   1.141 +		return EInvalidPointer;
   1.142 +	}
   1.143 +	
   1.144 +	aDes.Set((TUint8 *)(aSrc), strlen(aSrc));
   1.145 +	
   1.146 +	return ESuccess;
   1.147 +}
   1.148 +
   1.149 + /**
   1.150 +   * Converts a character stream to RBuf8
   1.151 +   * @param aSrc is char* which is to be converted to RBuf8
   1.152 +   * @param aDes is the pointer to the descriptor of type RBuf8 which will hold
   1.153 +   * the result of conversion.aDes needs to be allocated with sufficient amount 
   1.154 +   * of memory before being passed to function. This Descriptor should have 
   1.155 +   * a allocation that is equal to or greater than char*
   1.156 +   *
   1.157 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
   1.158 +   */
   1.159 +
   1.160 +EXPORT_C int CharToRbuf8(const char* aSrc, RBuf8& aDes)
   1.161 +{    
   1.162 +  if ( !aSrc )
   1.163 +  {
   1.164 +    return EInvalidPointer;
   1.165 +  }
   1.166 +        
   1.167 +  aDes.Copy((const unsigned char *)aSrc, strlen(aSrc));
   1.168 +	
   1.169 +	return ESuccess;	
   1.170 +}
   1.171 +