xml/libxml2libs/src/xmlengineutils/xmlengutils.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2006-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 methods allow to change XmlChar to TDesC
       
    15 // and TDesC to XmlChar
       
    16 //
       
    17 
       
    18 #include <stdapis/libxml2/libxml2_globals.h>
       
    19 #include <stdapis/libxml2/libxml2_entities.h>
       
    20 
       
    21 #include <xml/utils/xmlengmem.h>
       
    22 
       
    23 #include <utf.h>
       
    24 #include <escapeutils.h>
       
    25 
       
    26 EXPORT_C char* XmlEngEscapeForXmlValueL(
       
    27     const char* aUnescapedString )
       
    28     {
       
    29     xmlChar* res = xmlEncodeSpecialChars(NULL, (const xmlChar*)aUnescapedString);
       
    30     if(aUnescapedString && !res)
       
    31         {
       
    32         OOM_HAPPENED;
       
    33         }
       
    34     return (char*)res;
       
    35     }
       
    36 
       
    37 EXPORT_C char* XmlEngXmlCharFromDesL(
       
    38     const TDesC& aDes ) // UTF-16 descriptor to convert into UTF-8 zero-terminated string
       
    39     {
       
    40     TUint len = ((aDes.Length() + 4) | 0x1F);
       
    41     char* str = new(ELeave) char[len + 1];
       
    42     TPtr8 utf8buf((TUint8*)str, 0, len);
       
    43     TInt res = CnvUtfConverter::ConvertFromUnicodeToUtf8(utf8buf, aDes);
       
    44     if (res == 0)
       
    45         {
       
    46         str[utf8buf.Size()] = 0;
       
    47         return str;
       
    48         }
       
    49     //the buffer size was not large enough:
       
    50     //reallocate the larger buffer
       
    51     const TInt maxSizeOfSymbol = 4;
       
    52     len = len + res*maxSizeOfSymbol; // res contains number of non-converted UTF-16 characters
       
    53     delete str;
       
    54     str = new(ELeave) char[len + 1];
       
    55     TPtr8 utf8buf2((TUint8*)str, 0, len);
       
    56     res = CnvUtfConverter::ConvertFromUnicodeToUtf8(utf8buf2, aDes);
       
    57     if (res == 0)
       
    58         {
       
    59         str[utf8buf2.Size()] = 0;
       
    60         return str;
       
    61         }
       
    62     delete str;
       
    63     str = NULL;
       
    64     User::Leave(KErrGeneral); // this should never happen;
       
    65     return NULL;
       
    66     }
       
    67 
       
    68 EXPORT_C char* XmlEngXmlCharFromDes8L(
       
    69     const TDesC8& aDes )
       
    70     {
       
    71     TUint len = aDes.Length();
       
    72     char* str = new(ELeave) char[len + 1];
       
    73     memcpy(str, aDes.Ptr(), len);
       
    74     str[len] = 0;
       
    75     return str;
       
    76     }