os/ossrv/utilitylibraries/libutils/src/stringtodescriptor16.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/utilitylibraries/libutils/src/stringtodescriptor16.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 string 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 + /**
    1.28 +   * Converts a string to a descriptor of type TBuf16
    1.29 +   *
    1.30 +   * @param aSrc is the string to be converted , aDes is the 
    1.31 +   * reference to the descriptor where the result of conversion 
    1.32 +   * is stored,n_size specifies the conversion size of the char array 
    1.33 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
    1.34 +   * -3 is EStringNoData, -9 is EInsufficientSystemMemory)
    1.35 +   */
    1.36 +EXPORT_C  int StringToTbuf16(string& aSrc, TDes16& aDes)
    1.37 +{
    1.38 +  int retval = ESuccess;
    1.39 +	const char* charString = aSrc.c_str();
    1.40 +	int ilen = 0; 
    1.41 +	int minusone = -1;
    1.42 +	
    1.43 +    if('\0' == charString[0])
    1.44 +    {
    1.45 +    	return EStringNoData;
    1.46 +    }
    1.47 +        
    1.48 +    ilen = strlen(charString);
    1.49 +    
    1.50 +	if (ilen > aDes.MaxLength())
    1.51 +	{
    1.52 +		return EInsufficientMemory;
    1.53 +	}
    1.54 +	
    1.55 +	wchar_t* WcharString = new wchar_t[ilen];
    1.56 +	
    1.57 +	if(!WcharString)
    1.58 +	{
    1.59 +		return EInsufficientSystemMemory;
    1.60 +	}
    1.61 +	
    1.62 +	if(minusone == mbstowcs(WcharString, (const char*)charString, ilen))
    1.63 +	{
    1.64 +		retval = EInvalidMBSSequence;
    1.65 +	}
    1.66 +	else
    1.67 +	{
    1.68 +			WcharString[ilen] = L'\0';
    1.69 +		aDes.Copy((unsigned short*)WcharString,ilen);
    1.70 +	}
    1.71 +		
    1.72 +	delete []WcharString;	
    1.73 +	return retval;	
    1.74 +}
    1.75 +
    1.76 + /**
    1.77 +   * Converts a string to a descriptor of type Tptr16
    1.78 +   *
    1.79 +   * @param aSrc is the string to be converted , aDes is the 
    1.80 +   * reference to the descriptor where the result of conversion 
    1.81 +   * is stored 
    1.82 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
    1.83 +   * -3 is EStringNoData , -4 is EInvalidPointer)
    1.84 +   */
    1.85 +
    1.86 +EXPORT_C  int StringToTptr16 (string& aSrc, wchar_t *wPtr, TPtr16& aDes)
    1.87 +{
    1.88 +    int retval = ESuccess;
    1.89 +    int ilen = 0, ideslen = 0;
    1.90 +    const char* charString = aSrc.c_str();
    1.91 +    int minusone = -1;
    1.92 +    
    1.93 +    if('\0' == charString[0])
    1.94 +    {
    1.95 +    	return EStringNoData;
    1.96 +    }
    1.97 +    else if(!wPtr)
    1.98 +    {
    1.99 +    	return EInvalidPointer;
   1.100 +    }
   1.101 +    
   1.102 +	ilen = strlen(charString);
   1.103 +	ideslen = aDes.MaxLength();
   1.104 +	
   1.105 +	if (ilen > ideslen)
   1.106 +	{
   1.107 +		return EInsufficientMemory;
   1.108 +	}
   1.109 +	
   1.110 +	if(minusone != mbstowcs(wPtr, (const char*)charString, ilen))
   1.111 +	{
   1.112 +		aDes.Set((unsigned short *)wPtr, ilen, ideslen);
   1.113 +	}		
   1.114 +	else
   1.115 +	{
   1.116 +		retval = EInvalidMBSSequence;
   1.117 +	}
   1.118 +	
   1.119 +	return retval;	     
   1.120 +}
   1.121 +
   1.122 + /**
   1.123 +   * Converts a string to a descriptor of type Tptrc16
   1.124 +   *
   1.125 +   * @param aSrc is the string to be converted , aDes is the 
   1.126 +   * reference to the descriptor  where the result of conversion 
   1.127 +   * is stored 
   1.128 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
   1.129 +   * -3 is EStringNoData , -4 is EInvalidPointer)
   1.130 +   */
   1.131 +
   1.132 +EXPORT_C  int StringToTptrc16(string& aSrc, wchar_t* wptr, TPtrC16& aDes)
   1.133 +{
   1.134 +    unsigned int ilen = 0;
   1.135 +    int retval = ESuccess;
   1.136 +    const char* charString = aSrc.c_str();
   1.137 +    int minusone = -1;
   1.138 +    
   1.139 +    if (!wptr)
   1.140 +    {
   1.141 +    	return EInvalidPointer;
   1.142 +    }
   1.143 +    
   1.144 +    if('\0' == charString[0])
   1.145 +    {
   1.146 +    	return EStringNoData;
   1.147 +    }
   1.148 +    
   1.149 +	ilen = strlen(charString);
   1.150 +		
   1.151 +	if(minusone != mbstowcs(wptr, (const char*)charString, ilen ))
   1.152 +	{
   1.153 +		aDes.Set((TUint16 *)(wptr), ilen);	
   1.154 +	}
   1.155 +	else
   1.156 +	{
   1.157 +		retval = EInvalidMBSSequence;
   1.158 +	}
   1.159 +	
   1.160 +	return retval;	
   1.161 +}
   1.162 +
   1.163 + /**
   1.164 +   * Converts a string to a descriptor of type HBufc16
   1.165 +   *
   1.166 +   * @param aSrc is the string to be converted , aDes is the 
   1.167 +   * reference to the descriptor where the result of conversion 
   1.168 +   * is stored 
   1.169 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
   1.170 +   * -3 is EStringNoData, -4 is EInvalidPointer, -6 is EUseNewMaxL,
   1.171 +   * -9 is EInsufficientSystemMemory)
   1.172 +   */
   1.173 +
   1.174 +EXPORT_C int StringToHbufc16(string& aSrc , HBufC16* aDes)
   1.175 + {
   1.176 +    int retval = ESuccess;
   1.177 +    unsigned int ilen = 0, ilendes = 0; 
   1.178 +    const char* charString = aSrc.c_str();
   1.179 +    int minusone = -1;
   1.180 +   
   1.181 +    if('\0' == charString[0])
   1.182 +    {
   1.183 +    	return EStringNoData;
   1.184 +    }
   1.185 +    else if (!aDes)
   1.186 +    {
   1.187 +    	return EInvalidPointer;
   1.188 +    }
   1.189 +    
   1.190 +    ilen = strlen(charString);
   1.191 +    ilendes = aDes->Length();
   1.192 +    
   1.193 +    if (0 == ilendes)
   1.194 +    {
   1.195 +    	return EUseNewMaxL;
   1.196 +    }
   1.197 +    else if (ilendes < ilen)
   1.198 +    {
   1.199 +    	return EInsufficientMemory;
   1.200 +    }
   1.201 +    
   1.202 +    wchar_t* WcharString = new wchar_t[ilen];
   1.203 +	
   1.204 +	if(!WcharString)
   1.205 +	{
   1.206 +		return EInsufficientSystemMemory;
   1.207 +	}
   1.208 +	 
   1.209 +	if(minusone == mbstowcs(WcharString, (const char*)charString, ilen))
   1.210 +	{
   1.211 +		retval = EInvalidMBSSequence;
   1.212 +	}
   1.213 +	else
   1.214 +	{
   1.215 +	    TPtrC16 temp((unsigned short*)WcharString, ilen);
   1.216 +		*aDes = temp;
   1.217 +	} 
   1.218 +	
   1.219 +	delete[] WcharString;
   1.220 +	return retval;
   1.221 +}
   1.222 + 
   1.223 + /**
   1.224 +   * Converts a string to a descriptor of type RBufc16
   1.225 +   *
   1.226 +   * @param aSrc is the string to be converted , aDes is the 
   1.227 +   * reference to the descriptor where the result of conversion 
   1.228 +   * is stored 
   1.229 +   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
   1.230 +   * -3 is EStringNoData, -7 is EInvalidMBSSequence, -9 is EInsufficientSystemMemory)
   1.231 +   */
   1.232 +
   1.233 +EXPORT_C int StringToRbuf16(const string& aSrc, RBuf16& aDes)
   1.234 +{
   1.235 +    int retval = ESuccess;
   1.236 +    unsigned int ilen = 0;
   1.237 +    int minusone = -1;
   1.238 +    
   1.239 +    const char* charString = aSrc.c_str();
   1.240 +    
   1.241 +    if('\0' == charString[0])
   1.242 +    {
   1.243 +    	return EStringNoData;
   1.244 +    }
   1.245 +    
   1.246 +    ilen = strlen(charString);
   1.247 +    
   1.248 +	wchar_t* buf = new wchar_t[ilen];
   1.249 +    
   1.250 +    if (!buf)
   1.251 +    {
   1.252 +    	return EInsufficientMemory;
   1.253 +    }
   1.254 +
   1.255 +	if(minusone != mbstowcs(buf, charString, ilen))
   1.256 +	{
   1.257 +	  aDes.Copy((const unsigned short *)buf, ilen);		
   1.258 +	}
   1.259 +	else
   1.260 +	{
   1.261 +	  retval = EInvalidMBSSequence;		
   1.262 +	}
   1.263 +	
   1.264 +	delete []buf;
   1.265 +  return retval;	
   1.266 +}