utilitylibraries/libutils/src/stringtodescriptor8.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 string to Descriptor8 conversions
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "libutils.h"
       
    21   
       
    22 
       
    23 
       
    24  /**
       
    25    * Converts a string to a descriptor of type TBuf8
       
    26    *
       
    27    * @param aSrc is the string 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 StringToTbuf8(string& aSrc, TDes8& aDes)
       
    35 {
       
    36     const char* charString = aSrc.c_str();
       
    37     
       
    38     if('\0' == charString[0])
       
    39     {
       
    40     	return EStringNoData;
       
    41     }
       
    42     
       
    43     if (aDes.MaxLength() < strlen(charString))
       
    44     {
       
    45     	return EInsufficientMemory;
       
    46     }
       
    47     
       
    48 	aDes = (const TUint8*)(charString);
       
    49 	     
       
    50 	return ESuccess;
       
    51 }
       
    52 
       
    53  /**
       
    54    * Converts a string to a descriptor of type Tptrc8
       
    55    *
       
    56    * @param aSrc is the string to be converted , aDes is the 
       
    57    * reference to the descriptor where the result of conversion 
       
    58    * is stored
       
    59    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
    60    * -3 is EStringNoData)
       
    61    */
       
    62 
       
    63 EXPORT_C int StringToTptrc8(string& aSrc, TPtrC8& aDes)
       
    64 {    
       
    65     const char* charString = aSrc.c_str();
       
    66     if ('\0' == charString[0])
       
    67     {
       
    68         return EStringNoData;
       
    69     }
       
    70   
       
    71     aDes.Set(TPtrC8((TUint8 *)(charString)));
       
    72     
       
    73     return ESuccess; 
       
    74 } 
       
    75 
       
    76  /**
       
    77    * Converts a string to a descriptor of type TPtr8
       
    78    *
       
    79    * @param aSrc is the string to be converted , aDes is the 
       
    80    * reference to the descriptor where the result of conversion 
       
    81    * is stored
       
    82    * @return Status code (0 is ESuccess, -3 is EStringNoData)
       
    83    */
       
    84 
       
    85 EXPORT_C  int StringToTptr8 (string& aSrc, TPtr8& aDes)
       
    86 {
       
    87     const char* charString = aSrc.c_str();
       
    88     
       
    89     if ('\0' == charString[0])
       
    90     {
       
    91     	return EStringNoData;
       
    92     }
       
    93     
       
    94     unsigned int ilen = strlen(charString);
       
    95     unsigned int ideslen = aDes.MaxLength();
       
    96     if(ilen > ideslen)
       
    97     {
       
    98         return EInsufficientMemory;
       
    99     }
       
   100     
       
   101     aDes.Set((unsigned char *)charString, ilen, ideslen);
       
   102 	
       
   103   	return ESuccess;	
       
   104 } 
       
   105 
       
   106  /**
       
   107    * Converts a string to a descriptor of type HBufc8
       
   108    *
       
   109    * @param aSrc is the string to be converted , aDes is the 
       
   110    * reference to the descriptor where the result of conversion 
       
   111    * is stored 
       
   112    * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, 
       
   113    * -3 is EStringNoData , -5 is EUSENEWMAXL )
       
   114    */
       
   115 
       
   116 EXPORT_C int StringToHbufc8(string& aSrc , HBufC8* aDes)
       
   117 {
       
   118     unsigned int ilendes = 0;
       
   119      
       
   120     const char* charString = aSrc.c_str();
       
   121     if ('\0' == charString[0])
       
   122     {
       
   123     	return EStringNoData;
       
   124     }
       
   125     else if (!aDes)
       
   126     {
       
   127     	return EInvalidPointer;
       
   128     }
       
   129 	
       
   130 	ilendes = aDes->Length();
       
   131 	if(0 == ilendes)
       
   132 	{
       
   133 		return EUseNewMaxL;
       
   134 	}
       
   135 	
       
   136 	if (ilendes < strlen(charString))
       
   137 	{
       
   138 		return EInsufficientMemory;
       
   139 	}
       
   140 	
       
   141 	*aDes = (const TUint8*)charString;
       
   142 	return ESuccess;
       
   143 }
       
   144 
       
   145  /**
       
   146    * Converts a string to a descriptor of type RBuf8
       
   147    *
       
   148    * @param aSrc is the string to be converted , aDes is the 
       
   149    * reference to the descriptor where the result of conversion 
       
   150    * is stored
       
   151    * @return Status code (0 is ESuccess,-3 is EStringNoData)
       
   152    */  
       
   153 
       
   154 EXPORT_C int StringToRbuf8(const string& aSrc, RBuf8& aDes)
       
   155 {
       
   156     int retval = ESuccess;
       
   157     const char* charString = aSrc.c_str();
       
   158     if ('\0' == charString[0])
       
   159     {
       
   160         return EStringNoData;
       
   161     }
       
   162     
       
   163     int ilen = strlen(charString);
       
   164     
       
   165     if (KErrNone == aDes.Create(ilen))
       
   166     {
       
   167     	aDes.Copy((const unsigned char *)charString, ilen);	
       
   168     }
       
   169 	else 
       
   170 	{
       
   171         retval = EInsufficientSystemMemory;	
       
   172 	}
       
   173 	
       
   174 	return retval;
       
   175 }