os/ossrv/utilitylibraries/libutils/src/stringtodescriptor16.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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, -9 is EInsufficientSystemMemory)
    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, -4 is EInvalidPointer, -6 is EUseNewMaxL,
   168    * -9 is EInsufficientSystemMemory)
   169    */
   170 
   171 EXPORT_C int StringToHbufc16(string& aSrc , HBufC16* aDes)
   172  {
   173     int retval = ESuccess;
   174     unsigned int ilen = 0, ilendes = 0; 
   175     const char* charString = aSrc.c_str();
   176     int minusone = -1;
   177    
   178     if('\0' == charString[0])
   179     {
   180     	return EStringNoData;
   181     }
   182     else if (!aDes)
   183     {
   184     	return EInvalidPointer;
   185     }
   186     
   187     ilen = strlen(charString);
   188     ilendes = aDes->Length();
   189     
   190     if (0 == ilendes)
   191     {
   192     	return EUseNewMaxL;
   193     }
   194     else if (ilendes < ilen)
   195     {
   196     	return EInsufficientMemory;
   197     }
   198     
   199     wchar_t* WcharString = new wchar_t[ilen];
   200 	
   201 	if(!WcharString)
   202 	{
   203 		return EInsufficientSystemMemory;
   204 	}
   205 	 
   206 	if(minusone == mbstowcs(WcharString, (const char*)charString, ilen))
   207 	{
   208 		retval = EInvalidMBSSequence;
   209 	}
   210 	else
   211 	{
   212 	    TPtrC16 temp((unsigned short*)WcharString, ilen);
   213 		*aDes = temp;
   214 	} 
   215 	
   216 	delete[] WcharString;
   217 	return retval;
   218 }
   219  
   220  /**
   221    * Converts a string to a descriptor of type RBufc16
   222    *
   223    * @param aSrc is the string to be converted , aDes is the 
   224    * reference to the descriptor where the result of conversion 
   225    * is stored 
   226    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
   227    * -3 is EStringNoData, -7 is EInvalidMBSSequence, -9 is EInsufficientSystemMemory)
   228    */
   229 
   230 EXPORT_C int StringToRbuf16(const string& aSrc, RBuf16& aDes)
   231 {
   232     int retval = ESuccess;
   233     unsigned int ilen = 0;
   234     int minusone = -1;
   235     
   236     const char* charString = aSrc.c_str();
   237     
   238     if('\0' == charString[0])
   239     {
   240     	return EStringNoData;
   241     }
   242     
   243     ilen = strlen(charString);
   244     
   245 	wchar_t* buf = new wchar_t[ilen];
   246     
   247     if (!buf)
   248     {
   249     	return EInsufficientMemory;
   250     }
   251 
   252 	if(minusone != mbstowcs(buf, charString, ilen))
   253 	{
   254 	  aDes.Copy((const unsigned short *)buf, ilen);		
   255 	}
   256 	else
   257 	{
   258 	  retval = EInvalidMBSSequence;		
   259 	}
   260 	
   261 	delete []buf;
   262   return retval;	
   263 }