utilitylibraries/libutils/src/wchartodescriptor8.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 wchar * to Descriptor8 conversions
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "libutils.h"
       
    20   
       
    21 
       
    22 
       
    23  /**
       
    24    * Converts a wchar to a descriptor of type TBuf8
       
    25    *
       
    26    * @param aSrc is the wchar to be converted , aDes is the 
       
    27    * reference to the descriptor where the result of conversion 
       
    28    * is stored 
       
    29    * @return Status code (0 is ESuccess,-1 is EInsufficientMemory,
       
    30    * -3 is EStringNoData, -4 is EInvalidPointer )
       
    31    */
       
    32 
       
    33 EXPORT_C int WcharToTbuf8(const wchar_t* aSrc, TDes8& aDes)
       
    34 {
       
    35     int retval = ESuccess;
       
    36     unsigned int ilen = 0;
       
    37     int minusone = -1;
       
    38 	if ( !aSrc )
       
    39 	{
       
    40 		return EInvalidPointer;
       
    41 	}
       
    42 	else
       
    43 	{
       
    44 	    ilen = wcslen(aSrc);
       
    45 	    if (ilen*2 > aDes.MaxLength())
       
    46 	    {
       
    47 	    	return EInsufficientMemory;	
       
    48 	    } 
       
    49 	}
       
    50 	 
       
    51 	char *CharString = new char[ilen*2];
       
    52 	
       
    53 	if (!CharString)
       
    54 	{
       
    55 		return EInsufficientSystemMemory;
       
    56 	}
       
    57 	
       
    58 	if(minusone == wcstombs(CharString, (const wchar_t*)aSrc, ilen*2))
       
    59 	{
       
    60 		retval = EInvalidWCSSequence;
       
    61 	}
       
    62 	else 
       
    63 	{
       
    64 		aDes.Copy((unsigned char *)CharString, ilen*2);
       
    65 	}
       
    66 	
       
    67 	delete []CharString;	
       
    68 	return retval;
       
    69 }
       
    70 
       
    71  /** 
       
    72    * Converts a wchar to a descriptor of type HBufc8
       
    73    * 
       
    74    * @param aSrc is the Wstring to be converted, aDes is the 
       
    75    * reference to the descriptor where the result of conversion 
       
    76    * is stored 
       
    77    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory )
       
    78    */
       
    79 
       
    80 EXPORT_C int WcharToHbufc8(const wchar_t* aSrc, HBufC8* aDes)
       
    81 {
       
    82     unsigned int wlen = 0, ilendes = 0;
       
    83     int retval = ESuccess;
       
    84     int minusone = -1;
       
    85 	if ( !aSrc || !aDes)
       
    86 	{
       
    87 		return EInvalidPointer;
       
    88 	}
       
    89 	else
       
    90 	{
       
    91 		wlen = wcslen(aSrc);
       
    92         ilendes = aDes->Length();
       
    93 	    if(0 == ilendes)
       
    94 	    {
       
    95 	        return EUseNewMaxL;
       
    96 	    }
       
    97 	    else if (wlen > ilendes)
       
    98 	    {
       
    99 	        return EInsufficientMemory;	
       
   100 	    }
       
   101 	}
       
   102     
       
   103     char *CharString = new char[wlen*2];
       
   104 	
       
   105 	if (!CharString)
       
   106 	{
       
   107 		return EInsufficientSystemMemory;
       
   108 	}
       
   109 	
       
   110     	
       
   111 	if(minusone == wcstombs(CharString, aSrc, wlen*2))
       
   112 	{
       
   113 		retval = EInvalidWCSSequence;
       
   114 	}
       
   115 	else
       
   116 	{
       
   117 	    (*aDes) = (const TUint8 *)CharString;	
       
   118 	}
       
   119 	
       
   120 	delete []CharString;
       
   121 	return retval;
       
   122 }
       
   123 
       
   124  /**
       
   125    * Converts a wchar to a descriptor of type TPtr8
       
   126    *
       
   127    * @param aSrc is the wchar to be converted , aDes is the 
       
   128    * reference to the descriptor where the result of conversion 
       
   129    * is stored 
       
   130    * @return Status code (0 is ESuccess,-1 is EInsufficientMemory,
       
   131    * -3 is EStringNoData, -4 is EInvalidPointer )
       
   132    */
       
   133 
       
   134 EXPORT_C int WcharpToTptr8(const wchar_t* aSrc, char* cPtr, TPtr8& aDes)
       
   135 {
       
   136     int retval = ESuccess;
       
   137     unsigned int wlen = 0, ilendes = 0;
       
   138     int minusone = -1;
       
   139 	if ( !aSrc )
       
   140 	{
       
   141 		return EInvalidPointer;
       
   142 	}
       
   143 	else 
       
   144 	{
       
   145 	    wlen = wcslen(aSrc);
       
   146 	    ilendes = aDes.MaxLength();
       
   147 	    if (ilendes < wlen )
       
   148 	    {
       
   149 	    	return EInsufficientMemory;
       
   150 	    }	    	
       
   151 	}
       
   152 	
       
   153 	if(minusone != wcstombs(cPtr, (const wchar_t*)aSrc, wlen))
       
   154 	{
       
   155 		aDes.Set((TUint8*)cPtr, wlen, ilendes);	
       
   156 	}
       
   157 	else
       
   158 	{
       
   159 		retval = EInvalidWCSSequence;
       
   160 	}
       
   161 	
       
   162 	return retval;
       
   163 }
       
   164 
       
   165  /**
       
   166    * Converts a wchar to a descriptor of type TPtrC8
       
   167    *
       
   168    * @param aSrc is the wchar to be converted , aDes is the 
       
   169    * reference to the descriptor where the result of conversion 
       
   170    * is stored 
       
   171    * @return Status code (0 is ESuccess,-1 is EInsufficientMemory,
       
   172    *  -4 is EInvalidPointer )
       
   173    */ 
       
   174  
       
   175 EXPORT_C int WcharpToTptrc8(const wchar_t* aSrc, char* cPtr, TPtrC8& aDes)
       
   176 {
       
   177 
       
   178     int retval = ESuccess;
       
   179     unsigned int wlen = 0 ;
       
   180     int minusone = -1;
       
   181     
       
   182 	if ( !aSrc  || !cPtr)
       
   183 	{
       
   184 		return EInvalidPointer;
       
   185 	}
       
   186 	
       
   187 	wlen = wcslen(aSrc);
       
   188 	
       
   189 	if(minusone != wcstombs(cPtr, (const wchar_t*)aSrc, wlen))
       
   190 	{
       
   191 		aDes.Set((TUint8*)cPtr,wlen);
       
   192 	}
       
   193 	else
       
   194 	{
       
   195 		retval = EInvalidWCSSequence;
       
   196 	}
       
   197 	
       
   198 	return retval;
       
   199 }
       
   200 
       
   201 
       
   202  /**
       
   203    * Converts a wchar to a descriptor of type RBuf8
       
   204    *
       
   205    * @param aSrc is the wchar to be converted , aDes is the 
       
   206    * reference to the descriptor where the result of conversion 
       
   207    * is stored 
       
   208    * @return Status code (0 is ESuccess ,-1 is EInsufficientMemory, -4 is EInvalidPointer )
       
   209    */
       
   210 
       
   211 EXPORT_C int WcharToRbuf8(const wchar_t* aSrc, RBuf8& aDes)
       
   212 {
       
   213     int retval = ESuccess;
       
   214     unsigned  int wlen = 0;
       
   215     char* buf = NULL;
       
   216     int minusone = -1;
       
   217     
       
   218     if (!aSrc)
       
   219 	{
       
   220 		return EInvalidPointer;
       
   221 	}
       
   222 	
       
   223 	wlen = wcslen(aSrc);
       
   224 
       
   225 	buf = new char[wlen*2];
       
   226 	if (!buf)
       
   227 	{
       
   228 		return EInsufficientSystemMemory;
       
   229 	}
       
   230 
       
   231 	if(minusone != wcstombs(buf, (const wchar_t*)aSrc, wlen*2))
       
   232 	{
       
   233 	    if (KErrNone == aDes.Create(wlen*2))
       
   234 	    {
       
   235 	    	aDes.Copy((const unsigned char *)buf, wlen*2);	
       
   236 	    }
       
   237 		else 
       
   238 		{
       
   239 			retval = EInsufficientSystemMemory;
       
   240 		}			
       
   241 	}
       
   242 	else
       
   243 	{
       
   244 		retval = EInvalidWCSSequence;
       
   245 	}
       
   246 	
       
   247 	delete []buf;
       
   248 	return retval; 	
       
   249 }