os/ossrv/utilitylibraries/libutils/src/wchartodescriptor8.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 wchar * to Descriptor8 conversions
sl@0
    15
 *
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "libutils.h"
sl@0
    20
  
sl@0
    21
sl@0
    22
sl@0
    23
 /**
sl@0
    24
   * Converts a wchar to a descriptor of type TBuf8
sl@0
    25
   *
sl@0
    26
   * @param aSrc is the wchar to be converted , aDes is the 
sl@0
    27
   * reference to the descriptor where the result of conversion 
sl@0
    28
   * is stored 
sl@0
    29
   * @return Status code (0 is ESuccess,-1 is EInsufficientMemory,
sl@0
    30
   * -3 is EStringNoData, -4 is EInvalidPointer )
sl@0
    31
   */
sl@0
    32
sl@0
    33
EXPORT_C int WcharToTbuf8(const wchar_t* aSrc, TDes8& aDes)
sl@0
    34
{
sl@0
    35
    int retval = ESuccess;
sl@0
    36
    unsigned int ilen = 0;
sl@0
    37
    int minusone = -1;
sl@0
    38
	if ( !aSrc )
sl@0
    39
	{
sl@0
    40
		return EInvalidPointer;
sl@0
    41
	}
sl@0
    42
	else
sl@0
    43
	{
sl@0
    44
	    ilen = wcslen(aSrc);
sl@0
    45
	    if (ilen*2 > aDes.MaxLength())
sl@0
    46
	    {
sl@0
    47
	    	return EInsufficientMemory;	
sl@0
    48
	    } 
sl@0
    49
	}
sl@0
    50
	 
sl@0
    51
	char *CharString = new char[ilen*2];
sl@0
    52
	
sl@0
    53
	if (!CharString)
sl@0
    54
	{
sl@0
    55
		return EInsufficientSystemMemory;
sl@0
    56
	}
sl@0
    57
	
sl@0
    58
	if(minusone == wcstombs(CharString, (const wchar_t*)aSrc, ilen*2))
sl@0
    59
	{
sl@0
    60
		retval = EInvalidWCSSequence;
sl@0
    61
	}
sl@0
    62
	else 
sl@0
    63
	{
sl@0
    64
		aDes.Copy((unsigned char *)CharString, ilen*2);
sl@0
    65
	}
sl@0
    66
	
sl@0
    67
	delete []CharString;	
sl@0
    68
	return retval;
sl@0
    69
}
sl@0
    70
sl@0
    71
 /** 
sl@0
    72
   * Converts a wchar to a descriptor of type HBufc8
sl@0
    73
   * 
sl@0
    74
   * @param aSrc is the Wstring to be converted, aDes is the 
sl@0
    75
   * reference to the descriptor where the result of conversion 
sl@0
    76
   * is stored 
sl@0
    77
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory )
sl@0
    78
   */
sl@0
    79
sl@0
    80
EXPORT_C int WcharToHbufc8(const wchar_t* aSrc, HBufC8* aDes)
sl@0
    81
{
sl@0
    82
    unsigned int wlen = 0, ilendes = 0;
sl@0
    83
    int retval = ESuccess;
sl@0
    84
    int minusone = -1;
sl@0
    85
	if ( !aSrc || !aDes)
sl@0
    86
	{
sl@0
    87
		return EInvalidPointer;
sl@0
    88
	}
sl@0
    89
	else
sl@0
    90
	{
sl@0
    91
		wlen = wcslen(aSrc);
sl@0
    92
        ilendes = aDes->Length();
sl@0
    93
	    if(0 == ilendes)
sl@0
    94
	    {
sl@0
    95
	        return EUseNewMaxL;
sl@0
    96
	    }
sl@0
    97
	    else if (wlen > ilendes)
sl@0
    98
	    {
sl@0
    99
	        return EInsufficientMemory;	
sl@0
   100
	    }
sl@0
   101
	}
sl@0
   102
    
sl@0
   103
    char *CharString = new char[wlen*2];
sl@0
   104
	
sl@0
   105
	if (!CharString)
sl@0
   106
	{
sl@0
   107
		return EInsufficientSystemMemory;
sl@0
   108
	}
sl@0
   109
	
sl@0
   110
    	
sl@0
   111
	if(minusone == wcstombs(CharString, aSrc, wlen*2))
sl@0
   112
	{
sl@0
   113
		retval = EInvalidWCSSequence;
sl@0
   114
	}
sl@0
   115
	else
sl@0
   116
	{
sl@0
   117
	    (*aDes) = (const TUint8 *)CharString;	
sl@0
   118
	}
sl@0
   119
	
sl@0
   120
	delete []CharString;
sl@0
   121
	return retval;
sl@0
   122
}
sl@0
   123
sl@0
   124
 /**
sl@0
   125
   * Converts a wchar to a descriptor of type TPtr8
sl@0
   126
   *
sl@0
   127
   * @param aSrc is the wchar to be converted , aDes is the 
sl@0
   128
   * reference to the descriptor where the result of conversion 
sl@0
   129
   * is stored 
sl@0
   130
   * @return Status code (0 is ESuccess,-1 is EInsufficientMemory,
sl@0
   131
   * -3 is EStringNoData, -4 is EInvalidPointer )
sl@0
   132
   */
sl@0
   133
sl@0
   134
EXPORT_C int WcharpToTptr8(const wchar_t* aSrc, char* cPtr, TPtr8& aDes)
sl@0
   135
{
sl@0
   136
    int retval = ESuccess;
sl@0
   137
    unsigned int wlen = 0, ilendes = 0;
sl@0
   138
    int minusone = -1;
sl@0
   139
	if ( !aSrc )
sl@0
   140
	{
sl@0
   141
		return EInvalidPointer;
sl@0
   142
	}
sl@0
   143
	else 
sl@0
   144
	{
sl@0
   145
	    wlen = wcslen(aSrc);
sl@0
   146
	    ilendes = aDes.MaxLength();
sl@0
   147
	    if (ilendes < wlen )
sl@0
   148
	    {
sl@0
   149
	    	return EInsufficientMemory;
sl@0
   150
	    }	    	
sl@0
   151
	}
sl@0
   152
	
sl@0
   153
	if(minusone != wcstombs(cPtr, (const wchar_t*)aSrc, wlen))
sl@0
   154
	{
sl@0
   155
		aDes.Set((TUint8*)cPtr, wlen, ilendes);	
sl@0
   156
	}
sl@0
   157
	else
sl@0
   158
	{
sl@0
   159
		retval = EInvalidWCSSequence;
sl@0
   160
	}
sl@0
   161
	
sl@0
   162
	return retval;
sl@0
   163
}
sl@0
   164
sl@0
   165
 /**
sl@0
   166
   * Converts a wchar to a descriptor of type TPtrC8
sl@0
   167
   *
sl@0
   168
   * @param aSrc is the wchar to be converted , aDes is the 
sl@0
   169
   * reference to the descriptor where the result of conversion 
sl@0
   170
   * is stored 
sl@0
   171
   * @return Status code (0 is ESuccess,-1 is EInsufficientMemory,
sl@0
   172
   *  -4 is EInvalidPointer )
sl@0
   173
   */ 
sl@0
   174
 
sl@0
   175
EXPORT_C int WcharpToTptrc8(const wchar_t* aSrc, char* cPtr, TPtrC8& aDes)
sl@0
   176
{
sl@0
   177
sl@0
   178
    int retval = ESuccess;
sl@0
   179
    unsigned int wlen = 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
	wlen = wcslen(aSrc);
sl@0
   188
	
sl@0
   189
	if(minusone != wcstombs(cPtr, (const wchar_t*)aSrc, wlen))
sl@0
   190
	{
sl@0
   191
		aDes.Set((TUint8*)cPtr,wlen);
sl@0
   192
	}
sl@0
   193
	else
sl@0
   194
	{
sl@0
   195
		retval = EInvalidWCSSequence;
sl@0
   196
	}
sl@0
   197
	
sl@0
   198
	return retval;
sl@0
   199
}
sl@0
   200
sl@0
   201
sl@0
   202
 /**
sl@0
   203
   * Converts a wchar to a descriptor of type RBuf8
sl@0
   204
   *
sl@0
   205
   * @param aSrc is the wchar to be converted , aDes is the 
sl@0
   206
   * reference to the descriptor where the result of conversion 
sl@0
   207
   * is stored 
sl@0
   208
   * @return Status code (0 is ESuccess ,-1 is EInsufficientMemory, -4 is EInvalidPointer )
sl@0
   209
   */
sl@0
   210
sl@0
   211
EXPORT_C int WcharToRbuf8(const wchar_t* aSrc, RBuf8& aDes)
sl@0
   212
{
sl@0
   213
    int retval = ESuccess;
sl@0
   214
    unsigned  int wlen = 0;
sl@0
   215
    char* buf = NULL;
sl@0
   216
    int minusone = -1;
sl@0
   217
    
sl@0
   218
    if (!aSrc)
sl@0
   219
	{
sl@0
   220
		return EInvalidPointer;
sl@0
   221
	}
sl@0
   222
	
sl@0
   223
	wlen = wcslen(aSrc);
sl@0
   224
sl@0
   225
	buf = new char[wlen*2];
sl@0
   226
	if (!buf)
sl@0
   227
	{
sl@0
   228
		return EInsufficientSystemMemory;
sl@0
   229
	}
sl@0
   230
sl@0
   231
	if(minusone != wcstombs(buf, (const wchar_t*)aSrc, wlen*2))
sl@0
   232
	{
sl@0
   233
	  aDes.Copy((const unsigned char *)buf, wlen*2);		
sl@0
   234
	}
sl@0
   235
	else
sl@0
   236
	{
sl@0
   237
		retval = EInvalidWCSSequence;
sl@0
   238
	}
sl@0
   239
	
sl@0
   240
	delete []buf;
sl@0
   241
	return retval; 	
sl@0
   242
}