utilitylibraries/libutils/src/chartodescriptor16.cpp
changeset 34 5fae379060a7
parent 0 e4d67989cc36
child 57 2efc27d87e1c
equal deleted inserted replaced
31:ce057bb09d0b 34:5fae379060a7
    22 
    22 
    23 /** 
    23 /** 
    24   * Converts a character stream to TBuf16
    24   * Converts a character stream to TBuf16
    25   * 
    25   * 
    26   * @param aSrc is char*, aDes is the reference to the descriptor
    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)
    27   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
    28   */
    28   */
    29      
    29      
    30 EXPORT_C  int CharToTbuf16(const char* aSrc, TDes16& aDes)
    30 EXPORT_C  int CharToTbuf16(const char* aSrc, TDes16& aDes)
    31 {		
    31 {		
    32     int retval = ESuccess;
    32     int retval = ESuccess;
    44 	    if(ilen > aDes.MaxLength())
    44 	    if(ilen > aDes.MaxLength())
    45 	    {
    45 	    {
    46 	        return EInsufficientMemory;	
    46 	        return EInsufficientMemory;	
    47 	    }	
    47 	    }	
    48 	}    
    48 	}    
    49     wchar_t *WcharString = new wchar_t[ilen];
    49     wchar_t *WcharString = new wchar_t[ilen+1];
    50     if(!WcharString)
    50     if(!WcharString)
    51     {
    51     {
    52     	return EInsufficientSystemMemory;
    52     	return EInsufficientSystemMemory;
    53     }
    53     }
    54             
    54             
    67 
    67 
    68    /**
    68    /**
    69      * Converts a character stream to HBufc16
    69      * Converts a character stream to HBufc16
    70      * 
    70      * 
    71      * @param aSrc is char*, aDes is the reference to the descriptor
    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)
    72      * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -6 is EUseNewMaxL, -7 is EInvalidMBSSequence)
    73      */
    73      */
    74      
    74      
    75 EXPORT_C int CharToHbufc16(const char* aSrc, HBufC16* aDes)
    75 EXPORT_C int CharToHbufc16(const char* aSrc, HBufC16* aDes)
    76 {	
    76 {	
    77 	int retval = ESuccess; 
    77 	int retval = ESuccess; 
   119 }
   119 }
   120 
   120 
   121 /**
   121 /**
   122   * Converts a character stream to TPtr16
   122   * Converts a character stream to TPtr16
   123   *
   123   *
   124   * @param aSrc is char*, aDes is the reference to the descriptor
   124   * @param aSrc is char*, wPtr is wchar_t*, aDes is the reference to the descriptor
   125   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
   125   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
   126   */
   126   */
   127      
   127      
   128 EXPORT_C int CharpToTptr16(const char* aSrc, wchar_t *wPtr, TPtr16& aDes)
   128 EXPORT_C int CharpToTptr16(const char* aSrc, TPtr16& aDes)
   129 {
   129 {
   130     int retval = ESuccess;
   130     int retval = ESuccess;
   131     unsigned int ilen =0 , ilendes = 0;
   131     unsigned int ilen =0 , ilendes = 0;
   132     int minusone = -1;
   132     int minusone = -1;
   133     
   133     
   134 	if (!aSrc || !wPtr)
   134 	if (!aSrc)
   135 	{
   135 	{
   136 		return EInvalidPointer;
   136 		return EInvalidPointer;
   137 	}
   137 	}
   138 	else 
   138 	else 
   139 	{
   139 	{
   140 	    ilen = strlen(aSrc);
   140 	    ilen = strlen(aSrc);
   141 	    ilendes = aDes.MaxLength();
   141 	    ilendes = aDes.MaxLength();
   142 	    
   142 	    
   143 	    if (ilendes < ilen )
   143 	    if (ilendes < ilen)
   144 	    {
   144 	    {
   145 	    	return EInsufficientMemory;
   145 	    	return EInsufficientMemory;
   146 	    }
   146 	    }
   147 	}
   147 	}
   148 	
   148 	
       
   149   wchar_t *wPtr = new wchar_t[ilen+1];
       
   150 	
       
   151 	if (!wPtr)
       
   152 	{
       
   153 		return EInsufficientMemory;
       
   154 	}
       
   155 	
   149 	if(minusone != mbstowcs((wchar_t *)wPtr, (const char*)aSrc, ilen ))
   156 	if(minusone != mbstowcs((wchar_t *)wPtr, (const char*)aSrc, ilen ))
   150 	{
   157 	{
   151 	    aDes.Set((unsigned short *)wPtr, ilen , ilendes);	
   158 		aDes.Copy((unsigned short *)wPtr, ilen);
   152 	}
   159 	}
   153 	else
   160 	else
   154 	{
   161 	{
   155 		retval = EInvalidMBSSequence;
   162 		retval = EInvalidMBSSequence;
   156 	}
   163 	}
   157 	
   164 	
       
   165 	delete[] wPtr;
   158 	return retval;
   166 	return retval;
   159 }
   167 }
   160 
   168 
   161 /**
   169 /**
   162   * Converts a character stream to TPtrc16
   170   * Converts a character stream to TPtrc16
   163   *
   171   *
   164   * @param aSrc is char*, aDes is the reference to the descriptor
   172   * @param aSrc is char*, cPtr is wchar_t*, aDes is the reference to the descriptor
   165   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
   173   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
   166   */
   174   */
   167      
   175      
   168 EXPORT_C int CharpToTptrc16(char* aSrc ,wchar_t* cPtr, TPtrC16& aDes)
   176 EXPORT_C int CharpToTptrc16(char* aSrc ,wchar_t* cPtr, TPtrC16& aDes)
   169 {
   177 {
   170     int retval = ESuccess; 
   178     int retval = ESuccess; 
   192 
   200 
   193 /**
   201 /**
   194   * Converts a character stream to RBuf16
   202   * Converts a character stream to RBuf16
   195   * 
   203   * 
   196   * @param aSrc is char*, aDes is the reference to the descriptor
   204   * @param aSrc is char*, aDes is the reference to the descriptor
   197   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer)
   205   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
   198   */
   206   */
   199   
   207   
   200   
   208   
   201 EXPORT_C int CharToRbuf16(const char* aSrc, RBuf16& aDes)
   209 EXPORT_C int CharToRbuf16(const char* aSrc, RBuf16& aDes)
   202 {
   210 {
   215     	return EInsufficientSystemMemory;
   223     	return EInsufficientSystemMemory;
   216     }
   224     }
   217     
   225     
   218 	if(minusone != mbstowcs(buf, aSrc, ilen))
   226 	if(minusone != mbstowcs(buf, aSrc, ilen))
   219 	{
   227 	{
   220 	    if (KErrNone == aDes.Create(ilen))
   228 	    aDes.Copy((const unsigned short *)buf, ilen);	
   221 	    {
       
   222 	        aDes.Copy((const unsigned short *)buf, ilen);	
       
   223 	    }
       
   224 	}   
   229 	}   
   225 	else
   230 	else
   226 	{
   231 	{
   227 		retval = EInvalidMBSSequence;
   232 		retval = EInvalidMBSSequence;
   228 	}
   233 	}