utilitylibraries/libutils/src/wstringtodescriptor16.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 wstring to Descriptor16 conversions
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "libutils.h"
       
    21   
       
    22 
       
    23 
       
    24  /**
       
    25    * Converts a wstring to a descriptor of type TBufc16
       
    26    *
       
    27    * @param aSrc is the wstring to be converted , aDes is the 
       
    28    * reference to the descriptor where the result of conversion 
       
    29    * is stored 
       
    30    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
    31    * -3 is EStringNoData )
       
    32    */
       
    33 
       
    34 EXPORT_C int WstringToTbuf16(wstring& aSrc, TDes16& aDes)
       
    35 {
       
    36     int retval = ESuccess;
       
    37     const wchar_t* wcharString = aSrc.c_str();
       
    38     
       
    39     if (L'\0' == wcharString[0])
       
    40     {
       
    41     	return EStringNoData;
       
    42     }
       
    43     
       
    44     int len= wcslen(wcharString);
       
    45     if ((wcslen(wcharString)) > aDes.MaxLength())
       
    46     if (len > aDes.MaxLength())
       
    47     {
       
    48     	return EInsufficientMemory;
       
    49     }
       
    50     
       
    51 	aDes.Copy((const TUint16*)wcharString);
       
    52 	
       
    53     return retval;	
       
    54 }
       
    55 
       
    56  /**
       
    57    * Converts a wstring to a descriptor of type TPtr16
       
    58    *
       
    59    * @param aSrc is the wstring to be converted , aDes is the 
       
    60    * reference to the descriptor where the result of conversion 
       
    61    * is stored 
       
    62    * @return Status code (0 is ESuccess, -3 is EStringNoData)
       
    63    */
       
    64 
       
    65 EXPORT_C int WstringToTptr16(wstring& aSrc, TPtr16& aDes )
       
    66 {
       
    67     const wchar_t* wcharString = aSrc.c_str();
       
    68     unsigned int wlen = 0 , maxlen =0; 
       
    69     
       
    70     if (L'\0' == wcharString[0])
       
    71     {
       
    72     	return EStringNoData;
       
    73     }
       
    74     
       
    75     wlen = wcslen(wcharString);
       
    76     maxlen = aDes.MaxLength();
       
    77     
       
    78     if (maxlen < wlen)
       
    79     {
       
    80     	return EInsufficientMemory;
       
    81     }
       
    82     
       
    83 	aDes.Set((unsigned short *)wcharString, wlen, maxlen);
       
    84 	return ESuccess;
       
    85 }
       
    86 
       
    87  /**
       
    88    * Converts a wstring to a descriptor of type TPtrC16
       
    89    *
       
    90    * @param aSrc is the wstring to be converted , aDes is the 
       
    91    * reference to the descriptor where the result of conversion 
       
    92    * is stored 
       
    93    * @return Status code (0 is ESuccess,-3 is EStringNoData  )
       
    94    */
       
    95 
       
    96 EXPORT_C int WstringToTptrc16(wstring& aSrc, TPtrC16& aDes)
       
    97 {
       
    98   
       
    99     const wchar_t* wcharString = aSrc.c_str();
       
   100     
       
   101     if (L'\0' == wcharString[0])
       
   102     {
       
   103     	return EStringNoData;
       
   104     }
       
   105     
       
   106 	aDes.Set((TUint16 *)(wcharString));
       
   107 	return ESuccess;
       
   108 }
       
   109 
       
   110  /**
       
   111    * Converts a wstring to a descriptor of type HBufc16
       
   112    *
       
   113    * @param aSrc is the Wstring to be converted , aDes is the 
       
   114    * reference to the descriptor where the result of conversion 
       
   115    * is stored 
       
   116    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
   117    * -3 is EStringNoData )
       
   118    */
       
   119 
       
   120 EXPORT_C int WstringToHbufc16(wstring& aSrc, HBufC16* aDes)
       
   121 {
       
   122     unsigned int ilendes = 0; 
       
   123     const wchar_t* wcharString = aSrc.c_str();
       
   124     
       
   125     if (L'\0' == wcharString[0])
       
   126     {
       
   127     	return EStringNoData;
       
   128     }
       
   129     else if (!aDes)
       
   130     {
       
   131     	return EInvalidPointer;
       
   132     }
       
   133     
       
   134     ilendes = aDes->Length();
       
   135     
       
   136     if (0 == ilendes)
       
   137     {
       
   138     	return EUseNewMaxL;
       
   139     }
       
   140     else if (ilendes < wcslen(wcharString))
       
   141     {
       
   142     	return EInsufficientMemory;
       
   143     }
       
   144 
       
   145 	*aDes = (TUint16*)wcharString;
       
   146 	
       
   147 	return ESuccess;	
       
   148 }
       
   149  
       
   150  /**
       
   151    * Converts a wstring to a descriptor of type RBufc16
       
   152    *
       
   153    * @param aSrc is the wstring to be converted , aDes is the 
       
   154    * reference to the descriptor where the result of conversion 
       
   155    * is stored 
       
   156    * @return Status code (0 is ESuccess , -3 is EStringNoData )
       
   157    */
       
   158 
       
   159 EXPORT_C int WstringToRbuf16(const wstring& aSrc, RBuf16& aDes)
       
   160 {
       
   161     unsigned int wlen =0;
       
   162     int retval = ESuccess;
       
   163     const wchar_t* wcharString = aSrc.c_str();
       
   164     
       
   165     if (L'\0' == wcharString[0])
       
   166     {
       
   167     	return EStringNoData;
       
   168     }
       
   169     
       
   170     wlen = wcslen(wcharString);
       
   171     
       
   172     if (KErrNone == aDes.Create(wlen))
       
   173     {
       
   174         aDes.Copy((const unsigned short *)wcharString, wlen);	
       
   175     }
       
   176     else 
       
   177     {
       
   178     	retval = EInsufficientSystemMemory;
       
   179     }
       
   180 	
       
   181 	return retval;
       
   182 }