os/ossrv/utilitylibraries/libutils/src/chartodescriptor16.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/utilitylibraries/libutils/src/chartodescriptor16.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,237 @@
     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 Descriptor16 conversions
    1.18 + *
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +
    1.23 +#include "libutils.h"
    1.24 +  
    1.25 +
    1.26 +/** 
    1.27 +  * Converts a character stream to TBuf16
    1.28 +  * 
    1.29 +  * @param aSrc is char*, aDes is the reference to the descriptor
    1.30 +  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
    1.31 +  */
    1.32 +     
    1.33 +EXPORT_C  int CharToTbuf16(const char* aSrc, TDes16& aDes)
    1.34 +{		
    1.35 +    int retval = ESuccess;
    1.36 +    unsigned int ilen = 0;
    1.37 +    int minusone = -1;
    1.38 +
    1.39 +	    
    1.40 +	if (!aSrc)
    1.41 +	{	
    1.42 +	    return EInvalidPointer;	
    1.43 +	}	
    1.44 +	else 
    1.45 +	{	
    1.46 +	    ilen = strlen(aSrc);
    1.47 +	    if(ilen > aDes.MaxLength())
    1.48 +	    {
    1.49 +	        return EInsufficientMemory;	
    1.50 +	    }	
    1.51 +	}    
    1.52 +    wchar_t *WcharString = new wchar_t[ilen+1];
    1.53 +    if(!WcharString)
    1.54 +    {
    1.55 +    	return EInsufficientSystemMemory;
    1.56 +    }
    1.57 +            
    1.58 +	if(minusone == mbstowcs(WcharString, (const char*)aSrc, ilen))
    1.59 +	{   
    1.60 +		retval = EInvalidMBSSequence;
    1.61 +	}
    1.62 +	else 
    1.63 +	{
    1.64 +		aDes.Copy((unsigned short *)WcharString, ilen);
    1.65 +	}   
    1.66 +        
    1.67 +	delete[] WcharString;
    1.68 +	return retval;
    1.69 +}
    1.70 +
    1.71 +   /**
    1.72 +     * Converts a character stream to HBufc16
    1.73 +     * 
    1.74 +     * @param aSrc is char*, aDes is the reference to the descriptor
    1.75 +     * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -6 is EUseNewMaxL, -7 is EInvalidMBSSequence)
    1.76 +     */
    1.77 +     
    1.78 +EXPORT_C int CharToHbufc16(const char* aSrc, HBufC16* aDes)
    1.79 +{	
    1.80 +	int retval = ESuccess; 
    1.81 +	unsigned int ilen = 0, ilendes = 0; 
    1.82 +	int minusone = -1;
    1.83 +	if (!aSrc || !aDes)
    1.84 +	{
    1.85 +		return EInvalidPointer;
    1.86 +	}
    1.87 +	else 
    1.88 +	{
    1.89 +	    ilen = strlen (aSrc);
    1.90 +	    ilendes = aDes->Length();
    1.91 +	    
    1.92 +	    if(0 == ilendes)
    1.93 +	    {
    1.94 +	        return EUseNewMaxL;	
    1.95 +	    }     
    1.96 +	    else if (ilen > ilendes)
    1.97 +	    {
    1.98 +	    	return EInsufficientMemory;	
    1.99 +	    }	
   1.100 +	}
   1.101 +	
   1.102 +	wchar_t *wCharString = new wchar_t[ilen+1];
   1.103 +	
   1.104 +	if (!wCharString)
   1.105 +	{
   1.106 +		return EInsufficientMemory;
   1.107 +	}
   1.108 +	
   1.109 +	if(minusone == mbstowcs((wchar_t *)wCharString, (const char*)aSrc, ilen))
   1.110 +	{
   1.111 +		retval = EInvalidMBSSequence;
   1.112 +	}
   1.113 +	else 
   1.114 +	{
   1.115 +	    wCharString[ilen] = (wchar_t)'\0';
   1.116 +		TPtrC16 temp ((unsigned short *)wCharString, ilen);
   1.117 +		*aDes = temp; 
   1.118 +	}
   1.119 +	
   1.120 +	delete[] wCharString;
   1.121 +	return retval;
   1.122 +}
   1.123 +
   1.124 +/**
   1.125 +  * Converts a character stream to TPtr16
   1.126 +  *
   1.127 +  * @param aSrc is char*, wPtr is wchar_t*, aDes is the reference to the descriptor
   1.128 +  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
   1.129 +  */
   1.130 +     
   1.131 +EXPORT_C int CharpToTptr16(const char* aSrc, TPtr16& aDes)
   1.132 +{
   1.133 +    int retval = ESuccess;
   1.134 +    unsigned int ilen =0 , ilendes = 0;
   1.135 +    int minusone = -1;
   1.136 +    
   1.137 +	if (!aSrc)
   1.138 +	{
   1.139 +		return EInvalidPointer;
   1.140 +	}
   1.141 +	else 
   1.142 +	{
   1.143 +	    ilen = strlen(aSrc);
   1.144 +	    ilendes = aDes.MaxLength();
   1.145 +	    
   1.146 +	    if (ilendes < ilen)
   1.147 +	    {
   1.148 +	    	return EInsufficientMemory;
   1.149 +	    }
   1.150 +	}
   1.151 +	
   1.152 +  wchar_t *wPtr = new wchar_t[ilen+1];
   1.153 +	
   1.154 +	if (!wPtr)
   1.155 +	{
   1.156 +		return EInsufficientMemory;
   1.157 +	}
   1.158 +	
   1.159 +	if(minusone != mbstowcs((wchar_t *)wPtr, (const char*)aSrc, ilen ))
   1.160 +	{
   1.161 +		aDes.Copy((unsigned short *)wPtr, ilen);
   1.162 +	}
   1.163 +	else
   1.164 +	{
   1.165 +		retval = EInvalidMBSSequence;
   1.166 +	}
   1.167 +	
   1.168 +	delete[] wPtr;
   1.169 +	return retval;
   1.170 +}
   1.171 +
   1.172 +/**
   1.173 +  * Converts a character stream to TPtrc16
   1.174 +  *
   1.175 +  * @param aSrc is char*, cPtr is wchar_t*, aDes is the reference to the descriptor
   1.176 +  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
   1.177 +  */
   1.178 +     
   1.179 +EXPORT_C int CharpToTptrc16(char* aSrc ,wchar_t* cPtr, TPtrC16& aDes)
   1.180 +{
   1.181 +    int retval = ESuccess; 
   1.182 +    unsigned int ilen = 0;
   1.183 +    int minusone = -1;
   1.184 +    
   1.185 +	if ( !aSrc || !cPtr )
   1.186 +	{
   1.187 +		return EInvalidPointer;
   1.188 +	}
   1.189 +	
   1.190 +	ilen = strlen(aSrc);
   1.191 +
   1.192 +	if(minusone != mbstowcs((wchar_t*)cPtr, (const char*)aSrc, strlen(aSrc)))
   1.193 +	{
   1.194 +	    aDes.Set((TUint16*)cPtr, ilen);
   1.195 +	}
   1.196 +	else
   1.197 +	{
   1.198 +	    retval = EInvalidMBSSequence;
   1.199 +	}
   1.200 +	
   1.201 +	return retval; 
   1.202 +}
   1.203 +
   1.204 +/**
   1.205 +  * Converts a character stream to RBuf16
   1.206 +  * 
   1.207 +  * @param aSrc is char*, aDes is the reference to the descriptor
   1.208 +  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
   1.209 +  */
   1.210 +  
   1.211 +  
   1.212 +EXPORT_C int CharToRbuf16(const char* aSrc, RBuf16& aDes)
   1.213 +{
   1.214 +    int retval = ESuccess;
   1.215 +    unsigned int ilen = 0; 
   1.216 +    int minusone = -1;
   1.217 +    if ( !aSrc )
   1.218 +    {
   1.219 +    	return EInvalidPointer;
   1.220 +    }
   1.221 +    ilen = strlen(aSrc);
   1.222 +	
   1.223 +	wchar_t* buf = new wchar_t[ilen];
   1.224 +    if (!buf)
   1.225 +    {
   1.226 +    	return EInsufficientSystemMemory;
   1.227 +    }
   1.228 +    
   1.229 +	if(minusone != mbstowcs(buf, aSrc, ilen))
   1.230 +	{
   1.231 +	    aDes.Copy((const unsigned short *)buf, ilen);	
   1.232 +	}   
   1.233 +	else
   1.234 +	{
   1.235 +		retval = EInvalidMBSSequence;
   1.236 +	}
   1.237 +	
   1.238 +	delete []buf;
   1.239 +	return retval;
   1.240 +}