xml/libxml2libs/inc/xmlengineutils/xmlengxestrings.h
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 definitions of string used by
       
    15 // XmlEngine's modules.
       
    16 //
       
    17 
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @publishedPartner
       
    23  @released
       
    24 */
       
    25 
       
    26 #ifndef XESTRINGS_H
       
    27 #define XESTRINGS_H
       
    28 
       
    29 #include <e32base.h>
       
    30 
       
    31 /**
       
    32 Defines a constant string to be used as an argument value for arguments of type
       
    33 TDOMString and TDOMStringArg.
       
    34 
       
    35 No '\0' character can appear in the string.
       
    36 
       
    37 Usage:
       
    38 @code
       
    39     _XMLLIT(KLitName, "<some ASCII literal to be used as UTF-8 string>");
       
    40 	obj.SomeMethod(KLitName);   // argument type is TXmlEngConstString or TDOMStringArg
       
    41 @endcode
       
    42 */
       
    43 #define _XMLLIT(LitName, utf8String) const TUint8* const LitName = (const TUint8*) utf8String;
       
    44 
       
    45 /**
       
    46 Implements a constant string used within libxml2-based API's.
       
    47 
       
    48 TXmlEngConstString contains a pointer to a zero-terminated UTF-8 string.  The
       
    49 contents of the string is not supposed to be changed. From this point of view
       
    50 TXmlEngConstString instances are close to the concept of immutable strings.
       
    51 
       
    52 Ownership of the string is not defined for TXmlEngConstString. Thus, in
       
    53 different contexts (APIs), different string-handling policies may be applied.
       
    54 For instance, in XML Engine SAX API all reported via callbacks strings of
       
    55 TXmlEngConstString type are assumed owned by the parser.
       
    56 
       
    57 TXmlEngConstString is a base class for other specialized string classes in XML
       
    58 Engine APIs.
       
    59 */
       
    60 class TXmlEngConstString
       
    61     {
       
    62     friend class Libxml2_XmlAttributes;
       
    63 
       
    64 public:
       
    65     /** Default constructor */
       
    66     inline TXmlEngConstString();
       
    67 
       
    68     /**
       
    69     Constructor
       
    70 	@param aString The string for initialization.  Ownership is not transferred
       
    71 	and the string must stay in scope for the lifetime of this object.
       
    72     */
       
    73     inline TXmlEngConstString(const char* aString);
       
    74 
       
    75     /**
       
    76     Check if the string is not NULL
       
    77     @return ETrue if the string is not null, EFalse otherwise
       
    78     */
       
    79     inline TBool NotNull() const;
       
    80 
       
    81     /**
       
    82     Check if the string is NULL
       
    83     @return ETrue if the string is null, EFalse otherwise
       
    84     */
       
    85     inline TBool IsNull()  const;
       
    86 
       
    87     /**
       
    88     Gets a c string
       
    89     @return The string as a C string or NULL if null
       
    90     */
       
    91     inline const char* Cstring() const;
       
    92 
       
    93     /**
       
    94     Gets a c string or if NULL, return a default value.
       
    95     @param aDefaultValue default value
       
    96     @return If not NULL, the c string, otherwise aDefaultValue
       
    97     */
       
    98     inline const char* CstringDef(const char* aDefaultValue) const;
       
    99 
       
   100 	/**
       
   101 	Compares with another string.  NULL and "" are considered equal.
       
   102     
       
   103     @param aString The string to compare
       
   104     @return ETrue if equal, EFalse otherwise
       
   105     */
       
   106     inline TBool Equals(TXmlEngConstString aString) const;
       
   107 
       
   108     /**
       
   109     Compares with another string.  NULL and "" are considered equal.
       
   110     
       
   111     @param aString The string to compare
       
   112     @return 0 if equal, -1 otherwise
       
   113     */
       
   114     IMPORT_C TInt Compare(TXmlEngConstString aString) const;
       
   115 
       
   116 	/**
       
   117 	Allocates a HBufC from the string contents.  Ownership is transferred to
       
   118 	the caller.
       
   119 
       
   120     Typical use:
       
   121     @code
       
   122        ...
       
   123        HBufC* attrValue = attr.Value().AllocL();
       
   124        ...
       
   125     @endcode
       
   126     
       
   127 	@see AllocLC()
       
   128 	@leave KErrNoMemory Memory allocation failure
       
   129 	@leave KUriUtilsCannotConvert String cannot be converted
       
   130     */
       
   131     IMPORT_C HBufC* AllocL() const;
       
   132 
       
   133 	/**
       
   134 	Allocates a HBufC from the string contents and pushes the descriptor to
       
   135 	cleanup stack.  Ownership is transferred to the caller.
       
   136     
       
   137     Typical use:
       
   138     @code
       
   139        ...
       
   140        HBufC* attrValue = attr.Value().AllocLC();
       
   141        ...
       
   142     @endcode
       
   143 
       
   144 	@see AllocL()
       
   145 	@leave KErrNoMemory Memory allocation failure
       
   146 	@leave KUriUtilsCannotConvert String cannot be converted
       
   147     */
       
   148     IMPORT_C HBufC* AllocLC() const;
       
   149 
       
   150     /**
       
   151     Get a TPtrC8 that contains the string
       
   152     @return A point descriptor that holds the string
       
   153     */
       
   154     inline TPtrC8 PtrC8() const;
       
   155 
       
   156 	/**
       
   157 	Creates a new copy of the string contents.  Ownership for the new string is
       
   158 	transferred to the caller.
       
   159 
       
   160     @return A c string copy of the string contents
       
   161 	@leave KErrNoMemory Memory allocation failure
       
   162     */
       
   163     IMPORT_C char* CopyL()  const;
       
   164 
       
   165     /**
       
   166     Returns the size of the string in bytes
       
   167     @return The size
       
   168     */
       
   169     IMPORT_C TUint Size()   const;
       
   170 	
       
   171 	/**
       
   172 	Get the length of the string.  For non-ASCII strings, Size() != Length() due
       
   173 	to the UTF-8 encoding used for non-ASCII characters.
       
   174     @return The number of characters in the string
       
   175     */
       
   176     IMPORT_C TUint Length() const;
       
   177 
       
   178 protected:
       
   179 	/**
       
   180 	Set a new string value.  The old string is not freed.  Ownership is not
       
   181 	transferred.
       
   182     @param aString The new value
       
   183     */
       
   184     inline void Set(char* aString);
       
   185 
       
   186 protected:
       
   187     /** String value */
       
   188     char* iString;
       
   189 };
       
   190 
       
   191 /**
       
   192 General-purpose string used in libxml2-based API's.
       
   193 
       
   194 This class holds a zero-terminated sequence of bytes (c-style string).  It is
       
   195 commonly used to represent a sequence of UTF-8 characters.
       
   196 
       
   197 TXmlEngString objects should be treated as if they were pointers. The
       
   198 destructor for TXmlEngString does nothing.  This means that the developer needs
       
   199 to free the memory allocated by TXmlEngString by calling Free() or Close()
       
   200 (Close() is an alias for Free()) and to do so only once.
       
   201 
       
   202 The reason for this design is that the goal is to have a string class that is
       
   203 as small as a one-pointer sized object that is mostly stored on the stack, not
       
   204 on the heap (and therefore no additional memory allocation is required).  It is
       
   205 also as flexible as possible to allow developers to create their own derived
       
   206 classes which may provide auto_ptr-like or reference counting solutions.
       
   207 
       
   208 The contents of this class can be replaced, destroyed, copied and converted to
       
   209 c strings and Symbian descriptor types. 
       
   210 
       
   211 The contents of TXmlEngString may also be modified with AppendL().
       
   212   
       
   213 When TXmlEngString is the return type in libxml2 based API's, this usually
       
   214 indicates that a string has been newly allocated, which should be freed by the
       
   215 method caller.
       
   216 */
       
   217 class TXmlEngString: public TXmlEngConstString
       
   218 {
       
   219 public:
       
   220     /** Default constructor */
       
   221     inline TXmlEngString();
       
   222 
       
   223 	/**
       
   224 	Constructs a new string from a given c string.  Ownership is transferred
       
   225 	and the string must be freed with Free() or Close().
       
   226 
       
   227     @param aString A heap-based c string
       
   228     */
       
   229     inline TXmlEngString(char* aString);
       
   230 
       
   231     /** Frees the string */
       
   232     IMPORT_C void Free();
       
   233 
       
   234 	/**
       
   235 	Sets a new value.  The old string is freed.  Ownership is transferred and
       
   236 	the string must be freed with Free() or Close().
       
   237 
       
   238     @param aStr The new string
       
   239     */
       
   240     inline void Set(char* aStr);
       
   241 
       
   242     /**
       
   243 	Transfers a string and its ownership from another TXmlEngString object.  If
       
   244 	this object currently stores a string it is freed.
       
   245 
       
   246 	@param aSrc The source string
       
   247     */
       
   248     IMPORT_C void Set(TXmlEngString& aSrc);   
       
   249     
       
   250     /** Frees the string */
       
   251     inline void Close();
       
   252 
       
   253     /**
       
   254 	Pushes this string to the cleanup stack.  This must be matched with a
       
   255 	corresponding call to CleanupStack::Pop() or CleanupStack::PopAndDestroy().
       
   256 	@return This object
       
   257 	@leave KErrNoMemory Memory allocation failure
       
   258     */
       
   259     inline TXmlEngString PushL();
       
   260 
       
   261     /**
       
   262     Initializes the string, converting from a UTF-16 descriptor to
       
   263 	a UTF-8 zero-terminated string.
       
   264     @param aDes The new value
       
   265 	@leave - One of the system-wide error codes
       
   266     */
       
   267     IMPORT_C void SetL(const TDesC& aDes);
       
   268 
       
   269     /**
       
   270     Initializes the string from a descriptor
       
   271     @param aDes The new value
       
   272 	@leave - One of the system-wide error codes
       
   273     */
       
   274     IMPORT_C void SetL(const TDesC8& aDes);
       
   275 
       
   276 	/**
       
   277 	Creates a new UTF-16 HBufC from the UTF-8 string contents and transfers
       
   278 	ownership of the string.  The string held by this object is freed.
       
   279 
       
   280     Typical use:
       
   281     @code
       
   282       ...
       
   283       HBufC* attrValue = attr.WholeValueCopyL().AllocAndFreeL();
       
   284       ...
       
   285     @endcode
       
   286     
       
   287 	@see AllocAndFreeLC()
       
   288     @return The copied string
       
   289 	@leave KErrNoMemory Memory allocation failure
       
   290 	@leave KUriUtilsCannotConvert String cannot be converted
       
   291     */
       
   292     IMPORT_C HBufC* AllocAndFreeL();
       
   293 
       
   294     /**
       
   295 	Creates a new UTF-16 HBufC from the UTF-8 string contents and transfers
       
   296 	ownership of the string.  The result is placed on the cleanup stack.  The
       
   297 	string held by this object is freed.
       
   298 
       
   299     Typical use:
       
   300     @code
       
   301       ...
       
   302       HBufC* attrValue = attr.WholeValueCopyL().AllocAndFreeL();
       
   303       ...
       
   304     @endcode
       
   305     
       
   306 	@see AllocAndFreeL()
       
   307     @return The copied string
       
   308 	@leave KErrNoMemory Memory allocation failure
       
   309 	@leave KUriUtilsCannotConvert String cannot be converted
       
   310     */
       
   311     IMPORT_C HBufC* AllocAndFreeLC();
       
   312 
       
   313     /**
       
   314 	Appends new text to the string
       
   315     @param aStr The string to add
       
   316 	@leave KErrNoMemory Memory allocation failure
       
   317     */
       
   318     IMPORT_C void AppendL(TXmlEngString aStr);
       
   319 
       
   320     /**
       
   321 	Appends new text to the string.
       
   322     @param aStr1 The first string to append
       
   323     @param aStr2 The second string to append
       
   324 	@leave KErrNoMemory Memory allocation failure
       
   325     */
       
   326     IMPORT_C void AppendL(TXmlEngString  aStr1, TXmlEngString aStr2);
       
   327 
       
   328 protected:
       
   329     /** 
       
   330     Constructs the object from a TXmlEngConstString
       
   331     @param aStr The string to initialize from
       
   332     */
       
   333     inline TXmlEngString(const TXmlEngConstString& aStr): TXmlEngConstString(aStr.Cstring()) {}
       
   334 };
       
   335 
       
   336 #include <xml/utils/xmlengxestrings.inl>
       
   337 
       
   338 #endif // XESTRINGS_H
       
   339