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