utilitylibraries/libutils/src/chartodescriptor16.cpp
changeset 0 e4d67989cc36
child 34 5fae379060a7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utilitylibraries/libutils/src/chartodescriptor16.cpp	Tue Feb 02 02:01:42 2010 +0200
@@ -0,0 +1,232 @@
+/*
+* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
+* All rights reserved.
+* This component and the accompanying materials are made available
+* under the terms of "Eclipse Public License v1.0"
+* which accompanies this distribution, and is available
+* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* Initial Contributors:
+* Nokia Corporation - initial contribution.
+*
+* Contributors:
+*
+* Description:   Contains the source for char * to Descriptor16 conversions
+ *
+*/
+
+
+
+#include "libutils.h"
+  
+
+/** 
+  * Converts a character stream to TBuf16
+  * 
+  * @param aSrc is char*, aDes is the reference to the descriptor
+  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
+  */
+     
+EXPORT_C  int CharToTbuf16(const char* aSrc, TDes16& aDes)
+{		
+    int retval = ESuccess;
+    unsigned int ilen = 0;
+    int minusone = -1;
+
+	    
+	if (!aSrc)
+	{	
+	    return EInvalidPointer;	
+	}	
+	else 
+	{	
+	    ilen = strlen(aSrc);
+	    if(ilen > aDes.MaxLength())
+	    {
+	        return EInsufficientMemory;	
+	    }	
+	}    
+    wchar_t *WcharString = new wchar_t[ilen];
+    if(!WcharString)
+    {
+    	return EInsufficientSystemMemory;
+    }
+            
+	if(minusone == mbstowcs(WcharString, (const char*)aSrc, ilen))
+	{   
+		retval = EInvalidMBSSequence;
+	}
+	else 
+	{
+		aDes.Copy((unsigned short *)WcharString, ilen);
+	}   
+        
+	delete[] WcharString;
+	return retval;
+}
+
+   /**
+     * Converts a character stream to HBufc16
+     * 
+     * @param aSrc is char*, aDes is the reference to the descriptor
+     * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
+     */
+     
+EXPORT_C int CharToHbufc16(const char* aSrc, HBufC16* aDes)
+{	
+	int retval = ESuccess; 
+	unsigned int ilen = 0, ilendes = 0; 
+	int minusone = -1;
+	if (!aSrc || !aDes)
+	{
+		return EInvalidPointer;
+	}
+	else 
+	{
+	    ilen = strlen (aSrc);
+	    ilendes = aDes->Length();
+	    
+	    if(0 == ilendes)
+	    {
+	        return EUseNewMaxL;	
+	    }     
+	    else if (ilen > ilendes)
+	    {
+	    	return EInsufficientMemory;	
+	    }	
+	}
+	
+	wchar_t *wCharString = new wchar_t[ilen+1];
+	
+	if (!wCharString)
+	{
+		return EInsufficientMemory;
+	}
+	
+	if(minusone == mbstowcs((wchar_t *)wCharString, (const char*)aSrc, ilen))
+	{
+		retval = EInvalidMBSSequence;
+	}
+	else 
+	{
+	    wCharString[ilen] = (wchar_t)'\0';
+		TPtrC16 temp ((unsigned short *)wCharString, ilen);
+		*aDes = temp; 
+	}
+	
+	delete[] wCharString;
+	return retval;
+}
+
+/**
+  * Converts a character stream to TPtr16
+  *
+  * @param aSrc is char*, aDes is the reference to the descriptor
+  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
+  */
+     
+EXPORT_C int CharpToTptr16(const char* aSrc, wchar_t *wPtr, TPtr16& aDes)
+{
+    int retval = ESuccess;
+    unsigned int ilen =0 , ilendes = 0;
+    int minusone = -1;
+    
+	if (!aSrc || !wPtr)
+	{
+		return EInvalidPointer;
+	}
+	else 
+	{
+	    ilen = strlen(aSrc);
+	    ilendes = aDes.MaxLength();
+	    
+	    if (ilendes < ilen )
+	    {
+	    	return EInsufficientMemory;
+	    }
+	}
+	
+	if(minusone != mbstowcs((wchar_t *)wPtr, (const char*)aSrc, ilen ))
+	{
+	    aDes.Set((unsigned short *)wPtr, ilen , ilendes);	
+	}
+	else
+	{
+		retval = EInvalidMBSSequence;
+	}
+	
+	return retval;
+}
+
+/**
+  * Converts a character stream to TPtrc16
+  *
+  * @param aSrc is char*, aDes is the reference to the descriptor
+  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
+  */
+     
+EXPORT_C int CharpToTptrc16(char* aSrc ,wchar_t* cPtr, TPtrC16& aDes)
+{
+    int retval = ESuccess; 
+    unsigned int ilen = 0;
+    int minusone = -1;
+    
+	if ( !aSrc || !cPtr )
+	{
+		return EInvalidPointer;
+	}
+	
+	ilen = strlen(aSrc);
+
+	if(minusone != mbstowcs((wchar_t*)cPtr, (const char*)aSrc, strlen(aSrc)))
+	{
+	    aDes.Set((TUint16*)cPtr, ilen);
+	}
+	else
+	{
+	    retval = EInvalidMBSSequence;
+	}
+	
+	return retval; 
+}
+
+/**
+  * Converts a character stream to RBuf16
+  * 
+  * @param aSrc is char*, aDes is the reference to the descriptor
+  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
+  */
+  
+  
+EXPORT_C int CharToRbuf16(const char* aSrc, RBuf16& aDes)
+{
+    int retval = ESuccess;
+    unsigned int ilen = 0; 
+    int minusone = -1;
+    if ( !aSrc )
+    {
+    	return EInvalidPointer;
+    }
+    ilen = strlen(aSrc);
+	
+	wchar_t* buf = new wchar_t[ilen];
+    if (!buf)
+    {
+    	return EInsufficientSystemMemory;
+    }
+    
+	if(minusone != mbstowcs(buf, aSrc, ilen))
+	{
+	    if (KErrNone == aDes.Create(ilen))
+	    {
+	        aDes.Copy((const unsigned short *)buf, ilen);	
+	    }
+	}   
+	else
+	{
+		retval = EInvalidMBSSequence;
+	}
+	
+	delete []buf;
+	return retval;
+}