os/ossrv/utilitylibraries/libutils/src/chartodescriptor16.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:   Contains the source for char * to Descriptor16 conversions
sl@0
    15
 *
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
sl@0
    20
#include "libutils.h"
sl@0
    21
  
sl@0
    22
sl@0
    23
/** 
sl@0
    24
  * Converts a character stream to TBuf16
sl@0
    25
  * 
sl@0
    26
  * @param aSrc is char*, aDes is the reference to the descriptor
sl@0
    27
  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
sl@0
    28
  */
sl@0
    29
     
sl@0
    30
EXPORT_C  int CharToTbuf16(const char* aSrc, TDes16& aDes)
sl@0
    31
{		
sl@0
    32
    int retval = ESuccess;
sl@0
    33
    unsigned int ilen = 0;
sl@0
    34
    int minusone = -1;
sl@0
    35
sl@0
    36
	    
sl@0
    37
	if (!aSrc)
sl@0
    38
	{	
sl@0
    39
	    return EInvalidPointer;	
sl@0
    40
	}	
sl@0
    41
	else 
sl@0
    42
	{	
sl@0
    43
	    ilen = strlen(aSrc);
sl@0
    44
	    if(ilen > aDes.MaxLength())
sl@0
    45
	    {
sl@0
    46
	        return EInsufficientMemory;	
sl@0
    47
	    }	
sl@0
    48
	}    
sl@0
    49
    wchar_t *WcharString = new wchar_t[ilen+1];
sl@0
    50
    if(!WcharString)
sl@0
    51
    {
sl@0
    52
    	return EInsufficientSystemMemory;
sl@0
    53
    }
sl@0
    54
            
sl@0
    55
	if(minusone == mbstowcs(WcharString, (const char*)aSrc, ilen))
sl@0
    56
	{   
sl@0
    57
		retval = EInvalidMBSSequence;
sl@0
    58
	}
sl@0
    59
	else 
sl@0
    60
	{
sl@0
    61
		aDes.Copy((unsigned short *)WcharString, ilen);
sl@0
    62
	}   
sl@0
    63
        
sl@0
    64
	delete[] WcharString;
sl@0
    65
	return retval;
sl@0
    66
}
sl@0
    67
sl@0
    68
   /**
sl@0
    69
     * Converts a character stream to HBufc16
sl@0
    70
     * 
sl@0
    71
     * @param aSrc is char*, aDes is the reference to the descriptor
sl@0
    72
     * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -6 is EUseNewMaxL, -7 is EInvalidMBSSequence)
sl@0
    73
     */
sl@0
    74
     
sl@0
    75
EXPORT_C int CharToHbufc16(const char* aSrc, HBufC16* aDes)
sl@0
    76
{	
sl@0
    77
	int retval = ESuccess; 
sl@0
    78
	unsigned int ilen = 0, ilendes = 0; 
sl@0
    79
	int minusone = -1;
sl@0
    80
	if (!aSrc || !aDes)
sl@0
    81
	{
sl@0
    82
		return EInvalidPointer;
sl@0
    83
	}
sl@0
    84
	else 
sl@0
    85
	{
sl@0
    86
	    ilen = strlen (aSrc);
sl@0
    87
	    ilendes = aDes->Length();
sl@0
    88
	    
sl@0
    89
	    if(0 == ilendes)
sl@0
    90
	    {
sl@0
    91
	        return EUseNewMaxL;	
sl@0
    92
	    }     
sl@0
    93
	    else if (ilen > ilendes)
sl@0
    94
	    {
sl@0
    95
	    	return EInsufficientMemory;	
sl@0
    96
	    }	
sl@0
    97
	}
sl@0
    98
	
sl@0
    99
	wchar_t *wCharString = new wchar_t[ilen+1];
sl@0
   100
	
sl@0
   101
	if (!wCharString)
sl@0
   102
	{
sl@0
   103
		return EInsufficientMemory;
sl@0
   104
	}
sl@0
   105
	
sl@0
   106
	if(minusone == mbstowcs((wchar_t *)wCharString, (const char*)aSrc, ilen))
sl@0
   107
	{
sl@0
   108
		retval = EInvalidMBSSequence;
sl@0
   109
	}
sl@0
   110
	else 
sl@0
   111
	{
sl@0
   112
	    wCharString[ilen] = (wchar_t)'\0';
sl@0
   113
		TPtrC16 temp ((unsigned short *)wCharString, ilen);
sl@0
   114
		*aDes = temp; 
sl@0
   115
	}
sl@0
   116
	
sl@0
   117
	delete[] wCharString;
sl@0
   118
	return retval;
sl@0
   119
}
sl@0
   120
sl@0
   121
/**
sl@0
   122
  * Converts a character stream to TPtr16
sl@0
   123
  *
sl@0
   124
  * @param aSrc is char*, wPtr is wchar_t*, aDes is the reference to the descriptor
sl@0
   125
  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
sl@0
   126
  */
sl@0
   127
     
sl@0
   128
EXPORT_C int CharpToTptr16(const char* aSrc, TPtr16& aDes)
sl@0
   129
{
sl@0
   130
    int retval = ESuccess;
sl@0
   131
    unsigned int ilen =0 , ilendes = 0;
sl@0
   132
    int minusone = -1;
sl@0
   133
    
sl@0
   134
	if (!aSrc)
sl@0
   135
	{
sl@0
   136
		return EInvalidPointer;
sl@0
   137
	}
sl@0
   138
	else 
sl@0
   139
	{
sl@0
   140
	    ilen = strlen(aSrc);
sl@0
   141
	    ilendes = aDes.MaxLength();
sl@0
   142
	    
sl@0
   143
	    if (ilendes < ilen)
sl@0
   144
	    {
sl@0
   145
	    	return EInsufficientMemory;
sl@0
   146
	    }
sl@0
   147
	}
sl@0
   148
	
sl@0
   149
  wchar_t *wPtr = new wchar_t[ilen+1];
sl@0
   150
	
sl@0
   151
	if (!wPtr)
sl@0
   152
	{
sl@0
   153
		return EInsufficientMemory;
sl@0
   154
	}
sl@0
   155
	
sl@0
   156
	if(minusone != mbstowcs((wchar_t *)wPtr, (const char*)aSrc, ilen ))
sl@0
   157
	{
sl@0
   158
		aDes.Copy((unsigned short *)wPtr, ilen);
sl@0
   159
	}
sl@0
   160
	else
sl@0
   161
	{
sl@0
   162
		retval = EInvalidMBSSequence;
sl@0
   163
	}
sl@0
   164
	
sl@0
   165
	delete[] wPtr;
sl@0
   166
	return retval;
sl@0
   167
}
sl@0
   168
sl@0
   169
/**
sl@0
   170
  * Converts a character stream to TPtrc16
sl@0
   171
  *
sl@0
   172
  * @param aSrc is char*, cPtr is wchar_t*, aDes is the reference to the descriptor
sl@0
   173
  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
sl@0
   174
  */
sl@0
   175
     
sl@0
   176
EXPORT_C int CharpToTptrc16(char* aSrc ,wchar_t* cPtr, TPtrC16& aDes)
sl@0
   177
{
sl@0
   178
    int retval = ESuccess; 
sl@0
   179
    unsigned int ilen = 0;
sl@0
   180
    int minusone = -1;
sl@0
   181
    
sl@0
   182
	if ( !aSrc || !cPtr )
sl@0
   183
	{
sl@0
   184
		return EInvalidPointer;
sl@0
   185
	}
sl@0
   186
	
sl@0
   187
	ilen = strlen(aSrc);
sl@0
   188
sl@0
   189
	if(minusone != mbstowcs((wchar_t*)cPtr, (const char*)aSrc, strlen(aSrc)))
sl@0
   190
	{
sl@0
   191
	    aDes.Set((TUint16*)cPtr, ilen);
sl@0
   192
	}
sl@0
   193
	else
sl@0
   194
	{
sl@0
   195
	    retval = EInvalidMBSSequence;
sl@0
   196
	}
sl@0
   197
	
sl@0
   198
	return retval; 
sl@0
   199
}
sl@0
   200
sl@0
   201
/**
sl@0
   202
  * Converts a character stream to RBuf16
sl@0
   203
  * 
sl@0
   204
  * @param aSrc is char*, aDes is the reference to the descriptor
sl@0
   205
  * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -4 is EInvalidPointer, -7 is EInvalidMBSSequence)
sl@0
   206
  */
sl@0
   207
  
sl@0
   208
  
sl@0
   209
EXPORT_C int CharToRbuf16(const char* aSrc, RBuf16& aDes)
sl@0
   210
{
sl@0
   211
    int retval = ESuccess;
sl@0
   212
    unsigned int ilen = 0; 
sl@0
   213
    int minusone = -1;
sl@0
   214
    if ( !aSrc )
sl@0
   215
    {
sl@0
   216
    	return EInvalidPointer;
sl@0
   217
    }
sl@0
   218
    ilen = strlen(aSrc);
sl@0
   219
	
sl@0
   220
	wchar_t* buf = new wchar_t[ilen];
sl@0
   221
    if (!buf)
sl@0
   222
    {
sl@0
   223
    	return EInsufficientSystemMemory;
sl@0
   224
    }
sl@0
   225
    
sl@0
   226
	if(minusone != mbstowcs(buf, aSrc, ilen))
sl@0
   227
	{
sl@0
   228
	    aDes.Copy((const unsigned short *)buf, ilen);	
sl@0
   229
	}   
sl@0
   230
	else
sl@0
   231
	{
sl@0
   232
		retval = EInvalidMBSSequence;
sl@0
   233
	}
sl@0
   234
	
sl@0
   235
	delete []buf;
sl@0
   236
	return retval;
sl@0
   237
}