os/ossrv/utilitylibraries/libutils/src/descriptor8tochar.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 Descriptor8 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 TBuf8 to char*
sl@0
    26
   * @param aSrc is the descriptor of type TBuf8 that is to be converted to char*
sl@0
    27
   * @param aDes is a char* that will hold the result after conversion. Care should be taken to 
sl@0
    28
   * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
sl@0
    29
   * being allocated to the char* is being made known using n_size
sl@0
    30
   * @param n_size is the size of the char*. This should have a minimum value equal to size of the 
sl@0
    31
   * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
sl@0
    32
   */
sl@0
    33
sl@0
    34
EXPORT_C int Tbuf8ToChar(TDes8& aSrc, char* aDes, int& n_size)
sl@0
    35
{	
sl@0
    36
    unsigned int ilen = aSrc.Length();
sl@0
    37
    
sl@0
    38
    if (0 == ilen)
sl@0
    39
    {
sl@0
    40
    	return EDescriptorNoData;
sl@0
    41
    }
sl@0
    42
	else if(!aDes)
sl@0
    43
	{
sl@0
    44
		return EInvalidPointer;
sl@0
    45
	}
sl@0
    46
	else if (n_size < (ilen+1))
sl@0
    47
	{
sl@0
    48
		n_size = ilen + 1;
sl@0
    49
		return EInvalidSize;
sl@0
    50
	}
sl@0
    51
sl@0
    52
	memcpy(aDes , (const char *)aSrc.Ptr(), ilen);
sl@0
    53
	
sl@0
    54
	aDes[ilen] = '\0';
sl@0
    55
		
sl@0
    56
	return ESuccess;
sl@0
    57
}
sl@0
    58
sl@0
    59
 /**
sl@0
    60
    * Converts a descriptor of type TBufC8 to char*
sl@0
    61
   * @param aSrc is the descriptor of type TBufC8 that is to be converted to char*
sl@0
    62
   * @param aDes is a char* that will hold the result after conversion. Care should be taken to 
sl@0
    63
   * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
sl@0
    64
   * being allocated to the char* is being made known using n_size
sl@0
    65
   * @param n_size is the size of the char*. This should have a minimum value equal to size of the 
sl@0
    66
   * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
sl@0
    67
   */
sl@0
    68
sl@0
    69
EXPORT_C int Tbufc8ToChar(TDesC8& aSrc, char* aDes, int& n_size)
sl@0
    70
{	
sl@0
    71
    unsigned int ilen = aSrc.Length();
sl@0
    72
    
sl@0
    73
    if (0 == ilen)
sl@0
    74
    {
sl@0
    75
    	return EDescriptorNoData;
sl@0
    76
    }
sl@0
    77
    else if(!aDes)
sl@0
    78
    {
sl@0
    79
    	return EInvalidPointer;
sl@0
    80
    }
sl@0
    81
    else if(n_size < ilen+1)
sl@0
    82
    {
sl@0
    83
    	n_size = ilen;
sl@0
    84
    	return EInvalidSize;
sl@0
    85
    }
sl@0
    86
    	
sl@0
    87
	memcpy(aDes, aSrc.Ptr(), ilen);
sl@0
    88
	aDes[ilen] = '\0';
sl@0
    89
        
sl@0
    90
    return ESuccess;
sl@0
    91
}
sl@0
    92
sl@0
    93
/**
sl@0
    94
   * Converts a descriptor of type TLitC8 to char*
sl@0
    95
   * @param aSrc is the descriptor of type TLitC8 that is to be converted to char*
sl@0
    96
   * @param aDes is a char* that will hold the result after conversion. Care should be taken to 
sl@0
    97
   * allocate sufficient amount of memory to char* in the calling function. The amount of memory that is
sl@0
    98
   * being allocated to the char* is being made known using n_size
sl@0
    99
   * @param n_size is the size of the char*. This should have a minimum value equal to size of the 
sl@0
   100
   * descriptor. Incase its less, the conversion is going to fail and returns the corresponding error code
sl@0
   101
   */
sl@0
   102
	
sl@0
   103
EXPORT_C int Tlitc8ToChar(const TDesC8& aSrc, char* aDes, int& n_size)
sl@0
   104
{   
sl@0
   105
    unsigned int ilen = aSrc.Length();
sl@0
   106
    
sl@0
   107
    if (0 == ilen )
sl@0
   108
    {
sl@0
   109
    	return EDescriptorNoData;
sl@0
   110
    }     
sl@0
   111
    else if(!aDes)
sl@0
   112
    {
sl@0
   113
    	return EInvalidPointer;
sl@0
   114
    }
sl@0
   115
    else if (n_size < ilen )
sl@0
   116
    {
sl@0
   117
    	n_size = ilen + 1;
sl@0
   118
    	return EInvalidSize;
sl@0
   119
    }
sl@0
   120
    
sl@0
   121
    memcpy(aDes, (char *)aSrc.Ptr(), ilen);
sl@0
   122
	aDes[ilen] = '\0';
sl@0
   123
	
sl@0
   124
	return ESuccess;	 
sl@0
   125
}
sl@0
   126
sl@0
   127
sl@0
   128
 /**
sl@0
   129
   * Converts a descriptor of type TPtr8 to character stream
sl@0
   130
   *
sl@0
   131
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
   132
   * reference to the character sream where the result of conversion 
sl@0
   133
   * is stored , n_size specifies the conversion size of the string 
sl@0
   134
   * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
sl@0
   135
   *  -4 is EInvalidPointer)
sl@0
   136
   */
sl@0
   137
EXPORT_C int Tptr8ToChar(const TDes8& aSrc, char* aDes, int& n_size)
sl@0
   138
{
sl@0
   139
    unsigned int ilen = aSrc.Length();
sl@0
   140
	
sl@0
   141
	if (0 == ilen)
sl@0
   142
    {
sl@0
   143
    	return EDescriptorNoData;	
sl@0
   144
    }
sl@0
   145
	else if ( !aDes )
sl@0
   146
    {
sl@0
   147
        return EInvalidPointer;
sl@0
   148
    }
sl@0
   149
    else if (n_size < ilen)
sl@0
   150
    {
sl@0
   151
    	n_size = ilen + 1;
sl@0
   152
    	return EInvalidSize;
sl@0
   153
    }
sl@0
   154
    
sl@0
   155
	memcpy(aDes , (const char *)aSrc.Ptr(), ilen);
sl@0
   156
	aDes[ilen] = '\0';
sl@0
   157
    
sl@0
   158
    return ESuccess;
sl@0
   159
}
sl@0
   160
sl@0
   161
 /**
sl@0
   162
   * Converts a descriptor of type TPtrC8 to character stream
sl@0
   163
   *
sl@0
   164
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
   165
   * reference to the character sream where the result of conversion 
sl@0
   166
   * is stored , n_size specifies the conversion size of the string 
sl@0
   167
   * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
sl@0
   168
   *  -4 is EInvalidPointer)
sl@0
   169
   */
sl@0
   170
EXPORT_C int Tptrc8ToCharp(TPtrC8& aSrc, char* aDes, int& n_size)
sl@0
   171
{
sl@0
   172
    unsigned int ilen = aSrc.Length();
sl@0
   173
	if (0 == ilen)
sl@0
   174
    {
sl@0
   175
    	return EDescriptorNoData;	
sl@0
   176
    }
sl@0
   177
	else if ( !aDes )
sl@0
   178
    {
sl@0
   179
        return EInvalidPointer;
sl@0
   180
    }
sl@0
   181
    else if (n_size < ilen)
sl@0
   182
    {
sl@0
   183
    	n_size = ilen + 1;
sl@0
   184
    	return EInvalidSize;
sl@0
   185
    }
sl@0
   186
	
sl@0
   187
	memcpy(aDes , aSrc.Ptr(), ilen);
sl@0
   188
	aDes[ilen] = '\0';
sl@0
   189
    
sl@0
   190
    return ESuccess;
sl@0
   191
}
sl@0
   192
sl@0
   193
 /**
sl@0
   194
   * Converts a descriptor of type RBuf8 to character stream
sl@0
   195
   *
sl@0
   196
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
   197
   * reference to the character sream where the result of conversion 
sl@0
   198
   * is stored , n_size specifies the conversion size of the string 
sl@0
   199
   * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
sl@0
   200
   *  -4 is EInvalidPointer , -5 is EDescriptorNoData)
sl@0
   201
   */
sl@0
   202
   
sl@0
   203
EXPORT_C int Rbuf8ToChar(TDes8& aSrc, char* aDes, int& n_size)
sl@0
   204
{
sl@0
   205
    unsigned int ilen = aSrc.Length();
sl@0
   206
    
sl@0
   207
    if (0 == ilen)
sl@0
   208
    {
sl@0
   209
    	return EDescriptorNoData;	
sl@0
   210
    } 
sl@0
   211
	else if ( !aDes )
sl@0
   212
    {
sl@0
   213
        return EInvalidPointer;
sl@0
   214
    }
sl@0
   215
    else if (n_size < ilen)
sl@0
   216
    {
sl@0
   217
    	n_size = ilen + 1;
sl@0
   218
    	return EInvalidSize;
sl@0
   219
    }
sl@0
   220
    
sl@0
   221
	memcpy (aDes,(char *)aSrc.Ptr(), ilen);
sl@0
   222
	aDes[ilen] = '\0';
sl@0
   223
sl@0
   224
	return ESuccess;
sl@0
   225
}
sl@0
   226
sl@0
   227
 /**
sl@0
   228
   * Converts a descriptor of type HBufC8 to character stream
sl@0
   229
   *
sl@0
   230
   * @param aSrc is the descriptor to be converted , aDes is the 
sl@0
   231
   * reference to the character sream where the result of conversion 
sl@0
   232
   * is stored , n_size specifies the conversion size of the string 
sl@0
   233
   * @return Status code (0 is ESuccess, -2 is EInvalidSize ,
sl@0
   234
   *  -4 is EInvalidPointer , -5 is EDescriptorNoData)
sl@0
   235
   */
sl@0
   236
sl@0
   237
EXPORT_C int Hbufc8ToChar(HBufC8 *aSrc, char* aDes, int& n_size)
sl@0
   238
{
sl@0
   239
    unsigned int ilen = 0;
sl@0
   240
    
sl@0
   241
    if ( !aDes || !aSrc)
sl@0
   242
    {
sl@0
   243
        return EInvalidPointer;
sl@0
   244
    }
sl@0
   245
    else 
sl@0
   246
    {
sl@0
   247
        ilen = aSrc->Length();
sl@0
   248
        if (0 == ilen)
sl@0
   249
        {
sl@0
   250
        	return EDescriptorNoData;		
sl@0
   251
        }
sl@0
   252
        else if (n_size < ilen)
sl@0
   253
    	{
sl@0
   254
    		n_size = ilen + 1;
sl@0
   255
    		return EInvalidSize;
sl@0
   256
    	}  	
sl@0
   257
    } 	
sl@0
   258
    
sl@0
   259
	memcpy (aDes,(char *)aSrc->Ptr(), ilen);
sl@0
   260
	aDes[ilen] = '\0';
sl@0
   261
sl@0
   262
	return ESuccess;
sl@0
   263
}