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