utilitylibraries/libutils/src/descriptor16tostring.cpp
changeset 0 e4d67989cc36
child 34 5fae379060a7
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Contains the source for Descriptor16 to string conversions
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "libutils.h"
       
    21   
       
    22 
       
    23 
       
    24  /**
       
    25    * Converts a descriptor of type TBuf16 to string datatype
       
    26    *
       
    27    * @param aSrc is the descriptor to be converted , aDes is the 
       
    28    * reference to the string to which the result of conversion 
       
    29    * is stored ,
       
    30    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory)
       
    31    */
       
    32 
       
    33 EXPORT_C int Tbuf16ToString(TDes16& aSrc, string& aDes)
       
    34 {
       
    35     unsigned int ilen = aSrc.Length(); 
       
    36     int retval = ESuccess;
       
    37     int minusone = -1;
       
    38     char* charString = new char[ilen*2+1];
       
    39     
       
    40     wchar_t *wcharString = new wchar_t[ilen+1];
       
    41     
       
    42     if (!charString || !wcharString)
       
    43     {
       
    44     	return EInsufficientSystemMemory;
       
    45     }
       
    46     
       
    47     wmemcpy(wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
    48     wcharString[ilen] = L'\0';
       
    49     
       
    50     if(minusone != wcstombs(charString, wcharString, ilen*2))
       
    51     {
       
    52     	charString[ilen*2] = '\0';
       
    53         aDes.assign(charString);
       
    54     }
       
    55     else 
       
    56     {
       
    57     	retval = EInvalidWCSSequence;
       
    58     }
       
    59     
       
    60     delete []charString;
       
    61 	delete []wcharString;
       
    62 	
       
    63 	return retval;
       
    64 }
       
    65 
       
    66  /**
       
    67    * Converts a descriptor of type TBufC16 to string datatype
       
    68    *
       
    69    * @param aSrc is the descriptor to be converted , aDes is the 
       
    70    * reference to the string to which the result of conversion 
       
    71    * is stored ,
       
    72    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory)
       
    73    */
       
    74 
       
    75 EXPORT_C int Tbufc16ToString(TDesC16& aSrc, string& aDes)
       
    76 {
       
    77     int ilen = aSrc.Length(), retval = ESuccess;
       
    78     int minusone = -1;
       
    79     char* charString = new char[ilen*2+1];
       
    80     wchar_t *wcharString = new wchar_t[ilen+1];
       
    81     
       
    82     if (!charString || !wcharString)
       
    83     {
       
    84     	return EInsufficientSystemMemory;
       
    85     }
       
    86     
       
    87     wmemcpy(wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
    88     wcharString[ilen] = L'\0';
       
    89    
       
    90     if(minusone != wcstombs(charString, wcharString, ilen*2))
       
    91     {
       
    92     	charString[ilen*2] = '\0';
       
    93         aDes.assign(charString);
       
    94     }
       
    95     else 
       
    96     {
       
    97     	retval = EInvalidWCSSequence;
       
    98     }
       
    99     
       
   100     delete []charString;
       
   101 	delete []wcharString;
       
   102 	
       
   103 	return retval;
       
   104 }
       
   105 
       
   106  /**
       
   107    * Converts a descriptor of type TPtr16 to string datatype
       
   108    *
       
   109    * @param aSrc is the descriptor to be converted , aDes is the 
       
   110    * reference to the string to which the result of conversion 
       
   111    * is stored , 
       
   112    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory)
       
   113    */
       
   114 
       
   115 EXPORT_C int Tptr16ToString (TDes16& aSrc, string& aDes)
       
   116 {
       
   117     int retval = ESuccess;
       
   118     unsigned int ilen= aSrc.Length();
       
   119 	  int minusone = -1;
       
   120 	  char* charString = new char[ilen*2+1];
       
   121     wchar_t *wcharString = new wchar_t[ilen+1];
       
   122     
       
   123     if (!charString || !wcharString)
       
   124     {
       
   125     	return EInsufficientSystemMemory;
       
   126     }
       
   127     
       
   128     wmemcpy(wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
   129     wcharString[ilen] = L'\0';
       
   130    
       
   131     
       
   132 	if(minusone != wcstombs(charString, wcharString, ilen*2))
       
   133 	{
       
   134 	    charString[ilen*2] = '\0';
       
   135 		aDes.assign(charString);  	
       
   136 	}
       
   137 	else
       
   138 	{
       
   139 		retval = EInvalidWCSSequence;
       
   140 	}
       
   141     
       
   142 	delete []charString;
       
   143 	delete []wcharString;
       
   144 	
       
   145 	return retval;
       
   146 }
       
   147 
       
   148  /**
       
   149    * Converts a descriptor of type TPtrC16 to string datatype
       
   150    *
       
   151    * @param aSrc is the descriptor to be converted , aDes is the 
       
   152    * reference to the string to which the result of conversion 
       
   153    * is stored , 
       
   154    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory)
       
   155    */
       
   156 
       
   157 EXPORT_C int Tptrc16ToString (const TDesC16& aSrc, string& aDes)
       
   158 {
       
   159     int retval = ESuccess;	
       
   160 	  int ilen= aSrc.Length();
       
   161 	int minusone = -1;
       
   162 	  char* buf = new char[ilen*2 +1];
       
   163     wchar_t *wcharString = new wchar_t[ilen+1];
       
   164     
       
   165     if (!buf || !wcharString)
       
   166     {
       
   167     	return EInsufficientSystemMemory;
       
   168     }
       
   169     
       
   170     wmemcpy(wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
   171     wcharString[ilen] = L'\0';
       
   172    
       
   173 	if(minusone != wcstombs(buf, wcharString, ilen*2))
       
   174 	{
       
   175 	    buf[ilen*2] = '\0';
       
   176 		aDes.assign(buf);  	
       
   177 	}
       
   178 	else
       
   179 	{
       
   180 		retval =  EInvalidWCSSequence;
       
   181 	}
       
   182     
       
   183 	delete []buf;
       
   184 	delete []wcharString;
       
   185 	
       
   186 	return retval;
       
   187 }
       
   188 
       
   189  /**
       
   190    * Converts a descriptor of type HbufC16 to string datatype
       
   191    *
       
   192    * @param aSrc is the descriptor to be converted , aDes is the 
       
   193    * reference to the string to which the result of conversion 
       
   194    * is stored , n_size specifies the conversion size of the string 
       
   195    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory)
       
   196    * -2 is EInvalidSize , -5 is EDescriptorNoData)
       
   197    */
       
   198 
       
   199 EXPORT_C int Hbufc16ToString(HBufC16* aSrc, string& aDes, int& n_size)
       
   200 {
       
   201     int retval = ESuccess;
       
   202     unsigned int ilen=0;
       
   203     int minusone = -1;
       
   204     
       
   205   	if(!aSrc)
       
   206     {
       
   207     	return EInvalidPointer;
       
   208     }
       
   209     else
       
   210     {
       
   211     	ilen = aSrc->Size();
       
   212     	if (0 == ilen)
       
   213     	{
       
   214     		return EDescriptorNoData;
       
   215     	}
       
   216     	else if (n_size < ilen)
       
   217     	{
       
   218     		n_size = ilen; 
       
   219     		return EInvalidSize; 		
       
   220     	}
       
   221     }
       
   222     
       
   223 	char* buf = new char[ilen*2 +1];
       
   224     wchar_t *wcharString = new wchar_t[ilen+1];
       
   225     
       
   226     if (!buf || !wcharString)
       
   227     {
       
   228     	return EInsufficientSystemMemory;
       
   229     }
       
   230     
       
   231     wmemcpy(wcharString, (const wchar_t*)aSrc->Ptr(), ilen);
       
   232     wcharString[ilen] = L'\0';
       
   233    
       
   234 	
       
   235 	if(minusone != wcstombs(buf, wcharString, ilen*2))
       
   236 	{
       
   237 	    buf[ilen*2] = '\0';
       
   238 	    aDes.append(buf, ilen*2);		
       
   239 		
       
   240 	}
       
   241 	else 
       
   242 	{
       
   243 		retval = EInvalidWCSSequence;
       
   244 	}
       
   245 	
       
   246 	delete []buf;
       
   247 	delete []wcharString;
       
   248 		
       
   249 	return retval;
       
   250 }
       
   251 
       
   252  /**
       
   253    * Converts a descriptor of type RBuf16 to string datatype
       
   254    *
       
   255    * @param aSrc is the descriptor to be converted , aDes is the 
       
   256    * reference to the string to which the result of conversion 
       
   257    * is stored , 
       
   258    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory
       
   259    * -5 is EDescriptorNoData)
       
   260    */
       
   261 
       
   262 EXPORT_C int Rbuf16ToString(TDes16& aSrc, string& aDes)
       
   263 {
       
   264     unsigned int ilen = aSrc.Length();
       
   265     int retval = ESuccess ;
       
   266     int minusone = -1;
       
   267     if (0 == ilen)
       
   268     {
       
   269     	return EDescriptorNoData;
       
   270     }
       
   271     
       
   272     char* buf = new char[ilen*2 +1];
       
   273     wchar_t *wcharString = new wchar_t[ilen+1];
       
   274     
       
   275     if (!buf || !wcharString)
       
   276     {
       
   277     	return EInsufficientSystemMemory;
       
   278     }
       
   279     
       
   280     wmemcpy(wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
   281     wcharString[ilen] = L'\0';
       
   282    
       
   283 
       
   284     if(minusone != wcstombs(buf, wcharString, ilen*2))
       
   285 	{
       
   286 	    buf[ilen*2] = '\0';
       
   287 	    aDes.assign(buf, ilen*2);		
       
   288 		
       
   289 	}
       
   290 	else 
       
   291 	{
       
   292 		retval = EInvalidWCSSequence;
       
   293 	}
       
   294 	
       
   295     delete []buf;
       
   296     delete []wcharString;
       
   297     
       
   298 	return retval;
       
   299 }
       
   300 
       
   301  /**
       
   302    * Converts a descriptor of type TLit16 to string datatype
       
   303    *
       
   304    * @param aSrc is the descriptor to be converted , aDes is the 
       
   305    * reference to the string to which the result of conversion 
       
   306    * is stored , 
       
   307    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory
       
   308    * -5 is EDescriptorNoData)
       
   309    */
       
   310 
       
   311 EXPORT_C int Tlit16ToString(const TDesC16& aSrc, string& aDes)
       
   312 {   
       
   313    unsigned int ilen = 0; 
       
   314    int retval = ESuccess;
       
   315    ilen = aSrc.Length();
       
   316    int minusone = -1;
       
   317    
       
   318    if (0 == ilen)
       
   319    {
       
   320    		return EDescriptorNoData;
       
   321    }
       
   322 
       
   323    char* buf = new char[ilen*2 +1];
       
   324    wchar_t *wcharString = new wchar_t[ilen+1];
       
   325     
       
   326    if (!buf || !wcharString)
       
   327    {
       
   328        return EInsufficientSystemMemory;
       
   329    }
       
   330     
       
   331    wmemcpy(wcharString, (const wchar_t*)aSrc.Ptr(), ilen);
       
   332    wcharString[ilen] = L'\0';
       
   333    
       
   334 
       
   335    if(minusone != wcstombs(buf, wcharString, ilen*2))
       
   336    {
       
   337  	    buf[ilen*2] = '\0';
       
   338  	    aDes.assign(buf);
       
   339  	    
       
   340    }
       
   341    else 
       
   342    {
       
   343 	    retval = EInvalidWCSSequence;
       
   344    }
       
   345 
       
   346    delete []buf;
       
   347    delete []wcharString;	
       
   348    return retval; 
       
   349 }