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