// VERSIT.H
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//
 
#if !defined(__VERSIT_H__)
#define __VERSIT_H__

#if !defined(__E32BASE_H__)
#include <e32base.h>
#endif

#if !defined(__BADESCA_H__)
#include <badesca.h>
#endif

#if !defined(__VUID_H__)
#include "vuid.h"
#endif

#if !defined(__VTOKEN_H__)
#include "vtoken.h"
#endif

#if !defined(__VPROPERTY_H__)
#include "vprop.h"
#endif

#if !defined(__VUTIL_H__)
#include "vutil.h"
#endif

//
//	CVersitParser
//

class RFile;
class TVersitDateTime;

enum TVersitParserPanic
	{
	ECurrentTokenNotFound,
	EUnicodeUtilsNotFound,
	ENoPropertyValue,
	ENoWeekdayFound,
	EWeekNoOverTen,
	ENoRecognizedRepeatType
	};

GLREF_C void Panic(TVersitParserPanic aPanic);

class CVersitParser : public CBase
	{
public:
	IMPORT_C CVersitParser(TBool aFlags);
	IMPORT_C ~CVersitParser();
	IMPORT_C void InternalizeL(RFile& aInputFile,TInt& aBytesThroughFile);
	IMPORT_C virtual void InternalizeL(RReadStream& aStream);
	IMPORT_C virtual void InternalizeL(RReadStream& aStream,HBufC* aEntityName,CBufFlat* aCurrentToken,TChar& aCurrentChar);
	IMPORT_C void ExternalizeL(RFile& aOutputFile);
	IMPORT_C virtual void ExternalizeL(RWriteStream& aStream);
	IMPORT_C void AddEntityL(CVersitParser* aEntity);
	IMPORT_C void AddPropertyL(CParserProperty* aProperty,TBool aInternalizing=EFalse);
	IMPORT_C CArrayPtr<CVersitParser>* EntityL(const TDesC& aEntityName,TBool aTakeOwnership=ETrue);
	IMPORT_C CArrayPtr<CVersitParser>* ArrayOfEntities(TBool aTakeOwnership=ETrue);
	IMPORT_C CArrayPtr<CParserProperty>* PropertyL(const TDesC& aPropertyName,const TUid& aPropertyUid,TBool aTakeOwnership=ETrue);
	IMPORT_C CArrayPtr<CParserProperty>* ArrayOfProperties(TBool aTakeOwnership=ETrue);
	IMPORT_C virtual void ConvertAllPropertyDateTimesToMachineLocalL(const TTimeIntervalSeconds& aIncrement,const CVersitDaylight* aDaylight);
	IMPORT_C TUid RecognizeTokenL(const TDesC8& aToken);
#ifdef UNICODE
	IMPORT_C TUid RecognizeTokenL(const TDesC16& aToken);
#endif	
	IMPORT_C TBool IsValidParameterValue(TInt& aPos,const TDesC& aParamValue) const;
	IMPORT_C void SetEntityNameL(const TDesC& aEntityName);
	IMPORT_C TPtrC EntityName() const;
	IMPORT_C static TBool IsValidLabel(const TDesC& aLabel, TInt& aPos);
	static TInt Val(const TDesC8& aToken, TInt &aVal);
public:	
	enum TCharCodes
		{
		ESpace = ' ',
		EHTab = 9,
		ELineFeed = 10,
		ECarriageReturn = 13,
		};
	enum TVersitParserFlags
		{
		ESupportsVersion = 0x01,
		ECrLfToken = 0x02,
		EQuotedPrintable = 0x04,
		EBase64 = 0x08,
		EArrayProperty = 0x10
		};
protected:
	enum TTokenizeType
		{
		EAllExceptCRLFPunctuationAndWhiteSpace,
		EAllExceptCRLF,
		EAllExceptCRLFAndPunctuation,
		EEnd	// no char reading beyond final CRLF
		};
	IMPORT_C virtual void GetNextTokenL(TTokenizeType aTokenizeType=EAllExceptCRLFPunctuationAndWhiteSpace);
	IMPORT_C TBool CompareWithCurrentTokenL(const TDesC& aExpectedToken) const;
	IMPORT_C void GetAndCheckNextTokenL(const TDesC& aExpectedToken);
	IMPORT_C void ReadNextCharL();

	IMPORT_C virtual void ParseBeginL();
	IMPORT_C void ParseEndL();
	IMPORT_C TBool ParseEntityL();
	IMPORT_C void ParsePropertiesL();
	IMPORT_C virtual void ParsePropertyL(TBool& aPropertyParsed);
	
	IMPORT_C virtual void AppendBeginL();
	IMPORT_C void AppendEndL();
	
	IMPORT_C void SkipWhiteSpaceAndCrLfsToNextTokenL();
	IMPORT_C void SkipWhiteSpaceL();
	IMPORT_C void SkipToCrLfL();

	IMPORT_C virtual CVersitParser* MakeEntityL(const TUid& /*aEntityUid*/,const TDesC& /*aEntityName*/);
	IMPORT_C virtual CParserPropertyValue* MakePropertyValueL(const TUid& aPropertyUid);
	IMPORT_C void DecodePropertyValueL(CArrayPtr<CParserParam>* arrayOfParams);
	IMPORT_C virtual void DecodePropertyValueL(const TUid& aEncodingUid);
	IMPORT_C TUid GetFirstPropertyTokenL(TBool& aPropertyWasParsed);
	IMPORT_C CArrayPtr<CParserParam>*  GetPropertyParamsL();
	IMPORT_C HBufC* MakePropertyValueHBufCL();
	IMPORT_C CDesCArray* MakePropertyValueCDesCArrayL();
	IMPORT_C TVersitDateTime* MakePropertyValueDateTimeL();
	IMPORT_C TTimeIntervalSeconds* MakePropertyValueTimeZoneL();
	IMPORT_C CArrayPtr<TVersitDateTime>* MakePropertyValueMultiDateTimeL();
	IMPORT_C TInt MakePropertyValueIntL();
	IMPORT_C CVersitDaylight* MakePropertyValueDaylightL();
	IMPORT_C virtual CParserParam* MakeParamL(const TDesC& aName);
	IMPORT_C TVersitDateTime* DecodeDateTimeL(const TDesC8& aToken) const;
	IMPORT_C TTimeIntervalSeconds* DecodeTimeZoneL(const TDesC8& aToken) const;
	IMPORT_C TTime* DecodeTimePeriodL(const TDesC8& aToken) const;
	IMPORT_C TInt GetNumberL(const TDesC8& aToken) const;
	IMPORT_C virtual TUid RecognizeCurrentTokenL() const;
	IMPORT_C static void ResetAndDestroyArrayOfParams(TAny* aObject);	
	IMPORT_C static void ResetAndDestroyArrayOfProperties(TAny* aObject);	
	IMPORT_C static void ResetAndDestroyArrayOfEntities(TAny* aObject);	
	IMPORT_C static void ResetAndDestroyArrayOfDateTimes(TAny* aObject);	
	inline TBool IsWhiteSpace(TUint aChar) const;
	inline TBool IsCarriageReturnOrLineFeed(TUint aChar) const;
	inline TBool IsWhiteSpaceOrPunctuationToken(TUint aChar) const;
	inline TBool IsPunctuationToken(TUint aChar) const;

	inline TBool SupportsVersion();
	inline TBool CrLfToken();
	inline TBool QuotedPrintable();
	inline TBool Base64();
	inline TBool ArrayProperty();

	inline void SetSupportsVersion();
	inline void SetCrLfToken();
	inline void SetQuotedPrintable();
	inline void SetBase64();
	inline void SetArrayProperty();

	inline void ClearSupportsVersion();
	inline void ClearCrLfToken();
	inline void ClearEncoding();
	inline void ClearArrayProperty();

protected:
	TBool iFlags;
	CArrayPtr<CVersitParser>* iArrayOfEntities;
	CArrayPtr<CParserProperty>* iArrayOfProperties;
	HBufC* iEntityName;
	TBuf<KVersitMaxVersionLength> iDefaultVersion;
	CBufFlat* iCurrentToken;
	TChar iCurrentChar;
	RReadStream* iReadStream;
	RWriteStream* iWriteStream;
	CBufFlat* iDecodeBuffer;
	CVersitUnicodeUtils* iUnicodeUtils;
	TBool iReadDecodedStream;
	};

inline TBool CVersitParser::IsWhiteSpace(TUint aChar) const
	{return (aChar == ESpace || aChar == EHTab);}
inline TBool CVersitParser::IsCarriageReturnOrLineFeed(TUint aChar) const
	{return (aChar == ECarriageReturn || aChar == ELineFeed);}
inline TBool CVersitParser::IsWhiteSpaceOrPunctuationToken(TUint aChar) const
	{return (IsWhiteSpace(aChar) || IsPunctuationToken(aChar));}
inline TBool CVersitParser::IsPunctuationToken(TUint aChar) const
	{return (aChar == KVersitTokenColonVal || aChar == KVersitTokenSemiColonVal || aChar == KVersitTokenEqualsVal || aChar == KVersitTokenPeriodVal || aChar == KVersitTokenCommaVal || aChar == KVersitTokenLSquareBracketVal || aChar == KVersitTokenRSquareBracketVal);}	

inline TBool CVersitParser::SupportsVersion()
	{return iFlags & ESupportsVersion;}
inline TBool CVersitParser::CrLfToken()
	{return iFlags & ECrLfToken;}
inline TBool CVersitParser::QuotedPrintable()
	{return iFlags & EQuotedPrintable;}
inline TBool CVersitParser::Base64()
	{return iFlags & EBase64;}
inline TBool CVersitParser::ArrayProperty()
	{return iFlags & EArrayProperty;}

inline void CVersitParser::SetSupportsVersion()
	{iFlags |= ESupportsVersion;}
inline void CVersitParser::SetCrLfToken()
	{iFlags |= ECrLfToken;}
inline void CVersitParser::SetQuotedPrintable()
	{iFlags &= ~EBase64; iFlags |= EQuotedPrintable;}
inline void CVersitParser::SetBase64()
	{iFlags &= ~EQuotedPrintable; iFlags |= EBase64;}
inline void CVersitParser::SetArrayProperty()
	{iFlags |= EArrayProperty;}

inline void CVersitParser::ClearSupportsVersion()
	{iFlags &= ~ESupportsVersion;}
inline void CVersitParser::ClearCrLfToken()
	{iFlags &= ~ECrLfToken;}
inline void CVersitParser::ClearEncoding()
	{iFlags &= (~EQuotedPrintable & ~EBase64);}	
inline void CVersitParser::ClearArrayProperty()
	{iFlags &= ~EArrayProperty;}

#endif
