os/ossrv/utilitylibraries/libutils/src/stringtodescriptor16.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
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 string 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
 /**
sl@0
    25
   * Converts a string to a descriptor of type TBuf16
sl@0
    26
   *
sl@0
    27
   * @param aSrc is the string to be converted , aDes is the 
sl@0
    28
   * reference to the descriptor where the result of conversion 
sl@0
    29
   * is stored,n_size specifies the conversion size of the char array 
sl@0
    30
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
    31
   * -3 is EStringNoData, -9 is EInsufficientSystemMemory)
sl@0
    32
   */
sl@0
    33
EXPORT_C  int StringToTbuf16(string& aSrc, TDes16& aDes)
sl@0
    34
{
sl@0
    35
  int retval = ESuccess;
sl@0
    36
	const char* charString = aSrc.c_str();
sl@0
    37
	int ilen = 0; 
sl@0
    38
	int minusone = -1;
sl@0
    39
	
sl@0
    40
    if('\0' == charString[0])
sl@0
    41
    {
sl@0
    42
    	return EStringNoData;
sl@0
    43
    }
sl@0
    44
        
sl@0
    45
    ilen = strlen(charString);
sl@0
    46
    
sl@0
    47
	if (ilen > aDes.MaxLength())
sl@0
    48
	{
sl@0
    49
		return EInsufficientMemory;
sl@0
    50
	}
sl@0
    51
	
sl@0
    52
	wchar_t* WcharString = new wchar_t[ilen];
sl@0
    53
	
sl@0
    54
	if(!WcharString)
sl@0
    55
	{
sl@0
    56
		return EInsufficientSystemMemory;
sl@0
    57
	}
sl@0
    58
	
sl@0
    59
	if(minusone == mbstowcs(WcharString, (const char*)charString, ilen))
sl@0
    60
	{
sl@0
    61
		retval = EInvalidMBSSequence;
sl@0
    62
	}
sl@0
    63
	else
sl@0
    64
	{
sl@0
    65
			WcharString[ilen] = L'\0';
sl@0
    66
		aDes.Copy((unsigned short*)WcharString,ilen);
sl@0
    67
	}
sl@0
    68
		
sl@0
    69
	delete []WcharString;	
sl@0
    70
	return retval;	
sl@0
    71
}
sl@0
    72
sl@0
    73
 /**
sl@0
    74
   * Converts a string to a descriptor of type Tptr16
sl@0
    75
   *
sl@0
    76
   * @param aSrc is the string to be converted , aDes is the 
sl@0
    77
   * reference to the descriptor where the result of conversion 
sl@0
    78
   * is stored 
sl@0
    79
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
    80
   * -3 is EStringNoData , -4 is EInvalidPointer)
sl@0
    81
   */
sl@0
    82
sl@0
    83
EXPORT_C  int StringToTptr16 (string& aSrc, wchar_t *wPtr, TPtr16& aDes)
sl@0
    84
{
sl@0
    85
    int retval = ESuccess;
sl@0
    86
    int ilen = 0, ideslen = 0;
sl@0
    87
    const char* charString = aSrc.c_str();
sl@0
    88
    int minusone = -1;
sl@0
    89
    
sl@0
    90
    if('\0' == charString[0])
sl@0
    91
    {
sl@0
    92
    	return EStringNoData;
sl@0
    93
    }
sl@0
    94
    else if(!wPtr)
sl@0
    95
    {
sl@0
    96
    	return EInvalidPointer;
sl@0
    97
    }
sl@0
    98
    
sl@0
    99
	ilen = strlen(charString);
sl@0
   100
	ideslen = aDes.MaxLength();
sl@0
   101
	
sl@0
   102
	if (ilen > ideslen)
sl@0
   103
	{
sl@0
   104
		return EInsufficientMemory;
sl@0
   105
	}
sl@0
   106
	
sl@0
   107
	if(minusone != mbstowcs(wPtr, (const char*)charString, ilen))
sl@0
   108
	{
sl@0
   109
		aDes.Set((unsigned short *)wPtr, ilen, ideslen);
sl@0
   110
	}		
sl@0
   111
	else
sl@0
   112
	{
sl@0
   113
		retval = EInvalidMBSSequence;
sl@0
   114
	}
sl@0
   115
	
sl@0
   116
	return retval;	     
sl@0
   117
}
sl@0
   118
sl@0
   119
 /**
sl@0
   120
   * Converts a string to a descriptor of type Tptrc16
sl@0
   121
   *
sl@0
   122
   * @param aSrc is the string to be converted , aDes is the 
sl@0
   123
   * reference to the descriptor  where the result of conversion 
sl@0
   124
   * is stored 
sl@0
   125
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
   126
   * -3 is EStringNoData , -4 is EInvalidPointer)
sl@0
   127
   */
sl@0
   128
sl@0
   129
EXPORT_C  int StringToTptrc16(string& aSrc, wchar_t* wptr, TPtrC16& aDes)
sl@0
   130
{
sl@0
   131
    unsigned int ilen = 0;
sl@0
   132
    int retval = ESuccess;
sl@0
   133
    const char* charString = aSrc.c_str();
sl@0
   134
    int minusone = -1;
sl@0
   135
    
sl@0
   136
    if (!wptr)
sl@0
   137
    {
sl@0
   138
    	return EInvalidPointer;
sl@0
   139
    }
sl@0
   140
    
sl@0
   141
    if('\0' == charString[0])
sl@0
   142
    {
sl@0
   143
    	return EStringNoData;
sl@0
   144
    }
sl@0
   145
    
sl@0
   146
	ilen = strlen(charString);
sl@0
   147
		
sl@0
   148
	if(minusone != mbstowcs(wptr, (const char*)charString, ilen ))
sl@0
   149
	{
sl@0
   150
		aDes.Set((TUint16 *)(wptr), ilen);	
sl@0
   151
	}
sl@0
   152
	else
sl@0
   153
	{
sl@0
   154
		retval = EInvalidMBSSequence;
sl@0
   155
	}
sl@0
   156
	
sl@0
   157
	return retval;	
sl@0
   158
}
sl@0
   159
sl@0
   160
 /**
sl@0
   161
   * Converts a string to a descriptor of type HBufc16
sl@0
   162
   *
sl@0
   163
   * @param aSrc is the string to be converted , aDes is the 
sl@0
   164
   * reference to the descriptor where the result of conversion 
sl@0
   165
   * is stored 
sl@0
   166
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
   167
   * -3 is EStringNoData, -4 is EInvalidPointer, -6 is EUseNewMaxL,
sl@0
   168
   * -9 is EInsufficientSystemMemory)
sl@0
   169
   */
sl@0
   170
sl@0
   171
EXPORT_C int StringToHbufc16(string& aSrc , HBufC16* aDes)
sl@0
   172
 {
sl@0
   173
    int retval = ESuccess;
sl@0
   174
    unsigned int ilen = 0, ilendes = 0; 
sl@0
   175
    const char* charString = aSrc.c_str();
sl@0
   176
    int minusone = -1;
sl@0
   177
   
sl@0
   178
    if('\0' == charString[0])
sl@0
   179
    {
sl@0
   180
    	return EStringNoData;
sl@0
   181
    }
sl@0
   182
    else if (!aDes)
sl@0
   183
    {
sl@0
   184
    	return EInvalidPointer;
sl@0
   185
    }
sl@0
   186
    
sl@0
   187
    ilen = strlen(charString);
sl@0
   188
    ilendes = aDes->Length();
sl@0
   189
    
sl@0
   190
    if (0 == ilendes)
sl@0
   191
    {
sl@0
   192
    	return EUseNewMaxL;
sl@0
   193
    }
sl@0
   194
    else if (ilendes < ilen)
sl@0
   195
    {
sl@0
   196
    	return EInsufficientMemory;
sl@0
   197
    }
sl@0
   198
    
sl@0
   199
    wchar_t* WcharString = new wchar_t[ilen];
sl@0
   200
	
sl@0
   201
	if(!WcharString)
sl@0
   202
	{
sl@0
   203
		return EInsufficientSystemMemory;
sl@0
   204
	}
sl@0
   205
	 
sl@0
   206
	if(minusone == mbstowcs(WcharString, (const char*)charString, ilen))
sl@0
   207
	{
sl@0
   208
		retval = EInvalidMBSSequence;
sl@0
   209
	}
sl@0
   210
	else
sl@0
   211
	{
sl@0
   212
	    TPtrC16 temp((unsigned short*)WcharString, ilen);
sl@0
   213
		*aDes = temp;
sl@0
   214
	} 
sl@0
   215
	
sl@0
   216
	delete[] WcharString;
sl@0
   217
	return retval;
sl@0
   218
}
sl@0
   219
 
sl@0
   220
 /**
sl@0
   221
   * Converts a string to a descriptor of type RBufc16
sl@0
   222
   *
sl@0
   223
   * @param aSrc is the string to be converted , aDes is the 
sl@0
   224
   * reference to the descriptor where the result of conversion 
sl@0
   225
   * is stored 
sl@0
   226
   * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
sl@0
   227
   * -3 is EStringNoData, -7 is EInvalidMBSSequence, -9 is EInsufficientSystemMemory)
sl@0
   228
   */
sl@0
   229
sl@0
   230
EXPORT_C int StringToRbuf16(const string& aSrc, RBuf16& aDes)
sl@0
   231
{
sl@0
   232
    int retval = ESuccess;
sl@0
   233
    unsigned int ilen = 0;
sl@0
   234
    int minusone = -1;
sl@0
   235
    
sl@0
   236
    const char* charString = aSrc.c_str();
sl@0
   237
    
sl@0
   238
    if('\0' == charString[0])
sl@0
   239
    {
sl@0
   240
    	return EStringNoData;
sl@0
   241
    }
sl@0
   242
    
sl@0
   243
    ilen = strlen(charString);
sl@0
   244
    
sl@0
   245
	wchar_t* buf = new wchar_t[ilen];
sl@0
   246
    
sl@0
   247
    if (!buf)
sl@0
   248
    {
sl@0
   249
    	return EInsufficientMemory;
sl@0
   250
    }
sl@0
   251
sl@0
   252
	if(minusone != mbstowcs(buf, charString, ilen))
sl@0
   253
	{
sl@0
   254
	  aDes.Copy((const unsigned short *)buf, ilen);		
sl@0
   255
	}
sl@0
   256
	else
sl@0
   257
	{
sl@0
   258
	  retval = EInvalidMBSSequence;		
sl@0
   259
	}
sl@0
   260
	
sl@0
   261
	delete []buf;
sl@0
   262
  return retval;	
sl@0
   263
}