xml/libxml2libs/src/xmlengineutils/xmlengxmlstring.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // This file contains declarations of string methods used by
       
    15 // XmlEngine's modules.
       
    16 //
       
    17 
       
    18 #include <xml/utils/xmlengxestrings.h>
       
    19 #include <xml/utils/xmlengmem.h>
       
    20 #include <xml/utils/xmlengutils.h>
       
    21 
       
    22 #include <stdapis/libxml2/libxml2_xmlstring.h>
       
    23 #include <stdapis/libxml2/libxml2_globals.h>
       
    24 
       
    25 #include <utf.h>
       
    26 #include <escapeutils.h>
       
    27 
       
    28 //----------------------------------------------------------- TXmlEngConstString
       
    29 
       
    30 // ------------------------------------------------------------------------------
       
    31 // Compares two strings.
       
    32 //
       
    33 // @return Result as <b>strcmp()</b>
       
    34 // ------------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C TBool TXmlEngConstString::Compare(
       
    37     TXmlEngConstString aString ) const
       
    38     {
       
    39     char* str2 = aString.iString;
       
    40     char* str1 = iString;
       
    41     if (str1 == str2)
       
    42         {
       
    43         return(0);
       
    44         }
       
    45     if (!str1 && *str2 == '\x0')
       
    46         {
       
    47         return(0);
       
    48         }
       
    49     if (!str2 && *str1 == '\x0')
       
    50         {
       
    51         return(0);
       
    52         }
       
    53     if (!str1 )
       
    54         {
       
    55         return(-1);
       
    56         }
       
    57     if (!str2 )
       
    58         {
       
    59         return(1);
       
    60         }
       
    61 
       
    62     register int tmp;
       
    63     do  {
       
    64         tmp = *str1++ - *str2;
       
    65         if (tmp != 0) 
       
    66             return(tmp);
       
    67         } while (*str2++ != 0);
       
    68     return 0;
       
    69     }
       
    70 
       
    71 // ------------------------------------------------------------------------------
       
    72 // Returns copy of the TXmlEngConstString contents
       
    73 // ------------------------------------------------------------------------------
       
    74 //
       
    75 EXPORT_C char* TXmlEngConstString::CopyL() const
       
    76     {
       
    77 
       
    78     xmlChar* copy = xmlStrdup((xmlChar*)iString);
       
    79     OOM_IF(iString && !copy);
       
    80 
       
    81     return (char*) copy;
       
    82     }
       
    83 
       
    84 
       
    85 // ------------------------------------------------------------------------------
       
    86 // Returns size of string in bytes
       
    87 // ------------------------------------------------------------------------------
       
    88 //
       
    89 EXPORT_C TUint TXmlEngConstString::Size() const 
       
    90     {
       
    91 	if (iString)
       
    92 		{
       
    93 		return (TUint)strlen(iString);
       
    94 		}
       
    95     return 0;
       
    96     }
       
    97 
       
    98 // ------------------------------------------------------------------------------
       
    99 // @return Size of a string: a number of characters
       
   100 // @note for non-ASCII contents <b>Size() != Length()</b>
       
   101 // ------------------------------------------------------------------------------
       
   102 //
       
   103 EXPORT_C TUint TXmlEngConstString::Length()  const
       
   104     {
       
   105     // Note: return 0 for NULL strings
       
   106     if(!iString)
       
   107         return 0;
       
   108     TInt len = xmlUTF8Strlen((const xmlChar*)iString);
       
   109     return len != -1 ? (TUint)len : 0;
       
   110     }
       
   111 
       
   112 //---------------------------------------------------------------------- TXmlEngString
       
   113 
       
   114 // ------------------------------------------------------------------------------
       
   115 // Frees the string
       
   116 // ------------------------------------------------------------------------------
       
   117 //
       
   118 EXPORT_C void TXmlEngString::Free()
       
   119     {xmlFree(iString); iString = 0;}
       
   120 
       
   121 // ------------------------------------------------------------------------------
       
   122 // Transfers string and its ownership between TXmlEngString objects;
       
   123 //
       
   124 // Any previous content is freed. The source becomes empty (NULL).
       
   125 // This allows reusing TXmlEngString variables and free them only once.
       
   126 //
       
   127 // @note
       
   128 // This is only a convenience method: it allows avoiding many TXmlEngString variables
       
   129 // and corresponding calls of Free() method for each. In practice, having a separate
       
   130 // variable for string values makes code more clearer [No performance impact too].
       
   131 // ------------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C void TXmlEngString::Set(TXmlEngString& aSrc)
       
   134     {
       
   135     if(iString)
       
   136         {
       
   137         if(iString == aSrc.iString)
       
   138             {
       
   139             return;
       
   140             }
       
   141         else
       
   142             {
       
   143             delete iString;
       
   144             }
       
   145         }
       
   146     iString = aSrc.iString;
       
   147     aSrc.iString = 0;
       
   148     }
       
   149 
       
   150 inline HBufC* HBufCFromXmlCharLC(
       
   151     const void* aUtf8String )
       
   152     {
       
   153     if (!aUtf8String)
       
   154         return KNullDesC().AllocLC();
       
   155 
       
   156     HBufC* res = CnvUtfConverter::ConvertToUnicodeFromUtf8L(
       
   157                         TPtrC8((TUint8*)aUtf8String,
       
   158                         strlen((char*)aUtf8String)));		
       
   159     CleanupStack::PushL(res);
       
   160     return res;
       
   161     }
       
   162 
       
   163 inline HBufC* HBufCFromXmlCharL(
       
   164     const void* aUtf8String ) 
       
   165     {
       
   166     if(!aUtf8String)
       
   167         return KNullDesC().AllocL();
       
   168     else
       
   169         return CnvUtfConverter::ConvertToUnicodeFromUtf8L(
       
   170                     TPtrC8((TUint8*)aUtf8String,
       
   171                     strlen((char*)aUtf8String)));
       
   172     }
       
   173 
       
   174 /**
       
   175 Allocates UTF-16 heap descriptor from the TDOMString contents
       
   176 
       
   177 Typical use:
       
   178 @code
       
   179     ...
       
   180     HBufC* attrValue = attr.Value().AllocL();
       
   181     ...
       
   182 @endcode
       
   183 
       
   184 AllocLC() does the same, but returned HBufC* is already on the cleanup stack
       
   185 after a call to this method.
       
   186 */
       
   187 EXPORT_C HBufC* TXmlEngConstString::AllocL(void) const
       
   188     {
       
   189     return HBufCFromXmlCharL(iString); // Note: it's inlined
       
   190     }
       
   191 
       
   192 /**
       
   193 Allocates UTF-16 heap descriptor from the TXmlEngConstString contents and
       
   194 pushes the descriptor to cleanup stack.
       
   195 
       
   196 Typical use:
       
   197 @code
       
   198     ...
       
   199     HBufC* attrValue = attr.Value().AllocLC();
       
   200     ...
       
   201 @endcode
       
   202 */
       
   203 EXPORT_C HBufC* TXmlEngConstString::AllocLC(void)const
       
   204     {
       
   205     return HBufCFromXmlCharLC(iString);  // Note: it's inlined
       
   206     }
       
   207 
       
   208 // ------------------------------------------------------------------------------
       
   209 // Initialize TDOMString from Symbian descriptor.
       
   210 //
       
   211 // UTF-16 content of descriptor is converted into UTF-8 zero-terminated
       
   212 // c-string, for which memory is allocated.
       
   213 // ------------------------------------------------------------------------------
       
   214 //
       
   215 EXPORT_C void TXmlEngString::SetL(const TDesC& aDes)
       
   216     {
       
   217     if(iString)
       
   218         PushL();
       
   219     char* str = const_cast<char*> (XmlEngXmlCharFromDesL(aDes));
       
   220     if (iString)    
       
   221         CleanupStack::PopAndDestroy(iString);
       
   222     iString = str;
       
   223     }
       
   224 
       
   225 //
       
   226 // ------------------------------------------------------------------------------
       
   227 // Initialize TDOMString from Symbian descriptor.
       
   228 //
       
   229 // Byte content of descriptor is taken as UTF-8 string,
       
   230 // and zero-terminated c-string is created from it.
       
   231 // ------------------------------------------------------------------------------
       
   232 //
       
   233 EXPORT_C void TXmlEngString::SetL(const TDesC8& aDes8)
       
   234     {
       
   235     if(iString)
       
   236         PushL();
       
   237     char* str = const_cast<char*> (XmlEngXmlCharFromDes8L(aDes8));
       
   238     if (iString)    
       
   239         CleanupStack::PopAndDestroy(iString);
       
   240     iString = str;
       
   241     }
       
   242 
       
   243 // ------------------------------------------------------------------------------
       
   244 // Allocates heap descriptor as AllocL() and frees internal c-string.
       
   245 //
       
   246 // Typical use:
       
   247 // @code
       
   248 //    ...
       
   249 //    HBufC* attrValue = attr.WholeValueCopyL().AllocAndFreeL();
       
   250 //    ...
       
   251 // @endcode
       
   252 //
       
   253 // AllocAndFreeLC() does the same, but returned HBufC* is already on the cleanup stack
       
   254 // after a call to this method.
       
   255 // ------------------------------------------------------------------------------
       
   256 //
       
   257 EXPORT_C HBufC* TXmlEngString::AllocAndFreeL()
       
   258 {
       
   259     CleanupStack::PushL((TAny*)iString);  // PushL()  
       
   260     HBufC* ret = HBufCFromXmlCharL(iString); // inlined AllocL();
       
   261     CleanupStack::PopAndDestroy();
       
   262     iString = NULL;
       
   263     return ret;
       
   264 }
       
   265 
       
   266 /**
       
   267 Allocates heap descriptor as AllocLC() and frees internal c-string.
       
   268 
       
   269 More details in AllocAndFreeL() description.
       
   270 */
       
   271 EXPORT_C HBufC* TXmlEngString::AllocAndFreeLC()
       
   272 {
       
   273     // inlined AllocAndFreeL():
       
   274     CleanupStack::PushL((TAny*)iString);  // PushL()
       
   275     HBufC* hbuf = HBufCFromXmlCharL(iString); // inlined AllocL();
       
   276     CleanupStack::PopAndDestroy(); // iString
       
   277     iString = NULL;
       
   278     //
       
   279     CleanupStack::PushL(hbuf);
       
   280     return hbuf;
       
   281 }
       
   282 
       
   283 
       
   284 EXPORT_C void TXmlEngString::AppendL(TXmlEngString str)
       
   285     {
       
   286     if(str.iString)
       
   287         {
       
   288         TUint len1 = iString ? Size(): 0;
       
   289         TUint len2 = str.Size();
       
   290         char* res = new (ELeave) char[len1 + len2 + 1];
       
   291         OOM_IF_NULL(res);
       
   292         if (iString)
       
   293             memcpy(res, iString, len1);
       
   294         memcpy(res+len1, str.iString, len2);
       
   295         res[len1+len2] = 0;
       
   296         delete iString;
       
   297         iString = res;
       
   298         }
       
   299     }
       
   300 
       
   301 EXPORT_C void TXmlEngString::AppendL(TXmlEngString str1, TXmlEngString str2)
       
   302     {   
       
   303         AppendL(str1);
       
   304         AppendL(str2);
       
   305     }
       
   306