utilitylibraries/libutils/src/descriptor16towstring.cpp
changeset 0 e4d67989cc36
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 Descriptor16 to wstring conversions
       
    15  *
       
    16 */
       
    17 
       
    18 	
       
    19  		
       
    20 #include "libutils.h"
       
    21   
       
    22 
       
    23 
       
    24  		
       
    25  /**
       
    26    * Converts a descriptor of type TPtrC16 to Wstring
       
    27    *
       
    28    * @param aSrc is the descriptor to be converted , aDes is the 
       
    29    * reference to the Wstring array where the result of conversion 
       
    30    * is stored  
       
    31    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
    32    * -2 is EInvalidSize , -4 is EInvalidPointer , -5 is EDescriptorNoData)
       
    33    */
       
    34    		
       
    35 EXPORT_C int Tptrc16ToWstring(TPtrC16& aSrc, wstring& aDes)
       
    36 {	
       
    37    
       
    38     unsigned int ilen = aSrc.Length();
       
    39     if (0 == ilen)
       
    40 	{
       
    41 		return EDescriptorNoData;
       
    42 	}
       
    43 	
       
    44 	wchar_t* wcharString = new wchar_t[ilen+1];
       
    45 	if (!wcharString)
       
    46 	{
       
    47 		return EInsufficientSystemMemory;
       
    48 	}
       
    49 	
       
    50 	wmemcpy((wchar_t*)wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
    51 	wcharString[ilen] = L'\0';
       
    52 	
       
    53 	aDes.assign(wcharString);
       
    54 	
       
    55 	delete []wcharString;
       
    56 	
       
    57 	return ESuccess;
       
    58 }
       
    59 
       
    60  /**
       
    61    * Converts a descriptor of type Tbuf16 to Wstring
       
    62    *
       
    63    * @param aSrc is the descriptor to be converted , aDes is the 
       
    64    * reference to the Wstring array where the result of conversion 
       
    65    * is stored  
       
    66    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
    67    * -2 is EInvalidSize , -4 is EInvalidPointer , -5 is EDescriptorNoData)
       
    68    */		
       
    69 
       
    70 EXPORT_C int Tbuf16ToWstring(TDes16& aSrc, wstring& aDes)
       
    71 {	
       
    72    
       
    73     unsigned int ilen = aSrc.Length();
       
    74     if (0 == ilen)
       
    75 	{
       
    76 		return EDescriptorNoData;
       
    77 	}
       
    78 	
       
    79 	wchar_t* wcharString = new wchar_t[ilen+1];
       
    80 	if (!wcharString)
       
    81 	{
       
    82 		return EInsufficientSystemMemory;
       
    83 	}
       
    84 	
       
    85 	wmemcpy((wchar_t*)wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
    86 	wcharString[ilen] = L'\0';
       
    87 	
       
    88 	aDes.assign(wcharString);
       
    89 	
       
    90 	delete []wcharString;
       
    91 	return ESuccess;
       
    92 }
       
    93 
       
    94  /**
       
    95    * Converts a descriptor of type HBufc16 to Wstring
       
    96    *
       
    97    * @param aSrc is the descriptor to be converted , aDes is the 
       
    98    * reference to the Wstring array where the result of conversion 
       
    99    * is stored  
       
   100    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
   101    *  -4 is EInvalidPointer , -5 is EDescriptorNoData)
       
   102    */
       
   103 EXPORT_C int Hbufc16ToWstring(HBufC16* aSrc, wstring& aDes)
       
   104 {	
       
   105     unsigned int ilen =  0;	
       
   106     if (!aSrc)
       
   107     {
       
   108     	return EInvalidPointer;
       
   109     }
       
   110     else
       
   111     {
       
   112         ilen = aSrc->Length();
       
   113         if (0 == ilen )
       
   114         {
       
   115         	return EDescriptorNoData;	
       
   116         }  	
       
   117     }
       
   118     			
       
   119 	wchar_t* wcharString = new wchar_t[ilen+1];
       
   120 	if (!wcharString)
       
   121 	{
       
   122 		return EInsufficientSystemMemory;
       
   123 	}
       
   124 	
       
   125 	wmemcpy((wchar_t*)wcharString, (const wchar_t*)aSrc->Ptr(), ilen);
       
   126 	wcharString[ilen] = L'\0';
       
   127 	
       
   128 	aDes.assign(wcharString);
       
   129 	
       
   130 	delete []wcharString;
       
   131 	return ESuccess;
       
   132 }
       
   133 
       
   134  /**
       
   135    * Converts a descriptor of type TPtr16 to Wstring
       
   136    *
       
   137    * @param aSrc is the descriptor to be converted , aDes is the 
       
   138    * reference to the Wstring array where the result of conversion 
       
   139    * is stored  
       
   140    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
   141    *  -5 is EDescriptorNoData)
       
   142    */
       
   143 EXPORT_C int Tptr16ToWstring(TPtr16& aSrc, wstring& aDes)
       
   144 {
       
   145    
       
   146     unsigned int ilen = aSrc.Length();
       
   147     
       
   148 	if (0 == ilen)
       
   149 	{
       
   150 		return EDescriptorNoData;
       
   151 	}
       
   152 		
       
   153 	wchar_t* wcharString = new wchar_t[ilen+1];
       
   154 	if (!wcharString)
       
   155 	{
       
   156 		return EInsufficientSystemMemory;
       
   157 	}
       
   158 	
       
   159 	wmemcpy(wcharString, (wchar_t *)aSrc.Ptr(), ilen);
       
   160 	wcharString[ilen] = L'\0';
       
   161 	
       
   162 	aDes.assign(wcharString);
       
   163 	
       
   164 	delete []wcharString;
       
   165 	return ESuccess;
       
   166 }
       
   167 
       
   168 
       
   169  /**
       
   170    * Converts a descriptor of type RBuf16 to Wstring
       
   171    *
       
   172    * @param aSrc is the descriptor to be converted , aDes is the 
       
   173    * reference to the Wstring array where the result of conversion 
       
   174    * is stored  
       
   175    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
   176    *  -2 is EInvalidSize , -4 is EInvalidPointer , -5 is EDescriptorNoData)
       
   177    */
       
   178 EXPORT_C int Rbuf16ToWstring(TDes16& aSrc, wstring& aDes)
       
   179 {
       
   180     unsigned int ilen = aSrc.Length();
       
   181     if (0 == ilen)
       
   182     {
       
   183     	return EDescriptorNoData;
       
   184     }
       
   185     
       
   186     wchar_t* buf = new wchar_t[ilen+1];
       
   187     if(!buf)
       
   188     {
       
   189     	return EInsufficientSystemMemory;
       
   190     }
       
   191     
       
   192    	wmemcpy (buf,(wchar_t *)aSrc.Ptr(), ilen);
       
   193     buf[ilen]=L'\0';
       
   194     
       
   195     aDes.assign(buf);
       
   196 	delete[] buf;
       
   197 	
       
   198 	return ESuccess;
       
   199 }
       
   200 
       
   201  /**
       
   202    * Converts a descriptor of type TLitc16 to Wstring
       
   203    *
       
   204    * @param aSrc is the descriptor to be converted , aDes is the 
       
   205    * reference to the Wstring array where the result of conversion 
       
   206    * is stored  
       
   207    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
   208    * -5 is EDescriptorNoData)
       
   209    */
       
   210 EXPORT_C int Tlitc16ToWstring(TDesC16& aSrc, wstring& aDes)
       
   211 {	
       
   212  
       
   213     unsigned int ilen =  aSrc.Length();	
       
   214    	if (0 == ilen)
       
   215     {
       
   216     	return EDescriptorNoData;
       
   217     }		
       
   218 	
       
   219 	wchar_t* wcharString = new wchar_t[ilen+1];
       
   220 	if (!wcharString)
       
   221 	{
       
   222 		return EInsufficientSystemMemory;
       
   223 	}
       
   224 	
       
   225 	wmemcpy((wchar_t*)wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
   226 	wcharString[ilen] = L'\0';
       
   227 	
       
   228 	aDes.assign(wcharString);
       
   229 	
       
   230 	delete []wcharString;
       
   231 	return ESuccess;
       
   232 }