os/ossrv/utilitylibraries/libutils/src/descriptor16tochar.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 Descriptor to char * 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
 /**
sl@0
    25
   * Converts a descriptor of type TBuf16 to character stream
sl@0
    26
   *
sl@0
    27
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
    28
   * reference to the character sream where the result of conversion 
sl@0
    29
   * is stored , n_size specifies the conversion size of the string 
sl@0
    30
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
    31
   * -2 is EInvalidSize , -4 is EInvalidPointer, -8 is EInvalidWCSSequence)
sl@0
    32
   */
sl@0
    33
EXPORT_C int Tbuf16ToChar(TDes16& aSrc, char* aDes, int& n_size)
sl@0
    34
{	
sl@0
    35
    unsigned int ilen = aSrc.Length();
sl@0
    36
    int retval = ESuccess;
sl@0
    37
    wchar_t *temp16String = NULL;
sl@0
    38
    int minusone = -1;
sl@0
    39
    
sl@0
    40
    if (0 == ilen)
sl@0
    41
    {
sl@0
    42
    	return EDescriptorNoData;
sl@0
    43
    }
sl@0
    44
    else if(!aDes)
sl@0
    45
    {
sl@0
    46
    	return EInvalidPointer;
sl@0
    47
    }
sl@0
    48
    
sl@0
    49
    else if(n_size < ilen*2+1)
sl@0
    50
    {
sl@0
    51
    	n_size = ilen*2;
sl@0
    52
    	return EInvalidSize;
sl@0
    53
    }
sl@0
    54
        		
sl@0
    55
	temp16String = new wchar_t [ilen+1];
sl@0
    56
	if (!temp16String)
sl@0
    57
	{
sl@0
    58
		return EInsufficientSystemMemory;
sl@0
    59
	}
sl@0
    60
	
sl@0
    61
	wmemcpy(temp16String, (const wchar_t *)aSrc.Ptr(), ilen);
sl@0
    62
	temp16String[ilen] = L'\0'; 	
sl@0
    63
	
sl@0
    64
	if(minusone != wcstombs(aDes, (const wchar_t*)temp16String, ilen*2))
sl@0
    65
	{
sl@0
    66
	    aDes[ilen*2] = '\0'; 
sl@0
    67
	}
sl@0
    68
	else 
sl@0
    69
	{
sl@0
    70
		retval = EInvalidWCSSequence;
sl@0
    71
	}
sl@0
    72
	
sl@0
    73
	delete []temp16String;	
sl@0
    74
	return retval;
sl@0
    75
}
sl@0
    76
sl@0
    77
 /**
sl@0
    78
   * Converts a descriptor of type TBufC16 to character stream
sl@0
    79
   *
sl@0
    80
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
    81
   * reference to the character sream where the result of conversion 
sl@0
    82
   * is stored , n_size specifies the conversion size of the string
sl@0
    83
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
    84
   * -2 is EInvalidSize , -4 is EInvalidPointer, -5 is EDescriptorNoData
sl@0
    85
   * -8 is EInvalidWCSSequence)
sl@0
    86
   */	
sl@0
    87
EXPORT_C int Tbufc16ToChar(TDesC& aSrc, char* aDes, int& n_size)
sl@0
    88
{
sl@0
    89
    int retval = ESuccess;
sl@0
    90
    unsigned int ilen = 0 ;
sl@0
    91
    ilen = aSrc.Length();
sl@0
    92
    wchar_t* temp16String = NULL;
sl@0
    93
    int minusone = -1;
sl@0
    94
    
sl@0
    95
    if (0 == ilen)
sl@0
    96
    {
sl@0
    97
    	return EDescriptorNoData;
sl@0
    98
    } 
sl@0
    99
    else if (!aDes)
sl@0
   100
	{
sl@0
   101
		return EInvalidPointer;		
sl@0
   102
	}
sl@0
   103
	else if(n_size < ilen*2+1)
sl@0
   104
    {
sl@0
   105
    	n_size = ilen*2 + 1;
sl@0
   106
    	return EInvalidSize;
sl@0
   107
    }
sl@0
   108
sl@0
   109
    temp16String = new wchar_t [ilen+1];
sl@0
   110
	if (!temp16String)
sl@0
   111
	{
sl@0
   112
		return EInsufficientSystemMemory;
sl@0
   113
	}
sl@0
   114
	
sl@0
   115
	wmemcpy(temp16String, (const wchar_t *)aSrc.Ptr(), ilen);
sl@0
   116
	temp16String[ilen] = L'\0'; 	
sl@0
   117
		
sl@0
   118
	if(minusone != wcstombs(aDes, (const wchar_t*)temp16String, ilen*2))
sl@0
   119
	{
sl@0
   120
	     aDes[ilen*2] = '\0';    
sl@0
   121
	}
sl@0
   122
	else 
sl@0
   123
	{
sl@0
   124
		retval = EInvalidWCSSequence;
sl@0
   125
	}
sl@0
   126
	
sl@0
   127
    delete []temp16String;
sl@0
   128
	return retval;	
sl@0
   129
}
sl@0
   130
sl@0
   131
 /**
sl@0
   132
   * Converts a descriptor of type TLitc16 to character stream
sl@0
   133
   *
sl@0
   134
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
   135
   * reference to the character sream where the result of conversion 
sl@0
   136
   * is stored , n_size specifies the  conversion size of the string
sl@0
   137
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -2 is EInvalidSize
sl@0
   138
   * -3 is EDescriptorNoData , -4 is EInvalidPointer, -8 is EInvalidWCSSequence)
sl@0
   139
   */
sl@0
   140
sl@0
   141
EXPORT_C int Tlitc16ToChar(const TDesC16& aSrc, char* aDes, int& n_size)
sl@0
   142
{
sl@0
   143
    unsigned int ilen = 0;
sl@0
   144
    int retval = ESuccess;
sl@0
   145
    ilen = aSrc.Length();
sl@0
   146
    wchar_t* temp16String = NULL;
sl@0
   147
    int minusone = -1;
sl@0
   148
    
sl@0
   149
    if (0 == ilen )
sl@0
   150
    {
sl@0
   151
    	return EDescriptorNoData;
sl@0
   152
    }
sl@0
   153
    else if ( !aDes )
sl@0
   154
    {
sl@0
   155
        return EInvalidPointer;
sl@0
   156
    }
sl@0
   157
    else if (n_size < ilen*2 +1)
sl@0
   158
    {
sl@0
   159
    	n_size = ilen*2 + 1;
sl@0
   160
    	return EInvalidSize;
sl@0
   161
    }
sl@0
   162
      
sl@0
   163
  	
sl@0
   164
  	temp16String = new wchar_t [ilen+1];
sl@0
   165
	if (!temp16String)
sl@0
   166
	{
sl@0
   167
		return EInsufficientSystemMemory;
sl@0
   168
	}
sl@0
   169
	
sl@0
   170
	wmemcpy(temp16String, (const wchar_t*)aSrc.Ptr(), ilen);
sl@0
   171
	temp16String[ilen] = L'\0'; 
sl@0
   172
	
sl@0
   173
    if(minusone != wcstombs(aDes, temp16String, ilen*2))
sl@0
   174
	{
sl@0
   175
	     aDes[ilen*2] = '\0';    
sl@0
   176
	}
sl@0
   177
	else 
sl@0
   178
	{
sl@0
   179
		retval = EInvalidWCSSequence;
sl@0
   180
	}
sl@0
   181
	
sl@0
   182
	delete []temp16String;
sl@0
   183
	return retval;
sl@0
   184
}
sl@0
   185
sl@0
   186
 /**
sl@0
   187
   * Converts a descriptor of type TPtr16 to character stream
sl@0
   188
   *
sl@0
   189
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
   190
   * reference to the character sream where the result of conversion 
sl@0
   191
   * is stored , n_size specifies the conversion size of the string
sl@0
   192
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
   193
   * -2 is EInvalidSize , -4 is EInvalidPointer, -8 is EInvalidWCSSequence)
sl@0
   194
   */
sl@0
   195
sl@0
   196
EXPORT_C int Tptr16ToCharp (const TDes16& aSrc, char* aDes, int& n_size)
sl@0
   197
{
sl@0
   198
    unsigned int ilen = 0 ;
sl@0
   199
    int retval = ESuccess;
sl@0
   200
    ilen = aSrc.Length();
sl@0
   201
    wchar_t* temp16String = NULL;
sl@0
   202
    int minusone = -1;
sl@0
   203
    
sl@0
   204
	if ( !aDes )
sl@0
   205
    {
sl@0
   206
        return EInvalidPointer;
sl@0
   207
    }
sl@0
   208
    else if (n_size < ilen*2+1)
sl@0
   209
    {
sl@0
   210
    	n_size = ilen*2 + 1;
sl@0
   211
    	return EInvalidSize;
sl@0
   212
    }
sl@0
   213
    
sl@0
   214
    temp16String = new wchar_t [ilen+1];
sl@0
   215
	if (!temp16String)
sl@0
   216
	{
sl@0
   217
		return EInsufficientSystemMemory;
sl@0
   218
	}
sl@0
   219
	
sl@0
   220
	wmemcpy(temp16String, (const wchar_t *)aSrc.Ptr(), ilen);
sl@0
   221
	temp16String[ilen] = L'\0'; 
sl@0
   222
	
sl@0
   223
	if(minusone != wcstombs(aDes, temp16String, ilen*2))
sl@0
   224
	{
sl@0
   225
	     aDes[ilen*2] = '\0';    
sl@0
   226
	}
sl@0
   227
	else 
sl@0
   228
	{
sl@0
   229
		retval = EInvalidWCSSequence;
sl@0
   230
	}
sl@0
   231
	
sl@0
   232
	delete []temp16String;
sl@0
   233
	return retval;
sl@0
   234
}
sl@0
   235
sl@0
   236
 /**
sl@0
   237
   * Converts a descriptor of type TPtrC16 to character stream
sl@0
   238
   *
sl@0
   239
   * @param aSrc is the descriptor to be converted , aDes is the
sl@0
   240
   * reference to the  character sream where the result of conversion 
sl@0
   241
   * is stored , n_size specifies the conversion size of the string
sl@0
   242
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
   243
   * -2 is EInvalidSize , -4 is EInvalidPointer)
sl@0
   244
   */
sl@0
   245
sl@0
   246
EXPORT_C int Tptrc16ToCharp(TPtrC16& aSrc, char* aDes, int& n_size)
sl@0
   247
{
sl@0
   248
    int retval = ESuccess;
sl@0
   249
    unsigned int ilen = aSrc.Length();
sl@0
   250
    int minusone = -1;
sl@0
   251
    wchar_t* temp16String = NULL;
sl@0
   252
     
sl@0
   253
	if(!aDes)
sl@0
   254
    {
sl@0
   255
        return EInvalidPointer;
sl@0
   256
    }
sl@0
   257
    else if (n_size < ilen*2+1)
sl@0
   258
    {
sl@0
   259
    	n_size = ilen*2+1;
sl@0
   260
    	return EInvalidSize;
sl@0
   261
    }
sl@0
   262
       
sl@0
   263
    temp16String = new wchar_t [ilen+1];
sl@0
   264
	if (!temp16String)
sl@0
   265
	{
sl@0
   266
		return EInsufficientSystemMemory;
sl@0
   267
	}
sl@0
   268
	
sl@0
   269
	wmemcpy(temp16String, (const wchar_t *)aSrc.Ptr(), ilen);
sl@0
   270
	temp16String[ilen] = L'\0'; 
sl@0
   271
	
sl@0
   272
	if(minusone != wcstombs(aDes, temp16String, ilen*2))
sl@0
   273
	{
sl@0
   274
	     aDes[ilen*2] = '\0';    
sl@0
   275
	}
sl@0
   276
	else 
sl@0
   277
	{
sl@0
   278
		retval = EInvalidWCSSequence;
sl@0
   279
	}
sl@0
   280
	
sl@0
   281
	delete []temp16String;
sl@0
   282
	return retval;
sl@0
   283
}
sl@0
   284
sl@0
   285
 /**
sl@0
   286
   * Converts a descriptor of type RBuf16 to character stream
sl@0
   287
   *
sl@0
   288
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
   289
   * reference to the character sream where the result of conversion 
sl@0
   290
   * is stored , n_size specifies the conversion size of the string
sl@0
   291
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
   292
   * -2 is EInvalidSize , -4 is EInvalidPointer)
sl@0
   293
   */
sl@0
   294
sl@0
   295
EXPORT_C int Rbuf16ToChar(TDes16& aSrc, char* aDes, int& n_size)
sl@0
   296
{
sl@0
   297
    unsigned int ilen = aSrc.Length();
sl@0
   298
    int retval = ESuccess;
sl@0
   299
    wchar_t* temp16String = NULL;
sl@0
   300
    int minusone = -1;
sl@0
   301
    
sl@0
   302
    if (0 == ilen)
sl@0
   303
    {
sl@0
   304
    	return EDescriptorNoData;	
sl@0
   305
    } 
sl@0
   306
    else if(!aDes)
sl@0
   307
	{
sl@0
   308
		return EInvalidPointer;
sl@0
   309
	}
sl@0
   310
	else if (n_size < ilen*2+1)
sl@0
   311
	{
sl@0
   312
		n_size =  ilen*2+1;
sl@0
   313
		return EInvalidSize;
sl@0
   314
	}
sl@0
   315
    
sl@0
   316
    temp16String = new wchar_t [ilen+1];
sl@0
   317
	if (!temp16String)
sl@0
   318
	{
sl@0
   319
		return EInsufficientSystemMemory;
sl@0
   320
	}
sl@0
   321
	
sl@0
   322
	wmemcpy(temp16String,(const wchar_t *) aSrc.Ptr(), ilen);
sl@0
   323
	temp16String[ilen] = L'\0'; 
sl@0
   324
	
sl@0
   325
	if(minusone != wcstombs(aDes, temp16String, ilen*2))
sl@0
   326
	{
sl@0
   327
	     aDes[ilen*2] = '\0';    
sl@0
   328
	}
sl@0
   329
	else 
sl@0
   330
	{
sl@0
   331
		retval = EInvalidWCSSequence;
sl@0
   332
	}
sl@0
   333
	
sl@0
   334
    delete []temp16String;
sl@0
   335
	return retval;
sl@0
   336
}