email/imap4mtm/imapsession/src/cimapatom.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 1998-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 //
       
    15 
       
    16 #include "cimapatom.h"
       
    17 #include "cimapsessionconsts.h"
       
    18 
       
    19 CImapAtom* CImapAtom::NewLC()
       
    20 // static method
       
    21 	{
       
    22 	CImapAtom* self = new(ELeave)CImapAtom();
       
    23 	CleanupStack::PushL(self);
       
    24 	
       
    25 	// no ConstructL()
       
    26 	
       
    27 	return self;
       
    28 	}
       
    29 
       
    30 CImapAtom::CImapAtom()
       
    31 	{
       
    32 	}
       
    33 
       
    34 CImapAtom::~CImapAtom()
       
    35 	{
       
    36 	}
       
    37 
       
    38 /**
       
    39 During parsing, sets the string data of this atom.
       
    40 @param The string data of this atom.
       
    41 @param Whether the data was provided as a quoted string.
       
    42 */
       
    43 void CImapAtom::Set(const TDesC8& aAtom, TBool aAtomIsQuoted)
       
    44 	{
       
    45 	// Save this atom in here
       
    46 	iAtom.Set(aAtom);
       
    47 	iAtomIsQuoted = aAtomIsQuoted;
       
    48 	}
       
    49 
       
    50 /**
       
    51 During parsing, sets the child of this atom.
       
    52 @param aNewChild The child of this atom.  Ownership is not transferred.
       
    53 */
       
    54 void CImapAtom::AddChild(CImapAtom* aNewChild)
       
    55 	{
       
    56 	// Set child pointer
       
    57 	iChild=aNewChild;
       
    58 	}
       
    59 
       
    60 /**
       
    61 During parsing, sets the sibling of this atom.
       
    62 @param aNewNext The sibling of this atom.  Ownership is not transferred.
       
    63 */
       
    64 void CImapAtom::AddNext(CImapAtom* aNewNext)
       
    65 	{
       
    66 	// Set next pointer
       
    67 	iNext=aNewNext;
       
    68 	}
       
    69 
       
    70 /**
       
    71 @return The child of this atom, or NULL if this atom has no child.  Ownership is not transferred.
       
    72 */
       
    73 CImapAtom* CImapAtom::Child()
       
    74 	{
       
    75 	return iChild;
       
    76 	}
       
    77 
       
    78 /**
       
    79 @return The sibling of this atom, or NULL if this atom has no sibling.  Ownership is not transferred.
       
    80 */
       
    81 CImapAtom* CImapAtom::Next()
       
    82 	{
       
    83 	return iNext;
       
    84 	}
       
    85 
       
    86 /**
       
    87 Use CompareF to match the provided string with this atom's data.
       
    88 @param aVal the string to match
       
    89 @return whether this atom's data matches the provided string.
       
    90 */
       
    91 TBool CImapAtom::Match(const TDesC8& aVal)
       
    92 	{
       
    93 	// Compare and return result
       
    94 	return (iAtom.CompareF(aVal)==0);
       
    95 	}
       
    96 
       
    97 /**
       
    98 @param bNString whether a non-quoted "NIL" should be treated as a NULL string
       
    99 @return The string data of this atom
       
   100 */
       
   101 const TDesC8& CImapAtom::Atom(TBool aNString)
       
   102 	{
       
   103 	if (aNString && !iAtomIsQuoted && (iAtom.CompareF(KImapTxtNil()) == 0))
       
   104 		{
       
   105 		return KNullDesC8();
       
   106 		}
       
   107 		
       
   108 	return iAtom;
       
   109 	}
       
   110 
       
   111 /**
       
   112 @return Whether the data was provided as a quoted string.
       
   113 */
       
   114 TBool CImapAtom::AtomIsQuoted()
       
   115 	{
       
   116 	return iAtomIsQuoted;
       
   117 	}
       
   118 
       
   119 /**
       
   120 This method should only be used by CImapAtomParser.
       
   121 During parsing, a ReAllocL() may be required on the heap buffer that this atom and its 
       
   122 descendants' string data descriptor points at.
       
   123 This happens when the heap buffer needs to be expanded.
       
   124 If this causes the heap buffer's address to change, then this atom and its descendants' 
       
   125 pointer descriptors need updating.
       
   126 @param The address of the new heap buffer
       
   127 @param The address of the data within the heap buffer before the ReAllocL took place.
       
   128 */
       
   129 void CImapAtom::FixupL(const HBufC8 *aNewBuffer, const TText8 *aOldBuffer)
       
   130 	{
       
   131    // Fixup descriptor pointers
       
   132 	CArrayFixFlat<CImapAtom*>* atomStack = new (ELeave) CArrayFixFlat<CImapAtom*>(10);
       
   133 	CleanupStack::PushL(atomStack);
       
   134 
       
   135 	atomStack->AppendL(this);
       
   136 	CImapAtom* currentAtom;
       
   137 	while (atomStack->Count() != 0)
       
   138    		{
       
   139 		// Pop the top atom off of the stack
       
   140 		currentAtom = (*atomStack)[atomStack->Count() - 1];
       
   141  		atomStack->ResizeL(atomStack->Count() - 1);
       
   142  
       
   143 		// Fix up the current atom
       
   144 		if (currentAtom->iAtom.Length()>0)
       
   145 			{
       
   146 			// Find offset from start of old buffer
       
   147 			TInt start=(currentAtom->iAtom.Ptr()-aOldBuffer);
       
   148 
       
   149  			// Make new descriptor & assign it
       
   150 			TPtrC8 bufptr(aNewBuffer->Ptr()+start,currentAtom->iAtom.Length());
       
   151 			currentAtom->iAtom.Set(bufptr); // Note that we are setting the real iAtom not the copy returned by Atom()
       
   152 			}
       
   153  
       
   154 		// Add the first sibling to the stack,
       
   155 		// subsequent siblings are added when this sibling is visited
       
   156 		CImapAtom* siblingAtom = currentAtom->Next();
       
   157 		if (siblingAtom)
       
   158 			{
       
   159 			atomStack->AppendL(siblingAtom);
       
   160 			}
       
   161    
       
   162 		// Add child to the stack
       
   163 		CImapAtom* childAtom = currentAtom->Child();
       
   164 		if (childAtom)
       
   165 			{
       
   166 			atomStack->AppendL(childAtom);
       
   167 			}			
       
   168    		}
       
   169    
       
   170 	CleanupStack::PopAndDestroy(atomStack);
       
   171    	}
       
   172