// DIMEParser.h: interface for the DIMEParser class.
//
//
//////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2002 Robert Cunnings. All rights reserved.
//
// You may copy, modify, distribute or publish this code free of charge.
//
// NO WARRANTY: This software is provided AS IS, without warranty of any kind.
//
//////////////////////////////////////////////////////////////////////
//
// DIMEParser class - implementation of DIME parser in accordance
// with Internet-Draft "Direct Internet Message Encapsulation (DIME)"
// (draft-nielson-dime-02 June 17, 2002)
//  
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DIMEPARSER_H__EA478F30_B32E_4E50_9287_544E67F9D05D__INCLUDED_)
#define AFX_DIMEPARSER_H__EA478F30_B32E_4E50_9287_544E67F9D05D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <vector>
#include <string>
using namespace std;

#include "DIMEPayload.h"
#include "DIMESerializer.h"
#include "DIMEDeserializer.h"

class DIMEParser  
{
public:
	DIMEParser();
	virtual ~DIMEParser();

	void Clear();

	// can throw DIMEParserException
	bool SerializeInit(unsigned long ulRecordSize = 4096);
	// can throw DIMEParserException
	bool Serialize( char *pszBuffer,
					unsigned long ulSize,
					unsigned long *pBytesWritten);

	bool SerializationFinished() { return m_Serializer.Finished(); }
	unsigned long CalcSerialLength() { return m_Serializer.CalcSerialLength(); }
	unsigned long GetSerializationRecSize() { return m_Serializer.GetRecordSize(); }

	// can throw DIMEParserException
	bool DeserializeInit();
	// can throw DIMEParserException
	bool Deserialize(   char *pszBuffer,
						unsigned long ulSize);

	bool DeserializationFinished() { return m_Deserializer.Finished(); }

	bool AddPayload(IStorage *pIStorage,
					const string& strId,
					const string& strType,
					unsigned long ulTNF,
					unsigned char *pOptionData = NULL,
					unsigned long ulOptionLength = 0,
					unsigned long ulOptionType = 0);

	bool AddPayload(IStorage *pIStorage,
					const string& strId,
					const string& strType,
					unsigned long ulTNF,
					SAFEARRAY *pOptions);

	bool AddPayload(const char *pszBuffer,
					unsigned long ulSize,
					const string& strId,
					const string& strType,
					unsigned long ulTNF,
					unsigned char *pOptionData = NULL,
					unsigned long ulOptionLength = 0,
					unsigned long ulOptionType = 0);

	bool AddPayload(const string& strFilename,
					const string& strId,
					const string& strType,
					unsigned long ulTNF,
					unsigned char *pOptionData = NULL,
					unsigned long ulOptionLength = 0,
					unsigned long ulOptionType = 0);

	bool AddPayload(DIMEPayload *pPayload);

	vector<DIMEPayload *>& GetPayloads() { return m_arrPayloads; }
	wstring GenerateId();

protected:

	vector<DIMEPayload *>	m_arrPayloads;
	DIMESerializer			m_Serializer;
	DIMEDeserializer		m_Deserializer;

};

class DIMEParserException
{
public:
	DIMEParserException(const wstring& wstrMsg) : m_wstrErrorMsg(wstrMsg) {}
	wstring		m_wstrErrorMsg;
};

#endif // !defined(AFX_DIMEPARSER_H__EA478F30_B32E_4E50_9287_544E67F9D05D__INCLUDED_)
